How do I access the location of an animated object?

Hi everyone,

I have been trying to find a way to access the coordinates of the ‘cube’ object in an animation but I cannot seem to find anything that works.

I offset the cube up on the z axis and then let it fall. The animation lasts for 15 frames. I tried the following code:

  • import bpy
  • cube=bpy.data.objects[‘Cube’]
  • for f in range(sce.frame_start, sce.frame_end+1):
  • bpy.context.scene.frame_set(f)
  • print(“Frame %i” % f)
  • print(cube.location)

However, the ouput I get only contains the initial location, instead of the location at each frame:

Frame 1
<Vector (0.0000, 0.0000, 4.7058)>
Frame 2
<Vector (0.0000, 0.0000, 4.7058)>
Frame 3
<Vector (0.0000, 0.0000, 4.7058)>
Frame 4
<Vector (0.0000, 0.0000, 4.7058)>
Frame 5
<Vector (0.0000, 0.0000, 4.7058)>
Frame 6
<Vector (0.0000, 0.0000, 4.7058)>
Frame 7
<Vector (0.0000, 0.0000, 4.7058)>
Frame 8
<Vector (0.0000, 0.0000, 4.7058)>
Frame 9
<Vector (0.0000, 0.0000, 4.7058)>
Frame 10
<Vector (0.0000, 0.0000, 4.7058)>
Frame 11
<Vector (0.0000, 0.0000, 4.7058)>
Frame 12
<Vector (0.0000, 0.0000, 4.7058)>
Frame 13
<Vector (0.0000, 0.0000, 4.7058)>
Frame 14
<Vector (0.0000, 0.0000, 4.7058)>
Frame 15
<Vector (0.0000, 0.0000, 4.7058)>

What am I doing wrong here?

Thanks

I think you need add an app handler like bpy.app.handlers.frame_change_post

http://www.blender.org/api/blender_python_api_2_76_0/bpy.app.handlers.html?highlight=handler#module-bpy.app.handlers

Your script is reading the current position, not the frame change in animation

Thanks, but I have no idea how to use that! I read the link you sent me but did not understand much of it… How would you solve this issue of getting always the same coordinate?

Try this:


import bpy
from bpy.app.handlers import persistent


# ------------------------------------------------------
# Handler to detect frame change
#
# ------------------------------------------------------
@persistent
def frame_handler(dummy):
    obj = bpy.context.object
    print(obj.location)


bpy.app.handlers.frame_change_post.append(frame_handler)

Run first the script, and then play animation.

And this is an example:


&lt;Vector (0.0000, 0.0000, 0.0000)&gt;
&lt;Vector (0.1781, -0.0000, 0.0620)&gt;
&lt;Vector (0.7126, -0.0000, 0.2480)&gt;
&lt;Vector (1.5707, -0.0000, 0.5466)&gt;
&lt;Vector (2.6624, -0.0000, 0.9266)&gt;
&lt;Vector (3.8403, -0.0000, 1.3365)&gt;
&lt;Vector (4.9319, -0.0000, 1.7164)&gt;
&lt;Vector (5.7901, -0.0000, 2.0151)&gt;
&lt;Vector (6.3246, -0.0000, 2.2011)&gt;
&lt;Vector (6.5027, -0.0000, 2.2630)&gt;
&lt;Vector (0.0000, 0.0000, 0.0000)&gt;
&lt;Vector (0.1781, -0.0000, 0.0620)&gt;
&lt;Vector (0.7126, -0.0000, 0.2480)&gt;
&lt;Vector (1.5707, -0.0000, 0.5466)&gt;
&lt;Vector (2.6624, -0.0000, 0.9266)&gt;
&lt;Vector (3.8403, -0.0000, 1.3365)&gt;

That did not work. After running the script you posted using blender’s internal console, playing the animation (dropping the cube), I still got on the system console:

<Vector (0.0000, 0.0000, 4.0903)>
<Vector (0.0000, 0.0000, 4.0903)>
<Vector (0.0000, 0.0000, 4.0903)>
<Vector (0.0000, 0.0000, 4.0903)>
<Vector (0.0000, 0.0000, 4.0903)>
<Vector (0.0000, 0.0000, 4.0903)>
<Vector (0.0000, 0.0000, 4.0903)>
<Vector (0.0000, 0.0000, 4.0903)>
<Vector (0.0000, 0.0000, 4.0903)>
<Vector (0.0000, 0.0000, 4.0903)>
<Vector (0.0000, 0.0000, 4.0903)>

What is going on?

I am using blender 2.76b

  1. Open blender default cube scene
  2. Set location of cube in frame 1 and 20
  3. Open a Text Editor window
  4. Copy my code as new script annd run it
  5. Press Play animation
  6. Look in the console

I’m using 2.76b in Windows 64 bits

I cannot get it to work. I am running blender in Linux Ubuntu 64 bit.

When you say ‘2) Set location of cube in frame 1 and 20’ what do you mean? How do you set the location of the object for only this two frames? In my case, I was just dropping the cube using the rigid body simulator.

As I have realized, my code and your code both work but only for animated objects (via keyframing). However, none of these codes will display the coordinates for rigid body objects falling under the effect of gravity. Anyone knows why the codes are not working for rigid body simulations and how to do it?

Use

object.matrix_world.to_translation()

Have a look at http://blender.stackexchange.com/a/42340/15543

Oops, double post. Is “site temporarily unavailable” is the new successful post message 8^)?