Script for quick way to snap Origin to 3D Cursor? (2.79)

Is there a quick way to snap the Origin to 3D Cursor with a script?

Currently I select an element in edit mode, snap 3D Cursor to Selected, go into Object Mode and set Origin to 3D Cursor.

I copied this process from the Info window and pasted it into a text as:

import bpy


but get an error in the console window:

File "/Applications/", line 189, in __call__
ret = op_call(self.idname_py(), None, kw)
RuntimeError: Operator bpy.ops.view3d.snap_cursor_to_selected.poll() failed, context is incorrect
Error: Python script fail, look in the console for now...

Using Blender 2.79

You need to run the script inside a 3d view, that is why you are getting incorrect context.

This can be done with an operator.

Why don’t you assign a shortcut to Origin to 3d Cursor ? That should do what you want.

Object Menu->Set Origin -> right click ->assign shortcut (then save preferences)

Thanks for your reply.

I could assign Origin to 3D Cursor to a shortcut but that’s only one stop of three that I’d like to be a simpler process.

Regarding the context of the script - does it need to be written using parameters or properties of “view3d”?

You need to write an Operator class that can be run inside 3d view. Otherwise everytime you run that script, it runs inside the text editor context which is the wrong context for this kind of stuff.

See the Python templates in the text editor.


I’ll give it a go.

Had a look but my Blender Python is very basic. I’ll stick with the standard UI options for now.

You should be able to repurpose my addon, I was going to release as a utility. It lets you access “lock to 3d cursor” in 3d view as an operator. This works in both versions of Blender btw

bl_info = {
    "name": "ToggleCursor",
    "author": "Kursad Karatas",
    "version": (1, 0),
    "blender": (2, 80, 0),
    "location": "View3D > LockToCursor",
    "description": "Toggles the Lock to Cursor property",
    "warning": "",
    "wiki_url": "",
    "category": "Viewport",

import bpy

class ToggleLockCursor(bpy.types.Operator):
    """Toggles Lock to Cursor"""
    bl_idname = "view3d.toggle_lock_cursor"
    bl_label = "Toggle Lock to Cursor"

    def poll(cls, context):
        if context.space_data.type == 'VIEW_3D':
            print("We are in 3d view {}".format(context.space_data.lock_cursor))
            return True
        return False

    def execute(self, context):{"INFO"},"Lock state {}".format(context.space_data.lock_cursor))

        if context.space_data.lock_object:

        if context.space_data.lock_camera:

        if not context.space_data.lock_cursor:
        return {'FINISHED'}

def register():
    bl_info['blender'] = getattr(, "version")

def unregister():

if __name__ == "__main__":

Thanks for the script @kkar

I won’t change anything at the moment as Snapping Origin to Cursor is made easier in 2.80 via the right-click context popup.