Shapekey Values not acting the same in console vs. running a script

Can someone help me debug this? (or let me know that yes, I have found a bug)

In the console if I ask for a shape key’s value, it gives me the correct value based on which frame I’m on, but if I do the same thing in a while loop seeking through the frames, it always returns the same value. Weird. For instance:

In the console:


>>> Eyes = bpy.data.objects['Plane'].data.shape_keys.key_blocks['eyes']
>>> Eyes.value
0.41718316078186035
>>> bpy.ops.anim.change_frame(frame=5)
{'FINISHED'}
>>> Eyes.value
0.3277439475059509

That works, but if I write:


while i < LastFrame:
    i=i+1
    bpy.ops.anim.change_frame(frame=i)
    print(Eyes.value)

This just spits out a sequence of repeating, equal, values.

(Also as usual, am I just working wrong? What I really want is to be able to select all the shape key values, query max and min, then scale to normalize, but, y’know, Documentation…

I think this comes under http://www.blender.org/documentation/blender_python_api_2_62_1/info_gotcha.html#stale-data

You could throw in a scene.update(), or if your key values are only keyed query the fcurve directly.



for i in range(StartFrame, LastFrame):
    scene.frame_set(frame=i)
    print(Eyes.value)
    scene.update()

Seeing as you are looking at a frame change you could also put it in a frame change handler


import bpy


def frame_change(scene):
    # run some scripts if the cube has moved.
    Eyes = scene.objects["Plane"].data.shape_keys.key_blocks["eyes"]
    print(Eyes.value)
        
bpy.app.handlers.frame_change_post.append(frame_change)

OHHHHH. It’s one of those “don’t bog down the machine horribly” things.

Of course, I kind of knew I wasn’t making the best decision when I started using this method of finding max and min of an FCurve…