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