Fix Blenderplayer (c)
[blender.git] / release / scripts / startup / bl_operators / view3d.py
1 # ##### BEGIN GPL LICENSE BLOCK #####
2 #
3 #  This program is free software; you can redistribute it and/or
4 #  modify it under the terms of the GNU General Public License
5 #  as published by the Free Software Foundation; either version 2
6 #  of the License, or (at your option) any later version.
7 #
8 #  This program is distributed in the hope that it will be useful,
9 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 #  GNU General Public License for more details.
12 #
13 #  You should have received a copy of the GNU General Public License
14 #  along with this program; if not, write to the Free Software Foundation,
15 #  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 #
17 # ##### END GPL LICENSE BLOCK #####
18
19 # <pep8-80 compliant>
20
21 import bpy
22 from bpy.types import Operator
23 from bpy.props import BoolProperty
24
25
26 class VIEW3D_OT_edit_mesh_extrude_individual_move(Operator):
27     "Extrude individual elements and move"
28     bl_label = "Extrude Individual and Move"
29     bl_idname = "view3d.edit_mesh_extrude_individual_move"
30
31     @classmethod
32     def poll(cls, context):
33         obj = context.active_object
34         return (obj is not None and obj.mode == 'EDIT')
35
36     def execute(self, context):
37         mesh = context.object.data
38         select_mode = context.tool_settings.mesh_select_mode
39
40         totface = mesh.total_face_sel
41         totedge = mesh.total_edge_sel
42         #~ totvert = mesh.total_vert_sel
43
44         if select_mode[2] and totface == 1:
45             bpy.ops.mesh.extrude_region_move('INVOKE_REGION_WIN',
46                     TRANSFORM_OT_translate={
47                         "constraint_orientation": 'NORMAL',
48                         "constraint_axis": (False, False, True)})
49         elif select_mode[2] and totface > 1:
50             bpy.ops.mesh.extrude_faces_move('INVOKE_REGION_WIN')
51         elif select_mode[1] and totedge >= 1:
52             bpy.ops.mesh.extrude_edges_move('INVOKE_REGION_WIN')
53         else:
54             bpy.ops.mesh.extrude_vertices_move('INVOKE_REGION_WIN')
55
56         # ignore return from operators above because they are 'RUNNING_MODAL',
57         # and cause this one not to be freed. [#24671]
58         return {'FINISHED'}
59
60     def invoke(self, context, event):
61         return self.execute(context)
62
63
64 class VIEW3D_OT_edit_mesh_extrude_move(Operator):
65     "Extrude and move along normals"
66     bl_label = "Extrude and Move on Normals"
67     bl_idname = "view3d.edit_mesh_extrude_move_normal"
68
69     @classmethod
70     def poll(cls, context):
71         obj = context.active_object
72         return (obj is not None and obj.mode == 'EDIT')
73
74     @staticmethod
75     def extrude_region(context, use_vert_normals):
76         mesh = context.object.data
77
78         totface = mesh.total_face_sel
79         totedge = mesh.total_edge_sel
80         #~ totvert = mesh.total_vert_sel
81
82         if totface >= 1:
83             if use_vert_normals:
84                 bpy.ops.mesh.extrude_region_shrink_fatten('INVOKE_REGION_WIN',
85                         TRANSFORM_OT_shrink_fatten={})
86             else:
87                 bpy.ops.mesh.extrude_region_move('INVOKE_REGION_WIN',
88                         TRANSFORM_OT_translate={
89                             "constraint_orientation": 'NORMAL',
90                             "constraint_axis": (False, False, True)})
91
92         elif totedge == 1:
93             bpy.ops.mesh.extrude_region_move('INVOKE_REGION_WIN',
94                     TRANSFORM_OT_translate={
95                         "constraint_orientation": 'NORMAL',
96                         # not a popular choice, too restrictive for retopo.
97                         #~ "constraint_axis": (True, True, False)})
98                         "constraint_axis": (False, False, False)})
99         else:
100             bpy.ops.mesh.extrude_region_move('INVOKE_REGION_WIN')
101
102         # ignore return from operators above because they are 'RUNNING_MODAL',
103         # and cause this one not to be freed. [#24671]
104         return {'FINISHED'}
105
106     def execute(self, context):
107         return VIEW3D_OT_edit_mesh_extrude_move.extrude_region(context, False)
108
109     def invoke(self, context, event):
110         return self.execute(context)
111
112
113 class VIEW3D_OT_edit_mesh_extrude_shrink_fatten(Operator):
114     "Extrude and move along individual normals"
115     bl_label = "Extrude and Move on Individual Normals"
116     bl_idname = "view3d.edit_mesh_extrude_move_shrink_fatten"
117
118     @classmethod
119     def poll(cls, context):
120         obj = context.active_object
121         return (obj is not None and obj.mode == 'EDIT')
122
123     def execute(self, context):
124         return VIEW3D_OT_edit_mesh_extrude_move.extrude_region(context, True)
125
126     def invoke(self, context, event):
127         return self.execute(context)
128
129
130 class VIEW3D_OT_select_or_deselect_all(Operator):
131     "Select element under the mouse, deselect everything is there's nothing under the mouse"
132     bl_label = "Select or Deselect All"
133     bl_idname = "view3d.select_or_deselect_all"
134     bl_options = {'UNDO'}
135
136     extend = BoolProperty(
137             name="Extend",
138             description="Extend selection instead of deselecting everything first",
139             default=False,
140             )
141
142     toggle = BoolProperty(
143             name="Toggle",
144             description="Toggle the selection",
145             default=False,
146             )
147
148     deselect = BoolProperty(
149             name="Deselect",
150             description="Remove from selection",
151             default=False,
152             )
153
154     center = BoolProperty(
155             name="Center",
156             description="Use the object center when selecting, in editmode used to extend object selection",
157             default=False,
158             )
159
160     enumerate = BoolProperty(
161             name="Enumerate",
162             description="List objects under the mouse (object mode only)",
163             default=False,
164             )
165
166     object = BoolProperty(
167             name="Object",
168             description="Use object selection (editmode only)",
169             default=False,
170             )
171
172     @classmethod
173     def poll(cls, context):
174         active_object = context.active_object
175         if active_object:
176             return active_object.mode in {'EDIT', 'OBJECT', 'POSE'}
177         return True
178
179     def invoke(self, context, event):
180         x = event.mouse_region_x
181         y = event.mouse_region_y
182
183         if self.extend is False and self.toggle is False and self.deselect is False:
184             active_object = context.active_object
185
186             if active_object:
187                 if active_object.mode == 'EDIT':
188                     if active_object.type == 'MESH':
189                         bpy.ops.mesh.select_all(action='DESELECT')
190                     elif active_object.type == 'CURVE':
191                         bpy.ops.curve.select_all(action='DESELECT')
192                     elif active_object.type == 'SURFACE':
193                         bpy.ops.curve.select_all(action='DESELECT')
194                     elif active_object.type == 'LATTICE':
195                         bpy.ops.lattice.select_all(action='DESELECT')
196                     elif active_object.type == 'META':
197                         bpy.ops.mball.select_all(action='DESELECT')
198                     elif active_object.type == 'ARMATURE':
199                         bpy.ops.armature.select_all(action='DESELECT')
200                 elif active_object.mode == 'POSE':
201                     bpy.ops.pose.select_all(action='DESELECT')
202                 elif active_object.mode == 'PARTICLE_EDIT':
203                     bpy.ops.particle.select_all(action='DESELECT')
204                 else:
205                     bpy.ops.object.select_all(action='DESELECT')
206             else:
207                 bpy.ops.object.select_all(action='DESELECT')
208
209         return bpy.ops.view3d.select(extend=self.extend,
210                                      deselect=self.deselect,
211                                      toggle=self.toggle,
212                                      center=self.center,
213                                      enumerate=self.enumerate,
214                                      object=self.object,
215                                      location=(x, y))
216
217
218 classes = (
219     VIEW3D_OT_edit_mesh_extrude_individual_move,
220     VIEW3D_OT_edit_mesh_extrude_move,
221     VIEW3D_OT_edit_mesh_extrude_shrink_fatten,
222     VIEW3D_OT_select_or_deselect_all,
223 )