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