2.5 - More armature/bone operator tweaks
[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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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_arithb.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 /* ************************** quick tests **********************************/
63
64 /*  XXX This is a quick test operator to print names of all EditBones in context
65  *              that should be removed once tool coding starts...
66  */
67
68 static int armature_test_exec (bContext *C, wmOperator *op)
69 {
70         printf("EditMode Armature Test: \n");
71         
72         printf("\tSelected Bones \n");
73         CTX_DATA_BEGIN(C, EditBone*, ebone, selected_bones)
74         {
75                 printf("\t\tEditBone '%s' \n", ebone->name);
76         }
77         CTX_DATA_END;
78         
79         printf("\tEditable Bones \n");
80         CTX_DATA_BEGIN(C, EditBone*, ebone, selected_editable_bones) 
81         {
82                 printf("\t\tEditBone '%s' \n", ebone->name);
83         }
84         CTX_DATA_END;
85         
86         printf("\tActive Bone \n");
87         {
88                 EditBone *ebone= CTX_data_active_bone(C);
89                 if (ebone) printf("\t\tEditBone '%s' \n", ebone->name);
90                 else printf("\t\t<None> \n");
91         }
92         
93         return OPERATOR_FINISHED;
94 }
95
96 void ARMATURE_OT_test(wmOperatorType *ot)
97 {
98         /* identifiers */
99         ot->name= "Test Context";
100         ot->idname= "ARMATURE_OT_test";
101         
102         /* api callbacks */
103         ot->exec= armature_test_exec;
104 }
105
106 /* ************************** registration **********************************/
107
108 /* Both operators ARMATURE_OT_xxx and POSE_OT_xxx here */
109 void ED_operatortypes_armature(void)
110 {
111         /* EDIT ARMATURE */
112         WM_operatortype_append(ARMATURE_OT_bone_primitive_add);
113         
114         WM_operatortype_append(ARMATURE_OT_align);
115         WM_operatortype_append(ARMATURE_OT_calculate_roll);
116         WM_operatortype_append(ARMATURE_OT_switch_direction);
117         WM_operatortype_append(ARMATURE_OT_subdivs);
118         WM_operatortype_append(ARMATURE_OT_subdivide_simple);
119         WM_operatortype_append(ARMATURE_OT_subdivide_multi);
120         
121         WM_operatortype_append(ARMATURE_OT_parent_set);
122         WM_operatortype_append(ARMATURE_OT_parent_clear);
123         
124         WM_operatortype_append(ARMATURE_OT_select_all_toggle);
125         WM_operatortype_append(ARMATURE_OT_select_inverse);
126         WM_operatortype_append(ARMATURE_OT_select_hierarchy);
127         WM_operatortype_append(ARMATURE_OT_select_linked);
128
129         WM_operatortype_append(ARMATURE_OT_delete);
130         WM_operatortype_append(ARMATURE_OT_duplicate);
131         WM_operatortype_append(ARMATURE_OT_extrude);
132         WM_operatortype_append(ARMATURE_OT_click_extrude);
133         WM_operatortype_append(ARMATURE_OT_fill);
134         WM_operatortype_append(ARMATURE_OT_merge);
135         WM_operatortype_append(ARMATURE_OT_separate);
136         
137         WM_operatortype_append(ARMATURE_OT_autoside_names);
138         WM_operatortype_append(ARMATURE_OT_flip_names);
139         
140         WM_operatortype_append(ARMATURE_OT_flags_set);
141         
142         WM_operatortype_append(ARMATURE_OT_armature_layers);
143         WM_operatortype_append(ARMATURE_OT_bone_layers);
144
145         /* SKETCH */    
146         WM_operatortype_append(SKETCH_OT_gesture);
147         WM_operatortype_append(SKETCH_OT_delete);
148         WM_operatortype_append(SKETCH_OT_draw_stroke);
149         WM_operatortype_append(SKETCH_OT_draw_preview);
150         WM_operatortype_append(SKETCH_OT_finish_stroke);
151         WM_operatortype_append(SKETCH_OT_cancel_stroke);
152         WM_operatortype_append(SKETCH_OT_select);
153
154         /* POSE */
155         WM_operatortype_append(POSE_OT_hide);
156         WM_operatortype_append(POSE_OT_reveal);
157         
158         WM_operatortype_append(POSE_OT_apply);
159         
160         WM_operatortype_append(POSE_OT_rot_clear);
161         WM_operatortype_append(POSE_OT_loc_clear);
162         WM_operatortype_append(POSE_OT_scale_clear);
163         
164         WM_operatortype_append(POSE_OT_copy);
165         WM_operatortype_append(POSE_OT_paste);
166         
167         WM_operatortype_append(POSE_OT_select_all_toggle);
168         WM_operatortype_append(POSE_OT_select_inverse);
169
170         WM_operatortype_append(POSE_OT_select_parent);
171         WM_operatortype_append(POSE_OT_select_hierarchy);
172         WM_operatortype_append(POSE_OT_select_linked);
173         WM_operatortype_append(POSE_OT_select_constraint_target);
174         
175         WM_operatortype_append(POSE_OT_groups_menu);
176         WM_operatortype_append(POSE_OT_group_add);
177         WM_operatortype_append(POSE_OT_group_remove);
178         WM_operatortype_append(POSE_OT_group_assign);
179         WM_operatortype_append(POSE_OT_group_unassign);
180         
181         WM_operatortype_append(POSE_OT_paths_calculate);
182         WM_operatortype_append(POSE_OT_paths_clear);
183         
184         WM_operatortype_append(POSE_OT_autoside_names);
185         WM_operatortype_append(POSE_OT_flip_names);
186         
187         WM_operatortype_append(POSE_OT_flags_set);
188         
189         WM_operatortype_append(POSE_OT_armature_layers);
190         WM_operatortype_append(POSE_OT_bone_layers);
191         
192         /* POSELIB */
193         WM_operatortype_append(POSELIB_OT_browse_interactive);
194         
195         WM_operatortype_append(POSELIB_OT_pose_add);
196         WM_operatortype_append(POSELIB_OT_pose_remove);
197         WM_operatortype_append(POSELIB_OT_pose_rename);
198         
199         /* TESTS */
200         WM_operatortype_append(ARMATURE_OT_test); // XXX temp test for context iterators... to be removed
201 }
202
203 void ED_keymap_armature(wmWindowManager *wm)
204 {
205         ListBase *keymap;
206         wmKeymapItem *kmi;
207         
208         /* Armature ------------------------ */
209         keymap= WM_keymap_listbase(wm, "Armature", 0, 0);
210         
211         /* only set in editmode armature, by space_view3d listener */
212 //      WM_keymap_add_item(keymap, "ARMATURE_OT_hide", HKEY, KM_PRESS, 0, 0);
213         WM_keymap_add_item(keymap, "ARMATURE_OT_align", AKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
214         WM_keymap_add_item(keymap, "ARMATURE_OT_calculate_roll", NKEY, KM_PRESS, KM_CTRL, 0);
215         
216         WM_keymap_add_item(keymap, "ARMATURE_OT_switch_direction", FKEY, KM_PRESS, KM_ALT, 0);
217         
218         WM_keymap_add_item(keymap, "ARMATURE_OT_bone_primitive_add", AKEY, KM_PRESS, KM_SHIFT, 0);
219                 /* only the menu-version of subdivide is registered in keymaps for now */
220         WM_keymap_add_item(keymap, "ARMATURE_OT_subdivs", SKEY, KM_PRESS, KM_ALT, 0);
221         
222         WM_keymap_add_item(keymap, "ARMATURE_OT_parent_set", PKEY, KM_PRESS, KM_CTRL, 0);
223         WM_keymap_add_item(keymap, "ARMATURE_OT_parent_clear", PKEY, KM_PRESS, KM_ALT, 0);
224         
225         WM_keymap_add_item(keymap, "ARMATURE_OT_select_all_toggle", AKEY, KM_PRESS, 0, 0);
226         WM_keymap_add_item(keymap, "ARMATURE_OT_select_inverse", IKEY, KM_PRESS, KM_CTRL, 0);
227         
228         WM_keymap_add_item(keymap, "ARMATURE_OT_test", TKEY, KM_PRESS, 0, 0);  // XXX temp test for context iterators... to be removed
229
230         kmi= WM_keymap_add_item(keymap, "ARMATURE_OT_select_hierarchy", LEFTBRACKETKEY, KM_PRESS, 0, 0);
231                 RNA_enum_set(kmi->ptr, "direction", BONE_SELECT_PARENT);
232         kmi= WM_keymap_add_item(keymap, "ARMATURE_OT_select_hierarchy", LEFTBRACKETKEY, KM_PRESS, KM_SHIFT, 0);
233                 RNA_enum_set(kmi->ptr, "direction", BONE_SELECT_PARENT);
234                 RNA_boolean_set(kmi->ptr, "extend", 1);
235         
236         kmi= WM_keymap_add_item(keymap, "ARMATURE_OT_select_hierarchy", RIGHTBRACKETKEY, KM_PRESS, 0, 0);
237                 RNA_enum_set(kmi->ptr, "direction", BONE_SELECT_CHILD);
238         kmi= WM_keymap_add_item(keymap, "ARMATURE_OT_select_hierarchy", RIGHTBRACKETKEY, KM_PRESS, KM_SHIFT, 0);
239                 RNA_enum_set(kmi->ptr, "direction", BONE_SELECT_CHILD);
240                 RNA_boolean_set(kmi->ptr, "extend", 1);
241
242         WM_keymap_add_item(keymap, "ARMATURE_OT_select_linked", LKEY, KM_PRESS, 0, 0);
243         
244         WM_keymap_add_item(keymap, "ARMATURE_OT_delete", XKEY, KM_PRESS, 0, 0);
245         WM_keymap_add_item(keymap, "ARMATURE_OT_duplicate", DKEY, KM_PRESS, KM_SHIFT, 0);
246         WM_keymap_add_item(keymap, "ARMATURE_OT_extrude", EKEY, KM_PRESS, 0, 0);
247         kmi= WM_keymap_add_item(keymap, "ARMATURE_OT_extrude", EKEY, KM_PRESS, KM_SHIFT, 0);
248                 RNA_boolean_set(kmi->ptr, "forked", 1);
249         WM_keymap_add_item(keymap, "ARMATURE_OT_click_extrude", LEFTMOUSE, KM_PRESS, KM_CTRL, 0);
250         WM_keymap_add_item(keymap, "ARMATURE_OT_fill", FKEY, KM_PRESS, 0, 0);
251         WM_keymap_add_item(keymap, "ARMATURE_OT_merge", MKEY, KM_PRESS, KM_ALT, 0);
252         
253         WM_keymap_add_item(keymap, "ARMATURE_OT_separate", PKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
254         
255                 /* set flags */
256         kmi= WM_keymap_add_item(keymap, "ARMATURE_OT_flags_set", WKEY, KM_PRESS, KM_SHIFT, 0);
257                 RNA_enum_set(kmi->ptr, "mode", 2); // toggle
258         kmi= WM_keymap_add_item(keymap, "ARMATURE_OT_flags_set", WKEY, KM_PRESS, KM_CTRL|KM_SHIFT, 0);
259                 RNA_enum_set(kmi->ptr, "mode", 1); // enable
260         kmi= WM_keymap_add_item(keymap, "ARMATURE_OT_flags_set", WKEY, KM_PRESS, KM_ALT, 0);
261                 RNA_enum_set(kmi->ptr, "mode", 0); // clear
262                 
263                 /* armature/bone layers */
264         WM_keymap_add_item(keymap, "ARMATURE_OT_armature_layers", MKEY, KM_PRESS, KM_SHIFT, 0);
265         WM_keymap_add_item(keymap, "ARMATURE_OT_bone_layers", MKEY, KM_PRESS, 0, 0);
266         
267                 /* special transforms: */
268                 /*      1) envelope/b-bone size */
269         kmi= WM_keymap_add_item(keymap, "TFM_OT_transform", SKEY, KM_PRESS, KM_ALT, 0);
270                 RNA_enum_set(kmi->ptr, "mode", TFM_BONESIZE);
271                 /*      2) set roll */
272         kmi= WM_keymap_add_item(keymap, "TFM_OT_transform", RKEY, KM_PRESS, KM_CTRL, 0);
273                 RNA_enum_set(kmi->ptr, "mode", TFM_BONE_ROLL);
274         
275         /* Armature -> Etch-A-Ton ------------------------ */
276         WM_keymap_add_item(keymap, "SKETCH_OT_delete", XKEY, KM_PRESS, 0, 0);
277         WM_keymap_add_item(keymap, "SKETCH_OT_finish_stroke", SELECTMOUSE, KM_PRESS, 0, 0);
278         WM_keymap_add_item(keymap, "SKETCH_OT_cancel_stroke", ESCKEY, KM_PRESS, 0, 0);
279         WM_keymap_add_item(keymap, "SKETCH_OT_select", SELECTMOUSE, KM_PRESS, 0, 0);
280
281         /* Pose ------------------------ */
282         /* only set in posemode, by space_view3d listener */
283         keymap= WM_keymap_listbase(wm, "Pose", 0, 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_item(keymap, "POSE_OT_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                 // for now, we include hotkeys for copy/paste
298         WM_keymap_add_item(keymap, "POSE_OT_copy", CKEY, KM_PRESS, KM_CTRL, 0);
299         WM_keymap_add_item(keymap, "POSE_OT_paste", VKEY, KM_PRESS, KM_CTRL, 0);
300         kmi= WM_keymap_add_item(keymap, "POSE_OT_paste", VKEY, KM_PRESS, KM_CTRL|KM_SHIFT, 0);
301                 RNA_boolean_set(kmi->ptr, "flipped", 1);
302         
303         WM_keymap_add_item(keymap, "POSE_OT_select_all_toggle", AKEY, KM_PRESS, 0, 0);
304         WM_keymap_add_item(keymap, "POSE_OT_select_inverse", IKEY, KM_PRESS, KM_CTRL, 0);
305
306         WM_keymap_add_item(keymap, "POSE_OT_select_parent", PKEY, KM_PRESS, KM_SHIFT, 0);
307
308         kmi= WM_keymap_add_item(keymap, "POSE_OT_select_hierarchy", LEFTBRACKETKEY, KM_PRESS, 0, 0);
309                 RNA_enum_set(kmi->ptr, "direction", BONE_SELECT_PARENT);
310         kmi= WM_keymap_add_item(keymap, "POSE_OT_select_hierarchy", LEFTBRACKETKEY, KM_PRESS, KM_SHIFT, 0);
311                 RNA_enum_set(kmi->ptr, "direction", BONE_SELECT_PARENT);
312                 RNA_boolean_set(kmi->ptr, "extend", 1);
313         
314         kmi= WM_keymap_add_item(keymap, "POSE_OT_select_hierarchy", RIGHTBRACKETKEY, KM_PRESS, 0, 0);
315                 RNA_enum_set(kmi->ptr, "direction", BONE_SELECT_CHILD);
316         kmi= WM_keymap_add_item(keymap, "POSE_OT_select_hierarchy", RIGHTBRACKETKEY, KM_PRESS, KM_SHIFT, 0);
317                 RNA_enum_set(kmi->ptr, "direction", BONE_SELECT_CHILD);
318                 RNA_boolean_set(kmi->ptr, "extend", 1);
319
320         WM_keymap_add_item(keymap, "POSE_OT_select_linked", LKEY, KM_PRESS, 0, 0);
321         
322         WM_keymap_add_item(keymap, "POSE_OT_constraint_add_with_targets", CKEY, KM_PRESS, KM_CTRL|KM_SHIFT, 0);
323         WM_keymap_add_item(keymap, "POSE_OT_constraints_clear", CKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
324         WM_keymap_add_item(keymap, "POSE_OT_ik_add", IKEY, KM_PRESS, /*KM_CTRL|*/KM_SHIFT, 0);
325         WM_keymap_add_item(keymap, "POSE_OT_ik_clear", IKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
326         
327         WM_keymap_add_item(keymap, "POSE_OT_groups_menu", GKEY, KM_PRESS, KM_CTRL, 0);
328         
329                 /* set flags */
330         kmi= WM_keymap_add_item(keymap, "POSE_OT_flags_set", WKEY, KM_PRESS, KM_SHIFT, 0);
331                 RNA_enum_set(kmi->ptr, "mode", 2); // toggle
332         kmi= WM_keymap_add_item(keymap, "POSE_OT_flags_set", WKEY, KM_PRESS, KM_CTRL|KM_SHIFT, 0);
333                 RNA_enum_set(kmi->ptr, "mode", 1); // enable
334         kmi= WM_keymap_add_item(keymap, "POSE_OT_flags_set", WKEY, KM_PRESS, KM_ALT, 0);
335                 RNA_enum_set(kmi->ptr, "mode", 0); // clear
336                 
337                 /* armature/bone layers */
338         WM_keymap_add_item(keymap, "POSE_OT_armature_layers", MKEY, KM_PRESS, KM_SHIFT, 0);
339         WM_keymap_add_item(keymap, "POSE_OT_bone_layers", MKEY, KM_PRESS, 0, 0);
340         
341                 /* special transforms: */
342                 /*      1) envelope/b-bone size */
343         kmi= WM_keymap_add_item(keymap, "TFM_OT_transform", SKEY, KM_PRESS, KM_ALT, 0);
344                 RNA_enum_set(kmi->ptr, "mode", TFM_BONESIZE);
345         
346         // XXX this should probably be in screen instead... here for testing purposes in the meantime... - Aligorith
347         WM_keymap_verify_item(keymap, "ANIM_OT_insert_keyframe_menu", IKEY, KM_PRESS, 0, 0);
348         WM_keymap_verify_item(keymap, "ANIM_OT_delete_keyframe_v3d", IKEY, KM_PRESS, KM_ALT, 0);
349         
350         /* Pose -> PoseLib ------------- */
351         /* only set in posemode, by space_view3d listener */
352         WM_keymap_add_item(keymap, "POSELIB_OT_browse_interactive", LKEY, KM_PRESS, KM_CTRL, 0);
353         
354         WM_keymap_add_item(keymap, "POSELIB_OT_pose_add", LKEY, KM_PRESS, KM_SHIFT, 0);
355         WM_keymap_add_item(keymap, "POSELIB_OT_pose_remove", LKEY, KM_PRESS, KM_ALT, 0);
356         WM_keymap_add_item(keymap, "POSELIB_OT_pose_rename", LKEY, KM_PRESS, KM_CTRL|KM_SHIFT, 0);
357 }
358