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