1
$\begingroup$

I need to clear keyframes from the active shapekey using python. I got this far, this is removing the current keyframe on the active shapekey, but got no clue how to remove all of the keyframes.

def clear_shape_key_keyframes_selected():

    obj = bpy.context.active_object
    shapekeys = obj.data.shape_keys.key_blocks
    active_shapekey = shapekeys[obj.active_shape_key_index]
    active_shapekey_name = active_shapekey.name
    
    try:
        obj.data.shape_keys.keyframe_delete(f"key_blocks[\"{active_shapekey_name}\"].value")
    except RuntimeError:
        print("Shapekey has no animation data to delete")

other things I tried:

This does work, but deletes keyframes on all the shapekeys:

def clear_shape_key_keyframes():
    obj = bpy.context.active_object
    shape_keys = obj.data.shape_keys
    
    key_blocks = obj.data.shape_keys.key_blocks
    active_shapekey = key_blocks[obj.active_shape_key_index]

    shape_keys.animation_data_clear()

However, this does not work, and gives me the:

error "AttributeError: 'ShapeKey' object has no attribute 'animation_data_clear'"

def clear_shape_key_keyframes():
    obj = bpy.context.active_object
    shape_keys = obj.data.shape_keys
    
    key_blocks = obj.data.shape_keys.key_blocks
    active_shapekey = key_blocks[obj.active_shape_key_index]

    active_shapekey.animation_data_clear()  

Which is really odd since both seem to be shapekey objects.

$\endgroup$

2 Answers 2

2
$\begingroup$

You can do it using the shape keys animation data (from what you have in the first function in your question).

# get the data path
data_path = f"key_blocks[\"{active_shapekey_name}\"].value"

# get animation data
animation_data = obj.data.shape_keys.animation_data
# and its curves
curves = animation_data.action.fcurves

# loop over the curves
for curve in curves:
    # find the one that corresponds to the data path
    if curve.data_path == data_path:
        # remove it
        curves.remove(curve)
        break
$\endgroup$
1
  • $\begingroup$ thanks, this seems to be working. $\endgroup$
    – dan283
    Commented Jan 13, 2023 at 10:07
1
$\begingroup$

This script will automatically remove all keyframes for the actively selected shape key.

import bpy

obj = bpy.context.object
fcurves = bpy.context.object.data.shape_keys.animation_data.action.fcurves
for fc in fcurves:
    if fc.data_path.rsplit(".", 1)[0] == obj.active_shape_key.path_from_id():
        fcurves.remove(fc)
$\endgroup$
1
  • 1
    $\begingroup$ thanks, working great. appreciate it! $\endgroup$
    – dan283
    Commented Jan 13, 2023 at 10:08

You must log in to answer this question.

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