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