13b1155d61636a56bc86a368f405ec25169c908a
[blender.git] / source / blender / editors / object / object_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) 2008 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_screen.h"
56 #include "ED_object.h"
57
58 #include "object_intern.h"
59
60
61 /* ************************** registration **********************************/
62
63
64 void ED_operatortypes_object(void)
65 {
66         WM_operatortype_append(OBJECT_OT_location_clear);
67         WM_operatortype_append(OBJECT_OT_rotation_clear);
68         WM_operatortype_append(OBJECT_OT_scale_clear);
69         WM_operatortype_append(OBJECT_OT_origin_clear);
70         WM_operatortype_append(OBJECT_OT_visual_transform_apply);
71         WM_operatortype_append(OBJECT_OT_location_apply);
72         WM_operatortype_append(OBJECT_OT_scale_apply);
73         WM_operatortype_append(OBJECT_OT_rotation_apply);
74         WM_operatortype_append(OBJECT_OT_origin_set);
75         
76         WM_operatortype_append(OBJECT_OT_mode_set);
77         WM_operatortype_append(OBJECT_OT_editmode_toggle);
78         WM_operatortype_append(OBJECT_OT_posemode_toggle);
79         WM_operatortype_append(OBJECT_OT_proxy_make);
80         WM_operatortype_append(OBJECT_OT_restrictview_clear);
81         WM_operatortype_append(OBJECT_OT_restrictview_set);
82         WM_operatortype_append(OBJECT_OT_shade_smooth);
83         WM_operatortype_append(OBJECT_OT_shade_flat);
84
85         WM_operatortype_append(OBJECT_OT_parent_set);
86         WM_operatortype_append(OBJECT_OT_parent_no_inverse_set);
87         WM_operatortype_append(OBJECT_OT_parent_clear);
88         WM_operatortype_append(OBJECT_OT_vertex_parent_set);
89         WM_operatortype_append(OBJECT_OT_track_set);
90         WM_operatortype_append(OBJECT_OT_track_clear);
91         WM_operatortype_append(OBJECT_OT_slow_parent_set);
92         WM_operatortype_append(OBJECT_OT_slow_parent_clear);
93         WM_operatortype_append(OBJECT_OT_make_local);
94         WM_operatortype_append(OBJECT_OT_make_single_user);
95         WM_operatortype_append(OBJECT_OT_make_links_scene);
96         WM_operatortype_append(OBJECT_OT_make_links_data);
97         WM_operatortype_append(OBJECT_OT_move_to_layer);
98
99         WM_operatortype_append(OBJECT_OT_select_inverse);
100         WM_operatortype_append(OBJECT_OT_select_random);
101         WM_operatortype_append(OBJECT_OT_select_all);
102         WM_operatortype_append(OBJECT_OT_select_by_type);
103         WM_operatortype_append(OBJECT_OT_select_by_layer);
104         WM_operatortype_append(OBJECT_OT_select_linked);
105         WM_operatortype_append(OBJECT_OT_select_grouped);
106         WM_operatortype_append(OBJECT_OT_select_mirror);
107         WM_operatortype_append(OBJECT_OT_select_name); /* XXX - weak, not compat with linked objects */
108
109         WM_operatortype_append(GROUP_OT_create);
110         WM_operatortype_append(GROUP_OT_objects_remove);
111         WM_operatortype_append(GROUP_OT_objects_add_active);
112         WM_operatortype_append(GROUP_OT_objects_remove_active);
113
114         WM_operatortype_append(OBJECT_OT_delete);
115         WM_operatortype_append(OBJECT_OT_curve_add);
116         WM_operatortype_append(OBJECT_OT_text_add);
117         WM_operatortype_append(OBJECT_OT_surface_add);
118         WM_operatortype_append(OBJECT_OT_armature_add);
119         WM_operatortype_append(OBJECT_OT_lamp_add);
120         WM_operatortype_append(OBJECT_OT_add);
121         WM_operatortype_append(OBJECT_OT_effector_add);
122         WM_operatortype_append(OBJECT_OT_group_instance_add);
123         WM_operatortype_append(OBJECT_OT_metaball_add);
124         WM_operatortype_append(OBJECT_OT_duplicates_make_real);
125         WM_operatortype_append(OBJECT_OT_duplicate);
126         WM_operatortype_append(OBJECT_OT_join);
127         WM_operatortype_append(OBJECT_OT_join_shapes);
128         WM_operatortype_append(OBJECT_OT_convert);
129
130         WM_operatortype_append(OBJECT_OT_modifier_add);
131         WM_operatortype_append(OBJECT_OT_modifier_remove);
132         WM_operatortype_append(OBJECT_OT_modifier_move_up);
133         WM_operatortype_append(OBJECT_OT_modifier_move_down);
134         WM_operatortype_append(OBJECT_OT_modifier_apply);
135         WM_operatortype_append(OBJECT_OT_modifier_convert);
136         WM_operatortype_append(OBJECT_OT_modifier_copy);
137         WM_operatortype_append(OBJECT_OT_multires_subdivide);
138         WM_operatortype_append(OBJECT_OT_multires_higher_levels_delete);
139         WM_operatortype_append(OBJECT_OT_meshdeform_bind);
140         WM_operatortype_append(OBJECT_OT_explode_refresh);
141         
142         WM_operatortype_append(OBJECT_OT_constraint_add);
143         WM_operatortype_append(OBJECT_OT_constraint_add_with_targets);
144         WM_operatortype_append(POSE_OT_constraint_add);
145         WM_operatortype_append(POSE_OT_constraint_add_with_targets);
146         WM_operatortype_append(OBJECT_OT_constraints_clear);
147         WM_operatortype_append(POSE_OT_constraints_clear);
148         WM_operatortype_append(POSE_OT_ik_add);
149         WM_operatortype_append(POSE_OT_ik_clear);
150         WM_operatortype_append(CONSTRAINT_OT_delete);
151         WM_operatortype_append(CONSTRAINT_OT_move_up);
152         WM_operatortype_append(CONSTRAINT_OT_move_down);
153         WM_operatortype_append(CONSTRAINT_OT_stretchto_reset);
154         WM_operatortype_append(CONSTRAINT_OT_limitdistance_reset);
155         WM_operatortype_append(CONSTRAINT_OT_childof_set_inverse);
156         WM_operatortype_append(CONSTRAINT_OT_childof_clear_inverse);
157
158         WM_operatortype_append(OBJECT_OT_vertex_group_add);
159         WM_operatortype_append(OBJECT_OT_vertex_group_remove);
160         WM_operatortype_append(OBJECT_OT_vertex_group_assign);
161         WM_operatortype_append(OBJECT_OT_vertex_group_remove_from);
162         WM_operatortype_append(OBJECT_OT_vertex_group_select);
163         WM_operatortype_append(OBJECT_OT_vertex_group_deselect);
164         WM_operatortype_append(OBJECT_OT_vertex_group_copy_to_linked);
165         WM_operatortype_append(OBJECT_OT_vertex_group_copy);
166         WM_operatortype_append(OBJECT_OT_vertex_group_normalize);
167         WM_operatortype_append(OBJECT_OT_vertex_group_normalize_all);
168         WM_operatortype_append(OBJECT_OT_vertex_group_invert);
169         WM_operatortype_append(OBJECT_OT_vertex_group_levels);
170         WM_operatortype_append(OBJECT_OT_vertex_group_blend);
171         WM_operatortype_append(OBJECT_OT_vertex_group_clean);
172         WM_operatortype_append(OBJECT_OT_vertex_group_set_active);
173
174         WM_operatortype_append(OBJECT_OT_game_property_new);
175         WM_operatortype_append(OBJECT_OT_game_property_remove);
176
177         WM_operatortype_append(OBJECT_OT_shape_key_add);
178         WM_operatortype_append(OBJECT_OT_shape_key_remove);
179         WM_operatortype_append(OBJECT_OT_shape_key_clear);
180         WM_operatortype_append(OBJECT_OT_shape_key_mirror);
181         WM_operatortype_append(OBJECT_OT_shape_key_move);
182
183         WM_operatortype_append(LATTICE_OT_select_all);
184         WM_operatortype_append(LATTICE_OT_make_regular);
185
186         WM_operatortype_append(OBJECT_OT_group_add);
187         WM_operatortype_append(OBJECT_OT_group_remove);
188
189         WM_operatortype_append(OBJECT_OT_hook_add_selobj);
190         WM_operatortype_append(OBJECT_OT_hook_add_newobj);
191         WM_operatortype_append(OBJECT_OT_hook_remove);
192         WM_operatortype_append(OBJECT_OT_hook_select);
193         WM_operatortype_append(OBJECT_OT_hook_assign);
194         WM_operatortype_append(OBJECT_OT_hook_reset);
195         WM_operatortype_append(OBJECT_OT_hook_recenter);
196 }
197
198 void ED_operatormacros_object(void)
199 {
200         wmOperatorType *ot;
201         wmOperatorTypeMacro *otmacro;
202         
203         ot= WM_operatortype_append_macro("OBJECT_OT_duplicate_move", "Duplicate", OPTYPE_UNDO|OPTYPE_REGISTER);
204         if(ot) {
205                 WM_operatortype_macro_define(ot, "OBJECT_OT_duplicate");
206                 otmacro= WM_operatortype_macro_define(ot, "TFM_OT_translate");
207                 RNA_enum_set(otmacro->ptr, "proportional", PROP_EDIT_OFF);
208         }
209
210         /* grr, should be able to pass options on... */
211         ot= WM_operatortype_append_macro("OBJECT_OT_duplicate_move_linked", "Duplicate", OPTYPE_UNDO|OPTYPE_REGISTER);
212         if(ot) {
213                 otmacro= WM_operatortype_macro_define(ot, "OBJECT_OT_duplicate");
214                 RNA_boolean_set(otmacro->ptr, "linked", 1);
215                 otmacro= WM_operatortype_macro_define(ot, "TFM_OT_translate");
216                 RNA_enum_set(otmacro->ptr, "proportional", PROP_EDIT_OFF);
217         }
218 }
219
220 static int object_mode_poll(bContext *C)
221 {
222         Object *ob= CTX_data_active_object(C);
223         return (!ob || ob->mode == OB_MODE_OBJECT);
224 }
225
226 void ED_keymap_object(wmKeyConfig *keyconf)
227 {
228         wmKeyMap *keymap;
229         wmKeyMapItem *kmi;
230         int i;
231         
232         /* Objects, Regardless of Mode -------------------------------------------------- */
233         keymap= WM_keymap_find(keyconf, "Object Non-modal", 0, 0);
234         
235         /* Note: this keymap works disregarding mode */
236         kmi = WM_keymap_add_item(keymap, "OBJECT_OT_mode_set", TABKEY, KM_PRESS, 0, 0);
237                 RNA_enum_set(kmi->ptr, "mode", OB_MODE_EDIT);
238                 RNA_boolean_set(kmi->ptr, "toggle", 1);
239
240         kmi = WM_keymap_add_item(keymap, "OBJECT_OT_mode_set", TABKEY, KM_PRESS, KM_CTRL, 0);
241                 RNA_enum_set(kmi->ptr, "mode", OB_MODE_POSE);
242                 RNA_boolean_set(kmi->ptr, "toggle", 1);
243
244         kmi = WM_keymap_add_item(keymap, "OBJECT_OT_mode_set", VKEY, KM_PRESS, 0, 0);
245                 RNA_enum_set(kmi->ptr, "mode", OB_MODE_VERTEX_PAINT);
246                 RNA_boolean_set(kmi->ptr, "toggle", 1);
247         
248         kmi = WM_keymap_add_item(keymap, "OBJECT_OT_mode_set", VKEY, KM_PRESS, 0, 0);
249                 RNA_enum_set(kmi->ptr, "mode", OB_MODE_VERTEX_PAINT);
250                 RNA_boolean_set(kmi->ptr, "toggle", 1);
251         kmi = WM_keymap_add_item(keymap, "OBJECT_OT_mode_set", TABKEY, KM_PRESS, KM_CTRL, 0);
252                 RNA_enum_set(kmi->ptr, "mode", OB_MODE_WEIGHT_PAINT);
253                 RNA_boolean_set(kmi->ptr, "toggle", 1);
254         
255         WM_keymap_add_item(keymap, "OBJECT_OT_origin_set", CKEY, KM_PRESS, KM_ALT|KM_SHIFT|KM_CTRL, 0);
256
257         /* Object Mode ---------------------------------------------------------------- */
258         /* Note: this keymap gets disabled in non-objectmode,  */
259         keymap= WM_keymap_find(keyconf, "Object Mode", 0, 0);
260         keymap->poll= object_mode_poll;
261         
262         /* object mode supports PET now */
263         ED_object_generic_keymap(keyconf, keymap, TRUE);
264
265         WM_keymap_add_item(keymap, "OBJECT_OT_select_all", AKEY, KM_PRESS, 0, 0);
266         WM_keymap_add_item(keymap, "OBJECT_OT_select_inverse", IKEY, KM_PRESS, KM_CTRL, 0);
267         WM_keymap_add_item(keymap, "OBJECT_OT_select_linked", LKEY, KM_PRESS, KM_SHIFT, 0);
268         WM_keymap_add_item(keymap, "OBJECT_OT_select_grouped", GKEY, KM_PRESS, KM_SHIFT, 0);
269         WM_keymap_add_item(keymap, "OBJECT_OT_select_mirror", MKEY, KM_PRESS, KM_CTRL|KM_SHIFT, 0);
270         
271         WM_keymap_verify_item(keymap, "OBJECT_OT_parent_set", PKEY, KM_PRESS, KM_CTRL, 0);
272         WM_keymap_verify_item(keymap, "OBJECT_OT_parent_no_inverse_set", PKEY, KM_PRESS, KM_CTRL|KM_SHIFT, 0);
273         WM_keymap_verify_item(keymap, "OBJECT_OT_parent_clear", PKEY, KM_PRESS, KM_ALT, 0);
274         WM_keymap_verify_item(keymap, "OBJECT_OT_track_set", TKEY, KM_PRESS, KM_CTRL, 0);
275         WM_keymap_verify_item(keymap, "OBJECT_OT_track_clear", TKEY, KM_PRESS, KM_ALT, 0);
276         
277         WM_keymap_verify_item(keymap, "OBJECT_OT_constraint_add_with_targets", CKEY, KM_PRESS, KM_CTRL|KM_SHIFT, 0);
278         WM_keymap_verify_item(keymap, "OBJECT_OT_constraints_clear", CKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
279         
280         WM_keymap_verify_item(keymap, "OBJECT_OT_location_clear", GKEY, KM_PRESS, KM_ALT, 0);
281         WM_keymap_verify_item(keymap, "OBJECT_OT_rotation_clear", RKEY, KM_PRESS, KM_ALT, 0);
282         WM_keymap_verify_item(keymap, "OBJECT_OT_scale_clear", SKEY, KM_PRESS, KM_ALT, 0);
283         WM_keymap_verify_item(keymap, "OBJECT_OT_origin_clear", OKEY, KM_PRESS, KM_ALT, 0);
284         
285         WM_keymap_add_item(keymap, "OBJECT_OT_restrictview_clear", HKEY, KM_PRESS, KM_ALT, 0);
286         WM_keymap_add_item(keymap, "OBJECT_OT_restrictview_set", HKEY, KM_PRESS, 0, 0);
287         RNA_boolean_set(WM_keymap_add_item(keymap, "OBJECT_OT_restrictview_set", HKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "unselected", 1);
288
289         WM_keymap_add_item(keymap, "OBJECT_OT_move_to_layer", MKEY, KM_PRESS, 0, 0);
290         
291         WM_keymap_add_item(keymap, "OBJECT_OT_delete", XKEY, KM_PRESS, 0, 0);
292         WM_keymap_add_item(keymap, "OBJECT_OT_delete", DELKEY, KM_PRESS, 0, 0);
293         WM_keymap_add_menu(keymap, "INFO_MT_add", AKEY, KM_PRESS, KM_SHIFT, 0);
294
295         WM_keymap_add_item(keymap, "OBJECT_OT_duplicates_make_real", AKEY, KM_PRESS, KM_SHIFT|KM_CTRL, 0);
296
297         WM_keymap_add_menu(keymap, "VIEW3D_MT_object_apply", AKEY, KM_PRESS, KM_CTRL, 0);
298         WM_keymap_add_menu(keymap, "VIEW3D_MT_make_single_user", UKEY, KM_PRESS, 0, 0);
299         WM_keymap_add_menu(keymap, "VIEW3D_MT_make_links", LKEY, KM_PRESS, KM_CTRL, 0);
300
301         WM_keymap_add_item(keymap, "OBJECT_OT_duplicate_move", DKEY, KM_PRESS, KM_SHIFT, 0);
302         WM_keymap_add_item(keymap, "OBJECT_OT_duplicate_move_linked", DKEY, KM_PRESS, KM_ALT, 0);
303
304         WM_keymap_add_item(keymap, "OBJECT_OT_join", JKEY, KM_PRESS, KM_CTRL, 0);
305         WM_keymap_add_item(keymap, "OBJECT_OT_convert", CKEY, KM_PRESS, KM_ALT, 0);
306         WM_keymap_add_item(keymap, "OBJECT_OT_proxy_make", PKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
307         WM_keymap_add_item(keymap, "OBJECT_OT_make_local", LKEY, KM_PRESS, 0, 0);
308
309         // XXX this should probably be in screen instead... here for testing purposes in the meantime... - Aligorith
310         WM_keymap_verify_item(keymap, "ANIM_OT_keyframe_insert_menu", IKEY, KM_PRESS, 0, 0);
311         WM_keymap_verify_item(keymap, "ANIM_OT_keyframe_delete_v3d", IKEY, KM_PRESS, KM_ALT, 0);
312         
313         WM_keymap_verify_item(keymap, "GROUP_OT_create", GKEY, KM_PRESS, KM_CTRL, 0);
314         WM_keymap_verify_item(keymap, "GROUP_OT_objects_remove", GKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
315         WM_keymap_verify_item(keymap, "GROUP_OT_objects_add_active", GKEY, KM_PRESS, KM_SHIFT|KM_CTRL, 0);
316         WM_keymap_verify_item(keymap, "GROUP_OT_objects_remove_active", GKEY, KM_PRESS, KM_SHIFT|KM_ALT, 0);
317
318         for(i=1; i<=5; i++) {
319                 kmi = WM_keymap_add_item(keymap, "OBJECT_OT_subdivision_set", ZEROKEY+i, KM_PRESS, KM_CTRL, 0);
320                 RNA_int_set(kmi->ptr, "level", i);
321         }
322
323         /* Lattice -------------------------------------------------------------------- */
324         keymap= WM_keymap_find(keyconf, "Lattice", 0, 0);
325         keymap->poll= ED_operator_editlattice;
326
327         WM_keymap_add_item(keymap, "LATTICE_OT_select_all", AKEY, KM_PRESS, 0, 0);
328         
329                 /* menus */
330         WM_keymap_add_menu(keymap, "VIEW3D_MT_hook", HKEY, KM_PRESS, KM_CTRL, 0);
331
332         ED_object_generic_keymap(keyconf, keymap, TRUE);
333 }
334
335 void ED_object_generic_keymap(struct wmKeyConfig *keyconf, struct wmKeyMap *keymap, int do_pet)
336 {
337         wmKeyMapItem *km;
338
339         /* used by mesh, curve & lattice only */
340         if(do_pet) {
341                 /* context ops */
342                 km = WM_keymap_add_item(keymap, "WM_OT_context_cycle_enum", OKEY, KM_PRESS, KM_SHIFT, 0);
343                 RNA_string_set(km->ptr, "path", "scene.tool_settings.proportional_editing_falloff");
344
345                 km = WM_keymap_add_item(keymap, "WM_OT_context_toggle_enum", OKEY, KM_PRESS, 0, 0);
346                 RNA_string_set(km->ptr, "path", "scene.tool_settings.proportional_editing");
347                 RNA_string_set(km->ptr, "value_1", "DISABLED");
348                 RNA_string_set(km->ptr, "value_2", "ENABLED");
349
350                 km = WM_keymap_add_item(keymap, "WM_OT_context_toggle_enum", OKEY, KM_PRESS, KM_ALT, 0);
351                 RNA_string_set(km->ptr, "path", "scene.tool_settings.proportional_editing");
352                 RNA_string_set(km->ptr, "value_1", "DISABLED");
353                 RNA_string_set(km->ptr, "value_2", "CONNECTED");
354         }
355 }
356