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