script to place origin on vertex

Hi, in Blender it’s quite a hassle to place the origin on a particular vertex.
So I thought to make a tiny addon that can do that. (First time trying)

And came to the following lines;

bpy.ops.view3d.snap_cursor_to_selected()
bpy.ops.object.editmode_toggle()
bpy.ops.object.origin_set(type=‘ORIGIN_CURSOR’)
bpy.ops.view3d.snap_cursor_to_center()
bpy.ops.view3d.snap_selected_to_cursor(use_offset=False)

So the idea is Select object > Go in Edit mode > Select Vertex > Run this script.
But, it is not working.

Any idea what I have to add?

Are you just not able to get it to run? Make sure you have “import bpy” for the first line.

I tried with import bpy
and get this error:

Traceback (most recent call last):
File “\Text.001”, line 3, in <module>
File “C:\Program Files\Blender Foundation\Blender\2.79\scripts\modules\bpy\ops.py”, 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…

yeah, I know what’s up. I hate it when it does that. It’s a viewport context issue. To run a “bpy.ops.view3d” operator, you have to be in the 3D viewport. You can make it into a operator and run from 3D viewport or you can fake it out(make it think it’s running in 3D viewport from text editor).

Edit:
https://docs.blender.org/api/blender_python_api_current/bpy.ops.html#overriding-context
something like this should make it work.

<i>maximize 3d view in all windows
</i><b>import</b> <b>bpy</b>

<b>for</b> window <b>in</b> bpy.context.window_manager.windows:
    screen = window.screen

    <b>for</b> area <b>in</b> screen.areas:
        <b>if</b> area.type == 'VIEW_3D':
            override = {'window': window, 'screen': screen, 'area': area}
            bpy.ops.screen.screen_full_area(override) 
            <b>break</b>

This comes with 3d view pie menu addon that is included with Blender. If activated it’s in the origin menu. You can have a look at the code of the addon. You can have a look at my addon for manipulating orign as well https://blenderartists.org/forum/showthread.php?439220-Add-on-Improved-Origin-Pie-Menu&highlight=improved+origin+pie+menu.

@AWFS : thanks that works.
@MartinZ: Usually I used Shift-S and Shift-Ctrl-Alt-C to get this all done. If I use the pies I need both the origin pie and the cursor pie.
With script below I have to click once; (Or twice then to get out of the full-screen mode).

import bpy
for window in bpy.context.window_manager.windows:
screen = window.screen
for area in screen.areas:
if area.type == ‘VIEW_3D’:
override = {‘window’: window, ‘screen’: screen, ‘area’: area}
bpy.ops.screen.screen_full_area(override)
break
bpy.ops.view3d.snap_cursor_to_selected()
bpy.ops.object.editmode_toggle()
bpy.ops.object.origin_set(type=‘ORIGIN_CURSOR’)
bpy.ops.view3d.snap_cursor_to_center()
bpy.ops.view3d.snap_selected_to_cursor(use_offset= False)

I should make a button somewhere, but that’s above my head at the moment. (First little script).

Thanks

Save as py file and place in startup or addons. You didn’t get my other post.


bl_info = {
"name": "Origin to Vertex", 
"version": (1, 0, 0),
"blender": (2, 79, 0),
"location": "3D View(Edit Mode) &gt; Mesh &gt; Transform" ,
"description": "Sets the origin point to the selected vertex.",
"category": "3D View"}


import bpy


class OriginToVertex(bpy.types.Operator):
    """Sets the origin point to the selected vertex"""
    bl_idname = "object.origin_to_vertex"
    bl_label = "Origin to Vertex"

    @classmethod
    def poll(cls, context):
        return context.active_object is not None

    def execute(self, context):
        cursor_location_x = bpy.context.scene.cursor_location[0]
        cursor_location_y = bpy.context.scene.cursor_location[1]
        cursor_location_z = bpy.context.scene.cursor_location[2]
                
        bpy.ops.view3d.snap_cursor_to_selected()
        bpy.ops.object.mode_set(mode='OBJECT')
        bpy.ops.object.origin_set(type='ORIGIN_CURSOR')
        bpy.ops.view3d.snap_cursor_to_center()
        bpy.ops.view3d.snap_selected_to_cursor(use_offset= False)
        bpy.ops.object.mode_set(mode='EDIT')

        bpy.context.scene.cursor_location = (cursor_location_x, cursor_location_y, cursor_location_z)
                
        return {'FINISHED'}


def OriginToVertex_Menu(self, context):
    self.layout.separator()    
    self.layout.operator(OriginToVertex.bl_idname)


def register():
    bpy.utils.register_class(OriginToVertex)
    bpy.types.VIEW3D_MT_transform.append(OriginToVertex_Menu)


def unregister():
    bpy.utils.unregister_class(OriginToVertex)
    bpy.types.VIEW3D_MT_transform.remove(OriginToVertex_Menu)
    

if __name__ == "__main__":
    register()

@AWFS:
Ahw, that’s great.
After the line: bpy.context.scene.cursor_location = (cursor_location_x, cursor_location_y, cursor_location_z)
I added: bpy.ops.object.mode_set(mode=‘OBJECT’)

So what I have to do:
Go in Edit-Mode > Select a vertec > Press spacebar: type Vertex to find: Origin to vertex
And it wil end up with:

  - the Origin placed on the vertex of your choice
  - being in Object mode
  - Centered  (on Grid) 

Already very happy with it. Works great before saving assets and transform like scale.

:eyebrowlift:

just remove the line above it that keeps it in edit mode. Don’t have to add another line to switch to back object mode. You can remove the cursor code to if you want. That just keeps it in the current location.