63cef2781049509f65c1c20c065c0843317079ec
[blender-staging.git] / source / blender / editors / armature / armature_ops.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2009 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <stdlib.h>
30 #include <math.h>
31
32 #include "MEM_guardedalloc.h"
33
34
35 #include "BLI_math.h"
36 #include "BLI_blenlib.h"
37
38 #include "BKE_context.h"
39 #include "BKE_global.h"
40 #include "BKE_utildefines.h"
41
42 #include "RNA_access.h"
43
44 #include "WM_api.h"
45 #include "WM_types.h"
46
47 #include "ED_armature.h"
48 #include "ED_screen.h"
49 #include "ED_transform.h"
50
51 #include "armature_intern.h"
52
53 /* ************************** registration **********************************/
54
55 /* Both operators ARMATURE_OT_xxx and POSE_OT_xxx here */
56 void ED_operatortypes_armature(void)
57 {
58         /* EDIT ARMATURE */
59         WM_operatortype_append(ARMATURE_OT_bone_primitive_add);
60         
61         WM_operatortype_append(ARMATURE_OT_align);
62         WM_operatortype_append(ARMATURE_OT_calculate_roll);
63         WM_operatortype_append(ARMATURE_OT_switch_direction);
64         WM_operatortype_append(ARMATURE_OT_subdivs);
65         WM_operatortype_append(ARMATURE_OT_subdivide_simple);
66         WM_operatortype_append(ARMATURE_OT_subdivide_multi);
67         
68         WM_operatortype_append(ARMATURE_OT_parent_set);
69         WM_operatortype_append(ARMATURE_OT_parent_clear);
70         
71         WM_operatortype_append(ARMATURE_OT_select_all);
72         WM_operatortype_append(ARMATURE_OT_select_inverse);
73         WM_operatortype_append(ARMATURE_OT_select_hierarchy);
74         WM_operatortype_append(ARMATURE_OT_select_linked);
75
76         WM_operatortype_append(ARMATURE_OT_delete);
77         WM_operatortype_append(ARMATURE_OT_duplicate);
78         WM_operatortype_append(ARMATURE_OT_extrude);
79         WM_operatortype_append(ARMATURE_OT_click_extrude);
80         WM_operatortype_append(ARMATURE_OT_fill);
81         WM_operatortype_append(ARMATURE_OT_merge);
82         WM_operatortype_append(ARMATURE_OT_separate);
83         
84         WM_operatortype_append(ARMATURE_OT_autoside_names);
85         WM_operatortype_append(ARMATURE_OT_flip_names);
86         
87         WM_operatortype_append(ARMATURE_OT_flags_set);
88         
89         WM_operatortype_append(ARMATURE_OT_armature_layers);
90         WM_operatortype_append(ARMATURE_OT_bone_layers);
91
92         /* SKETCH */    
93         WM_operatortype_append(SKETCH_OT_gesture);
94         WM_operatortype_append(SKETCH_OT_delete);
95         WM_operatortype_append(SKETCH_OT_draw_stroke);
96         WM_operatortype_append(SKETCH_OT_draw_preview);
97         WM_operatortype_append(SKETCH_OT_finish_stroke);
98         WM_operatortype_append(SKETCH_OT_cancel_stroke);
99         WM_operatortype_append(SKETCH_OT_convert);
100         WM_operatortype_append(SKETCH_OT_select);
101
102         /* POSE */
103         WM_operatortype_append(POSE_OT_hide);
104         WM_operatortype_append(POSE_OT_reveal);
105         
106         WM_operatortype_append(POSE_OT_armature_apply);
107         WM_operatortype_append(POSE_OT_visual_transform_apply);
108         
109         WM_operatortype_append(POSE_OT_rot_clear);
110         WM_operatortype_append(POSE_OT_loc_clear);
111         WM_operatortype_append(POSE_OT_scale_clear);
112         
113         WM_operatortype_append(POSE_OT_copy);
114         WM_operatortype_append(POSE_OT_paste);
115         
116         WM_operatortype_append(POSE_OT_select_all);
117         WM_operatortype_append(POSE_OT_select_inverse);
118
119         WM_operatortype_append(POSE_OT_select_parent);
120         WM_operatortype_append(POSE_OT_select_hierarchy);
121         WM_operatortype_append(POSE_OT_select_linked);
122         WM_operatortype_append(POSE_OT_select_constraint_target);
123         WM_operatortype_append(POSE_OT_select_grouped);
124         
125         WM_operatortype_append(POSE_OT_group_add);
126         WM_operatortype_append(POSE_OT_group_remove);
127         WM_operatortype_append(POSE_OT_group_assign);
128         WM_operatortype_append(POSE_OT_group_unassign);
129         WM_operatortype_append(POSE_OT_group_select);
130         WM_operatortype_append(POSE_OT_group_deselect);
131         
132         WM_operatortype_append(POSE_OT_paths_calculate);
133         WM_operatortype_append(POSE_OT_paths_clear);
134         
135         WM_operatortype_append(POSE_OT_autoside_names);
136         WM_operatortype_append(POSE_OT_flip_names);
137         
138         WM_operatortype_append(POSE_OT_quaternions_flip);
139         
140         WM_operatortype_append(POSE_OT_flags_set);
141         
142         WM_operatortype_append(POSE_OT_armature_layers);
143         WM_operatortype_append(POSE_OT_bone_layers);
144         
145         /* POSELIB */
146         WM_operatortype_append(POSELIB_OT_browse_interactive);
147         
148         WM_operatortype_append(POSELIB_OT_pose_add);
149         WM_operatortype_append(POSELIB_OT_pose_remove);
150         WM_operatortype_append(POSELIB_OT_pose_rename);
151         
152         /* POSE SLIDING */
153         WM_operatortype_append(POSE_OT_push);
154         WM_operatortype_append(POSE_OT_relax);
155         WM_operatortype_append(POSE_OT_breakdown);
156 }
157
158 void ED_operatormacros_armature(void)
159 {
160         wmOperatorType *ot;
161         wmOperatorTypeMacro *otmacro;
162         
163         ot= WM_operatortype_append_macro("ARMATURE_OT_duplicate_move", "Duplicate", OPTYPE_UNDO|OPTYPE_REGISTER);
164         WM_operatortype_macro_define(ot, "ARMATURE_OT_duplicate");
165         otmacro= WM_operatortype_macro_define(ot, "TRANSFORM_OT_translate");
166         RNA_enum_set(otmacro->ptr, "proportional", 0);
167
168         ot= WM_operatortype_append_macro("ARMATURE_OT_extrude_move", "Extrude", OPTYPE_UNDO|OPTYPE_REGISTER);
169         otmacro=WM_operatortype_macro_define(ot, "ARMATURE_OT_extrude");
170         RNA_enum_set(otmacro->ptr, "forked", 0);
171         otmacro= WM_operatortype_macro_define(ot, "TRANSFORM_OT_translate");
172         RNA_enum_set(otmacro->ptr, "proportional", 0);
173         
174         // XXX would it be nicer to just be able to have standard extrude_move, but set the forked property separate?
175         // that would require fixing a properties bug 19733
176         ot= WM_operatortype_append_macro("ARMATURE_OT_extrude_forked", "Extrude Forked", OPTYPE_UNDO|OPTYPE_REGISTER);
177         otmacro=WM_operatortype_macro_define(ot, "ARMATURE_OT_extrude");
178         RNA_enum_set(otmacro->ptr, "forked", 1);
179         otmacro= WM_operatortype_macro_define(ot, "TRANSFORM_OT_translate");
180         RNA_enum_set(otmacro->ptr, "proportional", 0);
181 }
182
183 void ED_keymap_armature(wmKeyConfig *keyconf)
184 {
185         wmKeyMap *keymap;
186         wmKeyMapItem *kmi;
187         
188         /* Armature ------------------------ */
189         keymap= WM_keymap_find(keyconf, "Armature", 0, 0);
190         keymap->poll= ED_operator_editarmature;
191         
192         /* Armature -> Etch-A-Ton ------------------------ */
193         WM_keymap_add_item(keymap, "SKETCH_OT_delete", XKEY, KM_PRESS, 0, 0);
194         WM_keymap_add_item(keymap, "SKETCH_OT_delete", DELKEY, KM_PRESS, 0, 0);
195         WM_keymap_add_item(keymap, "SKETCH_OT_finish_stroke", RIGHTMOUSE, KM_PRESS, 0, 0);
196         WM_keymap_add_item(keymap, "SKETCH_OT_cancel_stroke", ESCKEY, KM_PRESS, 0, 0);
197         // Already part of view3d select
198         //WM_keymap_add_item(keymap, "SKETCH_OT_select", SELECTMOUSE, KM_PRESS, 0, 0);
199
200         /* sketch poll checks mode */   
201         WM_keymap_add_item(keymap, "SKETCH_OT_gesture", LEFTMOUSE, KM_PRESS, KM_SHIFT, 0);
202         WM_keymap_add_item(keymap, "SKETCH_OT_draw_stroke", LEFTMOUSE, KM_PRESS, 0, 0);
203         kmi = WM_keymap_add_item(keymap, "SKETCH_OT_draw_stroke", LEFTMOUSE, KM_PRESS, KM_CTRL, 0);
204         RNA_boolean_set(kmi->ptr, "snap", 1);
205         WM_keymap_add_item(keymap, "SKETCH_OT_draw_preview", MOUSEMOVE, KM_ANY, 0, 0);
206         kmi = WM_keymap_add_item(keymap, "SKETCH_OT_draw_preview", MOUSEMOVE, KM_ANY, KM_CTRL, 0);
207         RNA_boolean_set(kmi->ptr, "snap", 1);
208
209         /* only set in editmode armature, by space_view3d listener */
210 //      WM_keymap_add_item(keymap, "ARMATURE_OT_hide", HKEY, KM_PRESS, 0, 0);
211         WM_keymap_add_item(keymap, "ARMATURE_OT_align", AKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
212         WM_keymap_add_item(keymap, "ARMATURE_OT_calculate_roll", NKEY, KM_PRESS, KM_CTRL, 0);
213         
214         WM_keymap_add_item(keymap, "ARMATURE_OT_switch_direction", FKEY, KM_PRESS, KM_ALT, 0);
215         
216         WM_keymap_add_item(keymap, "ARMATURE_OT_bone_primitive_add", AKEY, KM_PRESS, KM_SHIFT, 0);
217         
218         WM_keymap_add_item(keymap, "ARMATURE_OT_parent_set", PKEY, KM_PRESS, KM_CTRL, 0);
219         WM_keymap_add_item(keymap, "ARMATURE_OT_parent_clear", PKEY, KM_PRESS, KM_ALT, 0);
220         
221         WM_keymap_add_item(keymap, "ARMATURE_OT_select_all", AKEY, KM_PRESS, 0, 0);
222         WM_keymap_add_item(keymap, "ARMATURE_OT_select_inverse", IKEY, KM_PRESS, KM_CTRL, 0);
223         
224         kmi= WM_keymap_add_item(keymap, "ARMATURE_OT_select_hierarchy", LEFTBRACKETKEY, KM_PRESS, 0, 0);
225                 RNA_enum_set(kmi->ptr, "direction", BONE_SELECT_PARENT);
226         kmi= WM_keymap_add_item(keymap, "ARMATURE_OT_select_hierarchy", LEFTBRACKETKEY, KM_PRESS, KM_SHIFT, 0);
227                 RNA_enum_set(kmi->ptr, "direction", BONE_SELECT_PARENT);
228                 RNA_boolean_set(kmi->ptr, "extend", 1);
229         
230         kmi= WM_keymap_add_item(keymap, "ARMATURE_OT_select_hierarchy", RIGHTBRACKETKEY, KM_PRESS, 0, 0);
231                 RNA_enum_set(kmi->ptr, "direction", BONE_SELECT_CHILD);
232         kmi= WM_keymap_add_item(keymap, "ARMATURE_OT_select_hierarchy", RIGHTBRACKETKEY, KM_PRESS, KM_SHIFT, 0);
233                 RNA_enum_set(kmi->ptr, "direction", BONE_SELECT_CHILD);
234                 RNA_boolean_set(kmi->ptr, "extend", 1);
235
236         WM_keymap_add_item(keymap, "ARMATURE_OT_select_linked", LKEY, KM_PRESS, 0, 0);
237         
238         WM_keymap_add_item(keymap, "ARMATURE_OT_delete", XKEY, KM_PRESS, 0, 0);
239         WM_keymap_add_item(keymap, "ARMATURE_OT_delete", DELKEY, KM_PRESS, 0, 0);
240         WM_keymap_add_item(keymap, "ARMATURE_OT_duplicate_move", DKEY, KM_PRESS, KM_SHIFT, 0);
241         WM_keymap_add_item(keymap, "ARMATURE_OT_extrude_move", EKEY, KM_PRESS, 0, 0);
242         WM_keymap_add_item(keymap, "ARMATURE_OT_extrude_forked", EKEY, KM_PRESS, KM_SHIFT, 0);
243         WM_keymap_add_item(keymap, "ARMATURE_OT_click_extrude", LEFTMOUSE, KM_CLICK, KM_CTRL, 0);
244         WM_keymap_add_item(keymap, "ARMATURE_OT_fill", FKEY, KM_PRESS, 0, 0);
245         WM_keymap_add_item(keymap, "ARMATURE_OT_merge", MKEY, KM_PRESS, KM_ALT, 0);
246         
247         WM_keymap_add_item(keymap, "ARMATURE_OT_separate", PKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
248         
249                 /* set flags */
250         kmi= WM_keymap_add_item(keymap, "ARMATURE_OT_flags_set", WKEY, KM_PRESS, KM_SHIFT, 0);
251                 RNA_enum_set(kmi->ptr, "mode", 2); // toggle
252         kmi= WM_keymap_add_item(keymap, "ARMATURE_OT_flags_set", WKEY, KM_PRESS, KM_CTRL|KM_SHIFT, 0);
253                 RNA_enum_set(kmi->ptr, "mode", 1); // enable
254         kmi= WM_keymap_add_item(keymap, "ARMATURE_OT_flags_set", WKEY, KM_PRESS, KM_ALT, 0);
255                 RNA_enum_set(kmi->ptr, "mode", 0); // clear
256                 
257                 /* armature/bone layers */
258         WM_keymap_add_item(keymap, "ARMATURE_OT_armature_layers", MKEY, KM_PRESS, KM_SHIFT, 0);
259         WM_keymap_add_item(keymap, "ARMATURE_OT_bone_layers", MKEY, KM_PRESS, 0, 0);
260         
261                 /* special transforms: */
262                 /*      1) envelope/b-bone size */
263         kmi= WM_keymap_add_item(keymap, "TRANSFORM_OT_transform", SKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
264                 RNA_enum_set(kmi->ptr, "mode", TFM_BONESIZE);
265                 /*      2) set roll */
266         kmi= WM_keymap_add_item(keymap, "TRANSFORM_OT_transform", RKEY, KM_PRESS, KM_CTRL, 0);
267                 RNA_enum_set(kmi->ptr, "mode", TFM_BONE_ROLL);
268                 
269                 /* menus */
270         WM_keymap_add_menu(keymap, "VIEW3D_MT_armature_specials", WKEY, KM_PRESS, 0, 0);
271
272         /* Pose ------------------------ */
273         /* only set in posemode, by space_view3d listener */
274         keymap= WM_keymap_find(keyconf, "Pose", 0, 0);
275         keymap->poll= ED_operator_posemode;
276         
277         // XXX: set parent is object-based operator, but it should also be available here...
278         WM_keymap_add_item(keymap, "OBJECT_OT_parent_set", PKEY, KM_PRESS, KM_CTRL, 0);
279         
280         WM_keymap_add_item(keymap, "POSE_OT_hide", HKEY, KM_PRESS, 0, 0);
281         kmi= WM_keymap_add_item(keymap, "POSE_OT_hide", HKEY, KM_PRESS, KM_SHIFT, 0);
282                 RNA_boolean_set(kmi->ptr, "unselected", 1);
283         WM_keymap_add_item(keymap, "POSE_OT_reveal", HKEY, KM_PRESS, KM_ALT, 0);
284         
285         WM_keymap_add_menu(keymap, "VIEW3D_MT_pose_apply", AKEY, KM_PRESS, KM_CTRL, 0);
286         
287         // TODO: clear pose
288         WM_keymap_add_item(keymap, "POSE_OT_rot_clear", RKEY, KM_PRESS, KM_ALT, 0);
289         WM_keymap_add_item(keymap, "POSE_OT_loc_clear", GKEY, KM_PRESS, KM_ALT, 0);
290         WM_keymap_add_item(keymap, "POSE_OT_scale_clear", SKEY, KM_PRESS, KM_ALT, 0);
291         
292         WM_keymap_add_item(keymap, "POSE_OT_quaternions_flip", FKEY, KM_PRESS, KM_ALT, 0);
293         
294         WM_keymap_add_item(keymap, "POSE_OT_copy", CKEY, KM_PRESS, KM_CTRL, 0);
295         WM_keymap_add_item(keymap, "POSE_OT_paste", VKEY, KM_PRESS, KM_CTRL, 0);
296         kmi= WM_keymap_add_item(keymap, "POSE_OT_paste", VKEY, KM_PRESS, KM_CTRL|KM_SHIFT, 0);
297                 RNA_boolean_set(kmi->ptr, "flipped", 1);
298         
299         WM_keymap_add_item(keymap, "POSE_OT_select_all", AKEY, KM_PRESS, 0, 0);
300         WM_keymap_add_item(keymap, "POSE_OT_select_inverse", IKEY, KM_PRESS, KM_CTRL, 0);
301
302         WM_keymap_add_item(keymap, "POSE_OT_select_parent", PKEY, KM_PRESS, KM_SHIFT, 0);
303
304         kmi= WM_keymap_add_item(keymap, "POSE_OT_select_hierarchy", LEFTBRACKETKEY, KM_PRESS, 0, 0);
305                 RNA_enum_set(kmi->ptr, "direction", BONE_SELECT_PARENT);
306         kmi= WM_keymap_add_item(keymap, "POSE_OT_select_hierarchy", LEFTBRACKETKEY, KM_PRESS, KM_SHIFT, 0);
307                 RNA_enum_set(kmi->ptr, "direction", BONE_SELECT_PARENT);
308                 RNA_boolean_set(kmi->ptr, "extend", 1);
309         
310         kmi= WM_keymap_add_item(keymap, "POSE_OT_select_hierarchy", RIGHTBRACKETKEY, KM_PRESS, 0, 0);
311                 RNA_enum_set(kmi->ptr, "direction", BONE_SELECT_CHILD);
312         kmi= WM_keymap_add_item(keymap, "POSE_OT_select_hierarchy", RIGHTBRACKETKEY, KM_PRESS, KM_SHIFT, 0);
313                 RNA_enum_set(kmi->ptr, "direction", BONE_SELECT_CHILD);
314                 RNA_boolean_set(kmi->ptr, "extend", 1);
315
316         WM_keymap_add_item(keymap, "POSE_OT_select_linked", LKEY, KM_PRESS, 0, 0);
317         WM_keymap_add_item(keymap, "POSE_OT_select_grouped", GKEY, KM_PRESS, KM_SHIFT, 0);
318         
319         WM_keymap_add_item(keymap, "POSE_OT_constraint_add_with_targets", CKEY, KM_PRESS, KM_CTRL|KM_SHIFT, 0);
320         WM_keymap_add_item(keymap, "POSE_OT_constraints_clear", CKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
321         WM_keymap_add_item(keymap, "POSE_OT_ik_add", IKEY, KM_PRESS, /*KM_CTRL|*/KM_SHIFT, 0);
322         WM_keymap_add_item(keymap, "POSE_OT_ik_clear", IKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
323         
324         WM_keymap_add_menu(keymap, "VIEW3D_MT_pose_group", GKEY, KM_PRESS, KM_CTRL, 0);
325         
326                 /* set flags */
327         kmi= WM_keymap_add_item(keymap, "POSE_OT_flags_set", WKEY, KM_PRESS, KM_SHIFT, 0);
328                 RNA_enum_set(kmi->ptr, "mode", 2); // toggle
329         kmi= WM_keymap_add_item(keymap, "POSE_OT_flags_set", WKEY, KM_PRESS, KM_CTRL|KM_SHIFT, 0);
330                 RNA_enum_set(kmi->ptr, "mode", 1); // enable
331         kmi= WM_keymap_add_item(keymap, "POSE_OT_flags_set", WKEY, KM_PRESS, KM_ALT, 0);
332                 RNA_enum_set(kmi->ptr, "mode", 0); // clear
333                 
334                 /* armature/bone layers */
335         WM_keymap_add_item(keymap, "POSE_OT_armature_layers", MKEY, KM_PRESS, KM_SHIFT, 0);
336         WM_keymap_add_item(keymap, "POSE_OT_bone_layers", MKEY, KM_PRESS, 0, 0);
337         
338                 /* special transforms: */
339                 /*      1) envelope/b-bone size */
340         kmi= WM_keymap_add_item(keymap, "TRANSFORM_OT_transform", SKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
341                 RNA_enum_set(kmi->ptr, "mode", TFM_BONESIZE);
342         
343                 /* keyframes management */
344         WM_keymap_verify_item(keymap, "ANIM_OT_keyframe_insert_menu", IKEY, KM_PRESS, 0, 0);
345         WM_keymap_verify_item(keymap, "ANIM_OT_keyframe_delete_v3d", IKEY, KM_PRESS, KM_ALT, 0);
346         WM_keymap_verify_item(keymap, "ANIM_OT_keying_set_active_set", IKEY, KM_PRESS, KM_CTRL|KM_SHIFT|KM_ALT, 0);
347         
348         /* Pose -> PoseLib ------------- */
349         /* only set in posemode, by space_view3d listener */
350         WM_keymap_add_item(keymap, "POSELIB_OT_browse_interactive", LKEY, KM_PRESS, KM_CTRL, 0);
351         
352         WM_keymap_add_item(keymap, "POSELIB_OT_pose_add", LKEY, KM_PRESS, KM_SHIFT, 0);
353         WM_keymap_add_item(keymap, "POSELIB_OT_pose_remove", LKEY, KM_PRESS, KM_ALT, 0);
354         WM_keymap_add_item(keymap, "POSELIB_OT_pose_rename", LKEY, KM_PRESS, KM_CTRL|KM_SHIFT, 0);
355         
356         /* Pose -> Pose Sliding ------------- */
357         /* only set in posemode, by space_view3d listener */
358         WM_keymap_add_item(keymap, "POSE_OT_push", EKEY, KM_PRESS, KM_CTRL, 0);
359         WM_keymap_add_item(keymap, "POSE_OT_relax", EKEY, KM_PRESS, KM_ALT, 0);
360         WM_keymap_add_item(keymap, "POSE_OT_breakdown", EKEY, KM_PRESS, KM_SHIFT, 0);
361 }
362