Moving pivot with custom script

Hi. I used to work a lot with this script on 2.79. All changes done in 2.8 are superb, except I´m really used to move pivot point in a single go anywhere where I have at least 1 selected vertex. Now I don´t know how to “upgrade” this to 2.8
Could anyone please lend me a hand? It´s a really short code:

bl_info = {
    'name': 'Move origin to selected',
    'author': '',
    'version': (0, 0, 1),
    'blender': (2, 6, 7),
    'location': '3d view > space bar > Origin Move to Selected',
    'description': 'in edit mode, sets object origin to the median of selected verts/edges/faces',
    'wiki_url': '',
    'tracker_url': '',
    'category': '3D View'}

import bpy

class MoveOrigin(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "object.origin_to_selected"
    bl_label = "Origin Move To Selected"
    bl_options = {'REGISTER', 'UNDO'}

    @classmethod
    def poll(cls, context):
        obj = context.active_object
        return obj is not None and obj.mode == 'EDIT'

    def execute(self, context):
        saved_location = bpy.context.scene.cursor_location.copy()
        bpy.ops.view3d.snap_cursor_to_selected()
                
        bpy.ops.object.mode_set(mode = 'OBJECT')
        bpy.ops.object.origin_set(type='ORIGIN_CURSOR')  
        bpy.context.scene.cursor_location = saved_location
        
        bpy.ops.object.mode_set(mode = 'EDIT')
        return {'FINISHED'}


def register():
    bpy.utils.register_class(MoveOrigin)


def unregister():
    bpy.utils.unregister_class(MoveOrigin)


if __name__ == "__main__":
    register()

Thank you very much.

Here you go.

bl_info = {
    'name': 'Move origin to selected',
    'author': '',
    'version': (0, 0, 1),
    'blender': (2, 80, 0),
    'location': '3d view > space bar > Origin Move to Selected',
    'description': 'in edit mode, sets object origin to the median of selected verts/edges/faces',
    'wiki_url': '',
    'tracker_url': '',
    'category': '3D View'}

import bpy

class MESH_OT_MoveOrigin(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "object.origin_to_selected"
    bl_label = "Origin Move To Selected"
    bl_options = {'REGISTER', 'UNDO'}

    @classmethod
    def poll(cls, context):
        obj = context.active_object
        return obj is not None and obj.mode == 'EDIT'

    def execute(self, context):
        saved_location = bpy.context.scene.cursor.location.copy()
        bpy.ops.view3d.snap_cursor_to_selected()
                
        bpy.ops.object.mode_set(mode = 'OBJECT')
        bpy.ops.object.origin_set(type='ORIGIN_CURSOR')  
        bpy.context.scene.cursor.location = saved_location
        
        bpy.ops.object.mode_set(mode = 'EDIT')
        return {'FINISHED'}


classes = (
            MESH_OT_MoveOrigin,
)

def register():
    from bpy.utils import register_class
    for cls in classes:
        register_class(cls)


def unregister():
    from bpy.utils import unregister_class
    for cls in reversed(classes):
        unregister_class(cls)


if __name__ == "__main__":
    register()

Hi. Thanks. Did you tested it? Not as easy as changing the versioning on the “description”, my friend.
Something changed in the api.
On 2.8 we cannot address RNA from the keyboard mapping anymore.
Sooo… ¿?¿?¿?¿?¿?

It works here - what is throwing an error now?
edit: I mean that I run the script, and then call the operator from the space bar as describe in the ‘description’ of the original script. I select two verts and run the operator in edit mode, and the origin snaps to the median of the selection and is back in edit mode…

Did you run the corrected script: saved_location = bpy.context.scene.cursor.location.copy()
It needs the ‘.’ instead of ‘_’

Thanks for the heads up, Craig. I restored a 2.79 .py script from the bin to the addon folder instead of the original one you sent here. My bad.
Yeah, I see it working now.
The “.” instead of “_”? wow. Api did make it for the better.
If I need to assign this to a custom shortcut menu, where is the EDIT MODE on Keymap (from user preferences)? I only see 3D View>Object mode.
I know I need to make a new “+” shortcut on edit mode to add this custom .py script to be triggered by the keyboard shortcut…but I can´t find it on the list…

I had to apply the class directly to a new shortcut keyboard:
and it doesn’t work:
object.origin_to_selected

After installing the add-on, you can then create a new shortcut entry under 3D View>Mesh(Global) and paste the operator ID there - but I changed mine to " for memory. testing, it worked here.

Well, I don´t know if this is conflict issue, but since the shortcut has to happen on the EDIT mode (and not global>mesh), here is not working.
I tried assigning it to other keys, but to no avail.
Also, restarted Blender.
Factory reset blender. Non working when I enter edit mode and apply my custom shortcut key. Any other place I´m missing to look?

oh shoot… I forgot to mention. I´m on 2.81 DD (daily developer’s version).
EDIT:
Tried it on 2.80 regular install.
No avail (oh snap! the screencap didn´t get the assigned shortcut: CTRL+SHIFT+1):

WHAT IN THE WORLD?! I switched to (any other key combination) and it works right off the bat!
I previously applied CTRL+SHIFT+1 and the combination was taken. I guess I need to re-think where I could place this new shortcut or delete the other pre-assigned ones.

1 Like

Not sure what was going on, but I set mine to " key and it works in edit mode here just fine, I hope you get it working soone. I use 2.81 daily as well, no point waiting for bug fixes to be packaged later on.

Hi Craig, thanks for your patience. I had to turn this off to re assign the most useful combo keys of all blender CTRL+SHIFT+1 to set origin to selected.

Working now. Let me see if I can do the next one in translating it for the current api…

1 Like

How to implement this script?
Copied the code, pasted it into a file, called it Pivot_to_Selected.py, install as add-ons, than add keymap for “object.origin_to_selected”. Seems all ok. Call for key - nothing. F3 - type “origin” - select “Origin move to selected” and nothing again. Whats wrong?

Add:
some how it start to work now…

Thanks to the community for this help. One of the most PROMINENT features, but it´s not implemented on a menu. Hope this helps as this is one of my main core plays in every Blender session. :smiley: