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