remove/comment unused vars
[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;
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         arm= ob->data;
684
685         /* if proxy-protected bones selected, some things (such as locks + displays) shouldn't be changable, 
686          * but for constraints (just add local constraints)
687          */
688         if (pose_has_protected_selected(ob, 0)) {
689                 i= BLI_countlist(&(pchanact->constraints)); /* if there are 24 or less, allow for the user to select constraints */
690                 if (i < 25)
691                         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");
692                 else
693                         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");
694         }
695         else {
696                 i= BLI_countlist(&(pchanact->constraints)); /* if there are 24 or less, allow for the user to select constraints */
697                 if (i < 25)
698                         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");
699                 else
700                         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");
701         }
702         
703         if (nr <= 0) 
704                 return;
705         
706         if (nr != 5)  {
707                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
708                         if ( (arm->layer & pchan->bone->layer) &&
709                                  (pchan->bone->flag & BONE_SELECTED) &&
710                                  (pchan != pchanact) ) 
711                         {
712                                 switch (nr) {
713                                         case 1: /* Local Location */
714                                                 VECCOPY(pchan->loc, pchanact->loc);
715                                                 break;
716                                         case 2: /* Local Rotation */
717                                                 QUATCOPY(pchan->quat, pchanact->quat);
718                                                 VECCOPY(pchan->eul, pchanact->eul);
719                                                 break;
720                                         case 3: /* Local Size */
721                                                 VECCOPY(pchan->size, pchanact->size);
722                                                 break;
723                                         case 4: /* All Constraints */
724                                         {
725                                                 ListBase tmp_constraints = {NULL, NULL};
726                                                 
727                                                 /* copy constraints to tmpbase and apply 'local' tags before 
728                                                  * appending to list of constraints for this channel
729                                                  */
730                                                 copy_constraints(&tmp_constraints, &pchanact->constraints, TRUE);
731                                                 if ((ob->proxy) && (pchan->bone->layer & arm->layer_protected)) {
732                                                         bConstraint *con;
733                                                         
734                                                         /* add proxy-local tags */
735                                                         for (con= tmp_constraints.first; con; con= con->next)
736                                                                 con->flag |= CONSTRAINT_PROXY_LOCAL;
737                                                 }
738                                                 BLI_movelisttolist(&pchan->constraints, &tmp_constraints);
739                                                 
740                                                 /* update flags (need to add here, not just copy) */
741                                                 pchan->constflag |= pchanact->constflag;
742                                                 
743                                                 if (ob->pose)
744                                                         ob->pose->flag |= POSE_RECALC;
745                                         }
746                                                 break;
747                                         case 6: /* Transform Locks */
748                                                 pchan->protectflag = pchanact->protectflag;
749                                                 break;
750                                         case 7: /* IK (DOF) settings */
751                                         {
752                                                 pchan->ikflag = pchanact->ikflag;
753                                                 VECCOPY(pchan->limitmin, pchanact->limitmin);
754                                                 VECCOPY(pchan->limitmax, pchanact->limitmax);
755                                                 VECCOPY(pchan->stiffness, pchanact->stiffness);
756                                                 pchan->ikstretch= pchanact->ikstretch;
757                                                 pchan->ikrotweight= pchanact->ikrotweight;
758                                                 pchan->iklinweight= pchanact->iklinweight;
759                                         }
760                                                 break;
761                                         case 8: /* Custom Bone Shape */
762                                                 pchan->custom = pchanact->custom;
763                                                 break;
764                                         case 9: /* Visual Location */
765                                                 armature_loc_pose_to_bone(pchan, pchanact->pose_mat[3], pchan->loc);
766                                                 break;
767                                         case 10: /* Visual Rotation */
768                                         {
769                                                 float delta_mat[4][4];
770                                                 
771                                                 armature_mat_pose_to_bone(pchan, pchanact->pose_mat, delta_mat);
772                                                 
773                                                 if (pchan->rotmode == ROT_MODE_AXISANGLE) {
774                                                         float tmp_quat[4];
775                                                         
776                                                         /* need to convert to quat first (in temp var)... */
777                                                         mat4_to_quat( tmp_quat,delta_mat);
778                                                         quat_to_axis_angle( pchan->rotAxis, &pchan->rotAngle,tmp_quat);
779                                                 }
780                                                 else if (pchan->rotmode == ROT_MODE_QUAT)
781                                                         mat4_to_quat( pchan->quat,delta_mat);
782                                                 else
783                                                         mat4_to_eulO( pchan->eul, pchan->rotmode,delta_mat);
784                                         }
785                                                 break;
786                                         case 11: /* Visual Size */
787                                         {
788                                                 float delta_mat[4][4], size[4];
789                                                 
790                                                 armature_mat_pose_to_bone(pchan, pchanact->pose_mat, delta_mat);
791                                                 mat4_to_size( size,delta_mat);
792                                                 VECCOPY(pchan->size, size);
793                                         }
794                                 }
795                         }
796                 }
797         } 
798         else { /* constraints, optional (note: max we can have is 24 constraints) */
799                 bConstraint *con, *con_back;
800                 int const_toggle[24]= {0}; /* XXX, initialize as 0 to quiet errors */
801                 ListBase const_copy = {NULL, NULL};
802                 
803                 BLI_duplicatelist(&const_copy, &(pchanact->constraints));
804                 
805                 /* build the puplist of constraints */
806                 for (con = pchanact->constraints.first, i=0; con; con=con->next, i++){
807                         const_toggle[i]= 1;
808 //                      add_numbut(i, TOG|INT, con->name, 0, 0, &(const_toggle[i]), "");
809                 }
810                 
811 //              if (!do_clever_numbuts("Select Constraints", i, REDRAW)) {
812 //                      BLI_freelistN(&const_copy);
813 //                      return;
814 //              }
815                 
816                 /* now build a new listbase from the options selected */
817                 for (i=0, con=const_copy.first; con; i++) {
818                         /* if not selected, free/remove it from the list */
819                         if (!const_toggle[i]) {
820                                 con_back= con->next;
821                                 BLI_freelinkN(&const_copy, con);
822                                 con= con_back;
823                         } 
824                         else
825                                 con= con->next;
826                 }
827                 
828                 /* Copy the temo listbase to the selected posebones */
829                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
830                         if ( (arm->layer & pchan->bone->layer) &&
831                                  (pchan->bone->flag & BONE_SELECTED) &&
832                                  (pchan!=pchanact) ) 
833                         {
834                                 ListBase tmp_constraints = {NULL, NULL};
835                                 
836                                 /* copy constraints to tmpbase and apply 'local' tags before 
837                                  * appending to list of constraints for this channel
838                                  */
839                                 copy_constraints(&tmp_constraints, &const_copy, TRUE);
840                                 if ((ob->proxy) && (pchan->bone->layer & arm->layer_protected)) {                                       
841                                         /* add proxy-local tags */
842                                         for (con= tmp_constraints.first; con; con= con->next)
843                                                 con->flag |= CONSTRAINT_PROXY_LOCAL;
844                                 }
845                                 BLI_movelisttolist(&pchan->constraints, &tmp_constraints);
846                                 
847                                 /* update flags (need to add here, not just copy) */
848                                 pchan->constflag |= pchanact->constflag;
849                         }
850                 }
851                 BLI_freelistN(&const_copy);
852                 update_pose_constraint_flags(ob->pose); /* we could work out the flags but its simpler to do this */
853                 
854                 if (ob->pose)
855                         ob->pose->flag |= POSE_RECALC;
856         }
857         
858         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);     // and all its relations
859         
860         BIF_undo_push("Copy Pose Attributes");
861         
862 }
863
864 /* ******************** copy/paste pose ********************** */
865
866 /* Global copy/paste buffer for pose - cleared on start/end session + before every copy operation */
867 static bPose *g_posebuf = NULL;
868
869 void free_posebuf(void) 
870 {
871         if (g_posebuf) {
872                 bPoseChannel *pchan;
873                 
874                 for (pchan= g_posebuf->chanbase.first; pchan; pchan= pchan->next) {
875                         if(pchan->prop) {
876                                 IDP_FreeProperty(pchan->prop);
877                                 MEM_freeN(pchan->prop);
878                         }
879                 }
880                 
881                 /* was copied without constraints */
882                 BLI_freelistN(&g_posebuf->chanbase);
883                 MEM_freeN(g_posebuf);
884         }
885         
886         g_posebuf=NULL;
887 }
888
889 /* ---- */
890
891 static int pose_copy_exec (bContext *C, wmOperator *op)
892 {
893         Object *ob= ED_object_pose_armature(CTX_data_active_object(C));
894         
895         /* sanity checking */
896         if ELEM(NULL, ob, ob->pose) {
897                 BKE_report(op->reports, RPT_ERROR, "No Pose to Copy");
898                 return OPERATOR_CANCELLED;
899         }
900
901         /* free existing pose buffer */
902         free_posebuf();
903         
904         /* sets chan->flag to POSE_KEY if bone selected, then copy those bones to the buffer */
905         set_pose_keys(ob);  
906         copy_pose(&g_posebuf, ob->pose, 0);
907         
908         
909         return OPERATOR_FINISHED;
910 }
911
912 void POSE_OT_copy (wmOperatorType *ot) 
913 {
914         /* identifiers */
915         ot->name= "Copy Pose";
916         ot->idname= "POSE_OT_copy";
917         ot->description= "Copies the current pose of the selected bones to copy/paste buffer";
918         
919         /* api callbacks */
920         ot->exec= pose_copy_exec;
921         ot->poll= ED_operator_posemode;
922         
923         /* flag */
924         ot->flag= OPTYPE_REGISTER;
925 }
926
927 /* ---- */
928
929 static int pose_paste_exec (bContext *C, wmOperator *op)
930 {
931         Scene *scene= CTX_data_scene(C);
932         Object *ob= ED_object_pose_armature(CTX_data_active_object(C));
933         bPoseChannel *chan, *pchan;
934         int flip= RNA_boolean_get(op->ptr, "flipped");
935         
936         /* sanity checks */
937         if ELEM(NULL, ob, ob->pose)
938                 return OPERATOR_CANCELLED;
939
940         if (g_posebuf == NULL) {
941                 BKE_report(op->reports, RPT_ERROR, "Copy buffer is empty");
942                 return OPERATOR_CANCELLED;
943         }
944         
945         /* Safely merge all of the channels in the buffer pose into any existing pose */
946         for (chan= g_posebuf->chanbase.first; chan; chan=chan->next) {
947                 if (chan->flag & POSE_KEY) {
948                         /* get the name - if flipping, we must flip this first */
949                         char name[32];
950                         if (flip)
951                                 flip_side_name(name, chan->name, 0);            /* 0 = don't strip off number extensions */
952                         else
953                                 BLI_strncpy(name, chan->name, sizeof(name));
954                         
955                         /* only copy when channel exists, poses are not meant to add random channels to anymore */
956                         pchan= get_pose_channel(ob->pose, name);
957                         
958                         if (pchan) {
959                                 /* only loc rot size 
960                                  *      - only copies transform info for the pose 
961                                  */
962                                 VECCOPY(pchan->loc, chan->loc);
963                                 VECCOPY(pchan->size, chan->size);
964                                 pchan->flag= chan->flag;
965                                 
966                                 /* check if rotation modes are compatible (i.e. do they need any conversions) */
967                                 if (pchan->rotmode == chan->rotmode) {
968                                         /* copy the type of rotation in use */
969                                         if (pchan->rotmode > 0) {
970                                                 VECCOPY(pchan->eul, chan->eul);
971                                         }
972                                         else if (pchan->rotmode == ROT_MODE_AXISANGLE) {
973                                                 VECCOPY(pchan->rotAxis, chan->rotAxis);
974                                                 pchan->rotAngle = chan->rotAngle;
975                                         }
976                                         else {
977                                                 QUATCOPY(pchan->quat, chan->quat);
978                                         }
979                                 }
980                                 else if (pchan->rotmode > 0) {
981                                         /* quat/axis-angle to euler */
982                                         if (chan->rotmode == ROT_MODE_AXISANGLE)
983                                                 axis_angle_to_eulO( pchan->eul, pchan->rotmode,chan->rotAxis, chan->rotAngle);
984                                         else
985                                                 quat_to_eulO( pchan->eul, pchan->rotmode,chan->quat);
986                                 }
987                                 else if (pchan->rotmode == ROT_MODE_AXISANGLE) {
988                                         /* quat/euler to axis angle */
989                                         if (chan->rotmode > 0)
990                                                 eulO_to_axis_angle(pchan->rotAxis, &pchan->rotAngle, chan->eul, chan->rotmode);
991                                         else    
992                                                 quat_to_axis_angle(pchan->rotAxis, &pchan->rotAngle, chan->quat);
993                                 }
994                                 else {
995                                         /* euler/axis-angle to quat */
996                                         if (chan->rotmode > 0)
997                                                 eulO_to_quat(pchan->quat, chan->eul, chan->rotmode);
998                                         else
999                                                 axis_angle_to_quat(pchan->quat, chan->rotAxis, pchan->rotAngle);
1000                                 }
1001                                 
1002                                 /* paste flipped pose? */
1003                                 if (flip) {
1004                                         pchan->loc[0]*= -1;
1005                                         
1006                                         /* has to be done as eulers... */
1007                                         if (pchan->rotmode > 0) {
1008                                                 pchan->eul[1] *= -1;
1009                                                 pchan->eul[2] *= -1;
1010                                         }
1011                                         else if (pchan->rotmode == ROT_MODE_AXISANGLE) {
1012                                                 float eul[3];
1013                                                 
1014                                                 axis_angle_to_eulO(eul, EULER_ORDER_DEFAULT, pchan->rotAxis, pchan->rotAngle);
1015                                                 eul[1]*= -1;
1016                                                 eul[2]*= -1;
1017                                                 eulO_to_axis_angle(pchan->rotAxis, &pchan->rotAngle, eul, EULER_ORDER_DEFAULT);
1018                                         }
1019                                         else {
1020                                                 float eul[3];
1021                                                 
1022                                                 normalize_qt(pchan->quat);
1023                                                 quat_to_eul(eul, pchan->quat);
1024                                                 eul[1]*= -1;
1025                                                 eul[2]*= -1;
1026                                                 eul_to_quat(pchan->quat, eul);
1027                                         }
1028                                 }
1029                                 
1030                                 /* ID properties 
1031                                  *      - only free the existing properties if the channel we're copying from has them
1032                                  *        NOTE: this means that if the pose depends on some pchan property, the pose may not be ok,
1033                                  *                  but this is better than loosing all the setting you've painstakingly added...
1034                                  */
1035                                 if (chan->prop) {
1036                                         /* free the old properties since we want to replace them now */
1037                                         if (pchan->prop) {
1038                                                 IDP_FreeProperty(pchan->prop);
1039                                                 MEM_freeN(pchan->prop);
1040                                                 pchan->prop= NULL;
1041                                         }
1042                                         
1043                                         /* now copy over the new copy of the properties */
1044                                         pchan->prop= IDP_CopyProperty(chan->prop);      
1045                                 }
1046                                 
1047                                 /* keyframing tagging */
1048                                 if (autokeyframe_cfra_can_key(scene, &ob->id)) {
1049                                         ListBase dsources = {NULL, NULL};
1050                                         
1051                                         /* get KeyingSet to use */
1052                                         KeyingSet *ks = ANIM_get_keyingset_for_autokeying(scene, "LocRotScale");
1053                                         
1054                                         /* now insert the keyframe(s) using the Keying Set
1055                                          *      1) add datasource override for the PoseChannel
1056                                          *      2) insert keyframes
1057                                          *      3) free the extra info 
1058                                          */
1059                                         ANIM_relative_keyingset_add_source(&dsources, &ob->id, &RNA_PoseBone, pchan); 
1060                                         ANIM_apply_keyingset(C, &dsources, NULL, ks, MODIFYKEY_MODE_INSERT, (float)CFRA);
1061                                         BLI_freelistN(&dsources);
1062                                         
1063                                         /* clear any unkeyed tags */
1064                                         if (chan->bone)
1065                                                 chan->bone->flag &= ~BONE_UNKEYED;
1066                                 }
1067                                 else {
1068                                         /* add unkeyed tags */
1069                                         if (chan->bone)
1070                                                 chan->bone->flag |= BONE_UNKEYED;
1071                                 }
1072                         }
1073                 }
1074         }
1075         
1076         /* Update event for pose and deformation children */
1077         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1078                 
1079         /* notifiers for updates */
1080         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1081
1082         return OPERATOR_FINISHED;
1083 }
1084
1085 void POSE_OT_paste (wmOperatorType *ot)
1086 {
1087         /* identifiers */
1088         ot->name= "Paste Pose";
1089         ot->idname= "POSE_OT_paste";
1090         ot->description= "Pastes the stored pose on to the current pose";
1091         
1092         /* api callbacks */
1093         ot->exec= pose_paste_exec;
1094         ot->poll= ED_operator_posemode;
1095         
1096         /* flag */
1097         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1098         
1099         /* properties */
1100         RNA_def_boolean(ot->srna, "flipped", 0, "Flipped on X-Axis", "Paste the stored pose flipped on to current pose");
1101 }
1102
1103 /* ********************************************** */
1104
1105
1106 static int pose_group_add_exec (bContext *C, wmOperator *UNUSED(op))
1107 {
1108         ScrArea *sa= CTX_wm_area(C);
1109         Object *ob;
1110         
1111         /* since this call may also be used from the buttons window, we need to check for where to get the object */
1112         if (sa->spacetype == SPACE_BUTS) 
1113                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1114         else
1115                 ob= ED_object_pose_armature(CTX_data_active_object(C));
1116                 
1117         /* only continue if there's an object */
1118         if (ob == NULL)
1119                 return OPERATOR_CANCELLED;
1120         
1121         /* for now, just call the API function for this */
1122         pose_add_group(ob);
1123         
1124         /* notifiers for updates */
1125         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1126         
1127         return OPERATOR_FINISHED;
1128 }
1129
1130 void POSE_OT_group_add (wmOperatorType *ot)
1131 {
1132         /* identifiers */
1133         ot->name= "Add Bone Group";
1134         ot->idname= "POSE_OT_group_add";
1135         ot->description= "Add a new bone group";
1136         
1137         /* api callbacks */
1138         ot->exec= pose_group_add_exec;
1139         ot->poll= ED_operator_posemode;
1140         
1141         /* flags */
1142         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1143 }
1144
1145
1146 static int pose_group_remove_exec (bContext *C, wmOperator *UNUSED(op))
1147 {
1148         ScrArea *sa= CTX_wm_area(C);
1149         Object *ob;
1150         
1151         /* since this call may also be used from the buttons window, we need to check for where to get the object */
1152         if (sa->spacetype == SPACE_BUTS) 
1153                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1154         else
1155                 ob= ED_object_pose_armature(CTX_data_active_object(C));
1156         
1157         /* only continue if there's an object */
1158         if (ob == NULL)
1159                 return OPERATOR_CANCELLED;
1160         
1161         /* for now, just call the API function for this */
1162         pose_remove_group(ob);
1163         
1164         /* notifiers for updates */
1165         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1166         
1167         return OPERATOR_FINISHED;
1168 }
1169
1170 void POSE_OT_group_remove (wmOperatorType *ot)
1171 {
1172         /* identifiers */
1173         ot->name= "Remove Bone Group";
1174         ot->idname= "POSE_OT_group_remove";
1175         ot->description= "Removes the active bone group";
1176         
1177         /* api callbacks */
1178         ot->exec= pose_group_remove_exec;
1179         ot->poll= ED_operator_posemode;
1180         
1181         /* flags */
1182         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1183 }
1184
1185 /* ------------ */
1186
1187 /* invoke callback which presents a list of bone-groups for the user to choose from */
1188 static int pose_groups_menu_invoke (bContext *C, wmOperator *op, wmEvent *UNUSED(evt))
1189 {
1190         ScrArea *sa= CTX_wm_area(C);
1191         Object *ob;
1192         bPose *pose;
1193         
1194         uiPopupMenu *pup;
1195         uiLayout *layout;
1196         bActionGroup *grp;
1197         int i;
1198         
1199         /* since this call may also be used from the buttons window, we need to check for where to get the object */
1200         if (sa->spacetype == SPACE_BUTS) 
1201                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1202         else
1203                 ob= ED_object_pose_armature(CTX_data_active_object(C));
1204         
1205         /* only continue if there's an object, and a pose there too */
1206         if (ELEM(NULL, ob, ob->pose)) 
1207                 return OPERATOR_CANCELLED;
1208         pose= ob->pose;
1209         
1210         /* if there's no active group (or active is invalid), create a new menu to find it */
1211         if (pose->active_group <= 0) {
1212                 /* create a new menu, and start populating it with group names */
1213                 pup= uiPupMenuBegin(C, op->type->name, ICON_NULL);
1214                 layout= uiPupMenuLayout(pup);
1215                 
1216                 /* special entry - allow to create new group, then use that 
1217                  *      (not to be used for removing though)
1218                  */
1219                 if (strstr(op->idname, "assign")) {
1220                         uiItemIntO(layout, "New Group", ICON_NULL, op->idname, "type", 0);
1221                         uiItemS(layout);
1222                 }
1223                 
1224                 /* add entries for each group */
1225                 for (grp= pose->agroups.first, i=1; grp; grp=grp->next, i++)
1226                         uiItemIntO(layout, grp->name, ICON_NULL, op->idname, "type", i);
1227                         
1228                 /* finish building the menu, and process it (should result in calling self again) */
1229                 uiPupMenuEnd(C, pup);
1230                 
1231                 return OPERATOR_CANCELLED;
1232         }
1233         else {
1234                 /* just use the active group index, and call the exec callback for the calling operator */
1235                 RNA_int_set(op->ptr, "type", pose->active_group);
1236                 return op->type->exec(C, op);
1237         }
1238 }
1239
1240 /* Assign selected pchans to the bone group that the user selects */
1241 static int pose_group_assign_exec (bContext *C, wmOperator *op)
1242 {
1243         ScrArea *sa= CTX_wm_area(C);
1244         Object *ob;
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
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         short done= 0;
1310         
1311         /* since this call may also be used from the buttons window, we need to check for where to get the object */
1312         if (sa->spacetype == SPACE_BUTS) 
1313                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1314         else
1315                 ob= ED_object_pose_armature(CTX_data_active_object(C));
1316         
1317         /* only continue if there's an object, and a pose there too */
1318         if (ELEM(NULL, ob, ob->pose))
1319                 return OPERATOR_CANCELLED;
1320         
1321         /* find selected bones to remove from all bone groups */
1322         CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pose_bones)
1323         {
1324                 if (pchan->agrp_index) {
1325                         pchan->agrp_index= 0;
1326                         done= 1;
1327                 }
1328         }
1329         CTX_DATA_END;
1330         
1331         /* notifiers for updates */
1332         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1333         
1334         /* report done status */
1335         if (done)
1336                 return OPERATOR_FINISHED;
1337         else
1338                 return OPERATOR_CANCELLED;
1339 }
1340
1341 void POSE_OT_group_unassign (wmOperatorType *ot)
1342 {
1343         /* identifiers */
1344         ot->name= "Remove Selected from Bone Groups";
1345         ot->idname= "POSE_OT_group_unassign";
1346         ot->description= "Remove selected bones from all bone groups";
1347         
1348         /* api callbacks */
1349         ot->exec= pose_group_unassign_exec;
1350         ot->poll= ED_operator_posemode;
1351         
1352         /* flags */
1353         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1354 }
1355
1356 static void pose_group_select(bContext *C, Object *ob, int select)
1357 {
1358         bPose *pose= ob->pose;
1359         
1360         CTX_DATA_BEGIN(C, bPoseChannel*, pchan, visible_pose_bones)
1361         {
1362                 if ((pchan->bone->flag & BONE_UNSELECTABLE)==0) {
1363                         if (select) {
1364                                 if (pchan->agrp_index == pose->active_group) 
1365                                         pchan->bone->flag |= BONE_SELECTED;
1366                         }
1367                         else {
1368                                 if (pchan->agrp_index == pose->active_group) 
1369                                         pchan->bone->flag &= ~BONE_SELECTED;
1370                         }
1371                 }
1372         }
1373         CTX_DATA_END;
1374 }
1375
1376 static int pose_group_select_exec (bContext *C, wmOperator *UNUSED(op))
1377 {
1378         ScrArea *sa= CTX_wm_area(C);
1379         Object *ob;
1380         
1381         /* since this call may also be used from the buttons window, we need to check for where to get the object */
1382         if (sa->spacetype == SPACE_BUTS) 
1383                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1384         else
1385                 ob= ED_object_pose_armature(CTX_data_active_object(C));
1386         
1387         /* only continue if there's an object, and a pose there too */
1388         if (ELEM(NULL, ob, ob->pose))
1389                 return OPERATOR_CANCELLED;
1390         
1391         pose_group_select(C, ob, 1);
1392         
1393         /* notifiers for updates */
1394         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1395         
1396         return OPERATOR_FINISHED;
1397 }
1398
1399 void POSE_OT_group_select (wmOperatorType *ot)
1400 {
1401         /* identifiers */
1402         ot->name= "Select Bones of Bone Group";
1403         ot->idname= "POSE_OT_group_select";
1404         ot->description= "Select bones in active Bone Group";
1405         
1406         /* api callbacks */
1407         ot->exec= pose_group_select_exec;
1408         ot->poll= ED_operator_posemode;
1409         
1410         /* flags */
1411         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1412 }
1413
1414 static int pose_group_deselect_exec (bContext *C, wmOperator *UNUSED(op))
1415 {
1416         ScrArea *sa= CTX_wm_area(C);
1417         Object *ob;
1418         
1419         /* since this call may also be used from the buttons window, we need to check for where to get the object */
1420         if (sa->spacetype == SPACE_BUTS) 
1421                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1422         else
1423                 ob= ED_object_pose_armature(CTX_data_active_object(C));
1424         
1425         /* only continue if there's an object, and a pose there too */
1426         if (ELEM(NULL, ob, ob->pose))
1427                 return OPERATOR_CANCELLED;
1428         
1429         pose_group_select(C, ob, 0);
1430         
1431         /* notifiers for updates */
1432         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1433         
1434         return OPERATOR_FINISHED;
1435 }
1436
1437 void POSE_OT_group_deselect (wmOperatorType *ot)
1438 {
1439         /* identifiers */
1440         ot->name= "Deselecte Bone Group";
1441         ot->idname= "POSE_OT_group_deselect";
1442         ot->description= "Deselect bones of active Bone Group";
1443         
1444         /* api callbacks */
1445         ot->exec= pose_group_deselect_exec;
1446         ot->poll= ED_operator_posemode;
1447         
1448         /* flags */
1449         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1450 }
1451
1452 /* ********************************************** */
1453
1454 static int pose_flip_names_exec (bContext *C, wmOperator *UNUSED(op))
1455 {
1456         Object *ob= ED_object_pose_armature(CTX_data_active_object(C));
1457         bArmature *arm;
1458         
1459         /* paranoia checks */
1460         if (ELEM(NULL, ob, ob->pose)) 
1461                 return OPERATOR_CANCELLED;
1462         arm= ob->data;
1463         
1464         /* loop through selected bones, auto-naming them */
1465         CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pose_bones)
1466         {
1467                 char newname[32];
1468                 flip_side_name(newname, pchan->name, TRUE);
1469                 ED_armature_bone_rename(arm, pchan->name, newname);
1470         }
1471         CTX_DATA_END;
1472         
1473         /* since we renamed stuff... */
1474         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1475
1476         /* note, notifier might evolve */
1477         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1478         
1479         return OPERATOR_FINISHED;
1480 }
1481
1482 void POSE_OT_flip_names (wmOperatorType *ot)
1483 {
1484         /* identifiers */
1485         ot->name= "Flip Names";
1486         ot->idname= "POSE_OT_flip_names";
1487         ot->description= "Flips (and corrects) the axis suffixes of the the names of selected bones";
1488         
1489         /* api callbacks */
1490         ot->exec= pose_flip_names_exec;
1491         ot->poll= ED_operator_posemode;
1492         
1493         /* flags */
1494         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1495 }
1496
1497 /* ------------------ */
1498
1499 static int pose_autoside_names_exec (bContext *C, wmOperator *op)
1500 {
1501         Object *ob= ED_object_pose_armature(CTX_data_active_object(C));
1502         bArmature *arm;
1503         char newname[32];
1504         short axis= RNA_enum_get(op->ptr, "axis");
1505         
1506         /* paranoia checks */
1507         if (ELEM(NULL, ob, ob->pose)) 
1508                 return OPERATOR_CANCELLED;
1509         arm= ob->data;
1510         
1511         /* loop through selected bones, auto-naming them */
1512         CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pose_bones)
1513         {
1514                 BLI_strncpy(newname, pchan->name, sizeof(newname));
1515                 if(bone_autoside_name(newname, 1, axis, pchan->bone->head[axis], pchan->bone->tail[axis]))
1516                         ED_armature_bone_rename(arm, pchan->name, newname);
1517         }
1518         CTX_DATA_END;
1519         
1520         /* since we renamed stuff... */
1521         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1522
1523         /* note, notifier might evolve */
1524         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1525         
1526         return OPERATOR_FINISHED;
1527 }
1528
1529 void POSE_OT_autoside_names (wmOperatorType *ot)
1530 {
1531         static EnumPropertyItem axis_items[]= {
1532                  {0, "XAXIS", 0, "X-Axis", "Left/Right"},
1533                 {1, "YAXIS", 0, "Y-Axis", "Front/Back"},
1534                 {2, "ZAXIS", 0, "Z-Axis", "Top/Bottom"},
1535                 {0, NULL, 0, NULL, NULL}};
1536         
1537         /* identifiers */
1538         ot->name= "AutoName by Axis";
1539         ot->idname= "POSE_OT_autoside_names";
1540         ot->description= "Automatically renames the selected bones according to which side of the target axis they fall on";
1541         
1542         /* api callbacks */
1543         ot->invoke= WM_menu_invoke;
1544         ot->exec= pose_autoside_names_exec;
1545         ot->poll= ED_operator_posemode;
1546         
1547         /* flags */
1548         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1549         
1550         /* settings */
1551         ot->prop= RNA_def_enum(ot->srna, "axis", axis_items, 0, "Axis", "Axis tag names with.");
1552 }
1553
1554 /* ********************************************** */
1555
1556 /* context active object, or weightpainted object with armature in posemode */
1557 void pose_activate_flipped_bone(Scene *scene)
1558 {
1559         Object *ob= OBACT;
1560
1561         if(ob==NULL) return;
1562
1563         if(ob->mode & OB_MODE_WEIGHT_PAINT) {
1564                 ob= modifiers_isDeformedByArmature(ob);
1565         }
1566
1567         if(ob && (ob->mode & OB_MODE_POSE)) {
1568                 bPoseChannel *pchanf;
1569                 bArmature *arm= ob->data;
1570
1571                 if(arm->act_bone) {
1572                         char name[32];
1573                         flip_side_name(name, arm->act_bone->name, TRUE);
1574                         
1575                         pchanf= get_pose_channel(ob->pose, name);
1576                         if(pchanf && pchanf->bone != arm->act_bone) {
1577                                 arm->act_bone->flag &= ~BONE_SELECTED;
1578                                 pchanf->bone->flag |= BONE_SELECTED;
1579                                 
1580                                 arm->act_bone= pchanf->bone;
1581                                 
1582                                 /* in weightpaint we select the associated vertex group too */
1583                                 if(ob->mode & OB_MODE_WEIGHT_PAINT) {
1584                                         ED_vgroup_select_by_name(OBACT, name);
1585                                         DAG_id_tag_update(&OBACT->id, OB_RECALC_DATA);
1586                                 }
1587                                 
1588                                 // XXX notifiers need to be sent to other editors to update
1589                                 
1590                         }                       
1591                 }
1592         }
1593 }
1594
1595
1596 /* ********************************************** */
1597
1598 /* Show all armature layers */
1599 static int pose_armature_layers_showall_poll (bContext *C)
1600 {
1601         /* this single operator can be used in posemode OR editmode for armatures */
1602         return ED_operator_posemode(C) || ED_operator_editarmature(C);
1603 }
1604
1605 static int pose_armature_layers_showall_exec (bContext *C, wmOperator *op)
1606 {
1607         Object *ob= ED_object_pose_armature(CTX_data_active_object(C));
1608         bArmature *arm = (ob)? ob->data : NULL;
1609         PointerRNA ptr;
1610         int maxLayers = (RNA_boolean_get(op->ptr, "all"))? 32 : 16;
1611         int layers[32] = {0}; /* hardcoded for now - we can only have 32 armature layers, so this should be fine... */
1612         int i;
1613         
1614         /* sanity checking */
1615         if (arm == NULL)
1616                 return OPERATOR_CANCELLED;
1617         
1618         /* use RNA to set the layers
1619          *      although it would be faster to just set directly using bitflags, we still
1620          *      need to setup a RNA pointer so that we get the "update" callbacks for free...
1621          */
1622         RNA_id_pointer_create(&arm->id, &ptr);
1623         
1624         for (i = 0; i < maxLayers; i++)
1625                 layers[i] = 1;
1626         
1627         RNA_boolean_set_array(&ptr, "layers", layers);
1628         
1629         /* note, notifier might evolve */
1630         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1631         
1632         /* done */
1633         return OPERATOR_FINISHED;
1634 }
1635
1636 void ARMATURE_OT_layers_show_all (wmOperatorType *ot)
1637 {
1638         /* identifiers */
1639         ot->name= "Show All Layers";
1640         ot->idname= "ARMATURE_OT_layers_show_all";
1641         ot->description= "Make all armature layers visible";
1642         
1643         /* callbacks */
1644         ot->exec= pose_armature_layers_showall_exec;
1645         ot->poll= pose_armature_layers_showall_poll;
1646         
1647         /* flags */
1648         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1649         
1650         /* properties */
1651         ot->prop = RNA_def_boolean(ot->srna, "all", 1, "All Layers", "Enable all layers or just the first 16 (top row)");
1652 }
1653
1654 /* ------------------- */
1655
1656 /* Present a popup to get the layers that should be used */
1657 static int pose_armature_layers_invoke (bContext *C, wmOperator *op, wmEvent *evt)
1658 {
1659         Object *ob= ED_object_pose_armature(CTX_data_active_object(C));
1660         bArmature *arm= (ob)? ob->data : NULL;
1661         PointerRNA ptr;
1662         int layers[32]; /* hardcoded for now - we can only have 32 armature layers, so this should be fine... */
1663         
1664         /* sanity checking */
1665         if (arm == NULL)
1666                 return OPERATOR_CANCELLED;
1667                 
1668         /* get RNA pointer to armature data to use that to retrieve the layers as ints to init the operator */
1669         RNA_id_pointer_create((ID *)arm, &ptr);
1670         RNA_boolean_get_array(&ptr, "layers", layers);
1671         RNA_boolean_set_array(op->ptr, "layers", layers);
1672         
1673         /* part to sync with other similar operators... */
1674         return WM_operator_props_popup(C, op, evt);
1675 }
1676
1677 /* Set the visible layers for the active armature (edit and pose modes) */
1678 static int pose_armature_layers_exec (bContext *C, wmOperator *op)
1679 {
1680         Object *ob= ED_object_pose_armature(CTX_data_active_object(C));
1681         bArmature *arm= (ob)? ob->data : NULL;
1682         PointerRNA ptr;
1683         int layers[32]; /* hardcoded for now - we can only have 32 armature layers, so this should be fine... */
1684         
1685         /* get the values set in the operator properties */
1686         RNA_boolean_get_array(op->ptr, "layers", layers);
1687         
1688         /* get pointer for armature, and write data there... */
1689         RNA_id_pointer_create((ID *)arm, &ptr);
1690         RNA_boolean_set_array(&ptr, "layers", layers);
1691         
1692         /* note, notifier might evolve */
1693         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1694         
1695         return OPERATOR_FINISHED;
1696 }
1697
1698
1699 void POSE_OT_armature_layers (wmOperatorType *ot)
1700 {
1701         /* identifiers */
1702         ot->name= "Change Armature Layers";
1703         ot->idname= "POSE_OT_armature_layers";
1704         ot->description= "Change the visible armature layers";
1705         
1706         /* callbacks */
1707         ot->invoke= pose_armature_layers_invoke;
1708         ot->exec= pose_armature_layers_exec;
1709         ot->poll= ED_operator_posemode;
1710         
1711         /* flags */
1712         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1713         
1714         /* properties */
1715         RNA_def_boolean_layer_member(ot->srna, "layers", 32, NULL, "Layer", "Armature layers to make visible");
1716 }
1717
1718 void ARMATURE_OT_armature_layers (wmOperatorType *ot)
1719 {
1720         /* identifiers */
1721         ot->name= "Change Armature Layers";
1722         ot->idname= "ARMATURE_OT_armature_layers";
1723         ot->description= "Change the visible armature layers";
1724         
1725         /* callbacks */
1726         ot->invoke= pose_armature_layers_invoke;
1727         ot->exec= pose_armature_layers_exec;
1728         ot->poll= ED_operator_editarmature;
1729         
1730         /* flags */
1731         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1732         
1733         /* properties */
1734         RNA_def_boolean_layer_member(ot->srna, "layers", 32, NULL, "Layer", "Armature layers to make visible");
1735 }
1736
1737 /* ------------------- */
1738
1739 /* Present a popup to get the layers that should be used */
1740 static int pose_bone_layers_invoke (bContext *C, wmOperator *op, wmEvent *evt)
1741 {
1742         int layers[32]= {0}; /* hardcoded for now - we can only have 32 armature layers, so this should be fine... */
1743         
1744         /* get layers that are active already */        
1745         CTX_DATA_BEGIN(C, bPoseChannel *, pchan, selected_pose_bones) 
1746         {
1747                 short bit;
1748                 
1749                 /* loop over the bits for this pchan's layers, adding layers where they're needed */
1750                 for (bit= 0; bit < 32; bit++) {
1751                         if (pchan->bone->layer & (1<<bit))
1752                                 layers[bit]= 1;
1753                 }
1754         }
1755         CTX_DATA_END;
1756         
1757         /* copy layers to operator */
1758         RNA_boolean_set_array(op->ptr, "layers", layers);
1759         
1760                 /* part to sync with other similar operators... */
1761         return WM_operator_props_popup(C, op, evt);
1762 }
1763
1764 /* Set the visible layers for the active armature (edit and pose modes) */
1765 static int pose_bone_layers_exec (bContext *C, wmOperator *op)
1766 {
1767         Object *ob= ED_object_pose_armature(CTX_data_active_object(C));
1768         bArmature *arm= (ob)? ob->data : NULL;
1769         PointerRNA ptr;
1770         int layers[32]; /* hardcoded for now - we can only have 32 armature layers, so this should be fine... */
1771         
1772         /* get the values set in the operator properties */
1773         RNA_boolean_get_array(op->ptr, "layers", layers);
1774         
1775         /* set layers of pchans based on the values set in the operator props */
1776         CTX_DATA_BEGIN(C, bPoseChannel *, pchan, selected_pose_bones) 
1777         {
1778                 /* get pointer for pchan, and write flags this way */
1779                 RNA_pointer_create((ID *)arm, &RNA_Bone, pchan->bone, &ptr);
1780                 RNA_boolean_set_array(&ptr, "layers", layers);
1781         }
1782         CTX_DATA_END;
1783         
1784         /* note, notifier might evolve */
1785         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1786         
1787         return OPERATOR_FINISHED;
1788 }
1789
1790 void POSE_OT_bone_layers (wmOperatorType *ot)
1791 {
1792         /* identifiers */
1793         ot->name= "Change Bone Layers";
1794         ot->idname= "POSE_OT_bone_layers";
1795         ot->description= "Change the layers that the selected bones belong to";
1796         
1797         /* callbacks */
1798         ot->invoke= pose_bone_layers_invoke;
1799         ot->exec= pose_bone_layers_exec;
1800         ot->poll= ED_operator_posemode;
1801         
1802         /* flags */
1803         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1804         
1805         /* properties */
1806         RNA_def_boolean_layer_member(ot->srna, "layers", 32, NULL, "Layer", "Armature layers that bone belongs to");
1807 }
1808
1809 /* ------------------- */
1810
1811 /* Present a popup to get the layers that should be used */
1812 static int armature_bone_layers_invoke (bContext *C, wmOperator *op, wmEvent *evt)
1813 {
1814         int layers[32]= {0}; /* hardcoded for now - we can only have 32 armature layers, so this should be fine... */
1815         
1816         /* get layers that are active already */
1817         CTX_DATA_BEGIN(C, EditBone *, ebone, selected_editable_bones) 
1818         {
1819                 short bit;
1820                 
1821                 /* loop over the bits for this pchan's layers, adding layers where they're needed */
1822                 for (bit= 0; bit < 32; bit++) {
1823                         if (ebone->layer & (1<<bit))
1824                                 layers[bit]= 1;
1825                 }
1826         }
1827         CTX_DATA_END;
1828         
1829         /* copy layers to operator */
1830         RNA_boolean_set_array(op->ptr, "layers", layers);
1831         
1832                 /* part to sync with other similar operators... */
1833         return WM_operator_props_popup(C, op, evt);
1834 }
1835
1836 /* Set the visible layers for the active armature (edit and pose modes) */
1837 static int armature_bone_layers_exec (bContext *C, wmOperator *op)
1838 {
1839         Object *ob= CTX_data_edit_object(C);
1840         bArmature *arm= (ob)? ob->data : NULL;
1841         PointerRNA ptr;
1842         int layers[32]; /* hardcoded for now - we can only have 32 armature layers, so this should be fine... */
1843         
1844         /* get the values set in the operator properties */
1845         RNA_boolean_get_array(op->ptr, "layers", layers);
1846         
1847         /* set layers of pchans based on the values set in the operator props */
1848         CTX_DATA_BEGIN(C, EditBone *, ebone, selected_editable_bones) 
1849         {
1850                 /* get pointer for pchan, and write flags this way */
1851                 RNA_pointer_create((ID *)arm, &RNA_EditBone, ebone, &ptr);
1852                 RNA_boolean_set_array(&ptr, "layers", layers);
1853         }
1854         CTX_DATA_END;
1855         
1856         /* note, notifier might evolve */
1857         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1858         
1859         return OPERATOR_FINISHED;
1860 }
1861
1862 void ARMATURE_OT_bone_layers (wmOperatorType *ot)
1863 {
1864         /* identifiers */
1865         ot->name= "Change Bone Layers";
1866         ot->idname= "ARMATURE_OT_bone_layers";
1867         ot->description= "Change the layers that the selected bones belong to";
1868         
1869         /* callbacks */
1870         ot->invoke= armature_bone_layers_invoke;
1871         ot->exec= armature_bone_layers_exec;
1872         ot->poll= ED_operator_editarmature;
1873         
1874         /* flags */
1875         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1876         
1877         /* properties */
1878         RNA_def_boolean_layer_member(ot->srna, "layers", 32, NULL, "Layer", "Armature layers that bone belongs to");
1879 }
1880
1881 /* ********************************************** */
1882
1883 static int pose_flip_quats_exec (bContext *C, wmOperator *UNUSED(op))
1884 {
1885         Scene *scene= CTX_data_scene(C);
1886         Object *ob= ED_object_pose_armature(CTX_data_active_object(C));
1887         KeyingSet *ks = ANIM_builtin_keyingset_get_named(NULL, "LocRotScale");
1888         
1889         /* loop through all selected pchans, flipping and keying (as needed) */
1890         CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pose_bones)
1891         {
1892                 /* only if bone is using quaternion rotation */
1893                 if (pchan->rotmode == ROT_MODE_QUAT) {
1894                         /* quaternions have 720 degree range */
1895                         pchan->quat[0]= -pchan->quat[0];
1896                         pchan->quat[1]= -pchan->quat[1];
1897                         pchan->quat[2]= -pchan->quat[2];
1898                         pchan->quat[3]= -pchan->quat[3];
1899                         
1900                         /* tagging */
1901                         if (autokeyframe_cfra_can_key(scene, &ob->id)) {
1902                                 ListBase dsources = {NULL, NULL};
1903                                 
1904                                 /* now insert the keyframe(s) using the Keying Set
1905                                  *      1) add datasource override for the PoseChannel
1906                                  *      2) insert keyframes
1907                                  *      3) free the extra info 
1908                                  */
1909                                 ANIM_relative_keyingset_add_source(&dsources, &ob->id, &RNA_PoseBone, pchan); 
1910                                 ANIM_apply_keyingset(C, &dsources, NULL, ks, MODIFYKEY_MODE_INSERT, (float)CFRA);
1911                                 BLI_freelistN(&dsources);
1912                                 
1913                                 /* clear any unkeyed tags */
1914                                 if (pchan->bone)
1915                                         pchan->bone->flag &= ~BONE_UNKEYED;
1916                         }
1917                         else {
1918                                 /* add unkeyed tags */
1919                                 if (pchan->bone)
1920                                         pchan->bone->flag |= BONE_UNKEYED;
1921                         }
1922                 }
1923         }
1924         CTX_DATA_END;
1925         
1926         /* notifiers and updates */
1927         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1928         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, ob);
1929         
1930         return OPERATOR_FINISHED;
1931 }
1932
1933 void POSE_OT_quaternions_flip (wmOperatorType *ot)
1934 {
1935         /* identifiers */
1936         ot->name = "Flip Quats";
1937         ot->idname= "POSE_OT_quaternions_flip";
1938         ot->description= "Flip quaternion values to achieve desired rotations, while maintaining the same orientations";
1939         
1940         /* callbacks */
1941         ot->exec= pose_flip_quats_exec;
1942         ot->poll= ED_operator_posemode;
1943         
1944         /* flags */
1945         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1946 }
1947
1948 /* ********************************************** */
1949
1950 /* context: active channel */
1951 #if 0
1952 void pose_special_editmenu(Scene *scene)
1953 {
1954         Object *obedit= scene->obedit; // XXX context
1955         Object *ob= OBACT;
1956         short nr;
1957         
1958         /* paranoia checks */
1959         if(!ob && !ob->pose) return;
1960         if(ob==obedit || (ob->mode & OB_MODE_POSE)==0) return;
1961         
1962         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");
1963         if(nr==1) {
1964                 pose_select_constraint_target(scene);
1965         }
1966         else if(nr==2) {
1967                 pose_flip_names();
1968         }
1969         else if(nr==3) {
1970                 pose_calculate_path(C, ob);
1971         }
1972         else if(nr==4) {
1973                 pose_clear_paths(ob);
1974         }
1975         else if(nr==5) {
1976                 pose_clear_user_transforms(ob);
1977         }
1978         else if(nr==6) {
1979                 pose_relax();
1980         }
1981         else if(ELEM3(nr, 7, 8, 9)) {
1982                 pose_autoside_names(nr-7);
1983         }
1984 }
1985
1986
1987 /* Restore selected pose-bones to 'action'-defined pose */
1988 static void pose_clear_user_transforms(Object *ob)
1989 {
1990         bArmature *arm= ob->data;
1991         bPoseChannel *pchan;
1992         
1993         if (ob->pose == NULL)
1994                 return;
1995         
1996         /* if the object has an action, restore pose to the pose defined by the action by clearing pose on selected bones */
1997         if (ob->action) {
1998                 /* find selected bones */
1999                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
2000                         if (pchan->bone && (pchan->bone->flag & BONE_SELECTED) && (pchan->bone->layer & arm->layer)) {
2001                                 /* just clear the BONE_UNKEYED flag, allowing this bone to get overwritten by actions again */
2002                                 pchan->bone->flag &= ~BONE_UNKEYED;
2003                         }
2004                 }
2005                 
2006                 /* clear pose locking flag 
2007                  *      - this will only clear the user-defined pose in the selected bones, where BONE_UNKEYED has been cleared
2008                  */
2009                 ob->pose->flag |= POSE_DO_UNLOCK;
2010         }
2011         else {
2012                 /* no action, so restore entire pose to rest pose (cannot restore only selected bones) */
2013                 rest_pose(ob->pose);
2014         }
2015         
2016         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
2017         BIF_undo_push("Clear User Transform");
2018 }
2019
2020 #endif