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