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