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