Add convex hull operator (bmesh operator and wm operator.)
[blender.git] / source / blender / editors / mesh / mesh_ops.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2009 Blender Foundation.
19  * All rights reserved.
20  *
21  * 
22  * Contributor(s): Blender Foundation
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/editors/mesh/mesh_ops.c
28  *  \ingroup edmesh
29  */
30
31
32 #include <stdlib.h>
33 #include <math.h>
34
35
36 #include "DNA_object_types.h"
37 #include "DNA_scene_types.h"
38
39 #include "BLI_math.h"
40 #include "BLI_blenlib.h"
41
42 #include "BKE_context.h"
43
44 #include "RNA_access.h"
45
46 #include "WM_api.h"
47 #include "WM_types.h"
48
49 #include "ED_object.h"
50 #include "ED_mesh.h"
51 #include "ED_screen.h"
52 #include "ED_view3d.h"
53
54
55 #include "mesh_intern.h"
56
57 /**************************** registration **********************************/
58
59 void ED_operatortypes_mesh(void)
60 {
61         WM_operatortype_append(MESH_OT_select_all);
62         WM_operatortype_append(MESH_OT_select_interior_faces);
63         WM_operatortype_append(MESH_OT_select_more);
64         WM_operatortype_append(MESH_OT_select_less);
65         WM_operatortype_append(MESH_OT_select_non_manifold);
66         WM_operatortype_append(MESH_OT_select_linked);
67         WM_operatortype_append(MESH_OT_select_linked_pick);
68         WM_operatortype_append(MESH_OT_select_random);
69         WM_operatortype_append(MESH_OT_hide);
70         WM_operatortype_append(MESH_OT_reveal);
71         WM_operatortype_append(MESH_OT_select_by_number_vertices);
72         WM_operatortype_append(MESH_OT_select_loose_verts);
73         WM_operatortype_append(MESH_OT_select_mirror);
74         WM_operatortype_append(MESH_OT_normals_make_consistent);
75         WM_operatortype_append(MESH_OT_merge);
76         WM_operatortype_append(MESH_OT_subdivide);
77         WM_operatortype_append(MESH_OT_faces_select_linked_flat);
78         WM_operatortype_append(MESH_OT_edges_select_sharp);
79         WM_operatortype_append(MESH_OT_primitive_plane_add);
80         WM_operatortype_append(MESH_OT_primitive_cube_add);
81         WM_operatortype_append(MESH_OT_primitive_circle_add);
82         WM_operatortype_append(MESH_OT_primitive_cylinder_add);
83         WM_operatortype_append(MESH_OT_primitive_cone_add);
84         WM_operatortype_append(MESH_OT_primitive_grid_add);
85         WM_operatortype_append(MESH_OT_primitive_monkey_add);
86         WM_operatortype_append(MESH_OT_primitive_uv_sphere_add);
87         WM_operatortype_append(MESH_OT_primitive_ico_sphere_add);
88         WM_operatortype_append(MESH_OT_duplicate);
89         WM_operatortype_append(MESH_OT_remove_doubles);
90         WM_operatortype_append(MESH_OT_vertices_sort);
91         WM_operatortype_append(MESH_OT_vertices_randomize);
92         WM_operatortype_append(MESH_OT_spin);
93         WM_operatortype_append(MESH_OT_screw);
94
95         WM_operatortype_append(MESH_OT_extrude_region);
96         WM_operatortype_append(MESH_OT_extrude_faces_indiv);
97         WM_operatortype_append(MESH_OT_extrude_edges_indiv);
98         WM_operatortype_append(MESH_OT_extrude_verts_indiv);
99
100         WM_operatortype_append(MESH_OT_split);
101         WM_operatortype_append(MESH_OT_extrude_repeat);
102         WM_operatortype_append(MESH_OT_edge_rotate);
103         WM_operatortype_append(MESH_OT_select_vertex_path);
104         WM_operatortype_append(MESH_OT_loop_to_region);
105         WM_operatortype_append(MESH_OT_region_to_loop);
106         WM_operatortype_append(MESH_OT_select_axis);
107         
108         WM_operatortype_append(MESH_OT_uvs_rotate);
109         WM_operatortype_append(MESH_OT_uvs_reverse);
110         WM_operatortype_append(MESH_OT_colors_rotate);
111         WM_operatortype_append(MESH_OT_colors_reverse);
112         
113         WM_operatortype_append(MESH_OT_fill);
114         WM_operatortype_append(MESH_OT_beautify_fill);
115         WM_operatortype_append(MESH_OT_quads_convert_to_tris);
116         WM_operatortype_append(MESH_OT_tris_convert_to_quads);
117         WM_operatortype_append(MESH_OT_dissolve);
118         WM_operatortype_append(MESH_OT_dissolve_limited);
119         WM_operatortype_append(MESH_OT_faces_shade_smooth);
120         WM_operatortype_append(MESH_OT_faces_shade_flat);
121         WM_operatortype_append(MESH_OT_sort_faces);
122
123         WM_operatortype_append(MESH_OT_delete);
124         WM_operatortype_append(MESH_OT_edge_collapse);
125         WM_operatortype_append(MESH_OT_edge_collapse_loop);
126
127         WM_operatortype_append(MESH_OT_separate);
128         WM_operatortype_append(MESH_OT_dupli_extrude_cursor);
129         WM_operatortype_append(MESH_OT_loop_select);
130         WM_operatortype_append(MESH_OT_edge_face_add);
131         WM_operatortype_append(MESH_OT_select_shortest_path);
132         WM_operatortype_append(MESH_OT_select_similar);
133         WM_operatortype_append(MESH_OT_loop_multi_select);
134         WM_operatortype_append(MESH_OT_mark_seam);
135         WM_operatortype_append(MESH_OT_mark_sharp);
136         WM_operatortype_append(MESH_OT_vertices_smooth);
137         WM_operatortype_append(MESH_OT_noise);
138         WM_operatortype_append(MESH_OT_flip_normals);
139         //WM_operatortype_append(MESH_OT_knife_cut);
140         WM_operatortype_append(MESH_OT_rip);
141         WM_operatortype_append(MESH_OT_blend_from_shape);
142         WM_operatortype_append(MESH_OT_shape_propagate_to_all);
143         
144         WM_operatortype_append(MESH_OT_uv_texture_add);
145         WM_operatortype_append(MESH_OT_uv_texture_remove);
146         WM_operatortype_append(MESH_OT_vertex_color_add);
147         WM_operatortype_append(MESH_OT_vertex_color_remove);
148         WM_operatortype_append(MESH_OT_sticky_add);
149         WM_operatortype_append(MESH_OT_sticky_remove);
150         WM_operatortype_append(MESH_OT_drop_named_image);
151
152         WM_operatortype_append(MESH_OT_edgering_select);
153         WM_operatortype_append(MESH_OT_loopcut);
154
155         WM_operatortype_append(MESH_OT_solidify);
156         WM_operatortype_append(MESH_OT_select_nth);
157         WM_operatortype_append(MESH_OT_vert_connect);
158         WM_operatortype_append(MESH_OT_vert_slide);
159         WM_operatortype_append(MESH_OT_knife_tool);
160
161         WM_operatortype_append(MESH_OT_bevel);
162
163         WM_operatortype_append(MESH_OT_select_next_loop);
164
165         WM_operatortype_append(MESH_OT_bridge_edge_loops);
166         WM_operatortype_append(MESH_OT_inset);
167         WM_operatortype_append(MESH_OT_wireframe);
168         WM_operatortype_append(MESH_OT_edge_split);
169
170         WM_operatortype_append(MESH_OT_convex_hull);
171
172 #ifdef WITH_GAMEENGINE
173         WM_operatortype_append(MESH_OT_navmesh_make);
174         WM_operatortype_append(MESH_OT_navmesh_face_copy);
175         WM_operatortype_append(MESH_OT_navmesh_face_add);
176         WM_operatortype_append(MESH_OT_navmesh_reset);
177         WM_operatortype_append(MESH_OT_navmesh_clear);
178 #endif
179 }
180
181 #if 0 /* UNUSED, remove? */
182 static int ED_operator_editmesh_face_select(bContext *C)
183 {
184         Object *obedit = CTX_data_edit_object(C);
185         if (obedit && obedit->type == OB_MESH) {
186                 BMEditMesh *em = BMEdit_FromObject(obedit);
187                 if (em && em->selectmode & SCE_SELECT_FACE) {
188                         return 1;
189                 }
190         }
191         return 0;
192 }
193 #endif
194
195 void ED_operatormacros_mesh(void)
196 {
197         wmOperatorType *ot;
198         wmOperatorTypeMacro *otmacro;
199         
200         ot = WM_operatortype_append_macro("MESH_OT_loopcut_slide", "Loop Cut and Slide", OPTYPE_UNDO | OPTYPE_REGISTER);
201         ot->description = "Cut mesh loop and slide it";
202         WM_operatortype_macro_define(ot, "MESH_OT_loopcut");
203         otmacro = WM_operatortype_macro_define(ot, "TRANSFORM_OT_edge_slide");
204         RNA_struct_idprops_unset(otmacro->ptr, "release_confirm");
205
206         ot = WM_operatortype_append_macro("MESH_OT_duplicate_move", "Add Duplicate", OPTYPE_UNDO | OPTYPE_REGISTER);
207         ot->description = "Duplicate mesh and move";
208         WM_operatortype_macro_define(ot, "MESH_OT_duplicate");
209         otmacro = WM_operatortype_macro_define(ot, "TRANSFORM_OT_translate");
210         RNA_enum_set(otmacro->ptr, "proportional", 0);
211         RNA_boolean_set(otmacro->ptr, "mirror", FALSE);
212
213         ot = WM_operatortype_append_macro("MESH_OT_rip_move", "Rip", OPTYPE_UNDO | OPTYPE_REGISTER);
214         ot->description = "Rip polygons and move the result";
215         WM_operatortype_macro_define(ot, "MESH_OT_rip");
216         otmacro = WM_operatortype_macro_define(ot, "TRANSFORM_OT_translate");
217         RNA_enum_set(otmacro->ptr, "proportional", 0);
218         RNA_boolean_set(otmacro->ptr, "mirror", FALSE);
219
220         ot = WM_operatortype_append_macro("MESH_OT_extrude_region_move", "Extrude Region and Move", OPTYPE_UNDO | OPTYPE_REGISTER);
221         ot->description = "Extrude region and move result";
222         otmacro = WM_operatortype_macro_define(ot, "MESH_OT_extrude_region");
223         otmacro = WM_operatortype_macro_define(ot, "TRANSFORM_OT_translate");
224         RNA_enum_set(otmacro->ptr, "proportional", 0);
225         RNA_boolean_set(otmacro->ptr, "mirror", FALSE);
226
227         ot = WM_operatortype_append_macro("MESH_OT_extrude_faces_move", "Extrude Individual Faces and Move", OPTYPE_UNDO | OPTYPE_REGISTER);
228         ot->description = "Extrude faces and move result";
229         otmacro = WM_operatortype_macro_define(ot, "MESH_OT_extrude_faces_indiv");
230         otmacro = WM_operatortype_macro_define(ot, "TRANSFORM_OT_shrink_fatten");
231         RNA_enum_set(otmacro->ptr, "proportional", 0);
232         RNA_boolean_set(otmacro->ptr, "mirror", FALSE);
233
234         ot = WM_operatortype_append_macro("MESH_OT_extrude_edges_move", "Extrude Only Edges and Move", OPTYPE_UNDO | OPTYPE_REGISTER);
235         ot->description = "Extrude edges and move result";
236         otmacro = WM_operatortype_macro_define(ot, "MESH_OT_extrude_edges_indiv");
237         otmacro = WM_operatortype_macro_define(ot, "TRANSFORM_OT_translate");
238         RNA_enum_set(otmacro->ptr, "proportional", 0);
239         RNA_boolean_set(otmacro->ptr, "mirror", FALSE);
240
241         ot = WM_operatortype_append_macro("MESH_OT_extrude_vertices_move", "Extrude Only Vertices and Move", OPTYPE_UNDO | OPTYPE_REGISTER);
242         ot->description = "Extrude vertices and move result";
243         otmacro = WM_operatortype_macro_define(ot, "MESH_OT_extrude_verts_indiv");
244         otmacro = WM_operatortype_macro_define(ot, "TRANSFORM_OT_translate");
245         RNA_enum_set(otmacro->ptr, "proportional", 0);
246         RNA_boolean_set(otmacro->ptr, "mirror", FALSE);
247 }
248
249 /* note mesh keymap also for other space? */
250 void ED_keymap_mesh(wmKeyConfig *keyconf)
251 {       
252         wmKeyMap *keymap;
253         wmKeyMapItem *kmi;
254         int i;
255         
256         keymap = WM_keymap_find(keyconf, "Mesh", 0, 0);
257         keymap->poll = ED_operator_editmesh;
258         
259         WM_keymap_add_item(keymap, "MESH_OT_loopcut_slide", RKEY, KM_PRESS, KM_CTRL, 0);
260
261         /* selecting */
262         /* standard mouse selection goes via space_view3d */
263         kmi = WM_keymap_add_item(keymap, "MESH_OT_loop_select", SELECTMOUSE, KM_PRESS, KM_ALT, 0);
264         RNA_boolean_set(kmi->ptr, "extend", FALSE);
265         kmi = WM_keymap_add_item(keymap, "MESH_OT_loop_select", SELECTMOUSE, KM_PRESS, KM_SHIFT | KM_ALT, 0);
266         RNA_boolean_set(kmi->ptr, "extend", TRUE);
267
268         kmi = WM_keymap_add_item(keymap, "MESH_OT_edgering_select", SELECTMOUSE, KM_PRESS, KM_ALT | KM_CTRL, 0);
269         RNA_boolean_set(kmi->ptr, "extend", FALSE);
270         kmi = WM_keymap_add_item(keymap, "MESH_OT_edgering_select", SELECTMOUSE, KM_PRESS, KM_SHIFT | KM_ALT | KM_CTRL, 0);
271         RNA_boolean_set(kmi->ptr, "extend", TRUE);
272
273         WM_keymap_add_item(keymap, "MESH_OT_select_shortest_path", SELECTMOUSE, KM_PRESS, KM_CTRL, 0);
274
275         kmi = WM_keymap_add_item(keymap, "MESH_OT_select_all", AKEY, KM_PRESS, 0, 0);
276         RNA_enum_set(kmi->ptr, "action", SEL_TOGGLE);
277         kmi = WM_keymap_add_item(keymap, "MESH_OT_select_all", IKEY, KM_PRESS, KM_CTRL, 0);
278         RNA_enum_set(kmi->ptr, "action", SEL_INVERT);
279
280         WM_keymap_add_item(keymap, "MESH_OT_select_more", PADPLUSKEY, KM_PRESS, KM_CTRL, 0);
281         WM_keymap_add_item(keymap, "MESH_OT_select_less", PADMINUS, KM_PRESS, KM_CTRL, 0);
282         WM_keymap_add_item(keymap, "MESH_OT_select_non_manifold", MKEY, KM_PRESS, (KM_CTRL | KM_SHIFT | KM_ALT), 0);
283         
284         WM_keymap_add_item(keymap, "MESH_OT_select_linked", LKEY, KM_PRESS, KM_CTRL, 0);
285         kmi = WM_keymap_add_item(keymap, "MESH_OT_select_linked_pick", LKEY, KM_PRESS, 0, 0);
286         RNA_boolean_set(kmi->ptr, "deselect", FALSE);
287         kmi = WM_keymap_add_item(keymap, "MESH_OT_select_linked_pick", LKEY, KM_PRESS, KM_SHIFT, 0);
288         RNA_boolean_set(kmi->ptr, "deselect", TRUE);
289         
290         WM_keymap_add_item(keymap, "MESH_OT_faces_select_linked_flat", FKEY, KM_PRESS, (KM_CTRL | KM_SHIFT | KM_ALT), 0);
291
292         WM_keymap_add_item(keymap, "MESH_OT_select_similar", GKEY, KM_PRESS, KM_SHIFT, 0);
293         
294         /* selection mode */
295         WM_keymap_add_menu(keymap, "VIEW3D_MT_edit_mesh_select_mode", TABKEY, KM_PRESS, KM_CTRL, 0);
296         
297         /* hide */
298         kmi = WM_keymap_add_item(keymap, "MESH_OT_hide", HKEY, KM_PRESS, 0, 0);
299         RNA_boolean_set(kmi->ptr, "unselected", FALSE);
300         kmi = WM_keymap_add_item(keymap, "MESH_OT_hide", HKEY, KM_PRESS, KM_SHIFT, 0);
301         RNA_boolean_set(kmi->ptr, "unselected", TRUE);
302         WM_keymap_add_item(keymap, "MESH_OT_reveal", HKEY, KM_PRESS, KM_ALT, 0);
303
304         /* tools */
305         kmi = WM_keymap_add_item(keymap, "MESH_OT_normals_make_consistent", NKEY, KM_PRESS, KM_CTRL, 0);
306         RNA_boolean_set(kmi->ptr, "inside", FALSE);
307         kmi = WM_keymap_add_item(keymap, "MESH_OT_normals_make_consistent", NKEY, KM_PRESS, KM_SHIFT | KM_CTRL, 0);
308         RNA_boolean_set(kmi->ptr, "inside", TRUE);
309         
310         WM_keymap_add_item(keymap, "VIEW3D_OT_edit_mesh_extrude_move_normal", EKEY, KM_PRESS, 0, 0); /* python operator */
311         WM_keymap_add_menu(keymap, "VIEW3D_MT_edit_mesh_extrude", EKEY, KM_PRESS, KM_ALT, 0);
312         
313         WM_keymap_add_item(keymap, "TRANSFORM_OT_edge_crease", EKEY, KM_PRESS, KM_SHIFT, 0);
314         
315         WM_keymap_add_item(keymap, "MESH_OT_spin", RKEY, KM_PRESS, KM_ALT, 0);
316         
317         WM_keymap_add_item(keymap, "MESH_OT_fill", FKEY, KM_PRESS, KM_ALT, 0);
318         WM_keymap_add_item(keymap, "MESH_OT_beautify_fill", FKEY, KM_PRESS, KM_SHIFT | KM_ALT, 0);
319
320         kmi = WM_keymap_add_item(keymap, "MESH_OT_quads_convert_to_tris", TKEY, KM_PRESS, KM_CTRL, 0);
321         RNA_boolean_set(kmi->ptr, "use_beauty", TRUE);
322         kmi = WM_keymap_add_item(keymap, "MESH_OT_quads_convert_to_tris", TKEY, KM_PRESS, KM_CTRL | KM_SHIFT, 0);
323         RNA_boolean_set(kmi->ptr, "use_beauty", FALSE);
324
325         WM_keymap_add_item(keymap, "MESH_OT_tris_convert_to_quads", JKEY, KM_PRESS, KM_ALT, 0);
326
327         WM_keymap_add_item(keymap, "MESH_OT_rip_move", VKEY, KM_PRESS, 0, 0);
328         WM_keymap_add_item(keymap, "MESH_OT_merge", MKEY, KM_PRESS, KM_ALT, 0);
329
330         WM_keymap_add_item(keymap, "TRANSFORM_OT_shrink_fatten", SKEY, KM_PRESS, KM_ALT, 0);
331
332         /* add/remove */
333         WM_keymap_add_item(keymap, "MESH_OT_edge_face_add", FKEY, KM_PRESS, 0, 0);
334 //      WM_keymap_add_item(keymap, "MESH_OT_skin", FKEY, KM_PRESS, KM_CTRL|KM_ALT, 0); /* python, removed */
335         WM_keymap_add_item(keymap, "MESH_OT_duplicate_move", DKEY, KM_PRESS, KM_SHIFT, 0);
336         
337         WM_keymap_add_menu(keymap, "INFO_MT_mesh_add", AKEY, KM_PRESS, KM_SHIFT, 0);
338         
339         WM_keymap_add_item(keymap, "MESH_OT_separate", PKEY, KM_PRESS, 0, 0);
340         WM_keymap_add_item(keymap, "MESH_OT_split", YKEY, KM_PRESS, 0, 0);
341         WM_keymap_add_item(keymap, "MESH_OT_vert_connect", JKEY, KM_PRESS, 0, 0);
342
343         /* Vertex Slide */
344         WM_keymap_add_item(keymap, "MESH_OT_vert_slide", VKEY, KM_PRESS, KM_SHIFT, 0);
345         /* use KM_CLICK because same key is used for tweaks */
346         kmi = WM_keymap_add_item(keymap, "MESH_OT_dupli_extrude_cursor", ACTIONMOUSE, KM_CLICK, KM_CTRL, 0);
347         RNA_boolean_set(kmi->ptr, "rotate_source", TRUE);
348         kmi = WM_keymap_add_item(keymap, "MESH_OT_dupli_extrude_cursor", ACTIONMOUSE, KM_CLICK, KM_SHIFT | KM_CTRL, 0);
349         RNA_boolean_set(kmi->ptr, "rotate_source", FALSE);
350
351         WM_keymap_add_menu(keymap, "VIEW3D_MT_edit_mesh_delete", XKEY, KM_PRESS, 0, 0);
352         WM_keymap_add_menu(keymap, "VIEW3D_MT_edit_mesh_delete", DELKEY, KM_PRESS, 0, 0);
353         
354         kmi = WM_keymap_add_item(keymap, "MESH_OT_knife_tool", KKEY, KM_PRESS, 0, 0);
355         RNA_boolean_set(kmi->ptr, "use_occlude_geometry", TRUE);
356         RNA_boolean_set(kmi->ptr, "only_select",          FALSE);
357
358         kmi = WM_keymap_add_item(keymap, "MESH_OT_knife_tool", KKEY, KM_PRESS, KM_SHIFT, 0);
359         RNA_boolean_set(kmi->ptr, "use_occlude_geometry", FALSE);
360         RNA_boolean_set(kmi->ptr, "only_select",          TRUE);
361         
362         WM_keymap_add_item(keymap, "OBJECT_OT_vertex_parent_set", PKEY, KM_PRESS, KM_CTRL, 0);
363
364         /* menus */
365         WM_keymap_add_menu(keymap, "VIEW3D_MT_edit_mesh_specials", WKEY, KM_PRESS, 0, 0);
366         WM_keymap_add_menu(keymap, "VIEW3D_MT_edit_mesh_faces", FKEY, KM_PRESS, KM_CTRL, 0);
367         WM_keymap_add_menu(keymap, "VIEW3D_MT_edit_mesh_edges", EKEY, KM_PRESS, KM_CTRL, 0);
368         WM_keymap_add_menu(keymap, "VIEW3D_MT_edit_mesh_vertices", VKEY, KM_PRESS, KM_CTRL, 0);
369         WM_keymap_add_menu(keymap, "VIEW3D_MT_hook", HKEY, KM_PRESS, KM_CTRL, 0);
370         WM_keymap_add_menu(keymap, "VIEW3D_MT_uv_map", UKEY, KM_PRESS, 0, 0);
371         WM_keymap_add_menu(keymap, "VIEW3D_MT_vertex_group", GKEY, KM_PRESS, KM_CTRL, 0);
372         
373         /* useful stuff from object-mode */
374         for (i = 0; i <= 5; i++) {
375                 kmi = WM_keymap_add_item(keymap, "OBJECT_OT_subdivision_set", ZEROKEY + i, KM_PRESS, KM_CTRL, 0);
376                 RNA_int_set(kmi->ptr, "level", i);
377         }
378         
379         ED_object_generic_keymap(keyconf, keymap, 3);
380         knifetool_modal_keymap(keyconf);
381 }
382