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