beae9c12bfe7b589734d0b05411964f3ddc3ad5d
[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         /* get KeyingSet to use */
1154         KeyingSet *ks = ANIM_get_keyingset_for_autokeying(scene, ANIM_KS_LOC_ROT_SCALE_ID);
1155
1156         /* sanity checks */
1157         if ELEM(NULL, ob, ob->pose)
1158                 return OPERATOR_CANCELLED;
1159
1160         if (g_posebuf == NULL) {
1161                 BKE_report(op->reports, RPT_ERROR, "Copy buffer is empty");
1162                 return OPERATOR_CANCELLED;
1163         }
1164         
1165         /* if selOnly option is enabled, if user hasn't selected any bones, 
1166          * just go back to default behaviour to be more in line with other pose tools
1167          */
1168         if (selOnly) {
1169                 if (CTX_DATA_COUNT(C, selected_pose_bones) == 0)
1170                         selOnly = 0;
1171         }
1172
1173         /* Safely merge all of the channels in the buffer pose into any existing pose */
1174         for (chan= g_posebuf->chanbase.first; chan; chan=chan->next) {
1175                 if (chan->flag & POSE_KEY) {
1176                         /* try to perform paste on this bone */
1177                         bPoseChannel *pchan = pose_bone_do_paste(ob, chan, selOnly, flip);
1178                         
1179                         if (pchan) {
1180                                 /* keyframing tagging for successful paste */
1181                                 ED_autokeyframe_pchan(C, scene, ob, pchan, ks);
1182                         }
1183                 }
1184         }
1185         
1186         /* Update event for pose and deformation children */
1187         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1188                 
1189         /* notifiers for updates */
1190         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1191
1192         return OPERATOR_FINISHED;
1193 }
1194
1195 void POSE_OT_paste (wmOperatorType *ot)
1196 {
1197         /* identifiers */
1198         ot->name= "Paste Pose";
1199         ot->idname= "POSE_OT_paste";
1200         ot->description= "Paste the stored pose on to the current pose";
1201         
1202         /* api callbacks */
1203         ot->exec= pose_paste_exec;
1204         ot->poll= ED_operator_posemode;
1205         
1206         /* flag */
1207         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1208         
1209         /* properties */
1210         RNA_def_boolean(ot->srna, "flipped", FALSE, "Flipped on X-Axis", "Paste the stored pose flipped on to current pose");
1211         RNA_def_boolean(ot->srna, "selected_mask", FALSE, "On Selected Only", "Only paste the stored pose on to selected bones in the current pose");
1212 }
1213
1214 /* ********************************************** */
1215 /* Bone Groups */
1216
1217 static int pose_group_add_exec (bContext *C, wmOperator *UNUSED(op))
1218 {
1219         ScrArea *sa= CTX_wm_area(C);
1220         Object *ob;
1221         
1222         /* since this call may also be used from the buttons window, we need to check for where to get the object */
1223         if (sa->spacetype == SPACE_BUTS) 
1224                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1225         else
1226                 ob= object_pose_armature_get(CTX_data_active_object(C));
1227                 
1228         /* only continue if there's an object */
1229         if (ob == NULL)
1230                 return OPERATOR_CANCELLED;
1231         
1232         /* for now, just call the API function for this */
1233         pose_add_group(ob);
1234         
1235         /* notifiers for updates */
1236         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1237         
1238         return OPERATOR_FINISHED;
1239 }
1240
1241 void POSE_OT_group_add (wmOperatorType *ot)
1242 {
1243         /* identifiers */
1244         ot->name= "Add Bone Group";
1245         ot->idname= "POSE_OT_group_add";
1246         ot->description= "Add a new bone group";
1247         
1248         /* api callbacks */
1249         ot->exec= pose_group_add_exec;
1250         ot->poll= ED_operator_posemode;
1251         
1252         /* flags */
1253         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1254 }
1255
1256
1257 static int pose_group_remove_exec (bContext *C, wmOperator *UNUSED(op))
1258 {
1259         ScrArea *sa= CTX_wm_area(C);
1260         Object *ob;
1261         
1262         /* since this call may also be used from the buttons window, we need to check for where to get the object */
1263         if (sa->spacetype == SPACE_BUTS) 
1264                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1265         else
1266                 ob= object_pose_armature_get(CTX_data_active_object(C));
1267         
1268         /* only continue if there's an object */
1269         if (ob == NULL)
1270                 return OPERATOR_CANCELLED;
1271         
1272         /* for now, just call the API function for this */
1273         pose_remove_group(ob);
1274         
1275         /* notifiers for updates */
1276         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1277         
1278         return OPERATOR_FINISHED;
1279 }
1280
1281 void POSE_OT_group_remove (wmOperatorType *ot)
1282 {
1283         /* identifiers */
1284         ot->name= "Remove Bone Group";
1285         ot->idname= "POSE_OT_group_remove";
1286         ot->description= "Removes the active bone group";
1287         
1288         /* api callbacks */
1289         ot->exec= pose_group_remove_exec;
1290         ot->poll= ED_operator_posemode;
1291         
1292         /* flags */
1293         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1294 }
1295
1296 /* ------------ */
1297
1298 /* invoke callback which presents a list of bone-groups for the user to choose from */
1299 static int pose_groups_menu_invoke (bContext *C, wmOperator *op, wmEvent *UNUSED(evt))
1300 {
1301         ScrArea *sa= CTX_wm_area(C);
1302         Object *ob;
1303         bPose *pose;
1304         
1305         uiPopupMenu *pup;
1306         uiLayout *layout;
1307         bActionGroup *grp;
1308         int i;
1309         
1310         /* since this call may also be used from the buttons window, we need to check for where to get the object */
1311         if (sa->spacetype == SPACE_BUTS) 
1312                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1313         else
1314                 ob= object_pose_armature_get(CTX_data_active_object(C));
1315         
1316         /* only continue if there's an object, and a pose there too */
1317         if (ELEM(NULL, ob, ob->pose)) 
1318                 return OPERATOR_CANCELLED;
1319         pose= ob->pose;
1320         
1321         /* if there's no active group (or active is invalid), create a new menu to find it */
1322         if (pose->active_group <= 0) {
1323                 /* create a new menu, and start populating it with group names */
1324                 pup= uiPupMenuBegin(C, op->type->name, ICON_NONE);
1325                 layout= uiPupMenuLayout(pup);
1326                 
1327                 /* special entry - allow to create new group, then use that 
1328                  *      (not to be used for removing though)
1329                  */
1330                 if (strstr(op->idname, "assign")) {
1331                         uiItemIntO(layout, "New Group", ICON_NONE, op->idname, "type", 0);
1332                         uiItemS(layout);
1333                 }
1334                 
1335                 /* add entries for each group */
1336                 for (grp= pose->agroups.first, i=1; grp; grp=grp->next, i++)
1337                         uiItemIntO(layout, grp->name, ICON_NONE, op->idname, "type", i);
1338                         
1339                 /* finish building the menu, and process it (should result in calling self again) */
1340                 uiPupMenuEnd(C, pup);
1341                 
1342                 return OPERATOR_CANCELLED;
1343         }
1344         else {
1345                 /* just use the active group index, and call the exec callback for the calling operator */
1346                 RNA_int_set(op->ptr, "type", pose->active_group);
1347                 return op->type->exec(C, op);
1348         }
1349 }
1350
1351 /* Assign selected pchans to the bone group that the user selects */
1352 static int pose_group_assign_exec (bContext *C, wmOperator *op)
1353 {
1354         ScrArea *sa= CTX_wm_area(C);
1355         Object *ob;
1356         bPose *pose;
1357         short done= 0;
1358         
1359         /* since this call may also be used from the buttons window, we need to check for where to get the object */
1360         if (sa->spacetype == SPACE_BUTS) 
1361                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1362         else
1363                 ob= object_pose_armature_get(CTX_data_active_object(C));
1364         
1365         /* only continue if there's an object, and a pose there too */
1366         if (ELEM(NULL, ob, ob->pose))
1367                 return OPERATOR_CANCELLED;
1368
1369         pose= ob->pose;
1370         
1371         /* set the active group number to the one from operator props 
1372          *      - if 0 after this, make a new group...
1373          */
1374         pose->active_group= RNA_int_get(op->ptr, "type");
1375         if (pose->active_group == 0)
1376                 pose_add_group(ob);
1377         
1378         /* add selected bones to group then */
1379         CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pose_bones)
1380         {
1381                 pchan->agrp_index= pose->active_group;
1382                 done= 1;
1383         }
1384         CTX_DATA_END;
1385
1386         /* notifiers for updates */
1387         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1388         
1389         /* report done status */
1390         if (done)
1391                 return OPERATOR_FINISHED;
1392         else
1393                 return OPERATOR_CANCELLED;
1394 }
1395
1396 void POSE_OT_group_assign (wmOperatorType *ot)
1397 {
1398         /* identifiers */
1399         ot->name= "Add Selected to Bone Group";
1400         ot->idname= "POSE_OT_group_assign";
1401         ot->description= "Add selected bones to the chosen bone group";
1402         
1403         /* api callbacks */
1404         ot->invoke= pose_groups_menu_invoke;
1405         ot->exec= pose_group_assign_exec;
1406         ot->poll= ED_operator_posemode;
1407         
1408         /* flags */
1409         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1410         
1411         /* properties */
1412         RNA_def_int(ot->srna, "type", 0, 0, 10, "Bone Group Index", "", 0, INT_MAX);
1413 }
1414
1415
1416 static int pose_group_unassign_exec (bContext *C, wmOperator *UNUSED(op))
1417 {
1418         ScrArea *sa= CTX_wm_area(C);
1419         Object *ob;
1420         short done= 0;
1421         
1422         /* since this call may also be used from the buttons window, we need to check for where to get the object */
1423         if (sa->spacetype == SPACE_BUTS) 
1424                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1425         else
1426                 ob= object_pose_armature_get(CTX_data_active_object(C));
1427         
1428         /* only continue if there's an object, and a pose there too */
1429         if (ELEM(NULL, ob, ob->pose))
1430                 return OPERATOR_CANCELLED;
1431         
1432         /* find selected bones to remove from all bone groups */
1433         CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pose_bones)
1434         {
1435                 if (pchan->agrp_index) {
1436                         pchan->agrp_index= 0;
1437                         done= 1;
1438                 }
1439         }
1440         CTX_DATA_END;
1441         
1442         /* notifiers for updates */
1443         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1444         
1445         /* report done status */
1446         if (done)
1447                 return OPERATOR_FINISHED;
1448         else
1449                 return OPERATOR_CANCELLED;
1450 }
1451
1452 void POSE_OT_group_unassign (wmOperatorType *ot)
1453 {
1454         /* identifiers */
1455         ot->name= "Remove Selected from Bone Groups";
1456         ot->idname= "POSE_OT_group_unassign";
1457         ot->description= "Remove selected bones from all bone groups";
1458         
1459         /* api callbacks */
1460         ot->exec= pose_group_unassign_exec;
1461         ot->poll= ED_operator_posemode;
1462         
1463         /* flags */
1464         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1465 }
1466
1467 static int group_move_exec(bContext *C, wmOperator *op)
1468 {
1469         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1470         bPose *pose= (ob) ? ob->pose : NULL;
1471         bPoseChannel *pchan;
1472         bActionGroup *grp;
1473         int dir= RNA_enum_get(op->ptr, "direction");
1474         int grpIndexA, grpIndexB;
1475
1476         if (ELEM(NULL, ob, pose))
1477                 return OPERATOR_CANCELLED;
1478         if (pose->active_group <= 0)
1479                 return OPERATOR_CANCELLED;
1480
1481         /* get group to move */
1482         grp= BLI_findlink(&pose->agroups, pose->active_group-1);
1483         if (grp == NULL)
1484                 return OPERATOR_CANCELLED;
1485
1486         /* move bone group */
1487         grpIndexA = pose->active_group;
1488         if (dir == 1) { /* up */
1489                 void *prev = grp->prev;
1490                 
1491                 if (prev == NULL)
1492                         return OPERATOR_FINISHED;
1493                         
1494                 BLI_remlink(&pose->agroups, grp);
1495                 BLI_insertlinkbefore(&pose->agroups, prev, grp);
1496                 
1497                 grpIndexB = grpIndexA - 1;
1498                 pose->active_group--;
1499         }
1500         else { /* down */
1501                 void *next = grp->next;
1502                 
1503                 if (next == NULL)
1504                         return OPERATOR_FINISHED;
1505                         
1506                 BLI_remlink(&pose->agroups, grp);
1507                 BLI_insertlinkafter(&pose->agroups, next, grp);
1508                 
1509                 grpIndexB = grpIndexA + 1;
1510                 pose->active_group++;
1511         }
1512
1513         /* fix changed bone group indices in bones (swap grpIndexA with grpIndexB) */
1514         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1515                 if (pchan->agrp_index == grpIndexB)
1516                         pchan->agrp_index= grpIndexA;
1517                 else if (pchan->agrp_index == grpIndexA)
1518                         pchan->agrp_index= grpIndexB;
1519         }
1520
1521         /* notifiers for updates */
1522         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1523
1524         return OPERATOR_FINISHED;
1525 }
1526
1527 void POSE_OT_group_move(wmOperatorType *ot)
1528 {
1529         static EnumPropertyItem group_slot_move[] = {
1530                 {1, "UP", 0, "Up", ""},
1531                 {-1, "DOWN", 0, "Down", ""},
1532                 {0, NULL, 0, NULL, NULL}
1533         };
1534
1535         /* identifiers */
1536         ot->name= "Move Bone Group";
1537         ot->idname= "POSE_OT_group_move";
1538         ot->description= "Change position of active Bone Group in list of Bone Groups";
1539
1540         /* api callbacks */
1541         ot->exec= group_move_exec;
1542         ot->poll= ED_operator_posemode;
1543
1544         /* flags */
1545         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1546
1547         RNA_def_enum(ot->srna, "direction", group_slot_move, 0, "Direction", "Direction to move, UP or DOWN");
1548 }
1549
1550 /* bone group sort element */
1551 typedef struct tSortActionGroup {
1552         bActionGroup *agrp;
1553         int          index;
1554 } tSortActionGroup;
1555
1556 /* compare bone groups by name */
1557 static int compare_agroup(const void *sgrp_a_ptr, const void *sgrp_b_ptr)
1558 {
1559         tSortActionGroup *sgrp_a= (tSortActionGroup *)sgrp_a_ptr;
1560         tSortActionGroup *sgrp_b= (tSortActionGroup *)sgrp_b_ptr;
1561
1562         return strcmp(sgrp_a->agrp->name, sgrp_b->agrp->name);
1563 }
1564
1565 static int group_sort_exec(bContext *C, wmOperator *UNUSED(op))
1566 {
1567         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1568         bPose *pose= (ob) ? ob->pose : NULL;
1569         bPoseChannel *pchan;
1570         tSortActionGroup *agrp_array;
1571         bActionGroup *agrp;
1572         int agrp_count;
1573         int i;
1574
1575         if (ELEM(NULL, ob, pose))
1576                 return OPERATOR_CANCELLED;
1577         if (pose->active_group <= 0)
1578                 return OPERATOR_CANCELLED;
1579
1580         /* create temporary array with bone groups and indices */
1581         agrp_count = BLI_countlist(&pose->agroups);
1582         agrp_array = MEM_mallocN(sizeof(tSortActionGroup) * agrp_count, "sort bone groups");
1583         for (agrp= pose->agroups.first, i= 0; agrp; agrp= agrp->next, i++) {
1584                 BLI_assert(i < agrp_count);
1585                 agrp_array[i].agrp = agrp;
1586                 agrp_array[i].index = i+1;
1587         }
1588
1589         /* sort bone groups by name */
1590         qsort(agrp_array, agrp_count, sizeof(tSortActionGroup), compare_agroup);
1591
1592         /* create sorted bone group list from sorted array */
1593         pose->agroups.first= pose->agroups.last= NULL;
1594         for (i= 0; i < agrp_count; i++) {
1595                 BLI_addtail(&pose->agroups, agrp_array[i].agrp);
1596         }
1597
1598         /* fix changed bone group indizes in bones */
1599         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1600                 for (i= 0; i < agrp_count; i++) {
1601                         if (pchan->agrp_index == agrp_array[i].index) {
1602                                 pchan->agrp_index= i+1;
1603                                 break;
1604                         }
1605                 }
1606         }
1607
1608         /* free temp resources */
1609         MEM_freeN(agrp_array);
1610
1611         /* notifiers for updates */
1612         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1613
1614         return OPERATOR_FINISHED;
1615 }
1616
1617 void POSE_OT_group_sort(wmOperatorType *ot)
1618 {
1619         /* identifiers */
1620         ot->name= "Sort Bone Groups";
1621         ot->idname= "POSE_OT_group_sort";
1622         ot->description= "Sort Bone Groups by their names in ascending order";
1623
1624         /* api callbacks */
1625         ot->exec= group_sort_exec;
1626         ot->poll= ED_operator_posemode;
1627
1628         /* flags */
1629         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1630 }
1631
1632 static void pose_group_select(bContext *C, Object *ob, int select)
1633 {
1634         bPose *pose= ob->pose;
1635         
1636         CTX_DATA_BEGIN(C, bPoseChannel*, pchan, visible_pose_bones)
1637         {
1638                 if ((pchan->bone->flag & BONE_UNSELECTABLE)==0) {
1639                         if (select) {
1640                                 if (pchan->agrp_index == pose->active_group) 
1641                                         pchan->bone->flag |= BONE_SELECTED;
1642                         }
1643                         else {
1644                                 if (pchan->agrp_index == pose->active_group) 
1645                                         pchan->bone->flag &= ~BONE_SELECTED;
1646                         }
1647                 }
1648         }
1649         CTX_DATA_END;
1650 }
1651
1652 static int pose_group_select_exec (bContext *C, wmOperator *UNUSED(op))
1653 {
1654         ScrArea *sa= CTX_wm_area(C);
1655         Object *ob;
1656         
1657         /* since this call may also be used from the buttons window, we need to check for where to get the object */
1658         if (sa->spacetype == SPACE_BUTS) 
1659                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1660         else
1661                 ob= object_pose_armature_get(CTX_data_active_object(C));
1662         
1663         /* only continue if there's an object, and a pose there too */
1664         if (ELEM(NULL, ob, ob->pose))
1665                 return OPERATOR_CANCELLED;
1666         
1667         pose_group_select(C, ob, 1);
1668         
1669         /* notifiers for updates */
1670         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1671         
1672         return OPERATOR_FINISHED;
1673 }
1674
1675 void POSE_OT_group_select (wmOperatorType *ot)
1676 {
1677         /* identifiers */
1678         ot->name= "Select Bones of Bone Group";
1679         ot->idname= "POSE_OT_group_select";
1680         ot->description= "Select bones in active Bone Group";
1681         
1682         /* api callbacks */
1683         ot->exec= pose_group_select_exec;
1684         ot->poll= ED_operator_posemode;
1685         
1686         /* flags */
1687         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1688 }
1689
1690 static int pose_group_deselect_exec (bContext *C, wmOperator *UNUSED(op))
1691 {
1692         ScrArea *sa= CTX_wm_area(C);
1693         Object *ob;
1694         
1695         /* since this call may also be used from the buttons window, we need to check for where to get the object */
1696         if (sa->spacetype == SPACE_BUTS) 
1697                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
1698         else
1699                 ob= object_pose_armature_get(CTX_data_active_object(C));
1700         
1701         /* only continue if there's an object, and a pose there too */
1702         if (ELEM(NULL, ob, ob->pose))
1703                 return OPERATOR_CANCELLED;
1704         
1705         pose_group_select(C, ob, 0);
1706         
1707         /* notifiers for updates */
1708         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1709         
1710         return OPERATOR_FINISHED;
1711 }
1712
1713 void POSE_OT_group_deselect (wmOperatorType *ot)
1714 {
1715         /* identifiers */
1716         ot->name= "Deselect Bone Group";
1717         ot->idname= "POSE_OT_group_deselect";
1718         ot->description= "Deselect bones of active Bone Group";
1719         
1720         /* api callbacks */
1721         ot->exec= pose_group_deselect_exec;
1722         ot->poll= ED_operator_posemode;
1723         
1724         /* flags */
1725         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1726 }
1727
1728 /* ********************************************** */
1729
1730 static int pose_flip_names_exec (bContext *C, wmOperator *UNUSED(op))
1731 {
1732         Object *ob= object_pose_armature_get(CTX_data_active_object(C));
1733         bArmature *arm;
1734         
1735         /* paranoia checks */
1736         if (ELEM(NULL, ob, ob->pose)) 
1737                 return OPERATOR_CANCELLED;
1738         arm= ob->data;
1739         
1740         /* loop through selected bones, auto-naming them */
1741         CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pose_bones)
1742         {
1743                 char newname[32];
1744                 flip_side_name(newname, pchan->name, TRUE);
1745                 ED_armature_bone_rename(arm, pchan->name, newname);
1746         }
1747         CTX_DATA_END;
1748         
1749         /* since we renamed stuff... */
1750         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1751
1752         /* note, notifier might evolve */
1753         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1754         
1755         return OPERATOR_FINISHED;
1756 }
1757
1758 void POSE_OT_flip_names (wmOperatorType *ot)
1759 {
1760         /* identifiers */
1761         ot->name= "Flip Names";
1762         ot->idname= "POSE_OT_flip_names";
1763         ot->description= "Flips (and corrects) the axis suffixes of the the names of selected bones";
1764         
1765         /* api callbacks */
1766         ot->exec= pose_flip_names_exec;
1767         ot->poll= ED_operator_posemode;
1768         
1769         /* flags */
1770         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1771 }
1772
1773 /* ------------------ */
1774
1775 static int pose_autoside_names_exec (bContext *C, wmOperator *op)
1776 {
1777         Object *ob= object_pose_armature_get(CTX_data_active_object(C));
1778         bArmature *arm;
1779         char newname[32];
1780         short axis= RNA_enum_get(op->ptr, "axis");
1781         
1782         /* paranoia checks */
1783         if (ELEM(NULL, ob, ob->pose)) 
1784                 return OPERATOR_CANCELLED;
1785         arm= ob->data;
1786         
1787         /* loop through selected bones, auto-naming them */
1788         CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pose_bones)
1789         {
1790                 BLI_strncpy(newname, pchan->name, sizeof(newname));
1791                 if(bone_autoside_name(newname, 1, axis, pchan->bone->head[axis], pchan->bone->tail[axis]))
1792                         ED_armature_bone_rename(arm, pchan->name, newname);
1793         }
1794         CTX_DATA_END;
1795         
1796         /* since we renamed stuff... */
1797         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1798
1799         /* note, notifier might evolve */
1800         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1801         
1802         return OPERATOR_FINISHED;
1803 }
1804
1805 void POSE_OT_autoside_names (wmOperatorType *ot)
1806 {
1807         static EnumPropertyItem axis_items[]= {
1808                 {0, "XAXIS", 0, "X-Axis", "Left/Right"},
1809                 {1, "YAXIS", 0, "Y-Axis", "Front/Back"},
1810                 {2, "ZAXIS", 0, "Z-Axis", "Top/Bottom"},
1811                 {0, NULL, 0, NULL, NULL}};
1812         
1813         /* identifiers */
1814         ot->name= "AutoName by Axis";
1815         ot->idname= "POSE_OT_autoside_names";
1816         ot->description= "Automatically renames the selected bones according to which side of the target axis they fall on";
1817         
1818         /* api callbacks */
1819         ot->invoke= WM_menu_invoke;
1820         ot->exec= pose_autoside_names_exec;
1821         ot->poll= ED_operator_posemode;
1822         
1823         /* flags */
1824         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1825         
1826         /* settings */
1827         ot->prop= RNA_def_enum(ot->srna, "axis", axis_items, 0, "Axis", "Axis tag names with");
1828 }
1829
1830 /* ********************************************** */
1831
1832 static int pose_bone_rotmode_exec (bContext *C, wmOperator *op)
1833 {
1834         Object *ob = CTX_data_active_object(C);
1835         int mode = RNA_enum_get(op->ptr, "type");
1836         
1837         /* set rotation mode of selected bones  */      
1838         CTX_DATA_BEGIN(C, bPoseChannel *, pchan, selected_pose_bones) 
1839         {
1840                 pchan->rotmode = mode;
1841         }
1842         CTX_DATA_END;
1843         
1844         /* notifiers and updates */
1845         DAG_id_tag_update((ID *)ob, OB_RECALC_DATA);
1846         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, ob);
1847         
1848         return OPERATOR_FINISHED;
1849 }
1850
1851 void POSE_OT_rotation_mode_set (wmOperatorType *ot)
1852 {
1853         /* identifiers */
1854         ot->name= "Set Rotation Mode";
1855         ot->idname= "POSE_OT_rotation_mode_set";
1856         ot->description= "Set the rotation representation used by selected bones";
1857         
1858         /* callbacks */
1859         ot->invoke= WM_menu_invoke;
1860         ot->exec= pose_bone_rotmode_exec;
1861         ot->poll= ED_operator_posemode;
1862         
1863         /* flags */
1864         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1865         
1866         /* properties */
1867         ot->prop= RNA_def_enum(ot->srna, "type", posebone_rotmode_items, 0, "Rotation Mode", "");
1868 }
1869
1870 /* ********************************************** */
1871
1872 /* Show all armature layers */
1873 static int pose_armature_layers_showall_poll (bContext *C)
1874 {
1875         /* this single operator can be used in posemode OR editmode for armatures */
1876         return ED_operator_posemode(C) || ED_operator_editarmature(C);
1877 }
1878
1879 static int pose_armature_layers_showall_exec (bContext *C, wmOperator *op)
1880 {
1881         Object *ob= object_pose_armature_get(CTX_data_active_object(C));
1882         bArmature *arm = (ob)? ob->data : NULL;
1883         PointerRNA ptr;
1884         int maxLayers = (RNA_boolean_get(op->ptr, "all"))? 32 : 16;
1885         int layers[32] = {0}; /* hardcoded for now - we can only have 32 armature layers, so this should be fine... */
1886         int i;
1887         
1888         /* sanity checking */
1889         if (arm == NULL)
1890                 return OPERATOR_CANCELLED;
1891         
1892         /* use RNA to set the layers
1893          *      although it would be faster to just set directly using bitflags, we still
1894          *      need to setup a RNA pointer so that we get the "update" callbacks for free...
1895          */
1896         RNA_id_pointer_create(&arm->id, &ptr);
1897         
1898         for (i = 0; i < maxLayers; i++)
1899                 layers[i] = 1;
1900         
1901         RNA_boolean_set_array(&ptr, "layers", layers);
1902         
1903         /* note, notifier might evolve */
1904         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1905         
1906         /* done */
1907         return OPERATOR_FINISHED;
1908 }
1909
1910 void ARMATURE_OT_layers_show_all (wmOperatorType *ot)
1911 {
1912         /* identifiers */
1913         ot->name= "Show All Layers";
1914         ot->idname= "ARMATURE_OT_layers_show_all";
1915         ot->description= "Make all armature layers visible";
1916         
1917         /* callbacks */
1918         ot->exec= pose_armature_layers_showall_exec;
1919         ot->poll= pose_armature_layers_showall_poll;
1920         
1921         /* flags */
1922         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1923         
1924         /* properties */
1925         ot->prop = RNA_def_boolean(ot->srna, "all", 1, "All Layers", "Enable all layers or just the first 16 (top row)");
1926 }
1927
1928 /* ------------------- */
1929
1930 /* Present a popup to get the layers that should be used */
1931 static int pose_armature_layers_invoke (bContext *C, wmOperator *op, wmEvent *evt)
1932 {
1933         Object *ob= object_pose_armature_get(CTX_data_active_object(C));
1934         bArmature *arm= (ob)? ob->data : NULL;
1935         PointerRNA ptr;
1936         int layers[32]; /* hardcoded for now - we can only have 32 armature layers, so this should be fine... */
1937         
1938         /* sanity checking */
1939         if (arm == NULL)
1940                 return OPERATOR_CANCELLED;
1941                 
1942         /* get RNA pointer to armature data to use that to retrieve the layers as ints to init the operator */
1943         RNA_id_pointer_create((ID *)arm, &ptr);
1944         RNA_boolean_get_array(&ptr, "layers", layers);
1945         RNA_boolean_set_array(op->ptr, "layers", layers);
1946         
1947         /* part to sync with other similar operators... */
1948         return WM_operator_props_popup(C, op, evt);
1949 }
1950
1951 /* Set the visible layers for the active armature (edit and pose modes) */
1952 static int pose_armature_layers_exec (bContext *C, wmOperator *op)
1953 {
1954         Object *ob= object_pose_armature_get(CTX_data_active_object(C));
1955         PointerRNA ptr;
1956         int layers[32]; /* hardcoded for now - we can only have 32 armature layers, so this should be fine... */
1957
1958         if (ELEM(NULL, ob, ob->data)) {
1959                 return OPERATOR_CANCELLED;
1960         }
1961
1962         /* get the values set in the operator properties */
1963         RNA_boolean_get_array(op->ptr, "layers", layers);
1964
1965         /* get pointer for armature, and write data there... */
1966         RNA_id_pointer_create((ID *)ob->data, &ptr);
1967         RNA_boolean_set_array(&ptr, "layers", layers);
1968
1969         /* note, notifier might evolve */
1970         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
1971
1972         return OPERATOR_FINISHED;
1973 }
1974
1975
1976 void POSE_OT_armature_layers (wmOperatorType *ot)
1977 {
1978         /* identifiers */
1979         ot->name= "Change Armature Layers";
1980         ot->idname= "POSE_OT_armature_layers";
1981         ot->description= "Change the visible armature layers";
1982         
1983         /* callbacks */
1984         ot->invoke= pose_armature_layers_invoke;
1985         ot->exec= pose_armature_layers_exec;
1986         ot->poll= ED_operator_posemode;
1987         
1988         /* flags */
1989         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1990         
1991         /* properties */
1992         RNA_def_boolean_layer_member(ot->srna, "layers", 32, NULL, "Layer", "Armature layers to make visible");
1993 }
1994
1995 void ARMATURE_OT_armature_layers (wmOperatorType *ot)
1996 {
1997         /* identifiers */
1998         ot->name= "Change Armature Layers";
1999         ot->idname= "ARMATURE_OT_armature_layers";
2000         ot->description= "Change the visible armature layers";
2001         
2002         /* callbacks */
2003         ot->invoke= pose_armature_layers_invoke;
2004         ot->exec= pose_armature_layers_exec;
2005         ot->poll= ED_operator_editarmature;
2006         
2007         /* flags */
2008         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2009         
2010         /* properties */
2011         RNA_def_boolean_layer_member(ot->srna, "layers", 32, NULL, "Layer", "Armature layers to make visible");
2012 }
2013
2014 /* ------------------- */
2015
2016 /* Present a popup to get the layers that should be used */
2017 static int pose_bone_layers_invoke (bContext *C, wmOperator *op, wmEvent *evt)
2018 {
2019         int layers[32]= {0}; /* hardcoded for now - we can only have 32 armature layers, so this should be fine... */
2020         
2021         /* get layers that are active already */        
2022         CTX_DATA_BEGIN(C, bPoseChannel *, pchan, selected_pose_bones) 
2023         {
2024                 short bit;
2025                 
2026                 /* loop over the bits for this pchan's layers, adding layers where they're needed */
2027                 for (bit= 0; bit < 32; bit++) {
2028                         if (pchan->bone->layer & (1<<bit))
2029                                 layers[bit]= 1;
2030                 }
2031         }
2032         CTX_DATA_END;
2033         
2034         /* copy layers to operator */
2035         RNA_boolean_set_array(op->ptr, "layers", layers);
2036         
2037                 /* part to sync with other similar operators... */
2038         return WM_operator_props_popup(C, op, evt);
2039 }
2040
2041 /* Set the visible layers for the active armature (edit and pose modes) */
2042 static int pose_bone_layers_exec (bContext *C, wmOperator *op)
2043 {
2044         Object *ob= object_pose_armature_get(CTX_data_active_object(C));
2045         PointerRNA ptr;
2046         int layers[32]; /* hardcoded for now - we can only have 32 armature layers, so this should be fine... */
2047
2048         if(ob==NULL || ob->data==NULL) {
2049                 return OPERATOR_CANCELLED;
2050         }
2051
2052         /* get the values set in the operator properties */
2053         RNA_boolean_get_array(op->ptr, "layers", layers);
2054
2055         /* set layers of pchans based on the values set in the operator props */
2056         CTX_DATA_BEGIN(C, bPoseChannel *, pchan, selected_pose_bones)
2057         {
2058                 /* get pointer for pchan, and write flags this way */
2059                 RNA_pointer_create((ID *)ob->data, &RNA_Bone, pchan->bone, &ptr);
2060                 RNA_boolean_set_array(&ptr, "layers", layers);
2061         }
2062         CTX_DATA_END;
2063
2064         /* note, notifier might evolve */
2065         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
2066
2067         return OPERATOR_FINISHED;
2068 }
2069
2070 void POSE_OT_bone_layers (wmOperatorType *ot)
2071 {
2072         /* identifiers */
2073         ot->name= "Change Bone Layers";
2074         ot->idname= "POSE_OT_bone_layers";
2075         ot->description= "Change the layers that the selected bones belong to";
2076         
2077         /* callbacks */
2078         ot->invoke= pose_bone_layers_invoke;
2079         ot->exec= pose_bone_layers_exec;
2080         ot->poll= ED_operator_posemode;
2081         
2082         /* flags */
2083         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2084         
2085         /* properties */
2086         RNA_def_boolean_layer_member(ot->srna, "layers", 32, NULL, "Layer", "Armature layers that bone belongs to");
2087 }
2088
2089 /* ------------------- */
2090
2091 /* Present a popup to get the layers that should be used */
2092 static int armature_bone_layers_invoke (bContext *C, wmOperator *op, wmEvent *evt)
2093 {
2094         int layers[32]= {0}; /* hardcoded for now - we can only have 32 armature layers, so this should be fine... */
2095         
2096         /* get layers that are active already */
2097         CTX_DATA_BEGIN(C, EditBone *, ebone, selected_editable_bones) 
2098         {
2099                 short bit;
2100                 
2101                 /* loop over the bits for this pchan's layers, adding layers where they're needed */
2102                 for (bit= 0; bit < 32; bit++) {
2103                         if (ebone->layer & (1<<bit))
2104                                 layers[bit]= 1;
2105                 }
2106         }
2107         CTX_DATA_END;
2108         
2109         /* copy layers to operator */
2110         RNA_boolean_set_array(op->ptr, "layers", layers);
2111         
2112                 /* part to sync with other similar operators... */
2113         return WM_operator_props_popup(C, op, evt);
2114 }
2115
2116 /* Set the visible layers for the active armature (edit and pose modes) */
2117 static int armature_bone_layers_exec (bContext *C, wmOperator *op)
2118 {
2119         Object *ob= CTX_data_edit_object(C);
2120         bArmature *arm= (ob)? ob->data : NULL;
2121         PointerRNA ptr;
2122         int layers[32]; /* hardcoded for now - we can only have 32 armature layers, so this should be fine... */
2123         
2124         /* get the values set in the operator properties */
2125         RNA_boolean_get_array(op->ptr, "layers", layers);
2126         
2127         /* set layers of pchans based on the values set in the operator props */
2128         CTX_DATA_BEGIN(C, EditBone *, ebone, selected_editable_bones) 
2129         {
2130                 /* get pointer for pchan, and write flags this way */
2131                 RNA_pointer_create((ID *)arm, &RNA_EditBone, ebone, &ptr);
2132                 RNA_boolean_set_array(&ptr, "layers", layers);
2133         }
2134         CTX_DATA_END;
2135         
2136         /* note, notifier might evolve */
2137         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
2138         
2139         return OPERATOR_FINISHED;
2140 }
2141
2142 void ARMATURE_OT_bone_layers (wmOperatorType *ot)
2143 {
2144         /* identifiers */
2145         ot->name= "Change Bone Layers";
2146         ot->idname= "ARMATURE_OT_bone_layers";
2147         ot->description= "Change the layers that the selected bones belong to";
2148         
2149         /* callbacks */
2150         ot->invoke= armature_bone_layers_invoke;
2151         ot->exec= armature_bone_layers_exec;
2152         ot->poll= ED_operator_editarmature;
2153         
2154         /* flags */
2155         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2156         
2157         /* properties */
2158         RNA_def_boolean_layer_member(ot->srna, "layers", 32, NULL, "Layer", "Armature layers that bone belongs to");
2159 }
2160
2161 /* ********************************************** */
2162 /* Flip Quats */
2163
2164 static int pose_flip_quats_exec (bContext *C, wmOperator *UNUSED(op))
2165 {
2166         Scene *scene= CTX_data_scene(C);
2167         Object *ob= object_pose_armature_get(CTX_data_active_object(C));
2168         KeyingSet *ks = ANIM_builtin_keyingset_get_named(NULL, ANIM_KS_LOC_ROT_SCALE_ID);
2169         
2170         /* loop through all selected pchans, flipping and keying (as needed) */
2171         CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pose_bones)
2172         {
2173                 /* only if bone is using quaternion rotation */
2174                 if (pchan->rotmode == ROT_MODE_QUAT) {
2175                         /* quaternions have 720 degree range */
2176                         negate_v4(pchan->quat);
2177
2178                         ED_autokeyframe_pchan(C, scene, ob, pchan, ks);
2179                 }
2180         }
2181         CTX_DATA_END;
2182         
2183         /* notifiers and updates */
2184         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
2185         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, ob);
2186         
2187         return OPERATOR_FINISHED;
2188 }
2189
2190 void POSE_OT_quaternions_flip (wmOperatorType *ot)
2191 {
2192         /* identifiers */
2193         ot->name = "Flip Quats";
2194         ot->idname= "POSE_OT_quaternions_flip";
2195         ot->description= "Flip quaternion values to achieve desired rotations, while maintaining the same orientations";
2196         
2197         /* callbacks */
2198         ot->exec= pose_flip_quats_exec;
2199         ot->poll= ED_operator_posemode;
2200         
2201         /* flags */
2202         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2203 }
2204
2205 /* ********************************************** */
2206 /* Clear User Transforms */
2207
2208 static int pose_clear_user_transforms_exec (bContext *C, wmOperator *UNUSED(op))
2209 {
2210         Scene *scene = CTX_data_scene(C);
2211         Object *ob = CTX_data_active_object(C);
2212         float cframe = (float)CFRA;
2213         
2214         if ((ob->adt) && (ob->adt->action)) {
2215                 /* XXX: this is just like this to avoid contaminating anything else; 
2216                  * just pose values should change, so this should be fine 
2217                  */
2218                 bPose *dummyPose = NULL;
2219                 Object workob = {{0}}; 
2220                 bPoseChannel *pchan;
2221                 
2222                 /* execute animation step for current frame using a dummy copy of the pose */           
2223                 copy_pose(&dummyPose, ob->pose, 0);
2224                 
2225                 BLI_strncpy(workob.id.name, "OB<ClearTfmWorkOb>", sizeof(workob.id.name));
2226                 workob.type = OB_ARMATURE;
2227                 workob.data = ob->data;
2228                 workob.adt = ob->adt;
2229                 workob.pose = dummyPose;
2230                 
2231                 BKE_animsys_evaluate_animdata(scene, &workob.id, workob.adt, cframe, ADT_RECALC_ANIM);
2232                 
2233                 /* copy back values, but on selected bones only  */
2234                 for (pchan = dummyPose->chanbase.first; pchan; pchan = pchan->next) {
2235                         pose_bone_do_paste(ob, pchan, 1, 0);
2236                 }
2237                 
2238                 /* free temp data - free manually as was copied without constraints */
2239                 if (dummyPose) {
2240                         for (pchan= dummyPose->chanbase.first; pchan; pchan= pchan->next) {
2241                                 if (pchan->prop) {
2242                                         IDP_FreeProperty(pchan->prop);
2243                                         MEM_freeN(pchan->prop);
2244                                 }
2245                         }
2246                         
2247                         /* was copied without constraints */
2248                         BLI_freelistN(&dummyPose->chanbase);
2249                         MEM_freeN(dummyPose);
2250                 }
2251         }
2252         else {
2253                 /* no animation, so just reset whole pose to rest pose 
2254                  * (cannot just restore for selected though)
2255                  */
2256                 rest_pose(ob->pose);
2257         }
2258         
2259         /* notifiers and updates */
2260         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
2261         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, ob);
2262         
2263         return OPERATOR_FINISHED;
2264 }
2265
2266 void POSE_OT_user_transforms_clear (wmOperatorType *ot)
2267 {
2268         /* identifiers */
2269         ot->name = "Clear User Transforms";
2270         ot->idname= "POSE_OT_user_transforms_clear";
2271         ot->description= "Reset pose on selected bones to keyframed state";
2272         
2273         /* callbacks */
2274         ot->exec= pose_clear_user_transforms_exec;
2275         ot->poll= ED_operator_posemode;
2276         
2277         /* flags */
2278         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2279 }
2280