1
$\begingroup$

Is there a way to batch / string commands together. The commands I want to execute right after each other is (Cursor to selected, then set origin to 3D cursor)

I would like to just select the faces of an object in edit mode.

Then:

  1. The cursor gets moved to the selected (In edit mode)
  2. The objects origin gets set to the cursor. (In object mode)

Example: ani1

Ideally I would assign a shortcut to this or an icon.

$\endgroup$

1 Answer 1

2
$\begingroup$

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()
$\endgroup$
3
  • $\begingroup$ active object only, Not support multi object for this script. $\endgroup$
    – X Y
    Jul 9, 2022 at 21:39
  • $\begingroup$ Thanks for the code when I ran it with F3 nothing happened. I changed bpy.ops.view3d.snap_cursor_to_active() to bpy.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 $\endgroup$
    – Rick T
    Jul 11, 2022 at 8:01
  • $\begingroup$ @RickT you're right. it works on the default cube, but doesn't work when i select an edge loop like in your scenario. but changing to bpy.ops.view3d.snap_cursor_to_selected() works for me. $\endgroup$ Jul 11, 2022 at 8:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .