0
$\begingroup$

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)
               else:
                   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?

$\endgroup$

2 Answers 2

1
$\begingroup$

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.. :)

$\endgroup$
1
  • $\begingroup$ Perfect! :) Thank you for helping out! Still getting used to Blender API as well as python. $\endgroup$
    – dan283
    Commented Nov 18, 2020 at 17:10
0
$\begingroup$

This is probably the other way around.

import bpy

End_Frame = bpy.context.scene.frame_end
ob = bpy.context.active_object
me = ob.data

kblocks = me.shape_keys.key_blocks
l = len(kblocks)

for a in range(1,l):  # This can point your specific shapkey
    kblocks[a].value = 0
    kblocks[a].keyframe_insert("value", frame=End_Frame - 3)
    kblocks[a].value = 1
    kblocks[a].keyframe_insert("value", frame=End_Frame)
$\endgroup$
1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .