I'm trying to track the (x,y,z) location of an animated object. For a small test, I translated the default cube ('Cube') along the x axis and baked keyframes. The animation plays fine.
I adapted the suggestion for this question to try and print the location at each keyframe.
import bpy
cube=bpy.data.objects['Cube']
for f in cube.animation_data.action.fcurves:
for k in f.keyframe_points:
pos=cube.location
print(pos)
Run from the shell, the output is
<Vector (0.0000, 0.0000, 0.0000)>
<Vector (0.0000, 0.0000, 0.0000)>
<Vector (0.0000, 0.0000, 0.0000)>
...
<Vector (0.0000, 0.0000, 0.0000)>
If I go to the last keyframe and execute cube.location, it correctly returns
Vector((6.924227714538574, 0.0, 0.0))
How can I get the location at each keyframe?
bpy.context.scene.frame_set(frame)and readingcube.locationbe a bad idea? – kilbee Dec 17 '13 at 23:18cube.locationreads the location from current context frame, thats why you are getting the results from the frame you run the script from. So to get the right results withcube.locationyou need to change the context with thebpy.context.scene.frame_set(frame). – kilbee Dec 17 '13 at 23:45