Shift Object's Origin to Cursor Location

Hi,

I have wracked my brain for the last while scouring the Blender Python API for a way to shift an object so that it’s origin is at the cursor location. There’s a button in the UI to do it, but I’ll be a monkey’s uncle if I can figure out the API call(s) required to duplicate the functionality.

Also, there does not seem to be a Cursor object … Although there is Window.GetCursorPos().

Anyone care to shed a little light on this?

Ah, just needed a little more looking through the forums. I found something that should do the trick. I’ll post the results when working.

1 Like

Don’t know if you got it working already, but here’s a solution. It works on the currently active object.

import Blender
from Blender import *

cursor = Window.GetCursorPos()
vcursor = Mathutils.Vector(cursor)
scn = Scene.GetCurrent()
sel = scn.objects.active
if sel:
    if sel.type=='Mesh':
        loc = sel.getLocation('worldspace')
        vloc = Mathutils.Vector(loc)
        d = vcursor-vloc
        sel.setLocation(cursor)
        m = sel.getData(False,True)
        for v in m.verts:
            v.co-=d
Blender.Redraw()

It’s a bit of a quick one, so no error handling included.

Hi, Crouch.

I did something sneaky, instead.

 # Centre object around cursor
#
bounds = object.getBoundBox()
sx = 0.0
sy = 0.0
sz = 0.0

for bound in bounds:
  sx += bound[0]
  sy += bound[1]
  sz += bound[2]

object.LocX = sx / 8
object.LocY = sy / 8
object.LocZ -= sz / 8

Since changing the object’s centre was proving too difficult (I tried three or four ways, including yours, all without luck), I approached the problem from a slightly different angle. The code above allowed me to resolve the issue.

The result is a text warping script; see my post in this forum for details.