# Getting location of the object with “follow path” and “track to” constraints — using Python

Is there any method to get location of an object which has "follow path" and "track to" constraints?

import bpy
scene = bpy.context.scene
scene.objects['Cube'].location[0]
scene.objects['Cube'].location[1]
scene.objects['Cube'].location[2]


The code above gives me constant location of an object, relative to the followed path I suppose

I would like to get global location instead, just like in case of an object with no constraints.

Any suggestions will be appreciated :)

scene.objects['Cube'].matrix_world[0][3]

scene.objects['Cube'].matrix_world[1][3]

scene.objects['Cube'].matrix_world[2][3]

bpy.context.active_object.matrix_world[0][3]

bpy.context.active_object.matrix_world[1][3]

bpy.context.active_object.matrix_world[2][3]

• it may be necessary to call a scene.update() before reading the values of matrix_world -- if you notice that the values don't change even though the UI does display changed values. – zeffii Sep 8 '15 at 15:09
• – zeffii Sep 8 '15 at 15:11
• Thank you atomicbezierslinger, ".matrix_world" do the job for the object location :) And how about the rotation? Could it be also read from this matrix? I've made some test, but I got not obvious results and I couldn't figure out which matrix elements correspond to rotation. – Alice Sep 9 '15 at 10:42
• Did you have any success? I am still not clear on that. – atomicbezierslinger Sep 9 '15 at 16:21
• There should be a decompose() function on the matrix that lets you determine the location, scale, and orientation. – Mutant Bob Dec 7 '15 at 15:39

Once you've set your constraints, you can see the object's location changing by dragging the time cursor in the timeline. The 'Python' way to do it is to use the function :

scene.frame_set(i_frame)


Then, you can use object.location to access the 3 Vector containing the location of the object. The indices of the location vector are simply the X, Y and Z components of the location (no link to the frames of the path). The same way, you can access to the rotation of the object using one of these :

object.rotation_euler
object.rotation_quaternion
object.rotation_axis_angle


Usually, the Euler reresentation of the rotation of the object is preferred. Both the location and rotation of the object are defined globally.

• I think this is where my problem is -while I'm draging the time cursor in the timeline, the values of x,y,z location of the object stay 0, although I can observe that the object is moving along the path in the viewport. This problem doesn't occur in case of the object with movement defined by simple keyframing the object location values. However I really need to have the "follow path" constraint. I use Blender 2.75. Could you please check if there is different result in your Blender instalation? – Alice Sep 7 '15 at 13:49