Sun position: add new Sky Texture elevation and rotation properties
[blender-addons.git] / mesh_auto_mirror.py
blob1d89d4e799c4bcfc2cd5de48fafacb7c10c99631
1 ######################################################################################################
2 # A simple add-on to auto cut in two and mirror an object #
3 # Actually partially uncommented (see further version) #
4 # Author: Lapineige, Bookyakuno #
5 # License: GPL v3 #
6 ######################################################################################################
7 # 2.8 update by Bookyakuno, meta-androcto
9 bl_info = {
10 "name": "Auto Mirror",
11 "description": "Super fast cutting and mirroring for mesh",
12 "author": "Lapineige",
13 "version": (2, 5, 3),
14 "blender": (2, 80, 0),
15 "location": "View 3D > Sidebar > Edit Tab > AutoMirror (panel)",
16 "warning": "",
17 "doc_url": "{BLENDER_MANUAL_URL}/addons/mesh/auto_mirror.html",
18 "category": "Mesh",
22 import bpy
23 from mathutils import Vector
25 import bmesh
26 import bpy
27 import collections
28 import mathutils
29 import math
30 from bpy_extras import view3d_utils
31 from bpy.types import (
32 Operator,
33 Menu,
34 Panel,
35 AddonPreferences,
36 PropertyGroup,
38 from bpy.props import (
39 BoolProperty,
40 EnumProperty,
41 FloatProperty,
42 IntProperty,
43 PointerProperty,
44 StringProperty,
48 # Operator
50 class AlignVertices(Operator):
52 """ Automatically cut an object along an axis """
54 bl_idname = "object.align_vertices"
55 bl_label = "Align Vertices on 1 Axis"
57 @classmethod
58 def poll(cls, context):
59 obj = context.active_object
60 return obj and obj.type == "MESH"
62 def execute(self, context):
63 automirror = context.scene.automirror
65 bpy.ops.object.mode_set(mode = 'OBJECT')
67 x1,y1,z1 = bpy.context.scene.cursor.location
68 bpy.ops.view3d.snap_cursor_to_selected()
70 x2,y2,z2 = bpy.context.scene.cursor.location
72 bpy.context.scene.cursor.location[0], \
73 bpy.context.scene.cursor.location[1], \
74 bpy.context.scene.cursor.location[2] = 0, 0, 0
76 #Vertices coordinate to 0 (local coordinate, so on the origin)
77 for vert in bpy.context.object.data.vertices:
78 if vert.select:
79 if automirror.axis == 'x':
80 axis = 0
81 elif automirror.axis == 'y':
82 axis = 1
83 elif automirror.axis == 'z':
84 axis = 2
85 vert.co[axis] = 0
87 bpy.context.scene.cursor.location = x2,y2,z2
89 bpy.ops.object.origin_set(type='ORIGIN_CURSOR')
91 bpy.context.scene.cursor.location = x1,y1,z1
93 bpy.ops.object.mode_set(mode = 'EDIT')
94 return {'FINISHED'}
97 class AutoMirror(bpy.types.Operator):
98 """ Automatically cut an object along an axis """
99 bl_idname = "object.automirror"
100 bl_label = "AutoMirror"
101 bl_options = {'REGISTER'} # 'UNDO' ?
103 @classmethod
104 def poll(cls, context):
105 obj = context.active_object
106 return obj and obj.type == "MESH"
108 def draw(self, context):
109 automirror = context.scene.automirror
111 layout = self.layout
112 if bpy.context.object and bpy.context.object.type == 'MESH':
113 layout.prop(automirror, "axis", text = "Mirror axis")
114 layout.prop(automirror, "orientation", text = "Orientation")
115 layout.prop(automirror, "threshold", text = "Threshold")
116 layout.prop(automirror, "toggle_edit", text = "Toggle edit")
117 layout.prop(automirror, "cut", text = "Cut and mirror")
118 if automirror.cut:
119 layout.prop(automirror, "clipping", text = "Clipping")
120 layout.prop(automirror, "mirror", text = "Apply mirror")
122 else:
123 layout.label(icon = "ERROR", text = "No mesh selected")
125 def get_local_axis_vector(self, context, X, Y, Z, orientation):
126 loc = context.object.location
127 bpy.ops.object.mode_set(mode = "OBJECT") # Needed to avoid to translate vertices
129 v1 = Vector((loc[0],loc[1],loc[2]))
130 bpy.ops.transform.translate(value = (X*orientation, Y*orientation, Z*orientation),
131 constraint_axis = ((X==1), (Y==1), (Z==1)),
132 orient_type = 'LOCAL')
133 v2 = Vector((loc[0],loc[1],loc[2]))
134 bpy.ops.transform.translate(value = (-X*orientation, -Y*orientation, -Z*orientation),
135 constraint_axis = ((X==1), (Y==1), (Z==1)),
136 orient_type = 'LOCAL')
138 bpy.ops.object.mode_set(mode="EDIT")
139 return v2-v1
141 def execute(self, context):
142 context.active_object.select_set(True)
144 automirror = context.scene.automirror
146 X,Y,Z = 0,0,0
148 if automirror.axis == 'x':
149 X = 1
150 elif automirror.axis == 'y':
151 Y = 1
152 elif automirror.axis == 'z':
153 Z = 1
155 current_mode = bpy.context.object.mode # Save the current mode
157 if bpy.context.object.mode != "EDIT":
158 bpy.ops.object.mode_set(mode = "EDIT") # Go to edit mode
160 bpy.ops.mesh.select_all(action = 'SELECT') # Select all the vertices
162 if automirror.orientation == 'positive':
163 orientation = 1
164 else:
165 orientation = -1
167 cut_normal = self.get_local_axis_vector(context, X, Y, Z, orientation)
169 # Cut the mesh
170 bpy.ops.mesh.bisect(
171 plane_co = (
172 bpy.context.object.location[0],
173 bpy.context.object.location[1],
174 bpy.context.object.location[2]
176 plane_no = cut_normal,
177 use_fill = False,
178 clear_inner = automirror.cut,
179 clear_outer = 0,
180 threshold = automirror.threshold
183 bpy.ops.object.align_vertices() # Use to align the vertices on the origin, needed by the "threshold"
185 if not automirror.toggle_edit:
186 bpy.ops.object.mode_set(mode = current_mode) # Reload previous mode
188 if automirror.cut:
189 bpy.ops.object.modifier_add(type = 'MIRROR') # Add a mirror modifier
190 bpy.context.object.modifiers[-1].use_axis[0] = X # Choose the axis to use, based on the cut's axis
191 bpy.context.object.modifiers[-1].use_axis[1] = Y
192 bpy.context.object.modifiers[-1].use_axis[2] = Z
193 bpy.context.object.modifiers[-1].use_clip = automirror.Use_Matcap
194 bpy.context.object.modifiers[-1].show_on_cage = automirror.show_on_cage
195 if automirror.apply_mirror:
196 bpy.ops.object.mode_set(mode = 'OBJECT')
197 bpy.ops.object.modifier_apply(apply_as = 'DATA',
198 modifier = bpy.context.object.modifiers[-1].name)
199 if automirror.toggle_edit:
200 bpy.ops.object.mode_set(mode = 'EDIT')
201 else:
202 bpy.ops.object.mode_set(mode = current_mode)
204 return {'FINISHED'}
207 # Panel
209 class VIEW3D_PT_BisectMirror(Panel):
210 bl_space_type = 'VIEW_3D'
211 bl_region_type = 'UI'
212 bl_label = "Auto Mirror"
213 bl_category = 'Edit'
214 bl_options = {'DEFAULT_CLOSED'}
216 def draw(self, context):
217 automirror = context.scene.automirror
219 layout = self.layout
220 col = layout.column(align=True)
222 layout = self.layout
224 if bpy.context.object and bpy.context.object.type == 'MESH':
225 layout.operator("object.automirror")
226 layout.prop(automirror, "axis", text = "Mirror Axis", expand=True)
227 layout.prop(automirror, "orientation", text = "Orientation")
228 layout.prop(automirror, "threshold", text = "Threshold")
229 layout.prop(automirror, "toggle_edit", text = "Toggle Edit")
230 layout.prop(automirror, "cut", text = "Cut and Mirror")
231 if bpy.context.scene.automirror.cut:
232 layout.prop(automirror, "Use_Matcap", text = "Use Clip")
233 layout.prop(automirror, "show_on_cage", text = "Editable")
234 layout.prop(automirror, "apply_mirror", text = "Apply Mirror")
236 else:
237 layout.label(icon="ERROR", text = "No mesh selected")
239 # Properties
240 class AutoMirrorProps(PropertyGroup):
241 axis : EnumProperty(
242 items = [("x", "X", "", 1),
243 ("y", "Y", "", 2),
244 ("z", "Z", "", 3)],
245 description="Axis used by the mirror modifier",
248 orientation : EnumProperty(
249 items = [("positive", "Positive", "", 1),("negative", "Negative", "", 2)],
250 description="Choose the side along the axis of the editable part (+/- coordinates)",
253 threshold : FloatProperty(
254 default= 0.001, min= 0.001,
255 description="Vertices closer than this distance are merged on the loopcut",
258 toggle_edit : BoolProperty(
259 default= False,
260 description="If not in edit mode, change mode to edit",
263 cut : BoolProperty(
264 default= True,
265 description="If enabeled, cut the mesh in two parts and mirror it. If not, just make a loopcut",
268 clipping : BoolProperty(
269 default=True,
272 Use_Matcap : BoolProperty(
273 default=True,
274 description="Use clipping for the mirror modifier",
277 show_on_cage : BoolProperty(
278 default=False,
279 description="Enable to edit the cage (it's the classical modifier's option)",
282 apply_mirror : BoolProperty(
283 description="Apply the mirror modifier (useful to symmetrise the mesh)",
287 # Add-ons Preferences Update Panel
289 # Define Panel classes for updating
290 panels = (
291 VIEW3D_PT_BisectMirror,
295 def update_panel(self, context):
296 message = ": Updating Panel locations has failed"
297 try:
298 for panel in panels:
299 if "bl_rna" in panel.__dict__:
300 bpy.utils.unregister_class(panel)
302 for panel in panels:
303 panel.bl_category = context.preferences.addons[__name__].preferences.category
304 bpy.utils.register_class(panel)
306 except Exception as e:
307 print("\n[{}]\n{}\n\nError:\n{}".format(__name__, message, e))
308 pass
311 class AutoMirrorAddonPreferences(AddonPreferences):
312 # this must match the addon name, use '__package__'
313 # when defining this in a submodule of a python package.
314 bl_idname = __name__
316 category: StringProperty(
317 name = "Tab Category",
318 description = "Choose a name for the category of the panel",
319 default = "Edit",
320 update = update_panel
323 def draw(self, context):
324 layout = self.layout
326 row = layout.row()
327 col = row.column()
328 col.label(text = "Tab Category:")
329 col.prop(self, "category", text = "")
331 # define classes for registration
332 classes = (
333 VIEW3D_PT_BisectMirror,
334 AutoMirror,
335 AlignVertices,
336 AutoMirrorAddonPreferences,
337 AutoMirrorProps,
341 # registering and menu integration
342 def register():
343 for cls in classes:
344 bpy.utils.register_class(cls)
346 bpy.types.Scene.automirror = PointerProperty(type = AutoMirrorProps)
347 update_panel(None, bpy.context)
349 # unregistering and removing menus
350 def unregister():
351 for cls in reversed(classes):
352 bpy.utils.unregister_class(cls)
354 del bpy.types.Scene.automirror
356 if __name__ == "__main__":
357 register()