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