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