# BPy Funtcion long to short?

i have a question. it is possible this function in any way to shorten ?

class ANIM_iXoClear(bpy.types.Operator):
bl_idname = "ixo.anim_clear"
bl_label = ""

def execute(self, context):
bpy.context.object.animation_data.action = bpy.data.actions['Body_00_Default']
bpy.ops.anim.keyframe_clear_v3d()
bpy.context.object.animation_data.action = bpy.data.actions['Body_01_Idle']
bpy.ops.anim.keyframe_clear_v3d()
bpy.context.object.animation_data.action = bpy.data.actions['Body_02_Attack']
bpy.ops.anim.keyframe_clear_v3d()
bpy.context.object.animation_data.action = bpy.data.actions['Body_03_Block']
bpy.ops.anim.keyframe_clear_v3d()
bpy.context.object.animation_data.action = bpy.data.actions['Leg_00_Default']
bpy.ops.anim.keyframe_clear_v3d()
bpy.context.object.animation_data.action = bpy.data.actions['Leg_01_Walk']
bpy.ops.anim.keyframe_clear_v3d()
bpy.context.object.animation_data.action = bpy.data.actions['Leg_02_Run']
bpy.ops.anim.keyframe_clear_v3d()
return {'FINISHED'}


because the operator "bpy.ops.anim.keyframe_clear_v3d()" is in it more than once, and that look not so clean :/ .

I hope I get an answer.

-best regards DevilSam

You can put the items in an array and then run a loop:

def execute(self, context):
parts = ['Body_00_Default', 'Body_01_Idle', 'Body_02_Attack', 'Body_03_Block', \
'Leg_00_Default', 'Leg_01_Walk', 'Leg_02_Run']

for part in parts:
bpy.context.object.animation_data.action = bpy.data.actions[part]
bpy.ops.anim.keyframe_clear_v3d()

return {'FINISHED'}


or if you really want to clear ALL the actions, you could try

for act in bpy.data.actions:
bpy.context.object.animation_data.action = act
bpy.ops.anim.keyframe_clear_v3d()


Strip the fcurves from the actions.

Rather than use the operator at all, could strip the action of fcurves using API method ActionFCurves.remove(...)

import bpy

action_names = ["Body_00_Default",
"Body_01_Idle",
"Body_02_Attack",
"Body_03_Block",
"Leg_00_Default",
"Leg_01_Walk",
"Leg_02_Run"]

actions = [(name, bpy.data.actions.get(name)) for name in action_names]

while actions:
name, a = actions.pop()
if a:
while a.fcurves:
a.fcurves.remove(a.fcurves[0])
else:
print("No action named", name)


Note this needs no context, only the names of the actions. Which is currently hardcoded into script. If you have stashed or pushed down your actions to the NLA then this list comprehension (py console code C is context)

>> [s.action for t in C.object.animation_data.nla_tracks
...        for s in t.strips if s.action]
[bpy.data.actions['Leg_02_Run'], bpy.data.actions['CubeAction'], bpy.data.actions['CubeAction.001'], bpy.data.actions['CubeAction.002']]


is all actions associated to object via NLA. Append the one in the slot too if desired.