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