Fix syntax for ID keyword.
[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 #include "DNA_object_types.h"
35 #include "DNA_scene_types.h"
36 #include "DNA_screen_types.h"
37 #include "DNA_space_types.h"
38 #include "DNA_userdef_types.h"
39 #include "DNA_view3d_types.h"
40 #include "DNA_windowmanager_types.h"
41
42 #include "BLI_math.h"
43 #include "BLI_blenlib.h"
44
45 #include "BKE_context.h"
46 #include "BKE_global.h"
47 #include "BKE_utildefines.h"
48
49 #include "RNA_access.h"
50 #include "RNA_define.h"
51
52 #include "WM_api.h"
53 #include "WM_types.h"
54
55 #include "ED_armature.h"
56 #include "ED_screen.h"
57 #include "ED_object.h"
58 #include "ED_transform.h"
59
60 #include "armature_intern.h"
61
62 /* ************************** registration **********************************/
63
64 /* Both operators ARMATURE_OT_xxx and POSE_OT_xxx here */
65 void ED_operatortypes_armature(void)
66 {
67         /* EDIT ARMATURE */
68         WM_operatortype_append(ARMATURE_OT_bone_primitive_add);
69         
70         WM_operatortype_append(ARMATURE_OT_align);
71         WM_operatortype_append(ARMATURE_OT_calculate_roll);
72         WM_operatortype_append(ARMATURE_OT_switch_direction);
73         WM_operatortype_append(ARMATURE_OT_subdivs);
74         WM_operatortype_append(ARMATURE_OT_subdivide_simple);
75         WM_operatortype_append(ARMATURE_OT_subdivide_multi);
76         
77         WM_operatortype_append(ARMATURE_OT_parent_set);
78         WM_operatortype_append(ARMATURE_OT_parent_clear);
79         
80         WM_operatortype_append(ARMATURE_OT_select_all);
81         WM_operatortype_append(ARMATURE_OT_select_inverse);
82         WM_operatortype_append(ARMATURE_OT_select_hierarchy);
83         WM_operatortype_append(ARMATURE_OT_select_linked);
84
85         WM_operatortype_append(ARMATURE_OT_delete);
86         WM_operatortype_append(ARMATURE_OT_duplicate);
87         WM_operatortype_append(ARMATURE_OT_extrude);
88         WM_operatortype_append(ARMATURE_OT_click_extrude);
89         WM_operatortype_append(ARMATURE_OT_fill);
90         WM_operatortype_append(ARMATURE_OT_merge);
91         WM_operatortype_append(ARMATURE_OT_separate);
92         
93         WM_operatortype_append(ARMATURE_OT_autoside_names);
94         WM_operatortype_append(ARMATURE_OT_flip_names);
95         
96         WM_operatortype_append(ARMATURE_OT_flags_set);
97         
98         WM_operatortype_append(ARMATURE_OT_armature_layers);
99         WM_operatortype_append(ARMATURE_OT_bone_layers);
100
101         /* SKETCH */    
102         WM_operatortype_append(SKETCH_OT_gesture);
103         WM_operatortype_append(SKETCH_OT_delete);
104         WM_operatortype_append(SKETCH_OT_draw_stroke);
105         WM_operatortype_append(SKETCH_OT_draw_preview);
106         WM_operatortype_append(SKETCH_OT_finish_stroke);
107         WM_operatortype_append(SKETCH_OT_cancel_stroke);
108         WM_operatortype_append(SKETCH_OT_select);
109
110         /* POSE */
111         WM_operatortype_append(POSE_OT_hide);
112         WM_operatortype_append(POSE_OT_reveal);
113         
114         WM_operatortype_append(POSE_OT_armature_apply);
115         WM_operatortype_append(POSE_OT_visual_transform_apply);
116         
117         WM_operatortype_append(POSE_OT_rot_clear);
118         WM_operatortype_append(POSE_OT_loc_clear);
119         WM_operatortype_append(POSE_OT_scale_clear);
120         
121         WM_operatortype_append(POSE_OT_copy);
122         WM_operatortype_append(POSE_OT_paste);
123         
124         WM_operatortype_append(POSE_OT_select_all);
125         WM_operatortype_append(POSE_OT_select_inverse);
126
127         WM_operatortype_append(POSE_OT_select_parent);
128         WM_operatortype_append(POSE_OT_select_hierarchy);
129         WM_operatortype_append(POSE_OT_select_linked);
130         WM_operatortype_append(POSE_OT_select_constraint_target);
131         WM_operatortype_append(POSE_OT_select_grouped);
132         
133         WM_operatortype_append(POSE_OT_group_add);
134         WM_operatortype_append(POSE_OT_group_remove);
135         WM_operatortype_append(POSE_OT_group_assign);
136         WM_operatortype_append(POSE_OT_group_unassign);
137         
138         WM_operatortype_append(POSE_OT_paths_calculate);
139         WM_operatortype_append(POSE_OT_paths_clear);
140         
141         WM_operatortype_append(POSE_OT_autoside_names);
142         WM_operatortype_append(POSE_OT_flip_names);
143         
144         WM_operatortype_append(POSE_OT_quaternions_flip);
145         
146         WM_operatortype_append(POSE_OT_flags_set);
147         
148         WM_operatortype_append(POSE_OT_armature_layers);
149         WM_operatortype_append(POSE_OT_bone_layers);
150         
151         /* POSELIB */
152         WM_operatortype_append(POSELIB_OT_browse_interactive);
153         
154         WM_operatortype_append(POSELIB_OT_pose_add);
155         WM_operatortype_append(POSELIB_OT_pose_remove);
156         WM_operatortype_append(POSELIB_OT_pose_rename);
157         
158         /* POSE SLIDING */
159         WM_operatortype_append(POSE_OT_push);
160         WM_operatortype_append(POSE_OT_relax);
161         WM_operatortype_append(POSE_OT_breakdown);
162 }
163
164 void ED_operatormacros_armature(void)
165 {
166         wmOperatorType *ot;
167         wmOperatorTypeMacro *otmacro;
168         
169         ot= WM_operatortype_append_macro("ARMATURE_OT_duplicate_move", "Duplicate", OPTYPE_UNDO|OPTYPE_REGISTER);
170         WM_operatortype_macro_define(ot, "ARMATURE_OT_duplicate");
171         otmacro= WM_operatortype_macro_define(ot, "TRANSFORM_OT_translate");
172         RNA_enum_set(otmacro->ptr, "proportional", 0);
173
174         ot= WM_operatortype_append_macro("ARMATURE_OT_extrude_move", "Extrude", OPTYPE_UNDO|OPTYPE_REGISTER);
175         otmacro=WM_operatortype_macro_define(ot, "ARMATURE_OT_extrude");
176         RNA_enum_set(otmacro->ptr, "forked", 0);
177         otmacro= WM_operatortype_macro_define(ot, "TRANSFORM_OT_translate");
178         RNA_enum_set(otmacro->ptr, "proportional", 0);
179         
180         // XXX would it be nicer to just be able to have standard extrude_move, but set the forked property separate?
181         // that would require fixing a properties bug 19733
182         ot= WM_operatortype_append_macro("ARMATURE_OT_extrude_forked", "Extrude Forked", OPTYPE_UNDO|OPTYPE_REGISTER);
183         otmacro=WM_operatortype_macro_define(ot, "ARMATURE_OT_extrude");
184         RNA_enum_set(otmacro->ptr, "forked", 1);
185         otmacro= WM_operatortype_macro_define(ot, "TRANSFORM_OT_translate");
186         RNA_enum_set(otmacro->ptr, "proportional", 0);
187 }
188
189 void ED_keymap_armature(wmKeyConfig *keyconf)
190 {
191         wmKeyMap *keymap;
192         wmKeyMapItem *kmi;
193         
194         /* Armature ------------------------ */
195         keymap= WM_keymap_find(keyconf, "Armature", 0, 0);
196         keymap->poll= ED_operator_editarmature;
197         
198         /* Armature -> Etch-A-Ton ------------------------ */
199         WM_keymap_add_item(keymap, "SKETCH_OT_delete", XKEY, KM_PRESS, 0, 0);
200         WM_keymap_add_item(keymap, "SKETCH_OT_delete", DELKEY, KM_PRESS, 0, 0);
201         WM_keymap_add_item(keymap, "SKETCH_OT_finish_stroke", SELECTMOUSE, KM_PRESS, 0, 0);
202         WM_keymap_add_item(keymap, "SKETCH_OT_cancel_stroke", ESCKEY, KM_PRESS, 0, 0);
203         WM_keymap_add_item(keymap, "SKETCH_OT_select", SELECTMOUSE, KM_PRESS, 0, 0);
204
205         /* sketch poll checks mode */   
206         WM_keymap_add_item(keymap, "SKETCH_OT_gesture", ACTIONMOUSE, KM_PRESS, KM_SHIFT, 0);
207         WM_keymap_add_item(keymap, "SKETCH_OT_draw_stroke", ACTIONMOUSE, KM_PRESS, 0, 0);
208         kmi = WM_keymap_add_item(keymap, "SKETCH_OT_draw_stroke", ACTIONMOUSE, KM_PRESS, KM_CTRL, 0);
209         RNA_boolean_set(kmi->ptr, "snap", 1);
210         WM_keymap_add_item(keymap, "SKETCH_OT_draw_preview", MOUSEMOVE, KM_ANY, 0, 0);
211         kmi = WM_keymap_add_item(keymap, "SKETCH_OT_draw_preview", MOUSEMOVE, KM_ANY, KM_CTRL, 0);
212         RNA_boolean_set(kmi->ptr, "snap", 1);
213
214         /* only set in editmode armature, by space_view3d listener */
215 //      WM_keymap_add_item(keymap, "ARMATURE_OT_hide", HKEY, KM_PRESS, 0, 0);
216         WM_keymap_add_item(keymap, "ARMATURE_OT_align", AKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
217         WM_keymap_add_item(keymap, "ARMATURE_OT_calculate_roll", NKEY, KM_PRESS, KM_CTRL, 0);
218         
219         WM_keymap_add_item(keymap, "ARMATURE_OT_switch_direction", FKEY, KM_PRESS, KM_ALT, 0);
220         
221         WM_keymap_add_item(keymap, "ARMATURE_OT_bone_primitive_add", AKEY, KM_PRESS, KM_SHIFT, 0);
222         
223         WM_keymap_add_item(keymap, "ARMATURE_OT_parent_set", PKEY, KM_PRESS, KM_CTRL, 0);
224         WM_keymap_add_item(keymap, "ARMATURE_OT_parent_clear", PKEY, KM_PRESS, KM_ALT, 0);
225         
226         WM_keymap_add_item(keymap, "ARMATURE_OT_select_all", AKEY, KM_PRESS, 0, 0);
227         WM_keymap_add_item(keymap, "ARMATURE_OT_select_inverse", IKEY, KM_PRESS, KM_CTRL, 0);
228         
229         kmi= WM_keymap_add_item(keymap, "ARMATURE_OT_select_hierarchy", LEFTBRACKETKEY, KM_PRESS, 0, 0);
230                 RNA_enum_set(kmi->ptr, "direction", BONE_SELECT_PARENT);
231         kmi= WM_keymap_add_item(keymap, "ARMATURE_OT_select_hierarchy", LEFTBRACKETKEY, KM_PRESS, KM_SHIFT, 0);
232                 RNA_enum_set(kmi->ptr, "direction", BONE_SELECT_PARENT);
233                 RNA_boolean_set(kmi->ptr, "extend", 1);
234         
235         kmi= WM_keymap_add_item(keymap, "ARMATURE_OT_select_hierarchy", RIGHTBRACKETKEY, KM_PRESS, 0, 0);
236                 RNA_enum_set(kmi->ptr, "direction", BONE_SELECT_CHILD);
237         kmi= WM_keymap_add_item(keymap, "ARMATURE_OT_select_hierarchy", RIGHTBRACKETKEY, KM_PRESS, KM_SHIFT, 0);
238                 RNA_enum_set(kmi->ptr, "direction", BONE_SELECT_CHILD);
239                 RNA_boolean_set(kmi->ptr, "extend", 1);
240
241         WM_keymap_add_item(keymap, "ARMATURE_OT_select_linked", LKEY, KM_PRESS, 0, 0);
242         
243         WM_keymap_add_item(keymap, "ARMATURE_OT_delete", XKEY, KM_PRESS, 0, 0);
244         WM_keymap_add_item(keymap, "ARMATURE_OT_delete", DELKEY, KM_PRESS, 0, 0);
245         WM_keymap_add_item(keymap, "ARMATURE_OT_duplicate_move", DKEY, KM_PRESS, KM_SHIFT, 0);
246         WM_keymap_add_item(keymap, "ARMATURE_OT_extrude_move", EKEY, KM_PRESS, 0, 0);
247         WM_keymap_add_item(keymap, "ARMATURE_OT_extrude_forked", EKEY, KM_PRESS, KM_SHIFT, 0);
248         WM_keymap_add_item(keymap, "ARMATURE_OT_click_extrude", LEFTMOUSE, KM_CLICK, KM_CTRL, 0);
249         WM_keymap_add_item(keymap, "ARMATURE_OT_fill", FKEY, KM_PRESS, 0, 0);
250         WM_keymap_add_item(keymap, "ARMATURE_OT_merge", MKEY, KM_PRESS, KM_ALT, 0);
251         
252         WM_keymap_add_item(keymap, "ARMATURE_OT_separate", PKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
253         
254                 /* set flags */
255         kmi= WM_keymap_add_item(keymap, "ARMATURE_OT_flags_set", WKEY, KM_PRESS, KM_SHIFT, 0);
256                 RNA_enum_set(kmi->ptr, "mode", 2); // toggle
257         kmi= WM_keymap_add_item(keymap, "ARMATURE_OT_flags_set", WKEY, KM_PRESS, KM_CTRL|KM_SHIFT, 0);
258                 RNA_enum_set(kmi->ptr, "mode", 1); // enable
259         kmi= WM_keymap_add_item(keymap, "ARMATURE_OT_flags_set", WKEY, KM_PRESS, KM_ALT, 0);
260                 RNA_enum_set(kmi->ptr, "mode", 0); // clear
261                 
262                 /* armature/bone layers */
263         WM_keymap_add_item(keymap, "ARMATURE_OT_armature_layers", MKEY, KM_PRESS, KM_SHIFT, 0);
264         WM_keymap_add_item(keymap, "ARMATURE_OT_bone_layers", MKEY, KM_PRESS, 0, 0);
265         
266                 /* special transforms: */
267                 /*      1) envelope/b-bone size */
268         kmi= WM_keymap_add_item(keymap, "TRANSFORM_OT_transform", SKEY, KM_PRESS, KM_ALT, 0);
269                 RNA_enum_set(kmi->ptr, "mode", TFM_BONESIZE);
270                 /*      2) set roll */
271         kmi= WM_keymap_add_item(keymap, "TRANSFORM_OT_transform", RKEY, KM_PRESS, KM_CTRL, 0);
272                 RNA_enum_set(kmi->ptr, "mode", TFM_BONE_ROLL);
273                 
274                 /* menus */
275         WM_keymap_add_menu(keymap, "VIEW3D_MT_armature_specials", WKEY, KM_PRESS, 0, 0);
276
277         /* Pose ------------------------ */
278         /* only set in posemode, by space_view3d listener */
279         keymap= WM_keymap_find(keyconf, "Pose", 0, 0);
280         keymap->poll= ED_operator_posemode;
281         
282         // XXX: set parent is object-based operator, but it should also be available here...
283         WM_keymap_add_item(keymap, "OBJECT_OT_parent_set", PKEY, KM_PRESS, KM_CTRL, 0);
284         
285         WM_keymap_add_item(keymap, "POSE_OT_hide", HKEY, KM_PRESS, 0, 0);
286         kmi= WM_keymap_add_item(keymap, "POSE_OT_hide", HKEY, KM_PRESS, KM_SHIFT, 0);
287                 RNA_boolean_set(kmi->ptr, "unselected", 1);
288         WM_keymap_add_item(keymap, "POSE_OT_reveal", HKEY, KM_PRESS, KM_ALT, 0);
289
290         WM_keymap_add_menu(keymap, "VIEW3D_MT_pose_apply", AKEY, KM_PRESS, KM_CTRL, 0);
291
292         // TODO: clear pose
293         WM_keymap_add_item(keymap, "POSE_OT_rot_clear", RKEY, KM_PRESS, KM_ALT, 0);
294         WM_keymap_add_item(keymap, "POSE_OT_loc_clear", GKEY, KM_PRESS, KM_ALT, 0);
295         WM_keymap_add_item(keymap, "POSE_OT_scale_clear", SKEY, KM_PRESS, KM_ALT, 0);
296         
297         WM_keymap_add_item(keymap, "POSE_OT_quaternions_flip", FKEY, KM_PRESS, KM_ALT, 0);
298         
299         WM_keymap_add_item(keymap, "POSE_OT_copy", CKEY, KM_PRESS, KM_CTRL, 0);
300         WM_keymap_add_item(keymap, "POSE_OT_paste", VKEY, KM_PRESS, KM_CTRL, 0);
301         kmi= WM_keymap_add_item(keymap, "POSE_OT_paste", VKEY, KM_PRESS, KM_CTRL|KM_SHIFT, 0);
302                 RNA_boolean_set(kmi->ptr, "flipped", 1);
303         
304         WM_keymap_add_item(keymap, "POSE_OT_select_all", AKEY, KM_PRESS, 0, 0);
305         WM_keymap_add_item(keymap, "POSE_OT_select_inverse", IKEY, KM_PRESS, KM_CTRL, 0);
306
307         WM_keymap_add_item(keymap, "POSE_OT_select_parent", PKEY, KM_PRESS, KM_SHIFT, 0);
308
309         kmi= WM_keymap_add_item(keymap, "POSE_OT_select_hierarchy", LEFTBRACKETKEY, KM_PRESS, 0, 0);
310                 RNA_enum_set(kmi->ptr, "direction", BONE_SELECT_PARENT);
311         kmi= WM_keymap_add_item(keymap, "POSE_OT_select_hierarchy", LEFTBRACKETKEY, KM_PRESS, KM_SHIFT, 0);
312                 RNA_enum_set(kmi->ptr, "direction", BONE_SELECT_PARENT);
313                 RNA_boolean_set(kmi->ptr, "extend", 1);
314         
315         kmi= WM_keymap_add_item(keymap, "POSE_OT_select_hierarchy", RIGHTBRACKETKEY, KM_PRESS, 0, 0);
316                 RNA_enum_set(kmi->ptr, "direction", BONE_SELECT_CHILD);
317         kmi= WM_keymap_add_item(keymap, "POSE_OT_select_hierarchy", RIGHTBRACKETKEY, KM_PRESS, KM_SHIFT, 0);
318                 RNA_enum_set(kmi->ptr, "direction", BONE_SELECT_CHILD);
319                 RNA_boolean_set(kmi->ptr, "extend", 1);
320
321         WM_keymap_add_item(keymap, "POSE_OT_select_linked", LKEY, KM_PRESS, 0, 0);
322         WM_keymap_add_item(keymap, "POSE_OT_select_grouped", GKEY, KM_PRESS, KM_SHIFT, 0);
323         
324         WM_keymap_add_item(keymap, "POSE_OT_constraint_add_with_targets", CKEY, KM_PRESS, KM_CTRL|KM_SHIFT, 0);
325         WM_keymap_add_item(keymap, "POSE_OT_constraints_clear", CKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
326         WM_keymap_add_item(keymap, "POSE_OT_ik_add", IKEY, KM_PRESS, /*KM_CTRL|*/KM_SHIFT, 0);
327         WM_keymap_add_item(keymap, "POSE_OT_ik_clear", IKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
328         
329         WM_keymap_add_menu(keymap, "VIEW3D_MT_pose_group", GKEY, KM_PRESS, KM_CTRL, 0);
330         
331                 /* set flags */
332         kmi= WM_keymap_add_item(keymap, "POSE_OT_flags_set", WKEY, KM_PRESS, KM_SHIFT, 0);
333                 RNA_enum_set(kmi->ptr, "mode", 2); // toggle
334         kmi= WM_keymap_add_item(keymap, "POSE_OT_flags_set", WKEY, KM_PRESS, KM_CTRL|KM_SHIFT, 0);
335                 RNA_enum_set(kmi->ptr, "mode", 1); // enable
336         kmi= WM_keymap_add_item(keymap, "POSE_OT_flags_set", WKEY, KM_PRESS, KM_ALT, 0);
337                 RNA_enum_set(kmi->ptr, "mode", 0); // clear
338                 
339                 /* armature/bone layers */
340         WM_keymap_add_item(keymap, "POSE_OT_armature_layers", MKEY, KM_PRESS, KM_SHIFT, 0);
341         WM_keymap_add_item(keymap, "POSE_OT_bone_layers", MKEY, KM_PRESS, 0, 0);
342         
343                 /* special transforms: */
344                 /*      1) envelope/b-bone size */
345         kmi= WM_keymap_add_item(keymap, "TRANSFORM_OT_transform", SKEY, KM_PRESS, KM_ALT, 0);
346                 RNA_enum_set(kmi->ptr, "mode", TFM_BONESIZE);
347         
348         // XXX this should probably be in screen instead... here for testing purposes in the meantime... - Aligorith
349         WM_keymap_verify_item(keymap, "ANIM_OT_keyframe_insert_menu", IKEY, KM_PRESS, 0, 0);
350         WM_keymap_verify_item(keymap, "ANIM_OT_keyframe_delete_v3d", IKEY, KM_PRESS, KM_ALT, 0);
351         
352         /* Pose -> PoseLib ------------- */
353         /* only set in posemode, by space_view3d listener */
354         WM_keymap_add_item(keymap, "POSELIB_OT_browse_interactive", LKEY, KM_PRESS, KM_CTRL, 0);
355         
356         WM_keymap_add_item(keymap, "POSELIB_OT_pose_add", LKEY, KM_PRESS, KM_SHIFT, 0);
357         WM_keymap_add_item(keymap, "POSELIB_OT_pose_remove", LKEY, KM_PRESS, KM_ALT, 0);
358         WM_keymap_add_item(keymap, "POSELIB_OT_pose_rename", LKEY, KM_PRESS, KM_CTRL|KM_SHIFT, 0);
359         
360         /* Pose -> Pose Sliding ------------- */
361         /* only set in posemode, by space_view3d listener */
362         WM_keymap_add_item(keymap, "POSE_OT_push", EKEY, KM_PRESS, KM_CTRL, 0);
363         WM_keymap_add_item(keymap, "POSE_OT_relax", EKEY, KM_PRESS, KM_ALT, 0);
364         WM_keymap_add_item(keymap, "POSE_OT_breakdown", EKEY, KM_PRESS, KM_SHIFT, 0);
365 }
366