I'm trying to change a text object on each frame in Blender 2.75a using the code from a previous answer (How can I make dynamic text in an animation?). With my script the text value is coming from the lens property of a camera which is keyframed.
I have added the script to the blender file and changed it to use the camera lens value. When I hit Run script and then move a frame it updates the text object in the 3D viewport as expected. But then when I move another frame it does not update. Nor does it work for Alt+A or if I render a sequence of frames.
As per Ray's suggestion I added a print function which showed frame and lens value. As I moved back the frame back and forth the frame number updated; however the lens value only changed the first time after hitting Run Script and then remained the same in the console. The lens value was changing in the UI.
Code is here: http://www.pasteall.org/61366/python
I've tried code to clear the handlers (
frame_change_pre.clear()) and closing down Blender and reopening to reset everything and the same behavior occurs.
Zeffii's suggestion of moving the
camera_lens = bpy.data.cameras['Camera'].lens line into the function partially worked.
My understanding of the problem now (thanks, everyone) is that when the script is run for the first time it establishes the handler and sets the variable with the lens value. From that point on when the frame moves the handler only executes the function and since the variable that holds the lens value is outside the function it is never updated. Moving the lens variable assignment to inside the function now means it is updated each time the handler is called.
However, now the update lags one frame behind. i.e:
- start on frame 1, UI and 3D view = 16.0.
- Move to frame 2, UI = 16.9, 3D view = 16.0.
- Move to frame 3, UI = 17.8, 3D view = 16.9.
I'm guessing the python handler is executing before the keyframe value is calculated. Tried changing to
frame_change_post but that didn't seem to work either... suggestions?