Make custom operator
bl_info = {
"name": "My Addon",
"author": "X Y",
"version": (0, 1),
"blender": (2, 80, 0),
"location": "View3D",
"description": "my operator",
"category": "Object",
}
import bpy
class MY_OP(bpy.types.Operator):
bl_idname = "view3d.my_operator"
bl_label = "some label"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
if not context.object:
print("object not find")
return {'CANCELLED'}
if context.object.type != 'MESH':
print("MESH Only")
return {'CANCELLED'}
obj = context.object
if obj.mode == 'OBJECT':
bpy.ops.object.origin_set(type='ORIGIN_CURSOR', center='MEDIAN')
elif obj.mode == 'EDIT':
bpy.ops.view3d.snap_cursor_to_active()
else:
print("OBJECT and EDIT mode Only")
return {'CANCELLED'}
return {'FINISHED'}
def register():
bpy.utils.register_class(MY_OP)
def unregister():
bpy.utils.unregister_class(MY_OP)
if name == "main":
register()
Install and Assign shortcut
How to duplicate parented objects as one object
Extra
import bpy
class MY_OP(bpy.types.Operator):
bl_idname = "view3d.my_operator"
bl_label = "some label"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
if not context.object:
print("object not find")
return {'CANCELLED'}
if context.object.type != 'MESH':
print("MESH Only")
return {'CANCELLED'}
obj = context.object
if obj.mode == 'OBJECT':
bpy.ops.view3d.snap_cursor_to_selected()
bpy.ops.object.origin_set(type='ORIGIN_CURSOR', center='MEDIAN')
elif obj.mode == 'EDIT':
bpy.ops.view3d.snap_cursor_to_selected()
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.origin_set(type='ORIGIN_CURSOR', center='MEDIAN')
bpy.ops.object.mode_set(mode='EDIT')
else:
print("OBJECT and EDIT mode Only")
return {'CANCELLED'}
return {'FINISHED'}
def register():
bpy.utils.register_class(MY_OP)
def unregister():
bpy.utils.unregister_class(MY_OP)
if name == "main":
register()
bpy.ops.view3d.snap_cursor_to_active()tobpy.ops.view3d.snap_cursor_to_selected()and the cursor moved but the origin didn't move until I went into Object mode and ran it again. Is there away to have this done all at once without running it twice (once in edit mode and once in object mode)? I'm using Blender 3.2.1 – Rick T Jul 11 '22 at 08:01bpy.ops.view3d.snap_cursor_to_selected()works for me. – Harry McKenzie Jul 11 '22 at 08:57