# How do I iterate over keyframes in python? [duplicate]

I'm trying to export animation data, but my bones never get updated from keyframe to keyframe. This is how I iterate over my actions and keyframes:

for action in bpy.data.actions:
armature.animation_data.action = action
bpy.ops.screen.frame_jump(0)
bpy.context.scene.update()
while {'FINISHED'} == bpy.ops.screen.keyframe_jump():
bpy.context.scene.update()
for boneName in bones_index:
bone = armature.pose.bones[boneName]
mat = glob_mat * mathutils.Matrix(bone.matrix)


The 'mat' variable is the bone matrix for each bone. Per frame, the 'mat' variable doesn't seem to change. For example, if I only look at the bone "Leg", the leg pose matrix doesn't seem to change keyframe to keyframe. The matrix is the correct matrix for frame 0, and it is different from my base pose. Am I not getting the pose matrices correctly? Or am I not correctly changing the scene? In the blender console, bpy.ops.screen.keyframe_jump() does exactly what I want, so I don't think that's the issue.

• Well, you are in luck I think. CodemanX just answered this for someone else a few days ago and the code is very easy to work with. blender.stackexchange.com/questions/8387/… Apr 12 '14 at 18:33
• @MarcClintDion If a question seems to be asking the same thing as a previous question, feel free to cast a "close as duplicate" vote.
– gandalf3
Apr 12 '14 at 20:05
• @gandalf3 it's not exactly the same (but very close). That in that thread, their solution was going from the start frame to the end frame (iterating over every single frame). What I want is to iterate over key frames. That's why bpy.ops.screen.keyframe_jump() is used. It jumps to the next key frame. That seems to work, and the code I have works perfectly in the interactive console. However, when I put it in a text file and run it from there, the pose bones are stuck in the pose of the first frame. I even call bpy.context.scene.update(), but that didn't seem to help Apr 12 '14 at 22:50

bpy.context.scene.frame_set(bpy.context.scene.frame_current)