From 4120fb1ea66c29b34216ad4c014997a71871d9f6 Mon Sep 17 00:00:00 2001 From: Pablo Vazquez Date: Fri, 18 Oct 2013 13:58:02 +0000 Subject: [PATCH] Amaranth Addon Mesh Symmetry tools - Find Asymmetric vertices - Make Symmetric vertices More info at http://caminandes.com/development/improving-blender/ --- scene_amaranth_toolset.py | 131 +++++++++++++++++++++++++++++++++++++- 1 file changed, 130 insertions(+), 1 deletion(-) diff --git a/scene_amaranth_toolset.py b/scene_amaranth_toolset.py index 9c86d296..aad22bd3 100755 --- a/scene_amaranth_toolset.py +++ b/scene_amaranth_toolset.py @@ -30,6 +30,7 @@ bl_info = { import bpy +import bmesh from bpy.types import Operator, AddonPreferences, Panel from bpy.props import BoolProperty from mathutils import Vector @@ -871,8 +872,137 @@ def particles_material_info(self, context): ob.material_slots[len(ob.material_slots)-1].name)) # // FEATURE: Particles Material indicator +# FEATURE: Mesh Symmetry Tools by Sergey Sharybin +class MESH_OT_find_asymmetric(Operator): + """ + Find asymmetric vertices + """ + + bl_idname = "mesh.find_asymmetric" + bl_label = "Find Asymmetric" + bl_options = {'UNDO', 'REGISTER'} + + @classmethod + def poll(cls, context): + object = context.object + if object: + return object.mode == 'EDIT' and object.type == 'MESH' + return False + + def execute(self, context): + threshold = 1e-6 + + object = context.object + bm = bmesh.from_edit_mesh(object.data) + + # Deselect all the vertices + for v in bm.verts: + v.select = False + + for v1 in bm.verts: + if abs(v1.co[0]) < threshold: + continue + + mirror_found = False + for v2 in bm.verts: + if v1 == v2: + continue + if v1.co[0] * v2.co[0] > 0.0: + continue + + mirror_coord = Vector(v2.co) + mirror_coord[0] *= -1 + if (mirror_coord - v1.co).length_squared < threshold: + mirror_found = True + break + if not mirror_found: + v1.select = True + + bm.select_flush_mode() + + bmesh.update_edit_mesh(object.data) + + return {'FINISHED'} + +class MESH_OT_make_symmetric(Operator): + """ + Make symmetric + """ + + bl_idname = "mesh.make_symmetric" + bl_label = "Make Symmetric" + bl_options = {'UNDO', 'REGISTER'} + + @classmethod + def poll(cls, context): + object = context.object + if object: + return object.mode == 'EDIT' and object.type == 'MESH' + return False + + def execute(self, context): + threshold = 1e-6 + + object = context.object + bm = bmesh.from_edit_mesh(object.data) + + for v1 in bm.verts: + if v1.co[0] < threshold: + continue + if not v1.select: + continue + + closest_vert = None + closest_distance = -1 + for v2 in bm.verts: + if v1 == v2: + continue + if v2.co[0] > threshold: + continue + if not v2.select: + continue + + mirror_coord = Vector(v2.co) + mirror_coord[0] *= -1 + distance = (mirror_coord - v1.co).length_squared + if closest_vert is None or distance < closest_distance: + closest_distance = distance + closest_vert = v2 + + if closest_vert: + closest_vert.select = False + closest_vert.co = Vector(v1.co) + closest_vert.co[0] *= -1 + v1.select = False + + for v1 in bm.verts: + if v1.select: + closest_vert = None + closest_distance = -1 + for v2 in bm.verts: + if v1 != v2: + mirror_coord = Vector(v2.co) + mirror_coord[0] *= -1 + distance = (mirror_coord - v1.co).length_squared + if closest_vert is None or distance < closest_distance: + closest_distance = distance + closest_vert = v2 + if closest_vert: + v1.select = False + v1.co = Vector(closest_vert.co) + v1.co[0] *= -1 + + bm.select_flush_mode() + bmesh.update_edit_mesh(object.data) + + return {'FINISHED'} +# // FEATURE: Mesh Symmetry Tools by Sergey Sharybin + + classes = (SCENE_OT_refresh, WM_OT_save_reload, + MESH_OT_find_asymmetric, + MESH_OT_make_symmetric, NODE_OT_AddTemplateVignette, NODE_MT_amaranth_templates, FILE_OT_directory_current_blend, @@ -1001,4 +1131,3 @@ def unregister(): if __name__ == "__main__": register() - -- 2.21.0