Update scene after adding keyframes

In my script I add some keyframes dynamically, and after it’s done I can see the new animation on the screen. Then I export a collada file and make sure that the keyframes are positioned correctly, which they are. However, when I export the Collada file in the script itself, the keyframes in the exported fall are all identical as if the keyframes didn’t update at all. It seems that Blender updates only after I run the script. How can I fix this?

I’m not sure if this will work in your case, but sometimes I use a little hack, i.e - I set scene’s frame to current frame.
So what I do is this:


scn = bpy.context.scene
frame = scn.frame_current
scn.frame_set(frame)

you can either export animation curves directly or loop trough frames by using scene.frame_set(…) and then read props like matrix_world etc.

don’t use frame_current = …, it won’t update object animations

I’m actually already using scene.frame_set(). The animation is keyframing correctly because after the script runs the animation looks fine. The problem lies with the collada export. If I export manually after my script is run, everything is fine. But if I call the collada export command on the last line of my script the output file is wrong.

It seems like there is another update step that happens after the script executes which allows the Collada export to receive updated information about the animations. I don’t know what this would be.

The problem lies with the collada export.

Actually the problem is in Blender itself. Blender does not support events correctly (recursively). Consider the event flow. Your script is run and an event is generated. So you set a frame, which generates several more events. But you still have not released the initial event that triggered the new events. Then, while still holding up the line you try to export an animation which sets the frame as well, generating more events. So to keep Blender from crashing the developers have a lock in place. The error is something like this “Unable to change property x because of state y.” More than likely the Collada exporter is experiencing this error, but trapping/masking it. The end result is that no more frame changes can really occur until your initial script completes. So the exporter just fetches the same data for each frame because it can not successfully change the frame. Kind of like the temporal equivalent of a rotational gimbal lock.

The solution is to fetch the animation data directly from bpy.data and not leverage scene.frame_set at all.