Move pivot to object center via python

Move pivot to object center via python

cjess
Enthusiast Enthusiast
1,336 Views
4 Replies
Message 1 of 5

Move pivot to object center via python

cjess
Enthusiast
Enthusiast

Hi-

I would like to center the pivot of an object via python script. It would be the equivalent of the "Move to Object Center" buttons in the Transform Window.

I know I can use this command to set the Rotation pivot manually:

 

setTransformNodeRotatePivot(node, x, y, z, worldSpace)

 

 

But I want the pivot at the object's center. So I'm looking for a different command, something like setTransformNodeRotatePivotCenter(node)

or I need to get the x,y,z values of the object's center first and transfer them via variables.

I am not aware of either option.

Thanks for any help!

 

0 Likes
Accepted solutions (1)
1,337 Views
4 Replies
Replies (4)
Message 2 of 5

seiferp
Community Manager
Community Manager

getBoundingBoxCenter() gives you a vector with the x, y, z coordinates of the object you than use to center your pivot.

0 Likes
Message 3 of 5

cjess
Enthusiast
Enthusiast

Thank you for your quick reply, not sure how I missed that. However, getBoundingBoxCenter() returns a Vec3f.

It does not support indexing so I cannot call a specific x, y or z value.

I was hoping something like this would only call the first value of the Vec3f

print getBoundingBoxCenter("myNode", false)[0]

 but it returns the error 

TypeError: 'Vec3f' object does not support indexing

 

So I'm not sure how to convert individual values from the Vec3f to floats as required by

setTransformNodeRotatePivot

 

Any more ideas? Vielen Dank schon mal

0 Likes
Message 4 of 5

marc.winter2
Advocate
Advocate
Accepted solution
bBoxCenter = getBoundingBoxCenter(getSelectedNode(), False)
print str(bBoxCenter.x()) + ',' + str(bBoxCenter.y()) + ',' + str(bBoxCenter.z())
Message 5 of 5

cjess
Enthusiast
Enthusiast

Thank you @seiferp and @marc.winter2 !

 

The command setTransformNodeRotatePivot requires float values for x,y and z positions so I changed the script just in case anyone else comes across this thread.

I also changed getSelectedNode()  to findNode("myNode")  as I am targeting a specific node every time.

 

bBoxCenter = getBoundingBoxCenter(findNode("myNode"), False)
setTransformNodeRotatePivot(findNode("myNode"), float(bBoxCenter.x()), float(bBoxCenter.y()), float(bBoxCenter.z()), false)

 

This was beyond my Python knowledge so thanks again, I learned something!

Stay healthy, Grüsse aus Los Angeles

0 Likes