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