Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.