COLLADA branch: merge from trunk -r 25745:26429.
[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., 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
1119 static int pose_group_add_exec (bContext *C, wmOperator *op)
1120 {
1121         ScrArea *sa= CTX_wm_area(C);
1122         Object *ob;
1123         
1124         /* since this call may also be used from the buttons window, we need to check for where to get the object */
1125         if (sa->spacetype == SPACE_BUTS) 
1126                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1127         else
1128                 ob= CTX_data_active_object(C);
1129                 
1130         /* only continue if there's an object */
1131         if (ob == NULL)
1132                 return OPERATOR_CANCELLED;
1133         
1134         /* for now, just call the API function for this */
1135         pose_add_group(ob);
1136         
1137         /* notifiers for updates */
1138         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1139         
1140         return OPERATOR_FINISHED;
1141 }
1142
1143 void POSE_OT_group_add (wmOperatorType *ot)
1144 {
1145         /* identifiers */
1146         ot->name= "Add Bone Group";
1147         ot->idname= "POSE_OT_group_add";
1148         ot->description= "Add a new bone group.";
1149         
1150         /* api callbacks */
1151         ot->exec= pose_group_add_exec;
1152         ot->poll= ED_operator_posemode;
1153         
1154         /* flags */
1155         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1156 }
1157
1158
1159 static int pose_group_remove_exec (bContext *C, wmOperator *op)
1160 {
1161         ScrArea *sa= CTX_wm_area(C);
1162         Object *ob;
1163         
1164         /* since this call may also be used from the buttons window, we need to check for where to get the object */
1165         if (sa->spacetype == SPACE_BUTS) 
1166                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1167         else
1168                 ob= CTX_data_active_object(C);
1169         
1170         /* only continue if there's an object */
1171         if (ob == NULL)
1172                 return OPERATOR_CANCELLED;
1173         
1174         /* for now, just call the API function for this */
1175         pose_remove_group(ob);
1176         
1177         /* notifiers for updates */
1178         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1179         
1180         return OPERATOR_FINISHED;
1181 }
1182
1183 void POSE_OT_group_remove (wmOperatorType *ot)
1184 {
1185         /* identifiers */
1186         ot->name= "Remove Bone Group";
1187         ot->idname= "POSE_OT_group_remove";
1188         ot->description= "Removes the active bone group.";
1189         
1190         /* api callbacks */
1191         ot->exec= pose_group_remove_exec;
1192         ot->poll= ED_operator_posemode;
1193         
1194         /* flags */
1195         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1196 }
1197
1198 /* ------------ */
1199
1200 /* invoke callback which presents a list of bone-groups for the user to choose from */
1201 static int pose_groups_menu_invoke (bContext *C, wmOperator *op, wmEvent *evt)
1202 {
1203         ScrArea *sa= CTX_wm_area(C);
1204         Object *ob;
1205         bPose *pose;
1206         
1207         uiPopupMenu *pup;
1208         uiLayout *layout;
1209         bActionGroup *grp;
1210         int i;
1211         
1212         /* since this call may also be used from the buttons window, we need to check for where to get the object */
1213         if (sa->spacetype == SPACE_BUTS) 
1214                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1215         else
1216                 ob= CTX_data_active_object(C);
1217         
1218         /* only continue if there's an object, and a pose there too */
1219         if (ELEM(NULL, ob, ob->pose)) 
1220                 return OPERATOR_CANCELLED;
1221         pose= ob->pose;
1222         
1223         /* if there's no active group (or active is invalid), create a new menu to find it */
1224         if (pose->active_group <= 0) {
1225                 /* create a new menu, and start populating it with group names */
1226                 pup= uiPupMenuBegin(C, op->type->name, 0);
1227                 layout= uiPupMenuLayout(pup);
1228                 
1229                 /* special entry - allow to create new group, then use that 
1230                  *      (not to be used for removing though)
1231                  */
1232                 if (strstr(op->idname, "assign")) {
1233                         uiItemIntO(layout, "New Group", 0, op->idname, "type", 0);
1234                         uiItemS(layout);
1235                 }
1236                 
1237                 /* add entries for each group */
1238                 for (grp= pose->agroups.first, i=1; grp; grp=grp->next, i++)
1239                         uiItemIntO(layout, grp->name, 0, op->idname, "type", i);
1240                         
1241                 /* finish building the menu, and process it (should result in calling self again) */
1242                 uiPupMenuEnd(C, pup);
1243                 
1244                 return OPERATOR_CANCELLED;
1245         }
1246         else {
1247                 /* just use the active group index, and call the exec callback for the calling operator */
1248                 RNA_int_set(op->ptr, "type", pose->active_group);
1249                 return op->type->exec(C, op);
1250         }
1251 }
1252
1253 /* Assign selected pchans to the bone group that the user selects */
1254 static int pose_group_assign_exec (bContext *C, wmOperator *op)
1255 {
1256         ScrArea *sa= CTX_wm_area(C);
1257         Object *ob;
1258         bArmature *arm;
1259         bPose *pose;
1260         short done= 0;
1261         
1262         /* since this call may also be used from the buttons window, we need to check for where to get the object */
1263         if (sa->spacetype == SPACE_BUTS) 
1264                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1265         else
1266                 ob= CTX_data_active_object(C);
1267         
1268         /* only continue if there's an object, and a pose there too */
1269         if (ELEM(NULL, ob, ob->pose))
1270                 return OPERATOR_CANCELLED;
1271         arm= ob->data;
1272         pose= ob->pose;
1273         
1274         /* set the active group number to the one from operator props 
1275          *      - if 0 after this, make a new group...
1276          */
1277         pose->active_group= RNA_int_get(op->ptr, "type");
1278         if (pose->active_group == 0)
1279                 pose_add_group(ob);
1280         
1281         /* add selected bones to group then */
1282         // NOTE: unfortunately, we cannot use the context-iterators here, since they might not be defined...
1283         CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pose_bones) {
1284                 pchan->agrp_index= pose->active_group;
1285                 done= 1;
1286         }
1287         CTX_DATA_END;
1288
1289         /* notifiers for updates */
1290         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1291         
1292         /* report done status */
1293         if (done)
1294                 return OPERATOR_FINISHED;
1295         else
1296                 return OPERATOR_CANCELLED;
1297 }
1298
1299 void POSE_OT_group_assign (wmOperatorType *ot)
1300 {
1301         /* identifiers */
1302         ot->name= "Add Selected to Bone Group";
1303         ot->idname= "POSE_OT_group_assign";
1304         ot->description= "Add selected bones to the chosen bone group.";
1305         
1306         /* api callbacks */
1307         ot->invoke= pose_groups_menu_invoke;
1308         ot->exec= pose_group_assign_exec;
1309         ot->poll= ED_operator_posemode;
1310         
1311         /* flags */
1312         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1313         
1314         /* properties */
1315         RNA_def_int(ot->srna, "type", 0, 0, 10, "Bone Group Index", "", 0, INT_MAX);
1316 }
1317
1318
1319 static int pose_group_unassign_exec (bContext *C, wmOperator *op)
1320 {
1321         ScrArea *sa= CTX_wm_area(C);
1322         Object *ob;
1323         bArmature *arm;
1324         bPose *pose;
1325         bPoseChannel *pchan;
1326         short done= 0;
1327         
1328         /* since this call may also be used from the buttons window, we need to check for where to get the object */
1329         if (sa->spacetype == SPACE_BUTS) 
1330                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1331         else
1332                 ob= CTX_data_active_object(C);
1333         
1334         /* only continue if there's an object, and a pose there too */
1335         if (ELEM(NULL, ob, ob->pose))
1336                 return OPERATOR_CANCELLED;
1337         pose= ob->pose;
1338         arm= ob->data;
1339         
1340         /* find selected bones to remove from all bone groups */
1341         // NOTE: unfortunately, we cannot use the context-iterators here, since they might not be defined...
1342         // CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pose_bones) 
1343         for (pchan= pose->chanbase.first; pchan; pchan= pchan->next) {
1344                 /* ensure that PoseChannel is on visible layer and is not hidden in PoseMode */
1345                 // NOTE: sync this view3d_context() in space_view3d.c
1346                 if ((pchan->bone) && (arm->layer & pchan->bone->layer) && !(pchan->bone->flag & BONE_HIDDEN_P)) {
1347                         if ((pchan->bone->flag & BONE_SELECTED) || (pchan->bone == arm->act_bone)) {
1348                                 if (pchan->agrp_index) {
1349                                         pchan->agrp_index= 0;
1350                                         done= 1;
1351                                 }
1352                         }
1353                 }
1354         }
1355         
1356         /* notifiers for updates */
1357         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1358         
1359         /* report done status */
1360         if (done)
1361                 return OPERATOR_FINISHED;
1362         else
1363                 return OPERATOR_CANCELLED;
1364 }
1365
1366 void POSE_OT_group_unassign (wmOperatorType *ot)
1367 {
1368         /* identifiers */
1369         ot->name= "Remove Selected from Bone Groups";
1370         ot->idname= "POSE_OT_group_unassign";
1371         ot->description= "Remove selected bones from all bone groups";
1372         
1373         /* api callbacks */
1374         ot->exec= pose_group_unassign_exec;
1375         ot->poll= ED_operator_posemode;
1376         
1377         /* flags */
1378         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1379 }
1380
1381 /* ********************************************** */
1382
1383 static int pose_flip_names_exec (bContext *C, wmOperator *op)
1384 {
1385         Object *ob= CTX_data_active_object(C);
1386         bArmature *arm;
1387         char newname[32];
1388         
1389         /* paranoia checks */
1390         if (ELEM(NULL, ob, ob->pose)) 
1391                 return OPERATOR_CANCELLED;
1392         arm= ob->data;
1393         
1394         /* loop through selected bones, auto-naming them */
1395         CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pose_bones)
1396         {
1397                 BLI_strncpy(newname, pchan->name, sizeof(newname));
1398                 bone_flip_name(newname, 1);     // 1 = do strip off number extensions
1399                 ED_armature_bone_rename(arm, pchan->name, newname);
1400         }
1401         CTX_DATA_END;
1402         
1403         /* since we renamed stuff... */
1404         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
1405
1406         /* note, notifier might evolve */
1407         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1408         
1409         return OPERATOR_FINISHED;
1410 }
1411
1412 void POSE_OT_flip_names (wmOperatorType *ot)
1413 {
1414         /* identifiers */
1415         ot->name= "Flip Names";
1416         ot->idname= "POSE_OT_flip_names";
1417         ot->description= "Flips (and corrects) the names of selected bones.";
1418         
1419         /* api callbacks */
1420         ot->exec= pose_flip_names_exec;
1421         ot->poll= ED_operator_posemode;
1422         
1423         /* flags */
1424         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1425 }
1426
1427 /* ------------------ */
1428
1429 static int pose_autoside_names_exec (bContext *C, wmOperator *op)
1430 {
1431         Object *ob= CTX_data_active_object(C);
1432         bArmature *arm;
1433         char newname[32];
1434         short axis= RNA_enum_get(op->ptr, "axis");
1435         
1436         /* paranoia checks */
1437         if (ELEM(NULL, ob, ob->pose)) 
1438                 return OPERATOR_CANCELLED;
1439         arm= ob->data;
1440         
1441         /* loop through selected bones, auto-naming them */
1442         CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pose_bones)
1443         {
1444                 BLI_strncpy(newname, pchan->name, sizeof(newname));
1445                 bone_autoside_name(newname, 1, axis, pchan->bone->head[axis], pchan->bone->tail[axis]);
1446                 ED_armature_bone_rename(arm, pchan->name, newname);
1447         }
1448         CTX_DATA_END;
1449         
1450         /* since we renamed stuff... */
1451         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
1452
1453         /* note, notifier might evolve */
1454         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1455         
1456         return OPERATOR_FINISHED;
1457 }
1458
1459 void POSE_OT_autoside_names (wmOperatorType *ot)
1460 {
1461         static EnumPropertyItem axis_items[]= {
1462                 {0, "XAXIS", 0, "X-Axis", "Left/Right"},
1463                 {1, "YAXIS", 0, "Y-Axis", "Front/Back"},
1464                 {2, "ZAXIS", 0, "Z-Axis", "Top/Bottom"},
1465                 {0, NULL, 0, NULL, NULL}};
1466         
1467         /* identifiers */
1468         ot->name= "AutoName by Axis";
1469         ot->idname= "POSE_OT_autoside_names";
1470         ot->description= "Automatically renames the selected bones according to which side of the target axis they fall on.";
1471         
1472         /* api callbacks */
1473         ot->invoke= WM_menu_invoke;
1474         ot->exec= pose_autoside_names_exec;
1475         ot->poll= ED_operator_posemode;
1476         
1477         /* flags */
1478         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1479         
1480         /* settings */
1481         ot->prop= RNA_def_enum(ot->srna, "axis", axis_items, 0, "Axis", "Axis tag names with.");
1482 }
1483
1484 /* ********************************************** */
1485
1486 /* context active object, or weightpainted object with armature in posemode */
1487 void pose_activate_flipped_bone(Scene *scene)
1488 {
1489         Object *ob= OBACT;
1490         bArmature *arm= ob->data;
1491         
1492         if(ob==NULL) return;
1493
1494         if(ob->mode && OB_MODE_WEIGHT_PAINT) {
1495                 ob= modifiers_isDeformedByArmature(ob);
1496         }
1497         if(ob && (ob->mode & OB_MODE_POSE)) {
1498                 bPoseChannel *pchanf;
1499                 
1500                 if(arm->act_bone) {
1501                         char name[32];
1502                         
1503                         BLI_strncpy(name, arm->act_bone->name, 32);
1504                         bone_flip_name(name, 1);        // 0 = do not strip off number extensions
1505                         
1506                         pchanf= get_pose_channel(ob->pose, name);
1507                         if(pchanf && pchanf->bone != arm->act_bone) {
1508                                 arm->act_bone->flag &= ~BONE_SELECTED;
1509                                 pchanf->bone->flag |= BONE_SELECTED;
1510
1511                                 arm->act_bone= pchanf->bone;
1512
1513                                 /* in weightpaint we select the associated vertex group too */
1514                                 if(ob->mode & OB_MODE_WEIGHT_PAINT) {
1515                                         ED_vgroup_select_by_name(OBACT, name);
1516                                         DAG_id_flush_update(&OBACT->id, OB_RECALC_DATA);
1517                                 }
1518                                 
1519                                 // XXX notifiers need to be sent to other editors to update
1520                                 
1521                         }                       
1522                 }
1523         }
1524 }
1525
1526
1527 /* ********************************************** */
1528
1529 /* Present a popup to get the layers that should be used */
1530 static int pose_armature_layers_invoke (bContext *C, wmOperator *op, wmEvent *evt)
1531 {
1532         Object *ob= CTX_data_active_object(C);
1533         bArmature *arm= (ob)? ob->data : NULL;
1534         PointerRNA ptr;
1535         int layers[32]; /* hardcoded for now - we can only have 32 armature layers, so this should be fine... */
1536         
1537         /* sanity checking */
1538         if (arm == NULL)
1539                 return OPERATOR_CANCELLED;
1540                 
1541         /* get RNA pointer to armature data to use that to retrieve the layers as ints to init the operator */
1542         RNA_id_pointer_create((ID *)arm, &ptr);
1543         RNA_boolean_get_array(&ptr, "layer", layers);
1544         RNA_boolean_set_array(op->ptr, "layers", layers);
1545         
1546         /* part to sync with other similar operators... */
1547         return WM_operator_props_popup(C, op, evt);
1548 }
1549
1550 /* Set the visible layers for the active armature (edit and pose modes) */
1551 static int pose_armature_layers_exec (bContext *C, wmOperator *op)
1552 {
1553         Object *ob= CTX_data_active_object(C);
1554         bArmature *arm= (ob)? ob->data : NULL;
1555         PointerRNA ptr;
1556         int layers[32]; /* hardcoded for now - we can only have 32 armature layers, so this should be fine... */
1557         
1558         /* get the values set in the operator properties */
1559         RNA_boolean_get_array(op->ptr, "layers", layers);
1560         
1561         /* get pointer for armature, and write data there... */
1562         RNA_id_pointer_create((ID *)arm, &ptr);
1563         RNA_boolean_set_array(&ptr, "layer", layers);
1564         
1565         /* note, notifier might evolve */
1566         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1567         
1568         return OPERATOR_FINISHED;
1569 }
1570
1571
1572 void POSE_OT_armature_layers (wmOperatorType *ot)
1573 {
1574         /* identifiers */
1575         ot->name= "Change Armature Layers";
1576         ot->idname= "POSE_OT_armature_layers";
1577         ot->description= "Change the visible armature layers.";
1578         
1579         /* callbacks */
1580         ot->invoke= pose_armature_layers_invoke;
1581         ot->exec= pose_armature_layers_exec;
1582         ot->poll= ED_operator_posemode;
1583         
1584         /* flags */
1585         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1586         
1587         /* properties */
1588         RNA_def_boolean_layer_member(ot->srna, "layers", 32, NULL, "Layer", "Armature layers to make visible");
1589 }
1590
1591 void ARMATURE_OT_armature_layers (wmOperatorType *ot)
1592 {
1593         /* identifiers */
1594         ot->name= "Change Armature Layers";
1595         ot->idname= "ARMATURE_OT_armature_layers";
1596         ot->description= "Change the visible armature layers.";
1597         
1598         /* callbacks */
1599         ot->invoke= pose_armature_layers_invoke;
1600         ot->exec= pose_armature_layers_exec;
1601         ot->poll= ED_operator_editarmature;
1602         
1603         /* flags */
1604         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1605         
1606         /* properties */
1607         RNA_def_boolean_layer_member(ot->srna, "layers", 32, NULL, "Layer", "Armature layers to make visible");
1608 }
1609
1610 /* ------------------- */
1611
1612 /* Present a popup to get the layers that should be used */
1613 static int pose_bone_layers_invoke (bContext *C, wmOperator *op, wmEvent *evt)
1614 {
1615         int layers[32]; /* hardcoded for now - we can only have 32 armature layers, so this should be fine... */
1616         
1617         /* get layers that are active already */
1618         memset(&layers, 0, sizeof(layers)); /* set all layers to be off by default */
1619         
1620         CTX_DATA_BEGIN(C, bPoseChannel *, pchan, selected_pose_bones) 
1621         {
1622                 short bit;
1623                 
1624                 /* loop over the bits for this pchan's layers, adding layers where they're needed */
1625                 for (bit= 0; bit < 32; bit++) {
1626                         if (pchan->bone->layer & (1<<bit))
1627                                 layers[bit]= 1;
1628                 }
1629         }
1630         CTX_DATA_END;
1631         
1632         /* copy layers to operator */
1633         RNA_boolean_set_array(op->ptr, "layers", layers);
1634         
1635                 /* part to sync with other similar operators... */
1636         return WM_operator_props_popup(C, op, evt);
1637 }
1638
1639 /* Set the visible layers for the active armature (edit and pose modes) */
1640 static int pose_bone_layers_exec (bContext *C, wmOperator *op)
1641 {
1642         Object *ob= CTX_data_active_object(C);
1643         bArmature *arm= (ob)? ob->data : NULL;
1644         PointerRNA ptr;
1645         int layers[32]; /* hardcoded for now - we can only have 32 armature layers, so this should be fine... */
1646         
1647         /* get the values set in the operator properties */
1648         RNA_boolean_get_array(op->ptr, "layers", layers);
1649         
1650         /* set layers of pchans based on the values set in the operator props */
1651         CTX_DATA_BEGIN(C, bPoseChannel *, pchan, selected_pose_bones) 
1652         {
1653                 /* get pointer for pchan, and write flags this way */
1654                 RNA_pointer_create((ID *)arm, &RNA_Bone, pchan->bone, &ptr);
1655                 RNA_boolean_set_array(&ptr, "layer", layers);
1656         }
1657         CTX_DATA_END;
1658         
1659         /* note, notifier might evolve */
1660         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1661         
1662         return OPERATOR_FINISHED;
1663 }
1664
1665 void POSE_OT_bone_layers (wmOperatorType *ot)
1666 {
1667         /* identifiers */
1668         ot->name= "Change Bone Layers";
1669         ot->idname= "POSE_OT_bone_layers";
1670         ot->description= "Change the layers that the selected bones belong to.";
1671         
1672         /* callbacks */
1673         ot->invoke= pose_bone_layers_invoke;
1674         ot->exec= pose_bone_layers_exec;
1675         ot->poll= ED_operator_posemode;
1676         
1677         /* flags */
1678         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1679         
1680         /* properties */
1681         RNA_def_boolean_layer_member(ot->srna, "layers", 32, NULL, "Layer", "Armature layers that bone belongs to");
1682 }
1683
1684 /* ------------------- */
1685
1686 /* Present a popup to get the layers that should be used */
1687 static int armature_bone_layers_invoke (bContext *C, wmOperator *op, wmEvent *evt)
1688 {
1689         int layers[32]; /* hardcoded for now - we can only have 32 armature layers, so this should be fine... */
1690         
1691         /* get layers that are active already */
1692         memset(&layers, 0, sizeof(layers)); /* set all layers to be off by default */
1693         
1694         CTX_DATA_BEGIN(C, EditBone *, ebone, selected_editable_bones) 
1695         {
1696                 short bit;
1697                 
1698                 /* loop over the bits for this pchan's layers, adding layers where they're needed */
1699                 for (bit= 0; bit < 32; bit++) {
1700                         if (ebone->layer & (1<<bit))
1701                                 layers[bit]= 1;
1702                 }
1703         }
1704         CTX_DATA_END;
1705         
1706         /* copy layers to operator */
1707         RNA_boolean_set_array(op->ptr, "layers", layers);
1708         
1709                 /* part to sync with other similar operators... */
1710         return WM_operator_props_popup(C, op, evt);
1711 }
1712
1713 /* Set the visible layers for the active armature (edit and pose modes) */
1714 static int armature_bone_layers_exec (bContext *C, wmOperator *op)
1715 {
1716         Object *ob= CTX_data_edit_object(C);
1717         bArmature *arm= (ob)? ob->data : NULL;
1718         PointerRNA ptr;
1719         int layers[32]; /* hardcoded for now - we can only have 32 armature layers, so this should be fine... */
1720         
1721         /* get the values set in the operator properties */
1722         RNA_boolean_get_array(op->ptr, "layers", layers);
1723         
1724         /* set layers of pchans based on the values set in the operator props */
1725         CTX_DATA_BEGIN(C, EditBone *, ebone, selected_editable_bones) 
1726         {
1727                 /* get pointer for pchan, and write flags this way */
1728                 RNA_pointer_create((ID *)arm, &RNA_EditBone, ebone, &ptr);
1729                 RNA_boolean_set_array(&ptr, "layer", layers);
1730         }
1731         CTX_DATA_END;
1732         
1733         /* note, notifier might evolve */
1734         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1735         
1736         return OPERATOR_FINISHED;
1737 }
1738
1739 void ARMATURE_OT_bone_layers (wmOperatorType *ot)
1740 {
1741         /* identifiers */
1742         ot->name= "Change Bone Layers";
1743         ot->idname= "ARMATURE_OT_bone_layers";
1744         ot->description= "Change the layers that the selected bones belong to.";
1745         
1746         /* callbacks */
1747         ot->invoke= armature_bone_layers_invoke;
1748         ot->exec= armature_bone_layers_exec;
1749         ot->poll= ED_operator_editarmature;
1750         
1751         /* flags */
1752         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1753         
1754         /* properties */
1755         RNA_def_boolean_layer_member(ot->srna, "layers", 32, NULL, "Layer", "Armature layers that bone belongs to");
1756 }
1757
1758 /* ********************************************** */
1759
1760 static int pose_flip_quats_exec (bContext *C, wmOperator *op)
1761 {
1762         Scene *scene= CTX_data_scene(C);
1763         Object *ob= CTX_data_active_object(C);
1764         
1765         bCommonKeySrc cks;
1766         ListBase dsources = {&cks, &cks};
1767         
1768         /* init common-key-source for use by KeyingSets */
1769         memset(&cks, 0, sizeof(bCommonKeySrc));
1770         cks.id= &ob->id;
1771         
1772         /* loop through all selected pchans, flipping and keying (as needed) */
1773         CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pose_bones)
1774         {
1775                 /* only if bone is using quaternion rotation */
1776                 if (pchan->rotmode == ROT_MODE_QUAT) {
1777                         /* quaternions have 720 degree range */
1778                         pchan->quat[0]= -pchan->quat[0];
1779                         pchan->quat[1]= -pchan->quat[1];
1780                         pchan->quat[2]= -pchan->quat[2];
1781                         pchan->quat[3]= -pchan->quat[3];
1782                         
1783                         /* perform auto-keying 
1784                          * NOTE: paths don't need recalculation here, since the orientations shouldn't have changed
1785                          */
1786                         if (autokeyframe_cfra_can_key(scene, &ob->id)) {
1787                                 /* Set keys on pose
1788                                  *      - KeyingSet to use depends on rotation mode 
1789                                  *      (but that's handled by the templates code)  
1790                                  */
1791                                 KeyingSet *ks= ANIM_builtin_keyingset_get_named(NULL, "Rotation");
1792                                 
1793                                 /* init cks for this PoseChannel, then use the relative KeyingSets to keyframe it */
1794                                 cks.pchan= pchan;
1795                                 
1796                                 modify_keyframes(scene, &dsources, NULL, ks, MODIFYKEY_MODE_INSERT, (float)CFRA);
1797                                 
1798                                 /* clear any unkeyed tags */
1799                                 if (pchan->bone)
1800                                         pchan->bone->flag &= ~BONE_UNKEYED;
1801                         }
1802                         else {
1803                                 /* add unkeyed tags */
1804                                 if (pchan->bone)
1805                                         pchan->bone->flag |= BONE_UNKEYED;
1806                         }
1807                 }
1808         }
1809         CTX_DATA_END;
1810         
1811         /* notifiers and updates */
1812         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
1813         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, ob);
1814         
1815         return OPERATOR_FINISHED;
1816 }
1817
1818 void POSE_OT_quaternions_flip (wmOperatorType *ot)
1819 {
1820         /* identifiers */
1821         ot->name = "Flip Quats";
1822         ot->idname= "POSE_OT_quaternions_flip";
1823         ot->description= "Flip quaternion values to achieve desired rotations, while maintaining the same orientations.";
1824         
1825         /* callbacks */
1826         ot->exec= pose_flip_quats_exec;
1827         ot->poll= ED_operator_posemode;
1828         
1829         /* flags */
1830         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1831 }
1832
1833 /* ********************************************** */
1834
1835 /* context: active channel */
1836 void pose_special_editmenu(Scene *scene)
1837 {
1838 #if 0
1839         Object *obedit= scene->obedit; // XXX context
1840         Object *ob= OBACT;
1841         short nr;
1842         
1843         /* paranoia checks */
1844         if(!ob && !ob->pose) return;
1845         if(ob==obedit || (ob->mode & OB_MODE_POSE)==0) return;
1846         
1847         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");
1848         if(nr==1) {
1849                 pose_select_constraint_target(scene);
1850         }
1851         else if(nr==2) {
1852                 pose_flip_names();
1853         }
1854         else if(nr==3) {
1855                 pose_calculate_path(C, ob);
1856         }
1857         else if(nr==4) {
1858                 pose_clear_paths(ob);
1859         }
1860         else if(nr==5) {
1861                 pose_clear_user_transforms(scene, ob);
1862         }
1863         else if(nr==6) {
1864                 pose_relax();
1865         }
1866         else if(ELEM3(nr, 7, 8, 9)) {
1867                 pose_autoside_names(nr-7);
1868         }
1869 #endif
1870 }
1871
1872 /* Restore selected pose-bones to 'action'-defined pose */
1873 void pose_clear_user_transforms(Scene *scene, Object *ob)
1874 {
1875         bArmature *arm= ob->data;
1876         bPoseChannel *pchan;
1877         
1878         if (ob->pose == NULL)
1879                 return;
1880         
1881         /* if the object has an action, restore pose to the pose defined by the action by clearing pose on selected bones */
1882         if (ob->action) {
1883                 /* find selected bones */
1884                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1885                         if (pchan->bone && (pchan->bone->flag & BONE_SELECTED) && (pchan->bone->layer & arm->layer)) {
1886                                 /* just clear the BONE_UNKEYED flag, allowing this bone to get overwritten by actions again */
1887                                 pchan->bone->flag &= ~BONE_UNKEYED;
1888                         }
1889                 }
1890                 
1891                 /* clear pose locking flag 
1892                  *      - this will only clear the user-defined pose in the selected bones, where BONE_UNKEYED has been cleared
1893                  */
1894                 ob->pose->flag |= POSE_DO_UNLOCK;
1895         }
1896         else {
1897                 /* no action, so restore entire pose to rest pose (cannot restore only selected bones) */
1898                 rest_pose(ob->pose);
1899         }
1900         
1901         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
1902         BIF_undo_push("Clear User Transform");
1903 }
1904