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