0
$\begingroup$

I have many objects that contain animation on their properties like location, rotation, and scale. I want to delete all keyframes from a specific property like a scale which I can do by right-clicking on that property and selecting Clear Keyframes but I want to do this for all selected objects using python.

$\endgroup$

1 Answer 1

2
$\begingroup$

clear keyframe of specific property of selected object

import bpy

def clear_keyframe(obj, data_path, ind=None):
    try:
        fcs = obj.animation_data.action.fcurves
        if ind is None:
            fc = fcs.find(f'{data_path}')
        else:
            fc = fcs.find(f'{data_path}', index=ind)
        fcs.remove(fc)
        print(f"{obj}  {fc}  removed")
    except:
        if hasattr(obj, "name"):
            print(f"{obj.name}  fcurve  remove fail")
        else:
            print(f"object  fcurve  remove fail")

for obj in bpy.context.selected_objects:
    clear_keyframe(obj, "location", 0)
    clear_keyframe(obj, "location", 1)
    clear_keyframe(obj, "location", 2)

clear all keyframes of all level one properties (not include modifiers, shape key, ...) of selected object

import bpy

def clear_keyframe(obj, data_path, ind=None):
    try:
        fcs = obj.animation_data.action.fcurves
        if ind is None:
            fc = fcs.find(f'{data_path}')
        else:
            fc = fcs.find(f'{data_path}', index=ind)
        fcs.remove(fc)
    except: pass

for obj in bpy.context.selected_objects:
    props = obj.bl_rna.properties
    for prop in props:
        array_length = getattr(prop, "array_length", None)
        if array_length is not None and array_length > 1:
            for r in range(array_length):
                clear_keyframe(obj, prop.identifier, r)
        else:
            clear_keyframe(obj, prop.identifier)
$\endgroup$
1
  • $\begingroup$ Thank you. It worked as imagined. $\endgroup$
    – Muzammil
    Commented Feb 17, 2023 at 10:58

You must log in to answer this question.

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