merge with trunk r39834
[blender.git] / source / blender / editors / mesh / mesh_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 /** \file blender/editors/mesh/mesh_ops.c
30  *  \ingroup edmesh
31  */
32
33
34 #include <stdlib.h>
35 #include <math.h>
36
37
38 #include "DNA_object_types.h"
39 #include "DNA_scene_types.h"
40
41 #include "BLI_math.h"
42 #include "BLI_blenlib.h"
43 #include "BLI_editVert.h"
44
45 #include "BLF_api.h"
46
47 #include "BKE_context.h"
48
49 #include "RNA_access.h"
50
51 #include "WM_api.h"
52 #include "WM_types.h"
53
54 #include "ED_object.h"
55 #include "ED_mesh.h"
56 #include "ED_screen.h"
57 #include "ED_view3d.h"
58
59
60 #include "mesh_intern.h"
61
62 /**************************** registration **********************************/
63
64 void ED_operatortypes_mesh(void)
65 {
66         WM_operatortype_append(MESH_OT_select_all);
67         WM_operatortype_append(MESH_OT_select_more);
68         WM_operatortype_append(MESH_OT_select_less);
69         WM_operatortype_append(MESH_OT_select_inverse);
70         WM_operatortype_append(MESH_OT_select_non_manifold);
71         WM_operatortype_append(MESH_OT_select_linked);
72         WM_operatortype_append(MESH_OT_select_linked_pick);
73         WM_operatortype_append(MESH_OT_select_random);
74         WM_operatortype_append(MESH_OT_hide);
75         WM_operatortype_append(MESH_OT_reveal);
76         WM_operatortype_append(MESH_OT_select_by_number_vertices);
77         WM_operatortype_append(MESH_OT_select_mirror);
78         WM_operatortype_append(MESH_OT_normals_make_consistent);
79         WM_operatortype_append(MESH_OT_merge);
80         WM_operatortype_append(MESH_OT_subdivide);
81         WM_operatortype_append(MESH_OT_faces_select_linked_flat);
82         WM_operatortype_append(MESH_OT_edges_select_sharp);
83         WM_operatortype_append(MESH_OT_primitive_plane_add);
84         WM_operatortype_append(MESH_OT_primitive_cube_add);
85         WM_operatortype_append(MESH_OT_primitive_circle_add);
86         WM_operatortype_append(MESH_OT_primitive_cylinder_add);
87         WM_operatortype_append(MESH_OT_primitive_cone_add);
88         WM_operatortype_append(MESH_OT_primitive_grid_add);
89         WM_operatortype_append(MESH_OT_primitive_monkey_add);
90         WM_operatortype_append(MESH_OT_primitive_uv_sphere_add);
91         WM_operatortype_append(MESH_OT_primitive_ico_sphere_add);
92         WM_operatortype_append(MESH_OT_fgon_clear);
93         WM_operatortype_append(MESH_OT_fgon_make);
94         WM_operatortype_append(MESH_OT_duplicate);
95         WM_operatortype_append(MESH_OT_remove_doubles);
96         WM_operatortype_append(MESH_OT_vertices_sort);
97         WM_operatortype_append(MESH_OT_vertices_randomize);
98         WM_operatortype_append(MESH_OT_extrude);
99         WM_operatortype_append(MESH_OT_spin);
100         WM_operatortype_append(MESH_OT_screw);
101         
102         WM_operatortype_append(MESH_OT_split);
103         WM_operatortype_append(MESH_OT_extrude_repeat);
104         WM_operatortype_append(MESH_OT_edge_rotate);
105         WM_operatortype_append(MESH_OT_select_vertex_path);
106         WM_operatortype_append(MESH_OT_loop_to_region);
107         WM_operatortype_append(MESH_OT_region_to_loop);
108         WM_operatortype_append(MESH_OT_select_axis);
109         
110         WM_operatortype_append(MESH_OT_uvs_rotate);
111         WM_operatortype_append(MESH_OT_uvs_mirror);
112         WM_operatortype_append(MESH_OT_colors_rotate);
113         WM_operatortype_append(MESH_OT_colors_mirror);
114         
115         WM_operatortype_append(MESH_OT_fill);
116         WM_operatortype_append(MESH_OT_beautify_fill);
117         WM_operatortype_append(MESH_OT_quads_convert_to_tris);
118         WM_operatortype_append(MESH_OT_tris_convert_to_quads);
119         WM_operatortype_append(MESH_OT_edge_flip);
120         WM_operatortype_append(MESH_OT_faces_shade_smooth);
121         WM_operatortype_append(MESH_OT_faces_shade_flat);
122         WM_operatortype_append(MESH_OT_sort_faces);
123
124         WM_operatortype_append(MESH_OT_delete);
125
126         WM_operatortype_append(MESH_OT_separate);
127         WM_operatortype_append(MESH_OT_dupli_extrude_cursor);
128         WM_operatortype_append(MESH_OT_loop_select);
129         WM_operatortype_append(MESH_OT_edge_face_add);
130         WM_operatortype_append(MESH_OT_select_shortest_path);
131         WM_operatortype_append(MESH_OT_select_similar);
132         WM_operatortype_append(MESH_OT_loop_multi_select);
133         WM_operatortype_append(MESH_OT_mark_seam);
134         WM_operatortype_append(MESH_OT_mark_sharp);
135         WM_operatortype_append(MESH_OT_vertices_smooth);
136         WM_operatortype_append(MESH_OT_noise);
137         WM_operatortype_append(MESH_OT_flip_normals);
138         WM_operatortype_append(MESH_OT_knife_cut);
139         WM_operatortype_append(MESH_OT_rip);
140         WM_operatortype_append(MESH_OT_blend_from_shape);
141         WM_operatortype_append(MESH_OT_shape_propagate_to_all);
142         
143         WM_operatortype_append(MESH_OT_uv_texture_add);
144         WM_operatortype_append(MESH_OT_uv_texture_remove);
145         WM_operatortype_append(MESH_OT_vertex_color_add);
146         WM_operatortype_append(MESH_OT_vertex_color_remove);
147         WM_operatortype_append(MESH_OT_sticky_add);
148         WM_operatortype_append(MESH_OT_sticky_remove);
149         WM_operatortype_append(MESH_OT_drop_named_image);
150
151         WM_operatortype_append(MESH_OT_edgering_select);
152         WM_operatortype_append(MESH_OT_loopcut);
153
154         WM_operatortype_append(MESH_OT_solidify);
155         WM_operatortype_append(MESH_OT_select_nth);
156 }
157
158 #if 0 /* UNUSED, remove? */
159 static int ED_operator_editmesh_face_select(bContext *C)
160 {
161         Object *obedit= CTX_data_edit_object(C);
162         if(obedit && obedit->type==OB_MESH) {
163                 EditMesh *em = ((Mesh *)obedit->data)->edit_mesh;
164                 if (em && em->selectmode & SCE_SELECT_FACE) {
165                         return 1;
166                 }
167         }
168         return 0;
169 }
170 #endif
171
172 void ED_operatormacros_mesh(void)
173 {
174         wmOperatorType *ot;
175         wmOperatorTypeMacro *otmacro;
176         
177         ot= WM_operatortype_append_macro("MESH_OT_loopcut_slide", _("Loop Cut and Slide"), OPTYPE_UNDO|OPTYPE_REGISTER);
178         ot->description = _("Cut mesh loop and slide it");
179         WM_operatortype_macro_define(ot, "MESH_OT_loopcut");
180         otmacro= WM_operatortype_macro_define(ot, "TRANSFORM_OT_edge_slide");
181         RNA_struct_idprops_unset(otmacro->ptr, "release_confirm");
182
183         ot= WM_operatortype_append_macro("MESH_OT_duplicate_move", _("Add Duplicate"), OPTYPE_UNDO|OPTYPE_REGISTER);
184         ot->description = _("Duplicate mesh and move");
185         WM_operatortype_macro_define(ot, "MESH_OT_duplicate");
186         otmacro= WM_operatortype_macro_define(ot, "TRANSFORM_OT_translate");
187         RNA_enum_set(otmacro->ptr, "proportional", 0);
188         RNA_boolean_set(otmacro->ptr, "mirror", 0);
189
190         ot= WM_operatortype_append_macro("MESH_OT_rip_move", _("Rip"), OPTYPE_UNDO|OPTYPE_REGISTER);
191         ot->description = _("Rip polygons and move the result");
192         WM_operatortype_macro_define(ot, "MESH_OT_rip");
193         otmacro= WM_operatortype_macro_define(ot, "TRANSFORM_OT_translate");
194         RNA_enum_set(otmacro->ptr, "proportional", 0);
195         RNA_boolean_set(otmacro->ptr, "mirror", 0);
196
197         ot= WM_operatortype_append_macro("MESH_OT_extrude_region_move", _("Extrude Region and Move"), OPTYPE_UNDO|OPTYPE_REGISTER);
198         ot->description = _("Extrude region and move result");
199         otmacro= WM_operatortype_macro_define(ot, "MESH_OT_extrude");
200         RNA_enum_set(otmacro->ptr, "type", 1);
201         otmacro= WM_operatortype_macro_define(ot, "TRANSFORM_OT_translate");
202         RNA_enum_set(otmacro->ptr, "proportional", 0);
203         RNA_boolean_set(otmacro->ptr, "mirror", 0);
204
205         ot= WM_operatortype_append_macro("MESH_OT_extrude_faces_move", _("Extrude Individual Faces and Move"), OPTYPE_UNDO|OPTYPE_REGISTER);
206         ot->description = _("Extrude faces and move result");
207         otmacro= WM_operatortype_macro_define(ot, "MESH_OT_extrude");
208         RNA_enum_set(otmacro->ptr, "type", 2);
209         otmacro= WM_operatortype_macro_define(ot, "TRANSFORM_OT_shrink_fatten");
210         RNA_enum_set(otmacro->ptr, "proportional", 0);
211         RNA_boolean_set(otmacro->ptr, "mirror", 0);
212
213         ot= WM_operatortype_append_macro("MESH_OT_extrude_edges_move", _("Extrude Only Edges and Move"), OPTYPE_UNDO|OPTYPE_REGISTER);
214         ot->description = _("Extrude edges and move result");
215         otmacro= WM_operatortype_macro_define(ot, "MESH_OT_extrude");
216         RNA_enum_set(otmacro->ptr, "type", 3);
217         otmacro= WM_operatortype_macro_define(ot, "TRANSFORM_OT_translate");
218         RNA_enum_set(otmacro->ptr, "proportional", 0);
219         RNA_boolean_set(otmacro->ptr, "mirror", 0);
220
221         ot= WM_operatortype_append_macro("MESH_OT_extrude_vertices_move", _("Extrude Only Vertices and Move"), OPTYPE_UNDO|OPTYPE_REGISTER);
222         ot->description = _("Extrude vertices and move result");
223         otmacro= WM_operatortype_macro_define(ot, "MESH_OT_extrude");
224         RNA_enum_set(otmacro->ptr, "type", 4);
225         otmacro= WM_operatortype_macro_define(ot, "TRANSFORM_OT_translate");
226         RNA_enum_set(otmacro->ptr, "proportional", 0);
227         RNA_boolean_set(otmacro->ptr, "mirror", 0);
228 }
229
230 /* note mesh keymap also for other space? */
231 void ED_keymap_mesh(wmKeyConfig *keyconf)
232 {       
233         wmKeyMap *keymap;
234         wmKeyMapItem *kmi;
235         int i;
236         
237         keymap= WM_keymap_find(keyconf, "Mesh", 0, 0);
238         keymap->poll= ED_operator_editmesh;
239         
240         WM_keymap_add_item(keymap, "MESH_OT_loopcut_slide", RKEY, KM_PRESS, KM_CTRL, 0);
241
242         /* selecting */
243         /* standard mouse selection goes via space_view3d */
244         WM_keymap_add_item(keymap, "MESH_OT_loop_select", SELECTMOUSE, KM_PRESS, KM_ALT, 0);
245         kmi= WM_keymap_add_item(keymap, "MESH_OT_loop_select", SELECTMOUSE, KM_PRESS, KM_SHIFT|KM_ALT, 0);
246         RNA_boolean_set(kmi->ptr, "extend", 1);
247
248         WM_keymap_add_item(keymap, "MESH_OT_edgering_select", SELECTMOUSE, KM_PRESS, KM_ALT|KM_CTRL, 0);
249         kmi= WM_keymap_add_item(keymap, "MESH_OT_edgering_select", SELECTMOUSE, KM_PRESS, KM_SHIFT|KM_ALT|KM_CTRL, 0);
250         RNA_boolean_set(kmi->ptr, "extend", 1);
251
252         WM_keymap_add_item(keymap, "MESH_OT_select_shortest_path", SELECTMOUSE, KM_PRESS, KM_CTRL, 0);
253
254         WM_keymap_add_item(keymap, "MESH_OT_select_all", AKEY, KM_PRESS, 0, 0);
255         WM_keymap_add_item(keymap, "MESH_OT_select_more", PADPLUSKEY, KM_PRESS, KM_CTRL, 0);
256         WM_keymap_add_item(keymap, "MESH_OT_select_less", PADMINUS, KM_PRESS, KM_CTRL, 0);
257         WM_keymap_add_item(keymap, "MESH_OT_select_inverse", IKEY, KM_PRESS, KM_CTRL, 0);
258         WM_keymap_add_item(keymap, "MESH_OT_select_non_manifold", MKEY, KM_PRESS, (KM_CTRL|KM_SHIFT|KM_ALT), 0);
259         
260         WM_keymap_add_item(keymap, "MESH_OT_select_linked", LKEY, KM_PRESS, KM_CTRL, 0);
261         WM_keymap_add_item(keymap, "MESH_OT_select_linked_pick", LKEY, KM_PRESS, 0, 0);
262         RNA_boolean_set(WM_keymap_add_item(keymap, "MESH_OT_select_linked_pick", LKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "deselect", 1);
263         
264         WM_keymap_add_item(keymap, "MESH_OT_faces_select_linked_flat", FKEY, KM_PRESS, (KM_CTRL|KM_SHIFT|KM_ALT), 0);
265
266         WM_keymap_add_item(keymap, "MESH_OT_select_similar", GKEY, KM_PRESS, KM_SHIFT, 0);
267         
268         /* selection mode */
269         WM_keymap_add_menu(keymap, "VIEW3D_MT_edit_mesh_select_mode", TABKEY, KM_PRESS, KM_CTRL, 0);
270         
271         /* hide */
272         WM_keymap_add_item(keymap, "MESH_OT_hide", HKEY, KM_PRESS, 0, 0);
273         RNA_boolean_set(WM_keymap_add_item(keymap, "MESH_OT_hide", HKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "unselected", 1);
274         WM_keymap_add_item(keymap, "MESH_OT_reveal", HKEY, KM_PRESS, KM_ALT, 0);
275
276         /* tools */
277         WM_keymap_add_item(keymap, "MESH_OT_normals_make_consistent", NKEY, KM_PRESS, KM_CTRL, 0);
278         RNA_boolean_set(WM_keymap_add_item(keymap, "MESH_OT_normals_make_consistent", NKEY, KM_PRESS, KM_SHIFT|KM_CTRL, 0)->ptr, "inside", 1);
279         
280         WM_keymap_add_item(keymap, "VIEW3D_OT_edit_mesh_extrude_move_normal", EKEY, KM_PRESS, 0, 0); /* python operator */
281         WM_keymap_add_menu(keymap, "VIEW3D_MT_edit_mesh_extrude", EKEY, KM_PRESS, KM_ALT, 0);
282         
283         WM_keymap_add_item(keymap, "TRANSFORM_OT_edge_crease", EKEY, KM_PRESS, KM_SHIFT, 0);
284         
285         WM_keymap_add_item(keymap, "MESH_OT_spin", RKEY, KM_PRESS, KM_ALT, 0);
286         
287         WM_keymap_add_item(keymap, "MESH_OT_fill", FKEY, KM_PRESS, KM_ALT, 0);
288         WM_keymap_add_item(keymap, "MESH_OT_beautify_fill", FKEY, KM_PRESS, KM_SHIFT|KM_ALT, 0);
289         WM_keymap_add_item(keymap, "MESH_OT_quads_convert_to_tris", TKEY, KM_PRESS, KM_CTRL, 0);
290         WM_keymap_add_item(keymap, "MESH_OT_tris_convert_to_quads", JKEY, KM_PRESS, KM_ALT, 0);
291         WM_keymap_add_item(keymap, "MESH_OT_edge_flip", FKEY, KM_PRESS, KM_SHIFT|KM_CTRL, 0);
292
293         WM_keymap_add_item(keymap, "MESH_OT_rip_move",VKEY, KM_PRESS, 0, 0);
294         WM_keymap_add_item(keymap, "MESH_OT_merge", MKEY, KM_PRESS, KM_ALT, 0);
295
296         WM_keymap_add_item(keymap, "TRANSFORM_OT_shrink_fatten", SKEY, KM_PRESS, KM_ALT, 0);
297
298         /* add/remove */
299         WM_keymap_add_item(keymap, "MESH_OT_edge_face_add", FKEY, KM_PRESS, 0, 0);
300 //      WM_keymap_add_item(keymap, "MESH_OT_skin", FKEY, KM_PRESS, KM_CTRL|KM_ALT, 0); /* python, removed */
301         WM_keymap_add_item(keymap, "MESH_OT_duplicate_move", DKEY, KM_PRESS, KM_SHIFT, 0);
302         
303         WM_keymap_add_menu(keymap, "INFO_MT_mesh_add", AKEY, KM_PRESS, KM_SHIFT, 0);
304         
305         WM_keymap_add_item(keymap, "MESH_OT_separate", PKEY, KM_PRESS, 0, 0);
306         WM_keymap_add_item(keymap, "MESH_OT_split", YKEY, KM_PRESS, 0, 0);
307
308         /* use KM_CLICK because same key is used for tweaks */
309         WM_keymap_add_item(keymap, "MESH_OT_dupli_extrude_cursor", ACTIONMOUSE, KM_CLICK, KM_CTRL, 0);
310         RNA_boolean_set(WM_keymap_add_item(keymap, "MESH_OT_dupli_extrude_cursor", ACTIONMOUSE, KM_CLICK, KM_SHIFT|KM_CTRL, 0)->ptr, "rotate_source", 0);
311
312         WM_keymap_add_item(keymap, "MESH_OT_delete", XKEY, KM_PRESS, 0, 0);
313         WM_keymap_add_item(keymap, "MESH_OT_delete", DELKEY, KM_PRESS, 0, 0);
314         
315         WM_keymap_add_item(keymap, "MESH_OT_knife_cut", LEFTMOUSE, KM_PRESS, 0, KKEY);
316         RNA_enum_set(WM_keymap_add_item(keymap, "MESH_OT_knife_cut", LEFTMOUSE, KM_PRESS, KM_SHIFT, KKEY)->ptr, "type", 2/*KNIFE_MIDPOINT*/);
317         
318         WM_keymap_add_item(keymap, "OBJECT_OT_vertex_parent_set", PKEY, KM_PRESS, KM_CTRL, 0);
319
320         /* menus */
321         WM_keymap_add_menu(keymap, "VIEW3D_MT_edit_mesh_specials", WKEY, KM_PRESS, 0, 0);
322         WM_keymap_add_menu(keymap, "VIEW3D_MT_edit_mesh_faces", FKEY, KM_PRESS, KM_CTRL, 0);
323         WM_keymap_add_menu(keymap, "VIEW3D_MT_edit_mesh_edges", EKEY, KM_PRESS, KM_CTRL, 0);
324         WM_keymap_add_menu(keymap, "VIEW3D_MT_edit_mesh_vertices", VKEY, KM_PRESS, KM_CTRL, 0);
325         WM_keymap_add_menu(keymap, "VIEW3D_MT_hook", HKEY, KM_PRESS, KM_CTRL, 0);
326         WM_keymap_add_menu(keymap, "VIEW3D_MT_uv_map", UKEY, KM_PRESS, 0, 0);
327         WM_keymap_add_menu(keymap, "VIEW3D_MT_vertex_group", GKEY, KM_PRESS, KM_CTRL, 0);
328         
329         /* useful stuff from object-mode */
330         for (i=0; i<=5; i++) {
331                 kmi = WM_keymap_add_item(keymap, "OBJECT_OT_subdivision_set", ZEROKEY+i, KM_PRESS, KM_CTRL, 0);
332                 RNA_int_set(kmi->ptr, "level", i);
333         }
334         
335         ED_object_generic_keymap(keyconf, keymap, 3);
336 }
337