How can I use a Python script to get the transformation of an object?
Background: The object location (in fact transformation - so location/scale/rotation) is defined by initial values which are used to calculate the final transformation.


initial transform (as set by the user and possibly set by animation FCurves and Drivers).as a tool author, to manipulate data this is what you'll want to access typically
final 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't directly manipulate it)


To get the input location you can simply do:

loc = bpy.context.object.location... or via its matrixbpy.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.