I need to keyframe shape keys and have a script that's nicely working -

import bpy

frames = bpy.context.scene.frame_end + 1

for frame in range(frames):
    for shapekey in bpy.data.shape_keys:
        for i, keyblock in enumerate(shapekey.key_blocks):
            if keyblock.name != 'Basis':
               curr = i - 1
               if curr != frame:
                   keyblock.value = 0
                   keyblock.keyframe_insert("value", frame=frame)
                   keyblock.value = 1
                   keyblock.keyframe_insert("value", frame=frame)

The issue is, I need it to work only on the active object, not every object in the scene.

Anyone any pointers?


Will this do?

import bpy

frames = bpy.context.scene.frame_end + 1

# For the active object...
ob = bpy.context.active_object
me = ob.data

# Remove ['Basis'] from a shallow copy of *ob's* shape-keys list.
kblocks = dict(me.shape_keys.key_blocks)
del kblocks['Basis']

# Keyframe shapekeys' values to 1 for the frame corresponding
# to their position in remaining list, 0 for other frames
for f in range(frames):
    for i, kb in enumerate(kblocks):
        kblocks[kb].value = (f == i)
        kblocks[kb].keyframe_insert("value", frame=f)

This may get burned down by a proper Pythonista.. :)

  • $\begingroup$ Perfect! :) Thank you for helping out! Still getting used to Blender API as well as python. $\endgroup$ – danulrich Nov 18 '20 at 17:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.