1

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

2 Answers2

4

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.

Ron Jensen
  • 3,421
  • 1
  • 9
  • 27
3

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)

batFINGER
  • 84,216
  • 10
  • 108
  • 233