Take the 2-minute tour ×
Blender Stack Exchange is a question and answer site for people who use Blender to create 3D graphics, animations, or games. It's 100% free, no registration required.

First post, hope it's not ignorant.

How can I use a Python script to obtain the exact coordinates of an object? I have not been able to find out how to do this in the documentation. Please forgive me if this is a stupid question.

share|improve this question

1 Answer 1

up vote 7 down vote accepted

Background: The object location (in fact transformation - so location/scale/rotation) is not quite as straightforward as you might think.

  • initial transform (as set by the user and possibly set by animation curves and drivers).
    as a tool author to manipulate data this is what you'll want to access typically
  • output transform (with parenting, constraints, and rigid body physics applied).
    when writing exporters or reading, typically this is what you will want to access since its whats displayed in the view (but you can' direcly manipulate it)

To get the input location you can simply do:

loc = bpy.context.object.location... or via its matrix
bpy.context.object.matrix_local.to_translation()

To get the output transformation you can only do this by accessing the worldspace matrix.

loc = bpy.context.object.matrix_world.to_translation()

You may want to get the loc/rotation/scale, in that case:

loc, rot, scale = bpy.context.object.matrix_world.decompose()


If you want to test the location for example, you can set the cursor position.

from bpy import context
context.scene.cursor_location = context.object.matrix_world.to_translation()

Or you can create a new empty:

import bpy
from bpy import context
obj = bpy.data.objects.new("Empty", None)
context.scene.objects.link(obj)
obj.matrix_world  = context.object.matrix_world

... note, if the matrix contains shear, you wont see this in the empty, for that try the MathVis addons.

share|improve this answer
    
What would be the best way for me to test the above code? The Blender-Python integration tutorials I have completed have been based around graphical results. –  Qu0rk Mar 7 at 5:09
    
Added notes on viewing the transformation. –  ideasman42 Mar 7 at 8:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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