cae6491378eff9da4b98ecc79b303245d9cba242
[blender-staging.git] / source / blender / editors / armature / poseobject.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * Contributor(s): Ton Roosendaal, Blender Foundation '05, full recode.
24  *                               Joshua Leung
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  * support for animation modes - Reevan McKay
28  */
29
30 #include <stdlib.h>
31 #include <stddef.h>
32 #include <string.h>
33
34 #include "MEM_guardedalloc.h"
35
36 #include "BLI_math.h"
37 #include "BLI_blenlib.h"
38 #include "BLI_dynstr.h"
39 #include "BLI_utildefines.h"
40
41 #include "DNA_anim_types.h"
42 #include "DNA_armature_types.h"
43 #include "DNA_constraint_types.h"
44 #include "DNA_scene_types.h"
45 #include "DNA_object_types.h"
46
47 #include "BKE_anim.h"
48 #include "BKE_idprop.h"
49 #include "BKE_action.h"
50 #include "BKE_armature.h"
51 #include "BKE_context.h"
52 #include "BKE_constraint.h"
53 #include "BKE_deform.h"
54 #include "BKE_depsgraph.h"
55 #include "BKE_modifier.h"
56 #include "BKE_report.h"
57
58
59 #include "RNA_access.h"
60 #include "RNA_define.h"
61
62 #include "WM_api.h"
63 #include "WM_types.h"
64
65 #include "ED_armature.h"
66 #include "ED_keyframing.h"
67 #include "ED_mesh.h"
68 #include "ED_screen.h"
69
70 #include "UI_interface.h"
71
72 #include "armature_intern.h"
73
74 /* ************* XXX *************** */
75 static int pupmenu(const char *UNUSED(dummy)) {return 0;}
76 static void error(const char *UNUSED(dummy)) {}
77 static void BIF_undo_push(const char *UNUSED(dummy)) {}
78 /* ************* XXX *************** */
79
80
81 static int object_pose_context(Object *ob)
82 {
83         if(     (ob) && 
84                 (ob->type == OB_ARMATURE) &&
85                 (ob->pose) &&
86                 (ob->mode & OB_MODE_POSE)
87         ) {
88                 return 1;
89         }
90         else {
91                 return 0;
92         }
93 }
94
95 Object *ED_object_pose_armature(Object *ob)
96 {
97         if(ob==NULL)
98                 return NULL;
99         
100         if(object_pose_context(ob))
101                 return ob;
102
103         ob= modifiers_isDeformedByArmature(ob);
104
105         if(object_pose_context(ob))
106                 return ob;
107
108         return NULL;
109 }
110
111
112 /* This function is used to indicate that a bone is selected and needs keyframes inserted */
113 void set_pose_keys (Object *ob)
114 {
115         bArmature *arm= ob->data;
116         bPoseChannel *chan;
117
118         if (ob->pose){
119                 for (chan=ob->pose->chanbase.first; chan; chan=chan->next){
120                         Bone *bone= chan->bone;
121                         if ((bone) && (bone->flag & BONE_SELECTED) && (arm->layer & bone->layer))
122                                 chan->flag |= POSE_KEY; 
123                         else
124                                 chan->flag &= ~POSE_KEY;
125                 }
126         }
127 }
128
129 /* This function is used to process the necessary updates for */
130 void ED_armature_enter_posemode(bContext *C, Base *base)
131 {
132         ReportList *reports= CTX_wm_reports(C);
133         Object *ob= base->object;
134         
135         if (ob->id.lib) {
136                 BKE_report(reports, RPT_WARNING, "Can't pose libdata");
137                 return;
138         }
139         
140         switch (ob->type) {
141                 case OB_ARMATURE:
142                         ob->restore_mode = ob->mode;
143                         ob->mode |= OB_MODE_POSE;
144                         
145                         WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_MODE_POSE, NULL);
146                         
147                         break;
148                 default:
149                         return;
150         }
151         
152         // XXX: disabled as this would otherwise cause a nasty loop...
153         //ED_object_toggle_modes(C, ob->mode);
154 }
155
156 void ED_armature_exit_posemode(bContext *C, Base *base)
157 {
158         if(base) {
159                 Object *ob= base->object;
160                 
161                 ob->restore_mode = ob->mode;
162                 ob->mode &= ~OB_MODE_POSE;
163                 
164                 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_MODE_OBJECT, NULL);
165         }       
166 }
167
168 /* if a selected or active bone is protected, throw error (oonly if warn==1) and return 1 */
169 /* only_selected==1 : the active bone is allowed to be protected */
170 static short pose_has_protected_selected(Object *ob, short warn)
171 {
172         /* check protection */
173         if (ob->proxy) {
174                 bPoseChannel *pchan;
175                 bArmature *arm= ob->data;
176
177                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
178                         if (pchan->bone && (pchan->bone->layer & arm->layer)) {
179                                 if (pchan->bone->layer & arm->layer_protected) {
180                                         if (pchan->bone->flag & BONE_SELECTED)
181                                            break;
182                                 }
183                         }
184                 }
185                 if (pchan) {
186                         if (warn) error("Cannot change Proxy protected bones");
187                         return 1;
188                 }
189         }
190         return 0;
191 }
192
193 /* only for real IK, not for auto-IK */
194 static int pose_channel_in_IK_chain(Object *ob, bPoseChannel *pchan, int level)
195 {
196         bConstraint *con;
197         Bone *bone;
198         
199         /* No need to check if constraint is active (has influence),
200          * since all constraints with CONSTRAINT_IK_AUTO are active */
201         for(con= pchan->constraints.first; con; con= con->next) {
202                 if(con->type==CONSTRAINT_TYPE_KINEMATIC) {
203                         bKinematicConstraint *data= con->data;
204                         if(data->rootbone == 0 || data->rootbone > level) {
205                                 if((data->flag & CONSTRAINT_IK_AUTO)==0)
206                                         return 1;
207                         }
208                 }
209         }
210         for(bone= pchan->bone->childbase.first; bone; bone= bone->next) {
211                 pchan= get_pose_channel(ob->pose, bone->name);
212                 if(pchan && pose_channel_in_IK_chain(ob, pchan, level + 1))
213                         return 1;
214         }
215         return 0;
216 }
217
218 int ED_pose_channel_in_IK_chain(Object *ob, bPoseChannel *pchan)
219 {
220         return pose_channel_in_IK_chain(ob, pchan, 0);
221 }
222
223 /* ********************************************** */
224 /* Motion Paths */
225
226 /* For the object with pose/action: update paths for those that have got them
227  * This should selectively update paths that exist...
228  *
229  * To be called from various tools that do incremental updates 
230  */
231 void ED_pose_recalculate_paths(Scene *scene, Object *ob)
232 {
233         ListBase targets = {NULL, NULL};
234         
235         /* set flag to force recalc, then grab the relevant bones to target */
236         ob->pose->avs.recalc |= ANIMVIZ_RECALC_PATHS;
237         animviz_get_object_motionpaths(ob, &targets);
238         
239         /* recalculate paths, then free */
240         animviz_calc_motionpaths(scene, &targets);
241         BLI_freelistN(&targets);
242 }
243
244 /* For the object with pose/action: create path curves for selected bones 
245  * This recalculates the WHOLE path within the pchan->pathsf and pchan->pathef range
246  */
247 static int pose_calculate_paths_exec (bContext *C, wmOperator *UNUSED(op))
248 {
249         ScrArea *sa= CTX_wm_area(C);
250         Scene *scene= CTX_data_scene(C);
251         Object *ob;
252         
253         /* since this call may also be used from the buttons window, we need to check for where to get the object */
254         if (sa->spacetype == SPACE_BUTS) 
255                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
256         else
257                 ob= ED_object_pose_armature(CTX_data_active_object(C));
258                 
259         if (ELEM(NULL, ob, ob->pose))
260                 return OPERATOR_CANCELLED;
261         
262         /* set up path data for bones being calculated */
263         CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pose_bones) 
264         {
265                 /* verify makes sure that the selected bone has a bone with the appropriate settings */
266                 animviz_verify_motionpaths(scene, ob, pchan);
267         }
268         CTX_DATA_END;
269         
270         /* calculate the bones that now have motionpaths... */
271         // TODO: only make for the selected bones?
272         ED_pose_recalculate_paths(scene, ob);
273         
274         /* notifiers for updates */
275         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
276         
277         return OPERATOR_FINISHED; 
278 }
279
280 void POSE_OT_paths_calculate (wmOperatorType *ot)
281 {
282         /* identifiers */
283         ot->name= "Calculate Bone Paths";
284         ot->idname= "POSE_OT_paths_calculate";
285         ot->description= "Calculate paths for the selected bones";
286         
287         /* api callbacks */
288         ot->exec= pose_calculate_paths_exec;
289         ot->poll= ED_operator_posemode;
290         
291         /* flags */
292         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
293 }
294
295 /* --------- */
296
297 /* for the object with pose/action: clear path curves for selected bones only */
298 void ED_pose_clear_paths(Object *ob)
299 {
300         bPoseChannel *pchan;
301         short skipped = 0;
302         
303         if ELEM(NULL, ob, ob->pose)
304                 return;
305         
306         /* free the motionpath blocks, but also take note of whether we skipped some... */
307         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
308                 if (pchan->mpath) {
309                         if ((pchan->bone) && (pchan->bone->flag & BONE_SELECTED)) {
310                                 animviz_free_motionpath(pchan->mpath);
311                                 pchan->mpath= NULL;
312                         }
313                         else 
314                                 skipped = 1;
315                 }
316         }
317         
318         /* if we didn't skip any, we shouldn't have any paths left */
319         if (skipped == 0)
320                 ob->pose->avs.path_bakeflag &= ~MOTIONPATH_BAKE_HAS_PATHS;
321 }
322
323 /* operator callback for this */
324 static int pose_clear_paths_exec (bContext *C, wmOperator *UNUSED(op))
325 {
326         ScrArea *sa= CTX_wm_area(C);
327         Object *ob;
328         
329         /* since this call may also be used from the buttons window, we need to check for where to get the object */
330         if (sa->spacetype == SPACE_BUTS) 
331                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
332         else
333                 ob= ED_object_pose_armature(CTX_data_active_object(C));
334                 
335         /* only continue if there's an object */
336         if ELEM(NULL, ob, ob->pose)
337                 return OPERATOR_CANCELLED;
338         
339         /* use the backend function for this */
340         ED_pose_clear_paths(ob);
341         
342         /* notifiers for updates */
343         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
344         
345         return OPERATOR_FINISHED; 
346 }
347
348 void POSE_OT_paths_clear (wmOperatorType *ot)
349 {
350         /* identifiers */
351         ot->name= "Clear Bone Paths";
352         ot->idname= "POSE_OT_paths_clear";
353         ot->description= "Clear path caches for selected bones";
354         
355         /* api callbacks */
356         ot->exec= pose_clear_paths_exec;
357         ot->poll= ED_operator_posemode;
358         
359         /* flags */
360         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
361 }
362
363 /* ******************* Select Constraint Target Operator ************* */
364
365 static int pose_select_constraint_target_exec(bContext *C, wmOperator *UNUSED(op))
366 {
367         Object *ob= ED_object_pose_armature(CTX_data_active_object(C));
368         bConstraint *con;
369         int found= 0;
370         
371         CTX_DATA_BEGIN(C, bPoseChannel *, pchan, visible_pose_bones) 
372         {
373                 if (pchan->bone->flag & BONE_SELECTED) {
374                         for (con= pchan->constraints.first; con; con= con->next) {
375                                 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
376                                 ListBase targets = {NULL, NULL};
377                                 bConstraintTarget *ct;
378                                 
379                                 if (cti && cti->get_constraint_targets) {
380                                         cti->get_constraint_targets(con, &targets);
381                                         
382                                         for (ct= targets.first; ct; ct= ct->next) {
383                                                 if ((ct->tar == ob) && (ct->subtarget[0])) {
384                                                         bPoseChannel *pchanc= get_pose_channel(ob->pose, ct->subtarget);
385                                                         if((pchanc) && !(pchanc->bone->flag & BONE_UNSELECTABLE)) {
386                                                                 pchanc->bone->flag |= BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL;
387                                                                 found= 1;
388                                                         }
389                                                 }
390                                         }
391                                         
392                                         if (cti->flush_constraint_targets)
393                                                 cti->flush_constraint_targets(con, &targets, 1);
394                                 }
395                         }
396                 }
397         }
398         CTX_DATA_END;
399
400         if (!found)
401                 return OPERATOR_CANCELLED;
402
403         WM_event_add_notifier(C, NC_OBJECT|ND_BONE_SELECT, ob);
404
405         return OPERATOR_FINISHED;
406 }
407
408 void POSE_OT_select_constraint_target(wmOperatorType *ot)
409 {
410         /* identifiers */
411         ot->name= "Select Constraint Target";
412         ot->idname= "POSE_OT_select_constraint_target";
413         ot->description= "Select bones used as targets for the currently selected bones";
414         
415         /* api callbacks */
416         ot->exec= pose_select_constraint_target_exec;
417         ot->poll= ED_operator_posemode;
418         
419         /* flags */
420         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
421 }
422
423 /* ******************* select hierarchy operator ************* */
424
425 static int pose_select_hierarchy_exec(bContext *C, wmOperator *op)
426 {
427         Object *ob= ED_object_pose_armature(CTX_data_active_object(C));
428         bArmature *arm= ob->data;
429         Bone *curbone, *pabone, *chbone;
430         int direction = RNA_enum_get(op->ptr, "direction");
431         int add_to_sel = RNA_boolean_get(op->ptr, "extend");
432         int found= 0;
433         
434         CTX_DATA_BEGIN(C, bPoseChannel *, pchan, visible_pose_bones) 
435         {
436                 curbone= pchan->bone;
437                 
438                 if ((curbone->flag & BONE_UNSELECTABLE)==0) {
439                         if (curbone == arm->act_bone) {
440                                 if (direction == BONE_SELECT_PARENT) {
441                                         if (pchan->parent == NULL) continue;
442                                         else pabone= pchan->parent->bone;
443                                         
444                                         if (PBONE_VISIBLE(arm, pabone)) {
445                                                 if (!add_to_sel) curbone->flag &= ~BONE_SELECTED;
446                                                 pabone->flag |= BONE_SELECTED;
447                                                 arm->act_bone= pabone;
448                                                 
449                                                 found= 1;
450                                                 break;
451                                         }
452                                 } 
453                                 else { /* direction == BONE_SELECT_CHILD */
454                                         if (pchan->child == NULL) continue;
455                                         else chbone = pchan->child->bone;
456                                         
457                                         if (PBONE_VISIBLE(arm, chbone)) {
458                                                 if (!add_to_sel) curbone->flag &= ~BONE_SELECTED;
459                                                 chbone->flag |= BONE_SELECTED;
460                                                 arm->act_bone= chbone;
461                                                 
462                                                 found= 1;
463                                                 break;
464                                         }
465                                 }
466                         }
467                 }
468         }
469         CTX_DATA_END;
470
471         if (found == 0)
472                 return OPERATOR_CANCELLED;
473
474         WM_event_add_notifier(C, NC_OBJECT|ND_BONE_SELECT, ob);
475
476         return OPERATOR_FINISHED;
477 }
478
479 void POSE_OT_select_hierarchy(wmOperatorType *ot)
480 {
481         static EnumPropertyItem direction_items[]= {
482                 {BONE_SELECT_PARENT, "PARENT", 0, "Select Parent", ""},
483                 {BONE_SELECT_CHILD, "CHILD", 0, "Select Child", ""},
484                 {0, NULL, 0, NULL, NULL}
485         };
486         
487         /* identifiers */
488         ot->name= "Select Hierarchy";
489         ot->idname= "POSE_OT_select_hierarchy";
490         ot->description= "Select immediate parent/children of selected bones";
491         
492         /* api callbacks */
493         ot->exec= pose_select_hierarchy_exec;
494         ot->poll= ED_operator_posemode;
495         
496         /* flags */
497         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
498         
499         /* props */
500         ot->prop= RNA_def_enum(ot->srna, "direction", direction_items, BONE_SELECT_PARENT, "Direction", "");
501         RNA_def_boolean(ot->srna, "extend", 0, "Add to Selection", "");
502         
503 }
504
505 /* ******************* select grouped operator ************* */
506
507 static short pose_select_same_group (bContext *C, Object *ob, short extend)
508 {
509         bArmature *arm= (ob)? ob->data : NULL;
510         bPose *pose= (ob)? ob->pose : NULL;
511         char *group_flags;
512         int numGroups = 0;
513         short changed=0, tagged=0;
514         
515         /* sanity checks */
516         if (ELEM3(NULL, ob, pose, arm))
517                 return 0;
518                 
519         /* count the number of groups */
520         numGroups= BLI_countlist(&pose->agroups);
521         if (numGroups == 0)
522                 return 0;
523                 
524         /* alloc a small array to keep track of the groups to use 
525          *      - each cell stores on/off state for whether group should be used
526          *      - size is numGroups + 1, since index=0 is used for no-group
527          */
528         group_flags= MEM_callocN(numGroups+1, "pose_select_same_group");
529         
530         CTX_DATA_BEGIN(C, bPoseChannel *, pchan, visible_pose_bones) 
531         {
532                 /* keep track of group as group to use later? */
533                 if (pchan->bone->flag & BONE_SELECTED) {
534                         group_flags[pchan->agrp_index] = 1;
535                         tagged= 1;
536                 }
537                 
538                 /* deselect all bones before selecting new ones? */
539                 if ((extend == 0) && (pchan->bone->flag & BONE_UNSELECTABLE)==0)
540                         pchan->bone->flag &= ~BONE_SELECTED;
541         }
542         CTX_DATA_END;
543         
544         /* small optimisation: only loop through bones a second time if there are any groups tagged */
545         if (tagged) {
546                 /* only if group matches (and is not selected or current bone) */
547                 CTX_DATA_BEGIN(C, bPoseChannel *, pchan, visible_pose_bones) 
548                 {
549                         if ((pchan->bone->flag & BONE_UNSELECTABLE)==0) {
550                                 /* check if the group used by this bone is counted */
551                                 if (group_flags[pchan->agrp_index]) {
552                                         pchan->bone->flag |= BONE_SELECTED;
553                                         changed= 1;
554                                 }
555                         }
556                 }
557                 CTX_DATA_END;
558         }
559         
560         /* free temp info */
561         MEM_freeN(group_flags);
562         
563         return changed;
564 }
565
566 static short pose_select_same_layer (bContext *C, Object *ob, short extend)
567 {
568         bPose *pose= (ob)? ob->pose : NULL;
569         bArmature *arm= (ob)? ob->data : NULL;
570         short changed= 0;
571         int layers= 0;
572         
573         if (ELEM3(NULL, ob, pose, arm))
574                 return 0;
575         
576         /* figure out what bones are selected */
577         CTX_DATA_BEGIN(C, bPoseChannel *, pchan, visible_pose_bones) 
578         {
579                 /* keep track of layers to use later? */
580                 if (pchan->bone->flag & BONE_SELECTED)
581                         layers |= pchan->bone->layer;
582                         
583                 /* deselect all bones before selecting new ones? */
584                 if ((extend == 0) && (pchan->bone->flag & BONE_UNSELECTABLE)==0)
585                         pchan->bone->flag &= ~BONE_SELECTED;
586         }
587         CTX_DATA_END;
588         if (layers == 0) 
589                 return 0;
590                 
591         /* select bones that are on same layers as layers flag */
592         CTX_DATA_BEGIN(C, bPoseChannel *, pchan, visible_pose_bones) 
593         {
594                 /* if bone is on a suitable layer, and the bone can have its selection changed, select it */
595                 if ((layers & pchan->bone->layer) && (pchan->bone->flag & BONE_UNSELECTABLE)==0) {
596                         pchan->bone->flag |= BONE_SELECTED;
597                         changed= 1;
598                 }
599         }
600         CTX_DATA_END;
601         
602         return changed;
603 }
604
605
606 static int pose_select_grouped_exec (bContext *C, wmOperator *op)
607 {
608         Object *ob= ED_object_pose_armature(CTX_data_active_object(C));
609         short extend= RNA_boolean_get(op->ptr, "extend");
610         short changed = 0;
611         
612         /* sanity check */
613         if (ELEM(NULL, ob, ob->pose))
614                 return OPERATOR_CANCELLED;
615                 
616         /* selection types 
617          * NOTE: for the order of these, see the enum in POSE_OT_select_grouped()
618          */
619         switch (RNA_enum_get(op->ptr, "type")) {
620                 case 1: /* group */
621                         changed= pose_select_same_group(C, ob, extend);
622                         break;
623                 default: /* layer */
624                         changed= pose_select_same_layer(C, ob, extend);
625                         break;
626         }
627         
628         /* notifiers for updates */
629         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
630         
631         /* report done status */
632         if (changed)
633                 return OPERATOR_FINISHED;
634         else
635                 return OPERATOR_CANCELLED;
636 }
637
638 void POSE_OT_select_grouped (wmOperatorType *ot)
639 {
640         static EnumPropertyItem prop_select_grouped_types[] = {
641                 {0, "LAYER", 0, "Layer", "Shared layers"},
642                 {1, "GROUP", 0, "Group", "Shared group"},
643                 {0, NULL, 0, NULL, NULL}
644         };
645
646         /* identifiers */
647         ot->name= "Select Grouped";
648         ot->description = "Select all visible bones grouped by similar properties";
649         ot->idname= "POSE_OT_select_grouped";
650         
651         /* api callbacks */
652         ot->invoke= WM_menu_invoke;
653         ot->exec= pose_select_grouped_exec;
654         ot->poll= ED_operator_posemode;
655         
656         /* flags */
657         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
658         
659         /* properties */
660         RNA_def_boolean(ot->srna, "extend", FALSE, "Extend", "Extend selection instead of deselecting everything first.");
661         ot->prop= RNA_def_enum(ot->srna, "type", prop_select_grouped_types, 0, "Type", "");
662 }
663
664 /* ********************************************** */
665
666 void pose_copy_menu(Scene *scene)
667 {
668         Object *obedit= scene->obedit; // XXX context
669         Object *ob= OBACT;
670         bArmature *arm= ob->data;
671         bPoseChannel *pchan, *pchanact;
672         short nr=0;
673         int i=0;
674         
675         /* paranoia checks */
676         if (ELEM(NULL, ob, ob->pose)) return;
677         if ((ob==obedit) || (ob->mode & OB_MODE_POSE)==0) return;
678         
679         pchan= get_active_posechannel(ob);
680         
681         if (pchan==NULL) return;
682         pchanact= pchan;
683         
684         /* if proxy-protected bones selected, some things (such as locks + displays) shouldn't be changable, 
685          * but for constraints (just add local constraints)
686          */
687         if (pose_has_protected_selected(ob, 0)) {
688                 i= BLI_countlist(&(pchanact->constraints)); /* if there are 24 or less, allow for the user to select constraints */
689                 if (i < 25)
690                         nr= pupmenu("Copy Pose Attributes %t|Local Location%x1|Local Rotation%x2|Local Size%x3|%l|Visual Location %x9|Visual Rotation%x10|Visual Size%x11|%l|Constraints (All)%x4|Constraints...%x5");
691                 else
692                         nr= pupmenu("Copy Pose Attributes %t|Local Location%x1|Local Rotation%x2|Local Size%x3|%l|Visual Location %x9|Visual Rotation%x10|Visual Size%x11|%l|Constraints (All)%x4");
693         }
694         else {
695                 i= BLI_countlist(&(pchanact->constraints)); /* if there are 24 or less, allow for the user to select constraints */
696                 if (i < 25)
697                         nr= pupmenu("Copy Pose Attributes %t|Local Location%x1|Local Rotation%x2|Local Size%x3|%l|Visual Location %x9|Visual Rotation%x10|Visual Size%x11|%l|Constraints (All)%x4|Constraints...%x5|%l|Transform Locks%x6|IK Limits%x7|Bone Shape%x8");
698                 else
699                         nr= pupmenu("Copy Pose Attributes %t|Local Location%x1|Local Rotation%x2|Local Size%x3|%l|Visual Location %x9|Visual Rotation%x10|Visual Size%x11|%l|Constraints (All)%x4|%l|Transform Locks%x6|IK Limits%x7|Bone Shape%x8");
700         }
701         
702         if (nr <= 0) 
703                 return;
704         
705         if (nr != 5)  {
706                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
707                         if ( (arm->layer & pchan->bone->layer) &&
708                                  (pchan->bone->flag & BONE_SELECTED) &&
709                                  (pchan != pchanact) ) 
710                         {
711                                 switch (nr) {
712                                         case 1: /* Local Location */
713                                                 VECCOPY(pchan->loc, pchanact->loc);
714                                                 break;
715                                         case 2: /* Local Rotation */
716                                                 QUATCOPY(pchan->quat, pchanact->quat);
717                                                 VECCOPY(pchan->eul, pchanact->eul);
718                                                 break;
719                                         case 3: /* Local Size */
720                                                 VECCOPY(pchan->size, pchanact->size);
721                                                 break;
722                                         case 4: /* All Constraints */
723                                         {
724                                                 ListBase tmp_constraints = {NULL, NULL};
725                                                 
726                                                 /* copy constraints to tmpbase and apply 'local' tags before 
727                                                  * appending to list of constraints for this channel
728                                                  */
729                                                 copy_constraints(&tmp_constraints, &pchanact->constraints, TRUE);
730                                                 if ((ob->proxy) && (pchan->bone->layer & arm->layer_protected)) {
731                                                         bConstraint *con;
732                                                         
733                                                         /* add proxy-local tags */
734                                                         for (con= tmp_constraints.first; con; con= con->next)
735                                                                 con->flag |= CONSTRAINT_PROXY_LOCAL;
736                                                 }
737                                                 BLI_movelisttolist(&pchan->constraints, &tmp_constraints);
738                                                 
739                                                 /* update flags (need to add here, not just copy) */
740                                                 pchan->constflag |= pchanact->constflag;
741                                                 
742                                                 if (ob->pose)
743                                                         ob->pose->flag |= POSE_RECALC;
744                                         }
745                                                 break;
746                                         case 6: /* Transform Locks */
747                                                 pchan->protectflag = pchanact->protectflag;
748                                                 break;
749                                         case 7: /* IK (DOF) settings */
750                                         {
751                                                 pchan->ikflag = pchanact->ikflag;
752                                                 VECCOPY(pchan->limitmin, pchanact->limitmin);
753                                                 VECCOPY(pchan->limitmax, pchanact->limitmax);
754                                                 VECCOPY(pchan->stiffness, pchanact->stiffness);
755                                                 pchan->ikstretch= pchanact->ikstretch;
756                                                 pchan->ikrotweight= pchanact->ikrotweight;
757                                                 pchan->iklinweight= pchanact->iklinweight;
758                                         }
759                                                 break;
760                                         case 8: /* Custom Bone Shape */
761                                                 pchan->custom = pchanact->custom;
762                                                 break;
763                                         case 9: /* Visual Location */
764                                                 armature_loc_pose_to_bone(pchan, pchanact->pose_mat[3], pchan->loc);
765                                                 break;
766                                         case 10: /* Visual Rotation */
767                                         {
768                                                 float delta_mat[4][4];
769                                                 
770                                                 armature_mat_pose_to_bone(pchan, pchanact->pose_mat, delta_mat);
771                                                 
772                                                 if (pchan->rotmode == ROT_MODE_AXISANGLE) {
773                                                         float tmp_quat[4];
774                                                         
775                                                         /* need to convert to quat first (in temp var)... */
776                                                         mat4_to_quat( tmp_quat,delta_mat);
777                                                         quat_to_axis_angle( pchan->rotAxis, &pchan->rotAngle,tmp_quat);
778                                                 }
779                                                 else if (pchan->rotmode == ROT_MODE_QUAT)
780                                                         mat4_to_quat( pchan->quat,delta_mat);
781                                                 else
782                                                         mat4_to_eulO( pchan->eul, pchan->rotmode,delta_mat);
783                                         }
784                                                 break;
785                                         case 11: /* Visual Size */
786                                         {
787                                                 float delta_mat[4][4], size[4];
788                                                 
789                                                 armature_mat_pose_to_bone(pchan, pchanact->pose_mat, delta_mat);
790                                                 mat4_to_size( size,delta_mat);
791                                                 VECCOPY(pchan->size, size);
792                                         }
793                                 }
794                         }
795                 }
796         } 
797         else { /* constraints, optional (note: max we can have is 24 constraints) */
798                 bConstraint *con, *con_back;
799                 int const_toggle[24]= {0}; /* XXX, initialize as 0 to quiet errors */
800                 ListBase const_copy = {NULL, NULL};
801                 
802                 BLI_duplicatelist(&const_copy, &(pchanact->constraints));
803                 
804                 /* build the puplist of constraints */
805                 for (con = pchanact->constraints.first, i=0; con; con=con->next, i++){
806                         const_toggle[i]= 1;
807 //                      add_numbut(i, TOG|INT, con->name, 0, 0, &(const_toggle[i]), "");
808                 }
809                 
810 //              if (!do_clever_numbuts("Select Constraints", i, REDRAW)) {
811 //                      BLI_freelistN(&const_copy);
812 //                      return;
813 //              }
814                 
815                 /* now build a new listbase from the options selected */
816                 for (i=0, con=const_copy.first; con; i++) {
817                         /* if not selected, free/remove it from the list */
818                         if (!const_toggle[i]) {
819                                 con_back= con->next;
820                                 BLI_freelinkN(&const_copy, con);
821                                 con= con_back;
822                         } 
823                         else
824                                 con= con->next;
825                 }
826                 
827                 /* Copy the temo listbase to the selected posebones */
828                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
829                         if ( (arm->layer & pchan->bone->layer) &&
830                                  (pchan->bone->flag & BONE_SELECTED) &&
831                                  (pchan!=pchanact) ) 
832                         {
833                                 ListBase tmp_constraints = {NULL, NULL};
834                                 
835                                 /* copy constraints to tmpbase and apply 'local' tags before 
836                                  * appending to list of constraints for this channel
837                                  */
838                                 copy_constraints(&tmp_constraints, &const_copy, TRUE);
839                                 if ((ob->proxy) && (pchan->bone->layer & arm->layer_protected)) {                                       
840                                         /* add proxy-local tags */
841                                         for (con= tmp_constraints.first; con; con= con->next)
842                                                 con->flag |= CONSTRAINT_PROXY_LOCAL;
843                                 }
844                                 BLI_movelisttolist(&pchan->constraints, &tmp_constraints);
845                                 
846                                 /* update flags (need to add here, not just copy) */
847                                 pchan->constflag |= pchanact->constflag;
848                         }
849                 }
850                 BLI_freelistN(&const_copy);
851                 update_pose_constraint_flags(ob->pose); /* we could work out the flags but its simpler to do this */
852                 
853                 if (ob->pose)
854                         ob->pose->flag |= POSE_RECALC;
855         }
856         
857         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);     // and all its relations
858         
859         BIF_undo_push("Copy Pose Attributes");
860         
861 }
862
863 /* ******************** copy/paste pose ********************** */
864
865 /* Global copy/paste buffer for pose - cleared on start/end session + before every copy operation */
866 static bPose *g_posebuf = NULL;
867
868 void free_posebuf(void) 
869 {
870         if (g_posebuf) {
871                 bPoseChannel *pchan;
872                 
873                 for (pchan= g_posebuf->chanbase.first; pchan; pchan= pchan->next) {
874                         if(pchan->prop) {
875                                 IDP_FreeProperty(pchan->prop);
876                                 MEM_freeN(pchan->prop);
877                         }
878                 }
879                 
880                 /* was copied without constraints */
881                 BLI_freelistN(&g_posebuf->chanbase);
882                 MEM_freeN(g_posebuf);
883         }
884         
885         g_posebuf=NULL;
886 }
887
888 /* ---- */
889
890 static int pose_copy_exec (bContext *C, wmOperator *op)
891 {
892         Object *ob= ED_object_pose_armature(CTX_data_active_object(C));
893         
894         /* sanity checking */
895         if ELEM(NULL, ob, ob->pose) {
896                 BKE_report(op->reports, RPT_ERROR, "No Pose to Copy");
897                 return OPERATOR_CANCELLED;
898         }
899
900         /* free existing pose buffer */
901         free_posebuf();
902         
903         /* sets chan->flag to POSE_KEY if bone selected, then copy those bones to the buffer */
904         set_pose_keys(ob);  
905         copy_pose(&g_posebuf, ob->pose, 0);
906         
907         
908         return OPERATOR_FINISHED;
909 }
910
911 void POSE_OT_copy (wmOperatorType *ot) 
912 {
913         /* identifiers */
914         ot->name= "Copy Pose";
915         ot->idname= "POSE_OT_copy";
916         ot->description= "Copies the current pose of the selected bones to copy/paste buffer";
917         
918         /* api callbacks */
919         ot->exec= pose_copy_exec;
920         ot->poll= ED_operator_posemode;
921         
922         /* flag */
923         ot->flag= OPTYPE_REGISTER;
924 }
925
926 /* ---- */
927
928 static int pose_paste_exec (bContext *C, wmOperator *op)
929 {
930         Scene *scene= CTX_data_scene(C);
931         Object *ob= ED_object_pose_armature(CTX_data_active_object(C));
932         bPoseChannel *chan, *pchan;
933         int flip= RNA_boolean_get(op->ptr, "flipped");
934         
935         /* sanity checks */
936         if ELEM(NULL, ob, ob->pose)
937                 return OPERATOR_CANCELLED;
938
939         if (g_posebuf == NULL) {
940                 BKE_report(op->reports, RPT_ERROR, "Copy buffer is empty");
941                 return OPERATOR_CANCELLED;
942         }
943         
944         /* Safely merge all of the channels in the buffer pose into any existing pose */
945         for (chan= g_posebuf->chanbase.first; chan; chan=chan->next) {
946                 if (chan->flag & POSE_KEY) {
947                         /* get the name - if flipping, we must flip this first */
948                         char name[32];
949                         if (flip)
950                                 flip_side_name(name, chan->name, 0);            /* 0 = don't strip off number extensions */
951                         else
952                                 BLI_strncpy(name, chan->name, sizeof(name));
953                         
954                         /* only copy when channel exists, poses are not meant to add random channels to anymore */
955                         pchan= get_pose_channel(ob->pose, name);
956                         
957                         if (pchan) {
958                                 /* only loc rot size 
959                                  *      - only copies transform info for the pose 
960                                  */
961                                 VECCOPY(pchan->loc, chan->loc);
962                                 VECCOPY(pchan->size, chan->size);
963                                 pchan->flag= chan->flag;
964                                 
965                                 /* check if rotation modes are compatible (i.e. do they need any conversions) */
966                                 if (pchan->rotmode == chan->rotmode) {
967                                         /* copy the type of rotation in use */
968                                         if (pchan->rotmode > 0) {
969                                                 VECCOPY(pchan->eul, chan->eul);
970                                         }
971                                         else if (pchan->rotmode == ROT_MODE_AXISANGLE) {
972                                                 VECCOPY(pchan->rotAxis, chan->rotAxis);
973                                                 pchan->rotAngle = chan->rotAngle;
974                                         }
975                                         else {
976                                                 QUATCOPY(pchan->quat, chan->quat);
977                                         }
978                                 }
979                                 else if (pchan->rotmode > 0) {
980                                         /* quat/axis-angle to euler */
981                                         if (chan->rotmode == ROT_MODE_AXISANGLE)
982                                                 axis_angle_to_eulO( pchan->eul, pchan->rotmode,chan->rotAxis, chan->rotAngle);
983                                         else
984                                                 quat_to_eulO( pchan->eul, pchan->rotmode,chan->quat);
985                                 }
986                                 else if (pchan->rotmode == ROT_MODE_AXISANGLE) {
987                                         /* quat/euler to axis angle */
988                                         if (chan->rotmode > 0)
989                                                 eulO_to_axis_angle(pchan->rotAxis, &pchan->rotAngle, chan->eul, chan->rotmode);
990                                         else    
991                                                 quat_to_axis_angle(pchan->rotAxis, &pchan->rotAngle, chan->quat);
992                                 }
993                                 else {
994                                         /* euler/axis-angle to quat */
995                                         if (chan->rotmode > 0)
996                                                 eulO_to_quat(pchan->quat, chan->eul, chan->rotmode);
997                                         else
998                                                 axis_angle_to_quat(pchan->quat, chan->rotAxis, pchan->rotAngle);
999                                 }
1000                                 
1001                                 /* paste flipped pose? */
1002                                 if (flip) {
1003                                         pchan->loc[0]*= -1;
1004                                         
1005                                         /* has to be done as eulers... */
1006                                         if (pchan->rotmode > 0) {
1007                                                 pchan->eul[1] *= -1;
1008                                                 pchan->eul[2] *= -1;
1009                                         }
1010                                         else if (pchan->rotmode == ROT_MODE_AXISANGLE) {
1011                                                 float eul[3];
1012                                                 
1013                                                 axis_angle_to_eulO(eul, EULER_ORDER_DEFAULT, pchan->rotAxis, pchan->rotAngle);
1014                                                 eul[1]*= -1;
1015                                                 eul[2]*= -1;
1016                                                 eulO_to_axis_angle(pchan->rotAxis, &pchan->rotAngle, eul, EULER_ORDER_DEFAULT);
1017                                         }
1018                                         else {
1019                                                 float eul[3];
1020                                                 
1021                                                 normalize_qt(pchan->quat);
1022                                                 quat_to_eul(eul, pchan->quat);
1023                                                 eul[1]*= -1;
1024                                                 eul[2]*= -1;
1025                                                 eul_to_quat(pchan->quat, eul);
1026                                         }
1027                                 }
1028                                 
1029                                 /* ID properties 
1030                                  *      - only free the existing properties if the channel we're copying from has them
1031                                  *        NOTE: this means that if the pose depends on some pchan property, the pose may not be ok,
1032                                  *                  but this is better than loosing all the setting you've painstakingly added...
1033                                  */
1034                                 if (chan->prop) {
1035                                         /* free the old properties since we want to replace them now */
1036                                         if (pchan->prop) {
1037                                                 IDP_FreeProperty(pchan->prop);
1038                                                 MEM_freeN(pchan->prop);
1039                                                 pchan->prop= NULL;
1040                                         }
1041                                         
1042                                         /* now copy over the new copy of the properties */
1043                                         pchan->prop= IDP_CopyProperty(chan->prop);      
1044                                 }
1045                                 
1046                                 /* keyframing tagging */
1047                                 if (autokeyframe_cfra_can_key(scene, &ob->id)) {
1048                                         ListBase dsources = {NULL, NULL};
1049                                         
1050                                         /* get KeyingSet to use */
1051                                         KeyingSet *ks = ANIM_get_keyingset_for_autokeying(scene, "LocRotScale");
1052                                         
1053                                         /* now insert the keyframe(s) using the Keying Set
1054                                          *      1) add datasource override for the PoseChannel
1055                                          *      2) insert keyframes
1056                                          *      3) free the extra info 
1057                                          */
1058                                         ANIM_relative_keyingset_add_source(&dsources, &ob->id, &RNA_PoseBone, pchan); 
1059                                         ANIM_apply_keyingset(C, &dsources, NULL, ks, MODIFYKEY_MODE_INSERT, (float)CFRA);
1060                                         BLI_freelistN(&dsources);
1061                                         
1062                                         /* clear any unkeyed tags */
1063                                         if (chan->bone)
1064                                                 chan->bone->flag &= ~BONE_UNKEYED;
1065                                 }
1066                                 else {
1067                                         /* add unkeyed tags */
1068                                         if (chan->bone)
1069                                                 chan->bone->flag |= BONE_UNKEYED;
1070                                 }
1071                         }
1072                 }
1073         }
1074         
1075         /* Update event for pose and deformation children */
1076         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1077                 
1078         /* notifiers for updates */
1079         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1080
1081         return OPERATOR_FINISHED;
1082 }
1083
1084 void POSE_OT_paste (wmOperatorType *ot)
1085 {
1086         /* identifiers */
1087         ot->name= "Paste Pose";
1088         ot->idname= "POSE_OT_paste";
1089         ot->description= "Pastes the stored pose on to the current pose";
1090         
1091         /* api callbacks */
1092         ot->exec= pose_paste_exec;
1093         ot->poll= ED_operator_posemode;
1094         
1095         /* flag */
1096         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1097         
1098         /* properties */
1099         RNA_def_boolean(ot->srna, "flipped", 0, "Flipped on X-Axis", "Paste the stored pose flipped on to current pose");
1100 }
1101
1102 /* ********************************************** */
1103
1104
1105 static int pose_group_add_exec (bContext *C, wmOperator *UNUSED(op))
1106 {
1107         ScrArea *sa= CTX_wm_area(C);
1108         Object *ob;
1109         
1110         /* since this call may also be used from the buttons window, we need to check for where to get the object */
1111         if (sa->spacetype == SPACE_BUTS) 
1112                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1113         else
1114                 ob= ED_object_pose_armature(CTX_data_active_object(C));
1115                 
1116         /* only continue if there's an object */
1117         if (ob == NULL)
1118                 return OPERATOR_CANCELLED;
1119         
1120         /* for now, just call the API function for this */
1121         pose_add_group(ob);
1122         
1123         /* notifiers for updates */
1124         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1125         
1126         return OPERATOR_FINISHED;
1127 }
1128
1129 void POSE_OT_group_add (wmOperatorType *ot)
1130 {
1131         /* identifiers */
1132         ot->name= "Add Bone Group";
1133         ot->idname= "POSE_OT_group_add";
1134         ot->description= "Add a new bone group";
1135         
1136         /* api callbacks */
1137         ot->exec= pose_group_add_exec;
1138         ot->poll= ED_operator_posemode;
1139         
1140         /* flags */
1141         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1142 }
1143
1144
1145 static int pose_group_remove_exec (bContext *C, wmOperator *UNUSED(op))
1146 {
1147         ScrArea *sa= CTX_wm_area(C);
1148         Object *ob;
1149         
1150         /* since this call may also be used from the buttons window, we need to check for where to get the object */
1151         if (sa->spacetype == SPACE_BUTS) 
1152                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1153         else
1154                 ob= ED_object_pose_armature(CTX_data_active_object(C));
1155         
1156         /* only continue if there's an object */
1157         if (ob == NULL)
1158                 return OPERATOR_CANCELLED;
1159         
1160         /* for now, just call the API function for this */
1161         pose_remove_group(ob);
1162         
1163         /* notifiers for updates */
1164         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1165         
1166         return OPERATOR_FINISHED;
1167 }
1168
1169 void POSE_OT_group_remove (wmOperatorType *ot)
1170 {
1171         /* identifiers */
1172         ot->name= "Remove Bone Group";
1173         ot->idname= "POSE_OT_group_remove";
1174         ot->description= "Removes the active bone group";
1175         
1176         /* api callbacks */
1177         ot->exec= pose_group_remove_exec;
1178         ot->poll= ED_operator_posemode;
1179         
1180         /* flags */
1181         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1182 }
1183
1184 /* ------------ */
1185
1186 /* invoke callback which presents a list of bone-groups for the user to choose from */
1187 static int pose_groups_menu_invoke (bContext *C, wmOperator *op, wmEvent *UNUSED(evt))
1188 {
1189         ScrArea *sa= CTX_wm_area(C);
1190         Object *ob;
1191         bPose *pose;
1192         
1193         uiPopupMenu *pup;
1194         uiLayout *layout;
1195         bActionGroup *grp;
1196         int i;
1197         
1198         /* since this call may also be used from the buttons window, we need to check for where to get the object */
1199         if (sa->spacetype == SPACE_BUTS) 
1200                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1201         else
1202                 ob= ED_object_pose_armature(CTX_data_active_object(C));
1203         
1204         /* only continue if there's an object, and a pose there too */
1205         if (ELEM(NULL, ob, ob->pose)) 
1206                 return OPERATOR_CANCELLED;
1207         pose= ob->pose;
1208         
1209         /* if there's no active group (or active is invalid), create a new menu to find it */
1210         if (pose->active_group <= 0) {
1211                 /* create a new menu, and start populating it with group names */
1212                 pup= uiPupMenuBegin(C, op->type->name, ICON_NULL);
1213                 layout= uiPupMenuLayout(pup);
1214                 
1215                 /* special entry - allow to create new group, then use that 
1216                  *      (not to be used for removing though)
1217                  */
1218                 if (strstr(op->idname, "assign")) {
1219                         uiItemIntO(layout, "New Group", ICON_NULL, op->idname, "type", 0);
1220                         uiItemS(layout);
1221                 }
1222                 
1223                 /* add entries for each group */
1224                 for (grp= pose->agroups.first, i=1; grp; grp=grp->next, i++)
1225                         uiItemIntO(layout, grp->name, ICON_NULL, op->idname, "type", i);
1226                         
1227                 /* finish building the menu, and process it (should result in calling self again) */
1228                 uiPupMenuEnd(C, pup);
1229                 
1230                 return OPERATOR_CANCELLED;
1231         }
1232         else {
1233                 /* just use the active group index, and call the exec callback for the calling operator */
1234                 RNA_int_set(op->ptr, "type", pose->active_group);
1235                 return op->type->exec(C, op);
1236         }
1237 }
1238
1239 /* Assign selected pchans to the bone group that the user selects */
1240 static int pose_group_assign_exec (bContext *C, wmOperator *op)
1241 {
1242         ScrArea *sa= CTX_wm_area(C);
1243         Object *ob;
1244         bArmature *arm;
1245         bPose *pose;
1246         short done= 0;
1247         
1248         /* since this call may also be used from the buttons window, we need to check for where to get the object */
1249         if (sa->spacetype == SPACE_BUTS) 
1250                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1251         else
1252                 ob= ED_object_pose_armature(CTX_data_active_object(C));
1253         
1254         /* only continue if there's an object, and a pose there too */
1255         if (ELEM(NULL, ob, ob->pose))
1256                 return OPERATOR_CANCELLED;
1257         arm= ob->data;
1258         pose= ob->pose;
1259         
1260         /* set the active group number to the one from operator props 
1261          *      - if 0 after this, make a new group...
1262          */
1263         pose->active_group= RNA_int_get(op->ptr, "type");
1264         if (pose->active_group == 0)
1265                 pose_add_group(ob);
1266         
1267         /* add selected bones to group then */
1268         CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pose_bones)
1269         {
1270                 pchan->agrp_index= pose->active_group;
1271                 done= 1;
1272         }
1273         CTX_DATA_END;
1274
1275         /* notifiers for updates */
1276         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1277         
1278         /* report done status */
1279         if (done)
1280                 return OPERATOR_FINISHED;
1281         else
1282                 return OPERATOR_CANCELLED;
1283 }
1284
1285 void POSE_OT_group_assign (wmOperatorType *ot)
1286 {
1287         /* identifiers */
1288         ot->name= "Add Selected to Bone Group";
1289         ot->idname= "POSE_OT_group_assign";
1290         ot->description= "Add selected bones to the chosen bone group";
1291         
1292         /* api callbacks */
1293         ot->invoke= pose_groups_menu_invoke;
1294         ot->exec= pose_group_assign_exec;
1295         ot->poll= ED_operator_posemode;
1296         
1297         /* flags */
1298         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1299         
1300         /* properties */
1301         RNA_def_int(ot->srna, "type", 0, 0, 10, "Bone Group Index", "", 0, INT_MAX);
1302 }
1303
1304
1305 static int pose_group_unassign_exec (bContext *C, wmOperator *UNUSED(op))
1306 {
1307         ScrArea *sa= CTX_wm_area(C);
1308         Object *ob;
1309         bArmature *arm;
1310         bPose *pose;
1311         short done= 0;
1312         
1313         /* since this call may also be used from the buttons window, we need to check for where to get the object */
1314         if (sa->spacetype == SPACE_BUTS) 
1315                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1316         else
1317                 ob= ED_object_pose_armature(CTX_data_active_object(C));
1318         
1319         /* only continue if there's an object, and a pose there too */
1320         if (ELEM(NULL, ob, ob->pose))
1321                 return OPERATOR_CANCELLED;
1322         pose= ob->pose;
1323         arm= ob->data;
1324         
1325         /* find selected bones to remove from all bone groups */
1326         CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pose_bones)
1327         {
1328                 if (pchan->agrp_index) {
1329                         pchan->agrp_index= 0;
1330                         done= 1;
1331                 }
1332         }
1333         CTX_DATA_END;
1334         
1335         /* notifiers for updates */
1336         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1337         
1338         /* report done status */
1339         if (done)
1340                 return OPERATOR_FINISHED;
1341         else
1342                 return OPERATOR_CANCELLED;
1343 }
1344
1345 void POSE_OT_group_unassign (wmOperatorType *ot)
1346 {
1347         /* identifiers */
1348         ot->name= "Remove Selected from Bone Groups";
1349         ot->idname= "POSE_OT_group_unassign";
1350         ot->description= "Remove selected bones from all bone groups";
1351         
1352         /* api callbacks */
1353         ot->exec= pose_group_unassign_exec;
1354         ot->poll= ED_operator_posemode;
1355         
1356         /* flags */
1357         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1358 }
1359
1360 static void pose_group_select(bContext *C, Object *ob, int select)
1361 {
1362         bPose *pose= ob->pose;
1363         
1364         CTX_DATA_BEGIN(C, bPoseChannel*, pchan, visible_pose_bones)
1365         {
1366                 if ((pchan->bone->flag & BONE_UNSELECTABLE)==0) {
1367                         if (select) {
1368                                 if (pchan->agrp_index == pose->active_group) 
1369                                         pchan->bone->flag |= BONE_SELECTED;
1370                         }
1371                         else {
1372                                 if (pchan->agrp_index == pose->active_group) 
1373                                         pchan->bone->flag &= ~BONE_SELECTED;
1374                         }
1375                 }
1376         }
1377         CTX_DATA_END;
1378 }
1379
1380 static int pose_group_select_exec (bContext *C, wmOperator *UNUSED(op))
1381 {
1382         ScrArea *sa= CTX_wm_area(C);
1383         Object *ob;
1384         
1385         /* since this call may also be used from the buttons window, we need to check for where to get the object */
1386         if (sa->spacetype == SPACE_BUTS) 
1387                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1388         else
1389                 ob= ED_object_pose_armature(CTX_data_active_object(C));
1390         
1391         /* only continue if there's an object, and a pose there too */
1392         if (ELEM(NULL, ob, ob->pose))
1393                 return OPERATOR_CANCELLED;
1394         
1395         pose_group_select(C, ob, 1);
1396         
1397         /* notifiers for updates */
1398         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1399         
1400         return OPERATOR_FINISHED;
1401 }
1402
1403 void POSE_OT_group_select (wmOperatorType *ot)
1404 {
1405         /* identifiers */
1406         ot->name= "Select Bones of Bone Group";
1407         ot->idname= "POSE_OT_group_select";
1408         ot->description= "Select bones in active Bone Group";
1409         
1410         /* api callbacks */
1411         ot->exec= pose_group_select_exec;
1412         ot->poll= ED_operator_posemode;
1413         
1414         /* flags */
1415         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1416 }
1417
1418 static int pose_group_deselect_exec (bContext *C, wmOperator *UNUSED(op))
1419 {
1420         ScrArea *sa= CTX_wm_area(C);
1421         Object *ob;
1422         
1423         /* since this call may also be used from the buttons window, we need to check for where to get the object */
1424         if (sa->spacetype == SPACE_BUTS) 
1425                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1426         else
1427                 ob= ED_object_pose_armature(CTX_data_active_object(C));
1428         
1429         /* only continue if there's an object, and a pose there too */
1430         if (ELEM(NULL, ob, ob->pose))
1431                 return OPERATOR_CANCELLED;
1432         
1433         pose_group_select(C, ob, 0);
1434         
1435         /* notifiers for updates */
1436         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1437         
1438         return OPERATOR_FINISHED;
1439 }
1440
1441 void POSE_OT_group_deselect (wmOperatorType *ot)
1442 {
1443         /* identifiers */
1444         ot->name= "Deselecte Bone Group";
1445         ot->idname= "POSE_OT_group_deselect";
1446         ot->description= "Deselect bones of active Bone Group";
1447         
1448         /* api callbacks */
1449         ot->exec= pose_group_deselect_exec;
1450         ot->poll= ED_operator_posemode;
1451         
1452         /* flags */
1453         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1454 }
1455
1456 /* ********************************************** */
1457
1458 static int pose_flip_names_exec (bContext *C, wmOperator *UNUSED(op))
1459 {
1460         Object *ob= ED_object_pose_armature(CTX_data_active_object(C));
1461         bArmature *arm;
1462         
1463         /* paranoia checks */
1464         if (ELEM(NULL, ob, ob->pose)) 
1465                 return OPERATOR_CANCELLED;
1466         arm= ob->data;
1467         
1468         /* loop through selected bones, auto-naming them */
1469         CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pose_bones)
1470         {
1471                 char newname[32];
1472                 flip_side_name(newname, pchan->name, TRUE);
1473                 ED_armature_bone_rename(arm, pchan->name, newname);
1474         }
1475         CTX_DATA_END;
1476         
1477         /* since we renamed stuff... */
1478         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1479
1480         /* note, notifier might evolve */
1481         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1482         
1483         return OPERATOR_FINISHED;
1484 }
1485
1486 void POSE_OT_flip_names (wmOperatorType *ot)
1487 {
1488         /* identifiers */
1489         ot->name= "Flip Names";
1490         ot->idname= "POSE_OT_flip_names";
1491         ot->description= "Flips (and corrects) the axis suffixes of the the names of selected bones";
1492         
1493         /* api callbacks */
1494         ot->exec= pose_flip_names_exec;
1495         ot->poll= ED_operator_posemode;
1496         
1497         /* flags */
1498         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1499 }
1500
1501 /* ------------------ */
1502
1503 static int pose_autoside_names_exec (bContext *C, wmOperator *op)
1504 {
1505         Object *ob= ED_object_pose_armature(CTX_data_active_object(C));
1506         bArmature *arm;
1507         char newname[32];
1508         short axis= RNA_enum_get(op->ptr, "axis");
1509         
1510         /* paranoia checks */
1511         if (ELEM(NULL, ob, ob->pose)) 
1512                 return OPERATOR_CANCELLED;
1513         arm= ob->data;
1514         
1515         /* loop through selected bones, auto-naming them */
1516         CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pose_bones)
1517         {
1518                 BLI_strncpy(newname, pchan->name, sizeof(newname));
1519                 if(bone_autoside_name(newname, 1, axis, pchan->bone->head[axis], pchan->bone->tail[axis]))
1520                         ED_armature_bone_rename(arm, pchan->name, newname);
1521         }
1522         CTX_DATA_END;
1523         
1524         /* since we renamed stuff... */
1525         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1526
1527         /* note, notifier might evolve */
1528         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1529         
1530         return OPERATOR_FINISHED;
1531 }
1532
1533 void POSE_OT_autoside_names (wmOperatorType *ot)
1534 {
1535         static EnumPropertyItem axis_items[]= {
1536                  {0, "XAXIS", 0, "X-Axis", "Left/Right"},
1537                 {1, "YAXIS", 0, "Y-Axis", "Front/Back"},
1538                 {2, "ZAXIS", 0, "Z-Axis", "Top/Bottom"},
1539                 {0, NULL, 0, NULL, NULL}};
1540         
1541         /* identifiers */
1542         ot->name= "AutoName by Axis";
1543         ot->idname= "POSE_OT_autoside_names";
1544         ot->description= "Automatically renames the selected bones according to which side of the target axis they fall on";
1545         
1546         /* api callbacks */
1547         ot->invoke= WM_menu_invoke;
1548         ot->exec= pose_autoside_names_exec;
1549         ot->poll= ED_operator_posemode;
1550         
1551         /* flags */
1552         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1553         
1554         /* settings */
1555         ot->prop= RNA_def_enum(ot->srna, "axis", axis_items, 0, "Axis", "Axis tag names with.");
1556 }
1557
1558 /* ********************************************** */
1559
1560 /* context active object, or weightpainted object with armature in posemode */
1561 void pose_activate_flipped_bone(Scene *scene)
1562 {
1563         Object *ob= OBACT;
1564         bArmature *arm= ob->data;
1565         
1566         if(ob==NULL) return;
1567
1568         if(ob->mode & OB_MODE_WEIGHT_PAINT) {
1569                 ob= modifiers_isDeformedByArmature(ob);
1570         }
1571         if(ob && (ob->mode & OB_MODE_POSE)) {
1572                 bPoseChannel *pchanf;
1573                 
1574                 if(arm->act_bone) {
1575                         char name[32];
1576                         flip_side_name(name, arm->act_bone->name, TRUE);
1577                         
1578                         pchanf= get_pose_channel(ob->pose, name);
1579                         if(pchanf && pchanf->bone != arm->act_bone) {
1580                                 arm->act_bone->flag &= ~BONE_SELECTED;
1581                                 pchanf->bone->flag |= BONE_SELECTED;
1582                                 
1583                                 arm->act_bone= pchanf->bone;
1584                                 
1585                                 /* in weightpaint we select the associated vertex group too */
1586                                 if(ob->mode & OB_MODE_WEIGHT_PAINT) {
1587                                         ED_vgroup_select_by_name(OBACT, name);
1588                                         DAG_id_tag_update(&OBACT->id, OB_RECALC_DATA);
1589                                 }
1590                                 
1591                                 // XXX notifiers need to be sent to other editors to update
1592                                 
1593                         }                       
1594                 }
1595         }
1596 }
1597
1598
1599 /* ********************************************** */
1600
1601 /* Show all armature layers */
1602 static int pose_armature_layers_showall_poll (bContext *C)
1603 {
1604         /* this single operator can be used in posemode OR editmode for armatures */
1605         return ED_operator_posemode(C) || ED_operator_editarmature(C);
1606 }
1607
1608 static int pose_armature_layers_showall_exec (bContext *C, wmOperator *op)
1609 {
1610         Object *ob= ED_object_pose_armature(CTX_data_active_object(C));
1611         bArmature *arm = (ob)? ob->data : NULL;
1612         PointerRNA ptr;
1613         int maxLayers = (RNA_boolean_get(op->ptr, "all"))? 32 : 16;
1614         int layers[32] = {0}; /* hardcoded for now - we can only have 32 armature layers, so this should be fine... */
1615         int i;
1616         
1617         /* sanity checking */
1618         if (arm == NULL)
1619                 return OPERATOR_CANCELLED;
1620         
1621         /* use RNA to set the layers
1622          *      although it would be faster to just set directly using bitflags, we still
1623          *      need to setup a RNA pointer so that we get the "update" callbacks for free...
1624          */
1625         RNA_id_pointer_create(&arm->id, &ptr);
1626         
1627         for (i = 0; i < maxLayers; i++)
1628                 layers[i] = 1;
1629         
1630         RNA_boolean_set_array(&ptr, "layers", layers);
1631         
1632         /* note, notifier might evolve */
1633         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1634         
1635         /* done */
1636         return OPERATOR_FINISHED;
1637 }
1638
1639 void ARMATURE_OT_layers_show_all (wmOperatorType *ot)
1640 {
1641         /* identifiers */
1642         ot->name= "Show All Layers";
1643         ot->idname= "ARMATURE_OT_layers_show_all";
1644         ot->description= "Make all armature layers visible";
1645         
1646         /* callbacks */
1647         ot->exec= pose_armature_layers_showall_exec;
1648         ot->poll= pose_armature_layers_showall_poll;
1649         
1650         /* flags */
1651         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1652         
1653         /* properties */
1654         ot->prop = RNA_def_boolean(ot->srna, "all", 1, "All Layers", "Enable all layers or just the first 16 (top row)");
1655 }
1656
1657 /* ------------------- */
1658
1659 /* Present a popup to get the layers that should be used */
1660 static int pose_armature_layers_invoke (bContext *C, wmOperator *op, wmEvent *evt)
1661 {
1662         Object *ob= ED_object_pose_armature(CTX_data_active_object(C));
1663         bArmature *arm= (ob)? ob->data : NULL;
1664         PointerRNA ptr;
1665         int layers[32]; /* hardcoded for now - we can only have 32 armature layers, so this should be fine... */
1666         
1667         /* sanity checking */
1668         if (arm == NULL)
1669                 return OPERATOR_CANCELLED;
1670                 
1671         /* get RNA pointer to armature data to use that to retrieve the layers as ints to init the operator */
1672         RNA_id_pointer_create((ID *)arm, &ptr);
1673         RNA_boolean_get_array(&ptr, "layers", layers);
1674         RNA_boolean_set_array(op->ptr, "layers", layers);
1675         
1676         /* part to sync with other similar operators... */
1677         return WM_operator_props_popup(C, op, evt);
1678 }
1679
1680 /* Set the visible layers for the active armature (edit and pose modes) */
1681 static int pose_armature_layers_exec (bContext *C, wmOperator *op)
1682 {
1683         Object *ob= ED_object_pose_armature(CTX_data_active_object(C));
1684         bArmature *arm= (ob)? ob->data : NULL;
1685         PointerRNA ptr;
1686         int layers[32]; /* hardcoded for now - we can only have 32 armature layers, so this should be fine... */
1687         
1688         /* get the values set in the operator properties */
1689         RNA_boolean_get_array(op->ptr, "layers", layers);
1690         
1691         /* get pointer for armature, and write data there... */
1692         RNA_id_pointer_create((ID *)arm, &ptr);
1693         RNA_boolean_set_array(&ptr, "layers", layers);
1694         
1695         /* note, notifier might evolve */
1696         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1697         
1698         return OPERATOR_FINISHED;
1699 }
1700
1701
1702 void POSE_OT_armature_layers (wmOperatorType *ot)
1703 {
1704         /* identifiers */
1705         ot->name= "Change Armature Layers";
1706         ot->idname= "POSE_OT_armature_layers";
1707         ot->description= "Change the visible armature layers";
1708         
1709         /* callbacks */
1710         ot->invoke= pose_armature_layers_invoke;
1711         ot->exec= pose_armature_layers_exec;
1712         ot->poll= ED_operator_posemode;
1713         
1714         /* flags */
1715         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1716         
1717         /* properties */
1718         RNA_def_boolean_layer_member(ot->srna, "layers", 32, NULL, "Layer", "Armature layers to make visible");
1719 }
1720
1721 void ARMATURE_OT_armature_layers (wmOperatorType *ot)
1722 {
1723         /* identifiers */
1724         ot->name= "Change Armature Layers";
1725         ot->idname= "ARMATURE_OT_armature_layers";
1726         ot->description= "Change the visible armature layers";
1727         
1728         /* callbacks */
1729         ot->invoke= pose_armature_layers_invoke;
1730         ot->exec= pose_armature_layers_exec;
1731         ot->poll= ED_operator_editarmature;
1732         
1733         /* flags */
1734         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1735         
1736         /* properties */
1737         RNA_def_boolean_layer_member(ot->srna, "layers", 32, NULL, "Layer", "Armature layers to make visible");
1738 }
1739
1740 /* ------------------- */
1741
1742 /* Present a popup to get the layers that should be used */
1743 static int pose_bone_layers_invoke (bContext *C, wmOperator *op, wmEvent *evt)
1744 {
1745         int layers[32]= {0}; /* hardcoded for now - we can only have 32 armature layers, so this should be fine... */
1746         
1747         /* get layers that are active already */        
1748         CTX_DATA_BEGIN(C, bPoseChannel *, pchan, selected_pose_bones) 
1749         {
1750                 short bit;
1751                 
1752                 /* loop over the bits for this pchan's layers, adding layers where they're needed */
1753                 for (bit= 0; bit < 32; bit++) {
1754                         if (pchan->bone->layer & (1<<bit))
1755                                 layers[bit]= 1;
1756                 }
1757         }
1758         CTX_DATA_END;
1759         
1760         /* copy layers to operator */
1761         RNA_boolean_set_array(op->ptr, "layers", layers);
1762         
1763                 /* part to sync with other similar operators... */
1764         return WM_operator_props_popup(C, op, evt);
1765 }
1766
1767 /* Set the visible layers for the active armature (edit and pose modes) */
1768 static int pose_bone_layers_exec (bContext *C, wmOperator *op)
1769 {
1770         Object *ob= ED_object_pose_armature(CTX_data_active_object(C));
1771         bArmature *arm= (ob)? ob->data : NULL;
1772         PointerRNA ptr;
1773         int layers[32]; /* hardcoded for now - we can only have 32 armature layers, so this should be fine... */
1774         
1775         /* get the values set in the operator properties */
1776         RNA_boolean_get_array(op->ptr, "layers", layers);
1777         
1778         /* set layers of pchans based on the values set in the operator props */
1779         CTX_DATA_BEGIN(C, bPoseChannel *, pchan, selected_pose_bones) 
1780         {
1781                 /* get pointer for pchan, and write flags this way */
1782                 RNA_pointer_create((ID *)arm, &RNA_Bone, pchan->bone, &ptr);
1783                 RNA_boolean_set_array(&ptr, "layers", layers);
1784         }
1785         CTX_DATA_END;
1786         
1787         /* note, notifier might evolve */
1788         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1789         
1790         return OPERATOR_FINISHED;
1791 }
1792
1793 void POSE_OT_bone_layers (wmOperatorType *ot)
1794 {
1795         /* identifiers */
1796         ot->name= "Change Bone Layers";
1797         ot->idname= "POSE_OT_bone_layers";
1798         ot->description= "Change the layers that the selected bones belong to";
1799         
1800         /* callbacks */
1801         ot->invoke= pose_bone_layers_invoke;
1802         ot->exec= pose_bone_layers_exec;
1803         ot->poll= ED_operator_posemode;
1804         
1805         /* flags */
1806         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1807         
1808         /* properties */
1809         RNA_def_boolean_layer_member(ot->srna, "layers", 32, NULL, "Layer", "Armature layers that bone belongs to");
1810 }
1811
1812 /* ------------------- */
1813
1814 /* Present a popup to get the layers that should be used */
1815 static int armature_bone_layers_invoke (bContext *C, wmOperator *op, wmEvent *evt)
1816 {
1817         int layers[32]= {0}; /* hardcoded for now - we can only have 32 armature layers, so this should be fine... */
1818         
1819         /* get layers that are active already */
1820         CTX_DATA_BEGIN(C, EditBone *, ebone, selected_editable_bones) 
1821         {
1822                 short bit;
1823                 
1824                 /* loop over the bits for this pchan's layers, adding layers where they're needed */
1825                 for (bit= 0; bit < 32; bit++) {
1826                         if (ebone->layer & (1<<bit))
1827                                 layers[bit]= 1;
1828                 }
1829         }
1830         CTX_DATA_END;
1831         
1832         /* copy layers to operator */
1833         RNA_boolean_set_array(op->ptr, "layers", layers);
1834         
1835                 /* part to sync with other similar operators... */
1836         return WM_operator_props_popup(C, op, evt);
1837 }
1838
1839 /* Set the visible layers for the active armature (edit and pose modes) */
1840 static int armature_bone_layers_exec (bContext *C, wmOperator *op)
1841 {
1842         Object *ob= CTX_data_edit_object(C);
1843         bArmature *arm= (ob)? ob->data : NULL;
1844         PointerRNA ptr;
1845         int layers[32]; /* hardcoded for now - we can only have 32 armature layers, so this should be fine... */
1846         
1847         /* get the values set in the operator properties */
1848         RNA_boolean_get_array(op->ptr, "layers", layers);
1849         
1850         /* set layers of pchans based on the values set in the operator props */
1851         CTX_DATA_BEGIN(C, EditBone *, ebone, selected_editable_bones) 
1852         {
1853                 /* get pointer for pchan, and write flags this way */
1854                 RNA_pointer_create((ID *)arm, &RNA_EditBone, ebone, &ptr);
1855                 RNA_boolean_set_array(&ptr, "layers", layers);
1856         }
1857         CTX_DATA_END;
1858         
1859         /* note, notifier might evolve */
1860         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1861         
1862         return OPERATOR_FINISHED;
1863 }
1864
1865 void ARMATURE_OT_bone_layers (wmOperatorType *ot)
1866 {
1867         /* identifiers */
1868         ot->name= "Change Bone Layers";
1869         ot->idname= "ARMATURE_OT_bone_layers";
1870         ot->description= "Change the layers that the selected bones belong to";
1871         
1872         /* callbacks */
1873         ot->invoke= armature_bone_layers_invoke;
1874         ot->exec= armature_bone_layers_exec;
1875         ot->poll= ED_operator_editarmature;
1876         
1877         /* flags */
1878         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1879         
1880         /* properties */
1881         RNA_def_boolean_layer_member(ot->srna, "layers", 32, NULL, "Layer", "Armature layers that bone belongs to");
1882 }
1883
1884 /* ********************************************** */
1885
1886 static int pose_flip_quats_exec (bContext *C, wmOperator *UNUSED(op))
1887 {
1888         Scene *scene= CTX_data_scene(C);
1889         Object *ob= ED_object_pose_armature(CTX_data_active_object(C));
1890         KeyingSet *ks = ANIM_builtin_keyingset_get_named(NULL, "LocRotScale");
1891         
1892         /* loop through all selected pchans, flipping and keying (as needed) */
1893         CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pose_bones)
1894         {
1895                 /* only if bone is using quaternion rotation */
1896                 if (pchan->rotmode == ROT_MODE_QUAT) {
1897                         /* quaternions have 720 degree range */
1898                         pchan->quat[0]= -pchan->quat[0];
1899                         pchan->quat[1]= -pchan->quat[1];
1900                         pchan->quat[2]= -pchan->quat[2];
1901                         pchan->quat[3]= -pchan->quat[3];
1902                         
1903                         /* tagging */
1904                         if (autokeyframe_cfra_can_key(scene, &ob->id)) {
1905                                 ListBase dsources = {NULL, NULL};
1906                                 
1907                                 /* now insert the keyframe(s) using the Keying Set
1908                                  *      1) add datasource override for the PoseChannel
1909                                  *      2) insert keyframes
1910                                  *      3) free the extra info 
1911                                  */
1912                                 ANIM_relative_keyingset_add_source(&dsources, &ob->id, &RNA_PoseBone, pchan); 
1913                                 ANIM_apply_keyingset(C, &dsources, NULL, ks, MODIFYKEY_MODE_INSERT, (float)CFRA);
1914                                 BLI_freelistN(&dsources);
1915                                 
1916                                 /* clear any unkeyed tags */
1917                                 if (pchan->bone)
1918                                         pchan->bone->flag &= ~BONE_UNKEYED;
1919                         }
1920                         else {
1921                                 /* add unkeyed tags */
1922                                 if (pchan->bone)
1923                                         pchan->bone->flag |= BONE_UNKEYED;
1924                         }
1925                 }
1926         }
1927         CTX_DATA_END;
1928         
1929         /* notifiers and updates */
1930         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1931         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, ob);
1932         
1933         return OPERATOR_FINISHED;
1934 }
1935
1936 void POSE_OT_quaternions_flip (wmOperatorType *ot)
1937 {
1938         /* identifiers */
1939         ot->name = "Flip Quats";
1940         ot->idname= "POSE_OT_quaternions_flip";
1941         ot->description= "Flip quaternion values to achieve desired rotations, while maintaining the same orientations";
1942         
1943         /* callbacks */
1944         ot->exec= pose_flip_quats_exec;
1945         ot->poll= ED_operator_posemode;
1946         
1947         /* flags */
1948         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1949 }
1950
1951 /* ********************************************** */
1952
1953 /* context: active channel */
1954 #if 0
1955 void pose_special_editmenu(Scene *scene)
1956 {
1957         Object *obedit= scene->obedit; // XXX context
1958         Object *ob= OBACT;
1959         short nr;
1960         
1961         /* paranoia checks */
1962         if(!ob && !ob->pose) return;
1963         if(ob==obedit || (ob->mode & OB_MODE_POSE)==0) return;
1964         
1965         nr= pupmenu("Specials%t|Select Constraint Target%x1|Flip Left-Right Names%x2|Calculate Paths%x3|Clear Paths%x4|Clear User Transform %x5|Relax Pose %x6|%l|AutoName Left-Right%x7|AutoName Front-Back%x8|AutoName Top-Bottom%x9");
1966         if(nr==1) {
1967                 pose_select_constraint_target(scene);
1968         }
1969         else if(nr==2) {
1970                 pose_flip_names();
1971         }
1972         else if(nr==3) {
1973                 pose_calculate_path(C, ob);
1974         }
1975         else if(nr==4) {
1976                 pose_clear_paths(ob);
1977         }
1978         else if(nr==5) {
1979                 pose_clear_user_transforms(ob);
1980         }
1981         else if(nr==6) {
1982                 pose_relax();
1983         }
1984         else if(ELEM3(nr, 7, 8, 9)) {
1985                 pose_autoside_names(nr-7);
1986         }
1987 }
1988
1989
1990 /* Restore selected pose-bones to 'action'-defined pose */
1991 static void pose_clear_user_transforms(Object *ob)
1992 {
1993         bArmature *arm= ob->data;
1994         bPoseChannel *pchan;
1995         
1996         if (ob->pose == NULL)
1997                 return;
1998         
1999         /* if the object has an action, restore pose to the pose defined by the action by clearing pose on selected bones */
2000         if (ob->action) {
2001                 /* find selected bones */
2002                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
2003                         if (pchan->bone && (pchan->bone->flag & BONE_SELECTED) && (pchan->bone->layer & arm->layer)) {
2004                                 /* just clear the BONE_UNKEYED flag, allowing this bone to get overwritten by actions again */
2005                                 pchan->bone->flag &= ~BONE_UNKEYED;
2006                         }
2007                 }
2008                 
2009                 /* clear pose locking flag 
2010                  *      - this will only clear the user-defined pose in the selected bones, where BONE_UNKEYED has been cleared
2011                  */
2012                 ob->pose->flag |= POSE_DO_UNLOCK;
2013         }
2014         else {
2015                 /* no action, so restore entire pose to rest pose (cannot restore only selected bones) */
2016                 rest_pose(ob->pose);
2017         }
2018         
2019         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
2020         BIF_undo_push("Clear User Transform");
2021 }
2022
2023 #endif