Update view port after scripting shape keys ?

I’m generating shape keys using code like this:

o = bpy.context.object
kb = o.data.shape_keys.key_blocks
kb[key_idx].data[vertex_idx].co =
vertex_position
kb[key_idx].value = shape_key_weight
kb[key_idx].keyframe_insert(“value”, frame = frame_idx)

The generation works fine, and after running the script the shape keys appear in the “object data / shape keys”. But moving the time slider doesn’t animate the object in the 3D view. I first have to click with the mouse into the shape key list (object data / shape keys), which apparently triggers some internal update, that makes the 3D view animate. This manual step is tedious / impossible when shape-animating many objects that way.

Is there a python command that triggers this update, after generating shape keys?

Or:

Is there something simple (not requiring clicking though all objects) that you can do in the GUI that triggers this update?

Saving and loading the file, doesn’t fix the inconsistent state.

There’s probably just a missing update call somewhere, I can look into it…eventually (or you could file a bug report).

Thanks, it would be cool, if that was fixed in the next release. I can write a bug report too, if you point me to right place.

But meanwhile I would be very happy about a workaround for this issue. What API calls could I potentially perform to trigger the update?

bpy.context.Scene.update() might do it.

Unfortunately not. I searched the documentation for “update” and tried most of them.

I did: http://projects.blender.org/tracker/?func=detail&aid=33682&group_id=9&atid=498

Also see the attached file to reproduce the bug. Follow the instructions in the script called “bug”.

Attachments

shape_key_update_bug.blend (445 KB)

The problem is that you did not insert a Basis for the shape keys to modify. Try this… Open up your file and insert a Basis shape key on the Cube. Then run your script and it works.

Thanks for the hint. I assumed that the first shape key added with:

bpy.context.object.shape_key_add(name=“key_0”,from_mix=False)

would be taken as the basis. But then I saw that the add button actually does:

bpy.ops.object.shape_key_add(from_mix=False)

Adding the above line, before adding the other keys solves the problem. I guess you can add all the shape keys via:

bpy.ops.object.shape_key_add

instead of of

bpy.context.object.shape_key_add

however, the former doesn’t support the “name” attribute.

Here the a working minimal example that animates vertex 4 of the active object:

import bpy

bpy.ops.object.shape_key_add(from_mix=False) # this solves the problem

bpy.context.object.shape_key_add(name=“key_0”,from_mix=False)

kb = bpy.context.object.data.shape_keys.key_blocks

kb[1].value = 0.0
kb[1].keyframe_insert(“value”, frame = 0)

kb[1].data[4].co = kb[1].data[4].co * 2.0

kb[1].value = 1.0
kb[1].keyframe_insert(“value”, frame = 100)