Skip to content

Instantly share code, notes, and snippets.

@batFINGER
Last active November 2, 2023 17:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save batFINGER/4cf6ba4ef50db12c0a85995296714ae9 to your computer and use it in GitHub Desktop.
Save batFINGER/4cf6ba4ef50db12c0a85995296714ae9 to your computer and use it in GitHub Desktop.
2.80 bse examples
bl_info = {
"name": "Set mesh origin to selected geometry",
"author": "batFINGER",
"version": (1, 0),
"blender": (2, 80, 0),
"description": "Set mesh origin to selected geometry",
"warning": "",
"wiki_url": "",
"category": "Add Mesh",
}
import bpy
import bmesh
from mathutils import Vector
class MESH_OT_origin_to_selection(bpy.types.Operator):
"""Set the mesh's origin to the selection"""
bl_idname = "mesh.origin_to_selection"
bl_label = "Set Origin to Selection"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
ob = context.edit_object
return ob and ob.type == 'MESH'
def execute(self, context):
ob = context.edit_object
mw = ob.matrix_world
me = ob.data
bm = bmesh.from_edit_mesh(me)
selverts = [v.co for v in bm.verts if v.select]
if len(selverts):
pt = sum(selverts, Vector()) / len(selverts)
#T = Matrix.Translation(-pt)
bmesh.ops.translate(bm, vec=-pt, verts=bm.verts)
mw.translation = mw @ pt
bmesh.update_edit_mesh(me)
return {'FINISHED'}
return {'CANCELLED'}
def draw_menu(self, context):
self.layout.operator("mesh.origin_to_selection")
def register():
bpy.utils.register_class(MESH_OT_origin_to_selection)
bpy.types.VIEW3D_MT_transform.append(draw_menu)
def unregister():
bpy.utils.unregister_class(MESH_OT_origin_to_selection)
bpy.types.VIEW3D_MT_transform.remove(draw_menu)
if __name__ == "__main__":
register()
@Hamzaa1000
Copy link

how to use man

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment