1
$\begingroup$

How to remove only rotation keyframes from current action with Python? Or location keyframes? Or scale? Is it passible?

$\endgroup$
1
$\begingroup$

Using FCurve.remove

The curve, as shown in graph editor, with keyframe points for each keyframe value pair is an fcurve. Removing an fcurve Will remove all keyframes for that FCurve.data_path, Fcurve.array_index pair

Similarly to this related (possible duplicate) question. Example script removes all Location, Rotation and Scale fcurves from an action. It tests the FCurve.data_path against the keyframe type list ["location", "rotation", "scale"]. Note "rotation" will match "rotation_euler" or "rotation_quaternion" etc, which will be the actual datapath (as there is no Object.rotation).

Avoided using the FCurves.find method with a list comprehension, and used pop to avoid possibly referencing a removed fcurve later.

import bpy

context = bpy.context
ob = context.object
ad = ob.animation_data

if ad:
    action = ad.action
    if action:
        remove_types = ["location", "scale", "rotation"]
        # select all that have datapath above
        fcurves = [fc for fc in action.fcurves
                for type in remove_types
                if fc.data_path.startswith(type)
                ]
        # remove fcurves
        while(fcurves):
            fc = fcurves.pop()
            action.fcurves.remove(fc)

To remove only y axis Loc, Rot and Scale also test for the Fcurve.array_index property which is 0, 1, 2 for x, y, z respectively.

        fcurves = [fc for fc in action.fcurves
                for type in remove_types
                if fc.data_path.startswith(type)
                and fc.array_index == 1
                ]
| improve this answer | |
$\endgroup$

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.