[solved] How to add keyframe for Shape Key?

Hi,

I try to add keyframe for Shape key by using Python.

Situation: Scene is empty, script creates Grid object, adds basic and second Shape key. Then it goes to edit mode and randomize mesh, goes back to object mode. Sets value of second key to 1. At this point I don’t know how to set it as keyframe.

Probably I should use keyframe_insert(), but I have no idea, what to write as data path.

RMB on value of key gives me “key_blocks[“Key 1”].value”, but Python sends me the error “TypeError: bpy_struct.keyframe_insert() property “key_blocks[“Key 1”]” not found”.

Any idea, what to write in data path?

Script:

bpy.context.scene.frame_current = 1
bpy.ops.mesh.primitive_grid_add(x_subdivisions=20, y_subdivisions=20, size=2, enter_editmode=False, location=(0, 0, 0))
bpy.context.object.name = "particle_emiter"
bpy.ops.object.shape_key_add(from_mix=False)
bpy.ops.object.shape_key_add(from_mix=False)
bpy.ops.object.editmode_toggle()
bpy.ops.transform.vertex_random(offset=2)
bpy.ops.object.editmode_toggle()
bpy.context.object.data.shape_keys.key_blocks["Key 1"].value = 1
bpy.context.object.keyframe_insert(data_path='key_blocks["Key 1"].value', frame=1) #error

I’ve found solution. Last line should be:

bpy.context.object.data.shape_keys.key_blocks["Key 1"].keyframe_insert(data_path='value', frame=1)

I need a python script to add a dummy keyframe to all shapekeys of selected object at one particular frame…

Can you suggest a suggest a script pls…

Still need it?

for single_shapekey in bpy.context.active_object.data.shape_keys.key_blocks:
     single_shapekey.keyframe_insert(data_path='value', frame=bpy.context.scene.frame_current)

It adds keyframes to all key-shapes but only in current frame and only to single ACTIVE object. After run script, move one frame on timeline, because Blender doesn’t autorefresh interface and looks like ther is no new keyframes.
Is this solution?

1 Like