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