code cleanup: use const events for modal and invoke operators.
[blender.git] / source / blender / editors / armature / pose_edit.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  *                 Reevan McKay (original NaN code)
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  *
27  * Pose Mode API's and Operators for Pose Mode armatures
28  */
29
30 /** \file blender/editors/armature/pose_edit.c
31  *  \ingroup edarmature
32  */
33
34 #include "BLI_math.h"
35 #include "BLI_blenlib.h"
36
37 #include "DNA_anim_types.h"
38 #include "DNA_armature_types.h"
39 #include "DNA_constraint_types.h"
40 #include "DNA_scene_types.h"
41 #include "DNA_object_types.h"
42
43 #include "BKE_anim.h"
44 #include "BKE_action.h"
45 #include "BKE_armature.h"
46 #include "BKE_context.h"
47 #include "BKE_deform.h"
48 #include "BKE_depsgraph.h"
49 #include "BKE_object.h"
50 #include "BKE_report.h"
51
52 #include "RNA_access.h"
53 #include "RNA_define.h"
54 #include "RNA_enum_types.h"
55
56 #include "WM_api.h"
57 #include "WM_types.h"
58
59 #include "ED_armature.h"
60 #include "ED_keyframing.h"
61 #include "ED_screen.h"
62 #include "ED_object.h"
63
64 #include "UI_interface.h"
65
66 #include "armature_intern.h"
67
68 /* matches logic with ED_operator_posemode_context() */
69 Object *ED_pose_object_from_context(bContext *C)
70 {
71         ScrArea *sa = CTX_wm_area(C);
72         Object *ob;
73
74         /* since this call may also be used from the buttons window, we need to check for where to get the object */
75         if (sa && sa->spacetype == SPACE_BUTS) {
76                 ob = ED_object_context(C);
77         }
78         else {
79                 ob = BKE_object_pose_armature_get(CTX_data_active_object(C));
80         }
81
82         return ob;
83 }
84
85 /* This function is used to process the necessary updates for */
86 void ED_armature_enter_posemode(bContext *C, Base *base)
87 {
88         ReportList *reports = CTX_wm_reports(C);
89         Object *ob = base->object;
90         
91         if (ob->id.lib) {
92                 BKE_report(reports, RPT_WARNING, "Cannot pose libdata");
93                 return;
94         }
95         
96         switch (ob->type) {
97                 case OB_ARMATURE:
98                         ob->restore_mode = ob->mode;
99                         ob->mode |= OB_MODE_POSE;
100                         
101                         WM_event_add_notifier(C, NC_SCENE | ND_MODE | NS_MODE_POSE, NULL);
102                         
103                         break;
104                 default:
105                         return;
106         }
107         
108         /* XXX: disabled as this would otherwise cause a nasty loop... */
109         //ED_object_toggle_modes(C, ob->mode);
110 }
111
112 void ED_armature_exit_posemode(bContext *C, Base *base)
113 {
114         if (base) {
115                 Object *ob = base->object;
116                 
117                 ob->restore_mode = ob->mode;
118                 ob->mode &= ~OB_MODE_POSE;
119                 
120                 WM_event_add_notifier(C, NC_SCENE | ND_MODE | NS_MODE_OBJECT, NULL);
121         }
122 }
123
124 /* if a selected or active bone is protected, throw error (oonly if warn == 1) and return 1 */
125 /* only_selected == 1: the active bone is allowed to be protected */
126 #if 0 /* UNUSED 2.5 */
127 static short pose_has_protected_selected(Object *ob, short warn)
128 {
129         /* check protection */
130         if (ob->proxy) {
131                 bPoseChannel *pchan;
132                 bArmature *arm = ob->data;
133
134                 for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
135                         if (pchan->bone && (pchan->bone->layer & arm->layer)) {
136                                 if (pchan->bone->layer & arm->layer_protected) {
137                                         if (pchan->bone->flag & BONE_SELECTED)
138                                                 break;
139                                 }
140                         }
141                 }
142                 if (pchan) {
143                         if (warn) error("Cannot change Proxy protected bones");
144                         return 1;
145                 }
146         }
147         return 0;
148 }
149 #endif
150
151 /* only for real IK, not for auto-IK */
152 static int pose_channel_in_IK_chain(Object *ob, bPoseChannel *pchan, int level)
153 {
154         bConstraint *con;
155         Bone *bone;
156         
157         /* No need to check if constraint is active (has influence),
158          * since all constraints with CONSTRAINT_IK_AUTO are active */
159         for (con = pchan->constraints.first; con; con = con->next) {
160                 if (con->type == CONSTRAINT_TYPE_KINEMATIC) {
161                         bKinematicConstraint *data = con->data;
162                         if (data->rootbone == 0 || data->rootbone > level) {
163                                 if ((data->flag & CONSTRAINT_IK_AUTO) == 0)
164                                         return 1;
165                         }
166                 }
167         }
168         for (bone = pchan->bone->childbase.first; bone; bone = bone->next) {
169                 pchan = BKE_pose_channel_find_name(ob->pose, bone->name);
170                 if (pchan && pose_channel_in_IK_chain(ob, pchan, level + 1))
171                         return 1;
172         }
173         return 0;
174 }
175
176 int ED_pose_channel_in_IK_chain(Object *ob, bPoseChannel *pchan)
177 {
178         return pose_channel_in_IK_chain(ob, pchan, 0);
179 }
180
181 /* ********************************************** */
182 /* Motion Paths */
183
184 /* For the object with pose/action: update paths for those that have got them
185  * This should selectively update paths that exist...
186  *
187  * To be called from various tools that do incremental updates 
188  */
189 void ED_pose_recalculate_paths(Scene *scene, Object *ob)
190 {
191         ListBase targets = {NULL, NULL};
192         
193         /* set flag to force recalc, then grab the relevant bones to target */
194         ob->pose->avs.recalc |= ANIMVIZ_RECALC_PATHS;
195         animviz_get_object_motionpaths(ob, &targets);
196         
197         /* recalculate paths, then free */
198         animviz_calc_motionpaths(scene, &targets);
199         BLI_freelistN(&targets);
200 }
201
202
203 /* show popup to determine settings */
204 static int pose_calculate_paths_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
205 {       
206         Object *ob = BKE_object_pose_armature_get(CTX_data_active_object(C));
207         
208         if (ELEM(NULL, ob, ob->pose))
209                 return OPERATOR_CANCELLED;
210         
211         /* set default settings from existing/stored settings */
212         {
213                 bAnimVizSettings *avs = &ob->pose->avs;
214                 PointerRNA avs_ptr;
215                 
216                 RNA_int_set(op->ptr, "start_frame", avs->path_sf);
217                 RNA_int_set(op->ptr, "end_frame", avs->path_ef);
218                 
219                 RNA_pointer_create(NULL, &RNA_AnimVizMotionPaths, avs, &avs_ptr);
220                 RNA_enum_set(op->ptr, "bake_location", RNA_enum_get(&avs_ptr, "bake_location"));
221         }
222         
223         /* show popup dialog to allow editing of range... */
224         // FIXME: hardcoded dimensions here are just arbitrary
225         return WM_operator_props_dialog_popup(C, op, 10 * UI_UNIT_X, 10 * UI_UNIT_Y);
226 }
227
228 /* For the object with pose/action: create path curves for selected bones 
229  * This recalculates the WHOLE path within the pchan->pathsf and pchan->pathef range
230  */
231 static int pose_calculate_paths_exec(bContext *C, wmOperator *op)
232 {
233         Object *ob = BKE_object_pose_armature_get(CTX_data_active_object(C));
234         Scene *scene = CTX_data_scene(C);
235         
236         if (ELEM(NULL, ob, ob->pose))
237                 return OPERATOR_CANCELLED;
238         
239         /* grab baking settings from operator settings */
240         {
241                 bAnimVizSettings *avs = &ob->pose->avs;
242                 PointerRNA avs_ptr;
243                 
244                 avs->path_sf = RNA_int_get(op->ptr, "start_frame");
245                 avs->path_ef = RNA_int_get(op->ptr, "end_frame");
246                 
247                 RNA_pointer_create(NULL, &RNA_AnimVizMotionPaths, avs, &avs_ptr);
248                 RNA_enum_set(&avs_ptr, "bake_location", RNA_enum_get(op->ptr, "bake_location"));
249         }
250         
251         /* set up path data for bones being calculated */
252         CTX_DATA_BEGIN (C, bPoseChannel *, pchan, selected_pose_bones)
253         {
254                 /* verify makes sure that the selected bone has a bone with the appropriate settings */
255                 animviz_verify_motionpaths(op->reports, scene, ob, pchan);
256         }
257         CTX_DATA_END;
258
259         /* calculate the bones that now have motionpaths... */
260         /* TODO: only make for the selected bones? */
261         ED_pose_recalculate_paths(scene, ob);
262
263         /* notifiers for updates */
264         WM_event_add_notifier(C, NC_OBJECT | ND_POSE, ob);
265
266         return OPERATOR_FINISHED; 
267 }
268
269 void POSE_OT_paths_calculate(wmOperatorType *ot)
270 {
271         /* identifiers */
272         ot->name = "Calculate Bone Paths";
273         ot->idname = "POSE_OT_paths_calculate";
274         ot->description = "Calculate paths for the selected bones";
275         
276         /* api callbacks */
277         ot->invoke = pose_calculate_paths_invoke;
278         ot->exec = pose_calculate_paths_exec;
279         ot->poll = ED_operator_posemode_exclusive;
280         
281         /* flags */
282         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
283         
284         /* properties */
285         RNA_def_int(ot->srna, "start_frame", 1, MINAFRAME, MAXFRAME, "Start", 
286                     "First frame to calculate bone paths on", MINFRAME, MAXFRAME / 2.0);
287         RNA_def_int(ot->srna, "end_frame", 250, MINAFRAME, MAXFRAME, "End", 
288                     "Last frame to calculate bone paths on", MINFRAME, MAXFRAME / 2.0);
289         
290         RNA_def_enum(ot->srna, "bake_location", motionpath_bake_location_items, 0, 
291                      "Bake Location", 
292                      "Which point on the bones is used when calculating paths");
293 }
294
295 /* --------- */
296
297 static int pose_update_paths_exec(bContext *C, wmOperator *UNUSED(op))
298 {
299         Object *ob = BKE_object_pose_armature_get(CTX_data_active_object(C));
300         Scene *scene = CTX_data_scene(C);
301         
302         if (ELEM(NULL, ob, scene))
303                 return OPERATOR_CANCELLED;
304
305         /* calculate the bones that now have motionpaths... */
306         /* TODO: only make for the selected bones? */
307         ED_pose_recalculate_paths(scene, ob);
308         
309         /* notifiers for updates */
310         WM_event_add_notifier(C, NC_OBJECT | ND_POSE, ob);
311         
312         return OPERATOR_FINISHED;
313 }
314
315 void POSE_OT_paths_update(wmOperatorType *ot)
316 {
317         /* identifiers */
318         ot->name = "Update Bone Paths";
319         ot->idname = "POSE_OT_paths_update";
320         ot->description = "Recalculate paths for bones that already have them";
321         
322         /* api callbakcs */
323         ot->exec = pose_update_paths_exec;
324         ot->poll = ED_operator_posemode_exclusive; /* TODO: this should probably check for active bone and/or existing paths */
325         
326         /* flags */
327         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
328 }
329
330 /* --------- */
331
332 /* for the object with pose/action: clear path curves for selected bones only */
333 static void ED_pose_clear_paths(Object *ob)
334 {
335         bPoseChannel *pchan;
336         short skipped = 0;
337         
338         if (ELEM(NULL, ob, ob->pose))
339                 return;
340         
341         /* free the motionpath blocks, but also take note of whether we skipped some... */
342         for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
343                 if (pchan->mpath) {
344                         if ((pchan->bone) && (pchan->bone->flag & BONE_SELECTED)) {
345                                 animviz_free_motionpath(pchan->mpath);
346                                 pchan->mpath = NULL;
347                         }
348                         else 
349                                 skipped = 1;
350                 }
351         }
352         
353         /* if we didn't skip any, we shouldn't have any paths left */
354         if (skipped == 0)
355                 ob->pose->avs.path_bakeflag &= ~MOTIONPATH_BAKE_HAS_PATHS;
356 }
357
358 /* operator callback for this */
359 static int pose_clear_paths_exec(bContext *C, wmOperator *UNUSED(op))
360 {
361         Object *ob = BKE_object_pose_armature_get(CTX_data_active_object(C));
362                 
363         /* only continue if there's an object */
364         if (ELEM(NULL, ob, ob->pose))
365                 return OPERATOR_CANCELLED;
366         
367         /* use the backend function for this */
368         ED_pose_clear_paths(ob);
369         
370         /* notifiers for updates */
371         WM_event_add_notifier(C, NC_OBJECT | ND_POSE, ob);
372         
373         return OPERATOR_FINISHED; 
374 }
375
376 void POSE_OT_paths_clear(wmOperatorType *ot)
377 {
378         /* identifiers */
379         ot->name = "Clear Bone Paths";
380         ot->idname = "POSE_OT_paths_clear";
381         ot->description = "Clear path caches for selected bones";
382         
383         /* api callbacks */
384         ot->exec = pose_clear_paths_exec;
385         ot->poll = ED_operator_posemode_exclusive;
386         
387         /* flags */
388         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
389 }
390
391 /* ********************************************** */
392 #if 0 /* UNUSED 2.5 */
393 static void pose_copy_menu(Scene *scene)
394 {
395         Object *obedit = scene->obedit; // XXX context
396         Object *ob = OBACT;
397         bArmature *arm;
398         bPoseChannel *pchan, *pchanact;
399         short nr = 0;
400         int i = 0;
401         
402         /* paranoia checks */
403         if (ELEM(NULL, ob, ob->pose)) return;
404         if ((ob == obedit) || (ob->mode & OB_MODE_POSE) == 0) return;
405         
406         pchan = BKE_pose_channel_active(ob);
407         
408         if (pchan == NULL) return;
409         pchanact = pchan;
410         arm = ob->data;
411
412         /* if proxy-protected bones selected, some things (such as locks + displays) shouldn't be changeable,
413          * but for constraints (just add local constraints)
414          */
415         if (pose_has_protected_selected(ob, 0)) {
416                 i = BLI_countlist(&(pchanact->constraints)); /* if there are 24 or less, allow for the user to select constraints */
417                 if (i < 25)
418                         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");
419                 else
420                         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");
421         }
422         else {
423                 i = BLI_countlist(&(pchanact->constraints)); /* if there are 24 or less, allow for the user to select constraints */
424                 if (i < 25)
425                         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");
426                 else
427                         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");
428         }
429         
430         if (nr <= 0) 
431                 return;
432         
433         if (nr != 5) {
434                 for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
435                         if ((arm->layer & pchan->bone->layer) &&
436                             (pchan->bone->flag & BONE_SELECTED) &&
437                             (pchan != pchanact) )
438                         {
439                                 switch (nr) {
440                                         case 1: /* Local Location */
441                                                 copy_v3_v3(pchan->loc, pchanact->loc);
442                                                 break;
443                                         case 2: /* Local Rotation */
444                                                 copy_qt_qt(pchan->quat, pchanact->quat);
445                                                 copy_v3_v3(pchan->eul, pchanact->eul);
446                                                 break;
447                                         case 3: /* Local Size */
448                                                 copy_v3_v3(pchan->size, pchanact->size);
449                                                 break;
450                                         case 4: /* All Constraints */
451                                         {
452                                                 ListBase tmp_constraints = {NULL, NULL};
453                                                 
454                                                 /* copy constraints to tmpbase and apply 'local' tags before 
455                                                  * appending to list of constraints for this channel
456                                                  */
457                                                 BKE_copy_constraints(&tmp_constraints, &pchanact->constraints, TRUE);
458                                                 if ((ob->proxy) && (pchan->bone->layer & arm->layer_protected)) {
459                                                         bConstraint *con;
460                                                         
461                                                         /* add proxy-local tags */
462                                                         for (con = tmp_constraints.first; con; con = con->next)
463                                                                 con->flag |= CONSTRAINT_PROXY_LOCAL;
464                                                 }
465                                                 BLI_movelisttolist(&pchan->constraints, &tmp_constraints);
466                                                 
467                                                 /* update flags (need to add here, not just copy) */
468                                                 pchan->constflag |= pchanact->constflag;
469                                                 
470                                                 if (ob->pose)
471                                                         ob->pose->flag |= POSE_RECALC;
472                                         }
473                                         break;
474                                         case 6: /* Transform Locks */
475                                                 pchan->protectflag = pchanact->protectflag;
476                                                 break;
477                                         case 7: /* IK (DOF) settings */
478                                         {
479                                                 pchan->ikflag = pchanact->ikflag;
480                                                 copy_v3_v3(pchan->limitmin, pchanact->limitmin);
481                                                 copy_v3_v3(pchan->limitmax, pchanact->limitmax);
482                                                 copy_v3_v3(pchan->stiffness, pchanact->stiffness);
483                                                 pchan->ikstretch = pchanact->ikstretch;
484                                                 pchan->ikrotweight = pchanact->ikrotweight;
485                                                 pchan->iklinweight = pchanact->iklinweight;
486                                         }
487                                         break;
488                                         case 8: /* Custom Bone Shape */
489                                                 pchan->custom = pchanact->custom;
490                                                 break;
491                                         case 9: /* Visual Location */
492                                                 BKE_armature_loc_pose_to_bone(pchan, pchanact->pose_mat[3], pchan->loc);
493                                                 break;
494                                         case 10: /* Visual Rotation */
495                                         {
496                                                 float delta_mat[4][4];
497                                                 
498                                                 BKE_armature_mat_pose_to_bone(pchan, pchanact->pose_mat, delta_mat);
499                                                 
500                                                 if (pchan->rotmode == ROT_MODE_AXISANGLE) {
501                                                         float tmp_quat[4];
502                                                         
503                                                         /* need to convert to quat first (in temp var)... */
504                                                         mat4_to_quat(tmp_quat, delta_mat);
505                                                         quat_to_axis_angle(pchan->rotAxis, &pchan->rotAngle, tmp_quat);
506                                                 }
507                                                 else if (pchan->rotmode == ROT_MODE_QUAT)
508                                                         mat4_to_quat(pchan->quat, delta_mat);
509                                                 else
510                                                         mat4_to_eulO(pchan->eul, pchan->rotmode, delta_mat);
511                                         }
512                                         break;
513                                         case 11: /* Visual Size */
514                                         {
515                                                 float delta_mat[4][4], size[4];
516                                                 
517                                                 BKE_armature_mat_pose_to_bone(pchan, pchanact->pose_mat, delta_mat);
518                                                 mat4_to_size(size, delta_mat);
519                                                 copy_v3_v3(pchan->size, size);
520                                         }
521                                 }
522                         }
523                 }
524         }
525         else { /* constraints, optional (note: max we can have is 24 constraints) */
526                 bConstraint *con, *con_back;
527                 int const_toggle[24] = {0}; /* XXX, initialize as 0 to quiet errors */
528                 ListBase const_copy = {NULL, NULL};
529                 
530                 BLI_duplicatelist(&const_copy, &(pchanact->constraints));
531                 
532                 /* build the puplist of constraints */
533                 for (con = pchanact->constraints.first, i = 0; con; con = con->next, i++) {
534                         const_toggle[i] = 1;
535 //                      add_numbut(i, TOG|INT, con->name, 0, 0, &(const_toggle[i]), "");
536                 }
537                 
538 //              if (!do_clever_numbuts("Select Constraints", i, REDRAW)) {
539 //                      BLI_freelistN(&const_copy);
540 //                      return;
541 //              }
542                 
543                 /* now build a new listbase from the options selected */
544                 for (i = 0, con = const_copy.first; con; i++) {
545                         /* if not selected, free/remove it from the list */
546                         if (!const_toggle[i]) {
547                                 con_back = con->next;
548                                 BLI_freelinkN(&const_copy, con);
549                                 con = con_back;
550                         }
551                         else
552                                 con = con->next;
553                 }
554                 
555                 /* Copy the temo listbase to the selected posebones */
556                 for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
557                         if ((arm->layer & pchan->bone->layer) &&
558                             (pchan->bone->flag & BONE_SELECTED) &&
559                             (pchan != pchanact) )
560                         {
561                                 ListBase tmp_constraints = {NULL, NULL};
562                                 
563                                 /* copy constraints to tmpbase and apply 'local' tags before 
564                                  * appending to list of constraints for this channel
565                                  */
566                                 BKE_copy_constraints(&tmp_constraints, &const_copy, TRUE);
567                                 if ((ob->proxy) && (pchan->bone->layer & arm->layer_protected)) {
568                                         /* add proxy-local tags */
569                                         for (con = tmp_constraints.first; con; con = con->next)
570                                                 con->flag |= CONSTRAINT_PROXY_LOCAL;
571                                 }
572                                 BLI_movelisttolist(&pchan->constraints, &tmp_constraints);
573                                 
574                                 /* update flags (need to add here, not just copy) */
575                                 pchan->constflag |= pchanact->constflag;
576                         }
577                 }
578                 BLI_freelistN(&const_copy);
579                 BKE_pose_update_constraint_flags(ob->pose); /* we could work out the flags but its simpler to do this */
580                 
581                 if (ob->pose)
582                         ob->pose->flag |= POSE_RECALC;
583         }
584         
585         DAG_id_tag_update(&ob->id, OB_RECALC_DATA); // and all its relations
586         
587         BIF_undo_push("Copy Pose Attributes");
588         
589 }
590 #endif
591
592 /* ********************************************** */
593
594 static int pose_flip_names_exec(bContext *C, wmOperator *UNUSED(op))
595 {
596         Object *ob = BKE_object_pose_armature_get(CTX_data_active_object(C));
597         bArmature *arm;
598         
599         /* paranoia checks */
600         if (ELEM(NULL, ob, ob->pose)) 
601                 return OPERATOR_CANCELLED;
602         arm = ob->data;
603         
604         /* loop through selected bones, auto-naming them */
605         CTX_DATA_BEGIN (C, bPoseChannel *, pchan, selected_pose_bones)
606         {
607                 char newname[MAXBONENAME];
608                 flip_side_name(newname, pchan->name, TRUE);
609                 ED_armature_bone_rename(arm, pchan->name, newname);
610         }
611         CTX_DATA_END;
612         
613         /* since we renamed stuff... */
614         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
615
616         /* note, notifier might evolve */
617         WM_event_add_notifier(C, NC_OBJECT | ND_POSE, ob);
618         
619         return OPERATOR_FINISHED;
620 }
621
622 void POSE_OT_flip_names(wmOperatorType *ot)
623 {
624         /* identifiers */
625         ot->name = "Flip Names";
626         ot->idname = "POSE_OT_flip_names";
627         ot->description = "Flips (and corrects) the axis suffixes of the the names of selected bones";
628         
629         /* api callbacks */
630         ot->exec = pose_flip_names_exec;
631         ot->poll = ED_operator_posemode;
632         
633         /* flags */
634         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
635 }
636
637 /* ------------------ */
638
639 static int pose_autoside_names_exec(bContext *C, wmOperator *op)
640 {
641         Object *ob = BKE_object_pose_armature_get(CTX_data_active_object(C));
642         bArmature *arm;
643         char newname[MAXBONENAME];
644         short axis = RNA_enum_get(op->ptr, "axis");
645         
646         /* paranoia checks */
647         if (ELEM(NULL, ob, ob->pose)) 
648                 return OPERATOR_CANCELLED;
649         arm = ob->data;
650         
651         /* loop through selected bones, auto-naming them */
652         CTX_DATA_BEGIN (C, bPoseChannel *, pchan, selected_pose_bones)
653         {
654                 BLI_strncpy(newname, pchan->name, sizeof(newname));
655                 if (bone_autoside_name(newname, 1, axis, pchan->bone->head[axis], pchan->bone->tail[axis]))
656                         ED_armature_bone_rename(arm, pchan->name, newname);
657         }
658         CTX_DATA_END;
659         
660         /* since we renamed stuff... */
661         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
662
663         /* note, notifier might evolve */
664         WM_event_add_notifier(C, NC_OBJECT | ND_POSE, ob);
665         
666         return OPERATOR_FINISHED;
667 }
668
669 void POSE_OT_autoside_names(wmOperatorType *ot)
670 {
671         static EnumPropertyItem axis_items[] = {
672                 {0, "XAXIS", 0, "X-Axis", "Left/Right"},
673                 {1, "YAXIS", 0, "Y-Axis", "Front/Back"},
674                 {2, "ZAXIS", 0, "Z-Axis", "Top/Bottom"},
675                 {0, NULL, 0, NULL, NULL}
676         };
677         
678         /* identifiers */
679         ot->name = "AutoName by Axis";
680         ot->idname = "POSE_OT_autoside_names";
681         ot->description = "Automatically renames the selected bones according to which side of the target axis they fall on";
682         
683         /* api callbacks */
684         ot->invoke = WM_menu_invoke;
685         ot->exec = pose_autoside_names_exec;
686         ot->poll = ED_operator_posemode;
687         
688         /* flags */
689         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
690         
691         /* settings */
692         ot->prop = RNA_def_enum(ot->srna, "axis", axis_items, 0, "Axis", "Axis tag names with");
693 }
694
695 /* ********************************************** */
696
697 static int pose_bone_rotmode_exec(bContext *C, wmOperator *op)
698 {
699         Object *ob = CTX_data_active_object(C);
700         int mode = RNA_enum_get(op->ptr, "type");
701         
702         /* set rotation mode of selected bones  */
703         CTX_DATA_BEGIN (C, bPoseChannel *, pchan, selected_pose_bones)
704         {
705                 pchan->rotmode = mode;
706         }
707         CTX_DATA_END;
708         
709         /* notifiers and updates */
710         DAG_id_tag_update((ID *)ob, OB_RECALC_DATA);
711         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, ob);
712         
713         return OPERATOR_FINISHED;
714 }
715
716 void POSE_OT_rotation_mode_set(wmOperatorType *ot)
717 {
718         /* identifiers */
719         ot->name = "Set Rotation Mode";
720         ot->idname = "POSE_OT_rotation_mode_set";
721         ot->description = "Set the rotation representation used by selected bones";
722         
723         /* callbacks */
724         ot->invoke = WM_menu_invoke;
725         ot->exec = pose_bone_rotmode_exec;
726         ot->poll = ED_operator_posemode;
727         
728         /* flags */
729         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
730         
731         /* properties */
732         ot->prop = RNA_def_enum(ot->srna, "type", posebone_rotmode_items, 0, "Rotation Mode", "");
733 }
734
735 /* ********************************************** */
736
737 /* Show all armature layers */
738 static int pose_armature_layers_showall_poll(bContext *C)
739 {
740         /* this single operator can be used in posemode OR editmode for armatures */
741         return ED_operator_posemode(C) || ED_operator_editarmature(C);
742 }
743
744 static int pose_armature_layers_showall_exec(bContext *C, wmOperator *op)
745 {
746         Object *ob = BKE_object_pose_armature_get(CTX_data_active_object(C));
747         bArmature *arm = (ob) ? ob->data : NULL;
748         PointerRNA ptr;
749         int maxLayers = (RNA_boolean_get(op->ptr, "all")) ? 32 : 16;
750         int layers[32] = {0}; /* hardcoded for now - we can only have 32 armature layers, so this should be fine... */
751         int i;
752         
753         /* sanity checking */
754         if (arm == NULL)
755                 return OPERATOR_CANCELLED;
756         
757         /* use RNA to set the layers
758          *  although it would be faster to just set directly using bitflags, we still
759          *      need to setup a RNA pointer so that we get the "update" callbacks for free...
760          */
761         RNA_id_pointer_create(&arm->id, &ptr);
762         
763         for (i = 0; i < maxLayers; i++)
764                 layers[i] = 1;
765         
766         RNA_boolean_set_array(&ptr, "layers", layers);
767         
768         /* note, notifier might evolve */
769         WM_event_add_notifier(C, NC_OBJECT | ND_POSE, ob);
770         
771         /* done */
772         return OPERATOR_FINISHED;
773 }
774
775 void ARMATURE_OT_layers_show_all(wmOperatorType *ot)
776 {
777         /* identifiers */
778         ot->name = "Show All Layers";
779         ot->idname = "ARMATURE_OT_layers_show_all";
780         ot->description = "Make all armature layers visible";
781         
782         /* callbacks */
783         ot->exec = pose_armature_layers_showall_exec;
784         ot->poll = pose_armature_layers_showall_poll;
785         
786         /* flags */
787         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
788         
789         /* properties */
790         ot->prop = RNA_def_boolean(ot->srna, "all", 1, "All Layers", "Enable all layers or just the first 16 (top row)");
791 }
792
793 /* ------------------- */
794
795 /* Present a popup to get the layers that should be used */
796 static int pose_armature_layers_invoke(bContext *C, wmOperator *op, const wmEvent *event)
797 {
798         Object *ob = BKE_object_pose_armature_get(CTX_data_active_object(C));
799         bArmature *arm = (ob) ? ob->data : NULL;
800         PointerRNA ptr;
801         int layers[32]; /* hardcoded for now - we can only have 32 armature layers, so this should be fine... */
802         
803         /* sanity checking */
804         if (arm == NULL)
805                 return OPERATOR_CANCELLED;
806                 
807         /* get RNA pointer to armature data to use that to retrieve the layers as ints to init the operator */
808         RNA_id_pointer_create((ID *)arm, &ptr);
809         RNA_boolean_get_array(&ptr, "layers", layers);
810         RNA_boolean_set_array(op->ptr, "layers", layers);
811         
812         /* part to sync with other similar operators... */
813         return WM_operator_props_popup(C, op, event);
814 }
815
816 /* Set the visible layers for the active armature (edit and pose modes) */
817 static int pose_armature_layers_exec(bContext *C, wmOperator *op)
818 {
819         Object *ob = BKE_object_pose_armature_get(CTX_data_active_object(C));
820         PointerRNA ptr;
821         int layers[32]; /* hardcoded for now - we can only have 32 armature layers, so this should be fine... */
822
823         if (ELEM(NULL, ob, ob->data)) {
824                 return OPERATOR_CANCELLED;
825         }
826
827         /* get the values set in the operator properties */
828         RNA_boolean_get_array(op->ptr, "layers", layers);
829
830         /* get pointer for armature, and write data there... */
831         RNA_id_pointer_create((ID *)ob->data, &ptr);
832         RNA_boolean_set_array(&ptr, "layers", layers);
833
834         /* note, notifier might evolve */
835         WM_event_add_notifier(C, NC_OBJECT | ND_POSE, ob);
836
837         return OPERATOR_FINISHED;
838 }
839
840
841 void POSE_OT_armature_layers(wmOperatorType *ot)
842 {
843         /* identifiers */
844         ot->name = "Change Armature Layers";
845         ot->idname = "POSE_OT_armature_layers";
846         ot->description = "Change the visible armature layers";
847         
848         /* callbacks */
849         ot->invoke = pose_armature_layers_invoke;
850         ot->exec = pose_armature_layers_exec;
851         ot->poll = ED_operator_posemode;
852         
853         /* flags */
854         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
855         
856         /* properties */
857         RNA_def_boolean_layer_member(ot->srna, "layers", 32, NULL, "Layer", "Armature layers to make visible");
858 }
859
860 void ARMATURE_OT_armature_layers(wmOperatorType *ot)
861 {
862         /* identifiers */
863         ot->name = "Change Armature Layers";
864         ot->idname = "ARMATURE_OT_armature_layers";
865         ot->description = "Change the visible armature layers";
866         
867         /* callbacks */
868         ot->invoke = pose_armature_layers_invoke;
869         ot->exec = pose_armature_layers_exec;
870         ot->poll = ED_operator_editarmature;
871         
872         /* flags */
873         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
874         
875         /* properties */
876         RNA_def_boolean_layer_member(ot->srna, "layers", 32, NULL, "Layer", "Armature layers to make visible");
877 }
878
879 /* ------------------- */
880
881 /* Present a popup to get the layers that should be used */
882 static int pose_bone_layers_invoke(bContext *C, wmOperator *op, const wmEvent *event)
883 {
884         int layers[32] = {0}; /* hardcoded for now - we can only have 32 armature layers, so this should be fine... */
885         
886         /* get layers that are active already */
887         CTX_DATA_BEGIN (C, bPoseChannel *, pchan, selected_pose_bones)
888         {
889                 short bit;
890                 
891                 /* loop over the bits for this pchan's layers, adding layers where they're needed */
892                 for (bit = 0; bit < 32; bit++) {
893                         if (pchan->bone->layer & (1 << bit))
894                                 layers[bit] = 1;
895                 }
896         }
897         CTX_DATA_END;
898         
899         /* copy layers to operator */
900         RNA_boolean_set_array(op->ptr, "layers", layers);
901         
902         /* part to sync with other similar operators... */
903         return WM_operator_props_popup(C, op, event);
904 }
905
906 /* Set the visible layers for the active armature (edit and pose modes) */
907 static int pose_bone_layers_exec(bContext *C, wmOperator *op)
908 {
909         Object *ob = BKE_object_pose_armature_get(CTX_data_active_object(C));
910         PointerRNA ptr;
911         int layers[32]; /* hardcoded for now - we can only have 32 armature layers, so this should be fine... */
912
913         if (ob == NULL || ob->data == NULL) {
914                 return OPERATOR_CANCELLED;
915         }
916
917         /* get the values set in the operator properties */
918         RNA_boolean_get_array(op->ptr, "layers", layers);
919
920         /* set layers of pchans based on the values set in the operator props */
921         CTX_DATA_BEGIN (C, bPoseChannel *, pchan, selected_pose_bones)
922         {
923                 /* get pointer for pchan, and write flags this way */
924                 RNA_pointer_create((ID *)ob->data, &RNA_Bone, pchan->bone, &ptr);
925                 RNA_boolean_set_array(&ptr, "layers", layers);
926         }
927         CTX_DATA_END;
928
929         /* note, notifier might evolve */
930         WM_event_add_notifier(C, NC_OBJECT | ND_POSE, ob);
931
932         return OPERATOR_FINISHED;
933 }
934
935 void POSE_OT_bone_layers(wmOperatorType *ot)
936 {
937         /* identifiers */
938         ot->name = "Change Bone Layers";
939         ot->idname = "POSE_OT_bone_layers";
940         ot->description = "Change the layers that the selected bones belong to";
941         
942         /* callbacks */
943         ot->invoke = pose_bone_layers_invoke;
944         ot->exec = pose_bone_layers_exec;
945         ot->poll = ED_operator_posemode_exclusive;
946         
947         /* flags */
948         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
949         
950         /* properties */
951         RNA_def_boolean_layer_member(ot->srna, "layers", 32, NULL, "Layer", "Armature layers that bone belongs to");
952 }
953
954 /* ------------------- */
955
956 /* Present a popup to get the layers that should be used */
957 static int armature_bone_layers_invoke(bContext *C, wmOperator *op, const wmEvent *event)
958 {
959         int layers[32] = {0}; /* hardcoded for now - we can only have 32 armature layers, so this should be fine... */
960         
961         /* get layers that are active already */
962         CTX_DATA_BEGIN (C, EditBone *, ebone, selected_editable_bones)
963         {
964                 short bit;
965                 
966                 /* loop over the bits for this pchan's layers, adding layers where they're needed */
967                 for (bit = 0; bit < 32; bit++) {
968                         if (ebone->layer & (1 << bit))
969                                 layers[bit] = 1;
970                 }
971         }
972         CTX_DATA_END;
973         
974         /* copy layers to operator */
975         RNA_boolean_set_array(op->ptr, "layers", layers);
976         
977         /* part to sync with other similar operators... */
978         return WM_operator_props_popup(C, op, event);
979 }
980
981 /* Set the visible layers for the active armature (edit and pose modes) */
982 static int armature_bone_layers_exec(bContext *C, wmOperator *op)
983 {
984         Object *ob = CTX_data_edit_object(C);
985         bArmature *arm = (ob) ? ob->data : NULL;
986         PointerRNA ptr;
987         int layers[32]; /* hardcoded for now - we can only have 32 armature layers, so this should be fine... */
988         
989         /* get the values set in the operator properties */
990         RNA_boolean_get_array(op->ptr, "layers", layers);
991         
992         /* set layers of pchans based on the values set in the operator props */
993         CTX_DATA_BEGIN (C, EditBone *, ebone, selected_editable_bones)
994         {
995                 /* get pointer for pchan, and write flags this way */
996                 RNA_pointer_create((ID *)arm, &RNA_EditBone, ebone, &ptr);
997                 RNA_boolean_set_array(&ptr, "layers", layers);
998         }
999         CTX_DATA_END;
1000         
1001         /* note, notifier might evolve */
1002         WM_event_add_notifier(C, NC_OBJECT | ND_POSE, ob);
1003         
1004         return OPERATOR_FINISHED;
1005 }
1006
1007 void ARMATURE_OT_bone_layers(wmOperatorType *ot)
1008 {
1009         /* identifiers */
1010         ot->name = "Change Bone Layers";
1011         ot->idname = "ARMATURE_OT_bone_layers";
1012         ot->description = "Change the layers that the selected bones belong to";
1013         
1014         /* callbacks */
1015         ot->invoke = armature_bone_layers_invoke;
1016         ot->exec = armature_bone_layers_exec;
1017         ot->poll = ED_operator_editarmature;
1018         
1019         /* flags */
1020         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1021         
1022         /* properties */
1023         RNA_def_boolean_layer_member(ot->srna, "layers", 32, NULL, "Layer", "Armature layers that bone belongs to");
1024 }
1025
1026 /* ********************************************** */
1027 /* Show/Hide Bones */
1028
1029 static int hide_selected_pose_bone_cb(Object *ob, Bone *bone, void *UNUSED(ptr)) 
1030 {
1031         bArmature *arm = ob->data;
1032         
1033         if (arm->layer & bone->layer) {
1034                 if (bone->flag & BONE_SELECTED) {
1035                         bone->flag |= BONE_HIDDEN_P;
1036                         bone->flag &= ~BONE_SELECTED;
1037                         if (arm->act_bone == bone)
1038                                 arm->act_bone = NULL;
1039                 }
1040         }
1041         return 0;
1042 }
1043
1044 static int hide_unselected_pose_bone_cb(Object *ob, Bone *bone, void *UNUSED(ptr)) 
1045 {
1046         bArmature *arm = ob->data;
1047         
1048         if (arm->layer & bone->layer) {
1049                 /* hrm... typo here? */
1050                 if ((bone->flag & BONE_SELECTED) == 0) {
1051                         bone->flag |= BONE_HIDDEN_P;
1052                         if (arm->act_bone == bone)
1053                                 arm->act_bone = NULL;
1054                 }
1055         }
1056         return 0;
1057 }
1058
1059 /* active object is armature in posemode, poll checked */
1060 static int pose_hide_exec(bContext *C, wmOperator *op) 
1061 {
1062         Object *ob = BKE_object_pose_armature_get(CTX_data_active_object(C));
1063         bArmature *arm = ob->data;
1064
1065         if (RNA_boolean_get(op->ptr, "unselected"))
1066                 bone_looper(ob, arm->bonebase.first, NULL, hide_unselected_pose_bone_cb);
1067         else
1068                 bone_looper(ob, arm->bonebase.first, NULL, hide_selected_pose_bone_cb);
1069         
1070         /* note, notifier might evolve */
1071         WM_event_add_notifier(C, NC_OBJECT | ND_BONE_SELECT, ob);
1072         
1073         return OPERATOR_FINISHED;
1074 }
1075
1076 void POSE_OT_hide(wmOperatorType *ot)
1077 {
1078         /* identifiers */
1079         ot->name = "Hide Selected";
1080         ot->idname = "POSE_OT_hide";
1081         ot->description = "Tag selected bones to not be visible in Pose Mode";
1082         
1083         /* api callbacks */
1084         ot->exec = pose_hide_exec;
1085         ot->poll = ED_operator_posemode;
1086         
1087         /* flags */
1088         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1089         
1090         /* props */
1091         RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "");
1092 }
1093
1094 static int show_pose_bone_cb(Object *ob, Bone *bone, void *UNUSED(ptr)) 
1095 {
1096         bArmature *arm = ob->data;
1097         
1098         if (arm->layer & bone->layer) {
1099                 if (bone->flag & BONE_HIDDEN_P) {
1100                         bone->flag &= ~BONE_HIDDEN_P;
1101                         bone->flag |= BONE_SELECTED;
1102                 }
1103         }
1104         
1105         return 0;
1106 }
1107
1108 /* active object is armature in posemode, poll checked */
1109 static int pose_reveal_exec(bContext *C, wmOperator *UNUSED(op)) 
1110 {
1111         Object *ob = BKE_object_pose_armature_get(CTX_data_active_object(C));
1112         bArmature *arm = ob->data;
1113         
1114         bone_looper(ob, arm->bonebase.first, NULL, show_pose_bone_cb);
1115         
1116         /* note, notifier might evolve */
1117         WM_event_add_notifier(C, NC_OBJECT | ND_BONE_SELECT, ob);
1118
1119         return OPERATOR_FINISHED;
1120 }
1121
1122 void POSE_OT_reveal(wmOperatorType *ot)
1123 {
1124         /* identifiers */
1125         ot->name = "Reveal Selected";
1126         ot->idname = "POSE_OT_reveal";
1127         ot->description = "Unhide all bones that have been tagged to be hidden in Pose Mode";
1128         
1129         /* api callbacks */
1130         ot->exec = pose_reveal_exec;
1131         ot->poll = ED_operator_posemode;
1132         
1133         /* flags */
1134         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1135 }
1136
1137 /* ********************************************** */
1138 /* Flip Quats */
1139
1140 static int pose_flip_quats_exec(bContext *C, wmOperator *UNUSED(op))
1141 {
1142         Scene *scene = CTX_data_scene(C);
1143         Object *ob = BKE_object_pose_armature_get(CTX_data_active_object(C));
1144         KeyingSet *ks = ANIM_builtin_keyingset_get_named(NULL, ANIM_KS_LOC_ROT_SCALE_ID);
1145         
1146         /* loop through all selected pchans, flipping and keying (as needed) */
1147         CTX_DATA_BEGIN (C, bPoseChannel *, pchan, selected_pose_bones)
1148         {
1149                 /* only if bone is using quaternion rotation */
1150                 if (pchan->rotmode == ROT_MODE_QUAT) {
1151                         /* quaternions have 720 degree range */
1152                         negate_v4(pchan->quat);
1153
1154                         ED_autokeyframe_pchan(C, scene, ob, pchan, ks);
1155                 }
1156         }
1157         CTX_DATA_END;
1158         
1159         /* notifiers and updates */
1160         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1161         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, ob);
1162         
1163         return OPERATOR_FINISHED;
1164 }
1165
1166 void POSE_OT_quaternions_flip(wmOperatorType *ot)
1167 {
1168         /* identifiers */
1169         ot->name = "Flip Quats";
1170         ot->idname = "POSE_OT_quaternions_flip";
1171         ot->description = "Flip quaternion values to achieve desired rotations, while maintaining the same orientations";
1172         
1173         /* callbacks */
1174         ot->exec = pose_flip_quats_exec;
1175         ot->poll = ED_operator_posemode;
1176         
1177         /* flags */
1178         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1179 }
1180