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