How can I delete keyframes and animation data for all selected objects? I am trying to do as the title suggests: remove all keyframes and animation data from all selected objects.
I am just starting out with Python and add-on development so this has proven to be a challenge for me. This is what I have sofar:
class CULT_ClearAnimData(Operator):
    bl_label = &quot;Clear Animation Data&quot;
    bl_idname = &quot;object.clear_anim_data&quot;
    bl_options = {'REGISTER', 'UNDO'}

    def execute(self, context):
        selected_obj = bpy.context.selected_objects
        scene = bpy.context.scene

        for x in selected_obj:
            selected_obj[x].select_set(True)
            bpy.ops.anim.keyframe_clear_v3d()

        return {'FINISHED'}

Edit: Got this to work:
class CULT_ClearAnimData(Operator):
bl_label = &quot;Clear Animation Data&quot;
bl_idname = &quot;object.clear_anim_data&quot;
bl_options = {'REGISTER', 'UNDO'}

def execute(self, context):
    sel_objs = [obj for obj in bpy.context.selected_objects]
    #bpy.ops.object.select_all(action='DESELECT')
    for obj in sel_objs:
        bpy.ops.anim.keyframe_clear_v3d()

    return {'FINISHED'}

I am not sure if my solution is &quot;proper&quot; or safe, but it works. Any alternative ways would be helpful.
One fell swoop approach.
copying the script from Delete animation of object
Uses Object.animation_data_clear() to remove all animation data from all selected objects.
import bpy

context = bpy.context
for ob in context.selected_objects:
    ob.animation_data_clear()

this will orphan any action not associated with other objects, and they will be dealt with on file save / reload.
Otherwise
orphans = [a for a in bpy.data.actions if not a.users]
while orphans:
    bpy.data.actions.remove(orphans.pop())