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