Last active
November 2, 2023 17:24
-
-
Save batFINGER/4cf6ba4ef50db12c0a85995296714ae9 to your computer and use it in GitHub Desktop.
2.80 bse examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
how to use man