Skip to content

Instantly share code, notes, and snippets.

@razvanab
Created March 10, 2023 17:33
Show Gist options
  • Save razvanab/ea8f7f1ea59e013f34bcb9e1e3e4cc6b to your computer and use it in GitHub Desktop.
Save razvanab/ea8f7f1ea59e013f34bcb9e1e3e4cc6b to your computer and use it in GitHub Desktop.
bl_info = {
"name": "Align Vertices",
"author": "ChatGPT",
"description": "Align Vertices on a given axis",
"category": "Mesh",
}
import bpy
import bmesh
class AlignVerticesOperator(bpy.types.Operator):
"""Align selected vertices along a specified axis"""
bl_idname = "mesh.align_vertices"
bl_label = "Align Vertices"
bl_options = {'REGISTER', 'UNDO'}
axis: bpy.props.EnumProperty(
name="Axis",
items=[
("X", "X", ""),
("Y", "Y", ""),
("Z", "Z", ""),
],
default="Z",
)
@classmethod
def poll(cls, context):
return context.active_object is not None and context.active_object.mode == 'EDIT'
def execute(self, context):
obj = context.active_object
mesh = obj.data
bm = bmesh.from_edit_mesh(mesh)
# get the selected vertices
selected_verts = [v for v in bm.verts if v.select]
# find the min and max values along the selected axis
min_val = min(getattr(v.co, self.axis.lower()) for v in selected_verts)
max_val = max(getattr(v.co, self.axis.lower()) for v in selected_verts)
# calculate the center point along the selected axis
center = (min_val + max_val) / 2
# move each selected vertex to the center point along the selected axis
for v in selected_verts:
setattr(v.co, self.axis.lower(), center)
# update the mesh
bmesh.update_edit_mesh(mesh)
return {'FINISHED'}
def menu_func(self, context):
self.layout.operator(AlignVerticesOperator.bl_idname)
def register():
bpy.utils.register_class(AlignVerticesOperator)
bpy.types.VIEW3D_MT_edit_mesh_vertices.append(menu_func)
def unregister():
bpy.utils.unregister_class(AlignVerticesOperator)
bpy.types.VIEW3D_MT_edit_mesh_vertex.remove(menu_func)
if __name__ == "__main__":
register()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment