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