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