1
$\begingroup$

I just want to get the pivot location of the 3d model!

enter image description here

I tried to set the cursor to pivot position and print the cursor location

import bpy

bpy.ops.view3d.snap_cursor_to_selected()
print(bpy.context.scene.cursor.location)

I had this error:

RuntimeError: Operator bpy.ops.view3d.snap_cursor_to_selected.poll() failed, context is incorrect
Error: Python script failed, check the message in the system console
$\endgroup$
4
$\begingroup$

Objects pivot around their origins, or (0, 0, 0) in their local space. An object's position is simply the location of its origin in world space. You can get this as a Vector from the object's location variable:

import bpy

obj = bpy.context.active_object
print(obj.location)

If you actually did want to set the cursor location:

bpy.context.scene.cursor.location = obj.location

See How do you get an object's position and rotation through script? for a more thorough look.

$\endgroup$
3
  • $\begingroup$ I know how to get the location of the object I just want to know is it possible to get the pivot location? because when I change the location of the object pivot will not change. pivot is a kind of local position am I right? $\endgroup$ Sep 7 '20 at 16:47
  • $\begingroup$ so If we convert the world position to the object position is it pivot location? $\endgroup$ Sep 7 '20 at 16:51
  • $\begingroup$ I re-wrote the first sentence of my answer, does that make it clearer? $\endgroup$
    – Ron Jensen
    Sep 7 '20 at 17:03
3
$\begingroup$

Global and Local

The scene cursor location is in global coordinates.

For an object ob

What you see in the location field of an object is its basis matrix translation.

ob.matrix_basis.translation # == ob.location

, Rarely for an object with parents and / or constraints will ob.location also be the global location of the origin of the object. An object with ob.location == (0, 0, 0) can be globally anywhere in scene via parenting or a copy location constraint to another object.

Using ob.matrix_world.translation will ensure global (or world) coordinates, hence to set the cursor location to the objects global location

scene.cursor.location = ob.matrix_world.translation

Be very wary of mixing the two. (global and local)

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.