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 = "Clear Animation Data"
bl_idname = "object.clear_anim_data"
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 = "Clear Animation Data"
bl_idname = "object.clear_anim_data"
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 "proper" or safe, but it works. Any alternative ways would be helpful.
sel_obj = [*bpy.context.selected_objects]
orsel_obj = list(bpy.context.selected_objects)
, or use a tuple to make it immutable, which IMO scans and acts a bit cleaner. Also, you're not actually passingobj
tokeyframe_clear_v3d()
, so you're not actually usingsel_objs
or the loop— I think you only have to callkeyframe_clear_v3d()
once, and it already does all selected objects. $\endgroup$