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