A python tool to delete extraneous keyframes from a selected object or objects in Maya. Extraneous keyframes are defined as non-changing attributes between keys.
mc.window(width=150)
mc.columnLayout(adjustableColumn=True)
mc.button(label='Clean Up Keys of Selected',command="cleanupKeys(mc.ls(selection=True),mc.keyframe(sel, query=True, attribute='translateX'))")
mc.showWindow()
Fig 0: Simple interface that calls the script by creating an array of objects containing the selected objects and creating an array of keyed frames based on one of the attributes.
import maya.cmds as mc
attributes= [
'translateX',
'translateY',
'translateZ',
'rotateX',
'rotateY',
'rotateZ',
'scaleX',
'scaleY',
'scaleZ',
'visibility']
def cleanupKeys(selection, aArray):
for obj in selection:
i = 0
tArray = mc.keyframe(obj, query=True, attribute='translateX')
for t in tArray:
i+=1
for a in aArray:
if i >= len(tArray):
break
checkAttr(obj,t,tArray[i],a)
def checkAttr(obj, t0, t1, attr):
att0 = mc.getAttr(obj+'.'+attr, time=t0)
att1 = mc.getAttr(obj+'.'+attr, time=t1)
if (att0==att1):
mc.cutKey(obj, time=(t0,t0), clear=True, option='keys', attribute=attr)
Key check and deletion are taken care of with the checkAttr(obj,t0,t1,attr)
method which takes in an object obj
and compares the passed in attribute attr
at given time values t0
and t1
.
cleanupKeys(selection, aArray)
is the main command that calls checkAttr(obj,t0,t1,attr)
while iterating through the selection list and checking through the attributes in the list of provided attributes. This is done so the user can provide custom attributes to check by editing the attributes array.
Fig 1: After selecting both objects we see the keyframes attributed to them.
Fig 2: After running the script with the objects to clean selected we see that some keyframes are missing from the timeline and that the attributes of the pCube, which are listed on the right, are in light pink denoting that those keyframes are nonexistent.
Fig 3: Selecting just the pCube we see that frame 1 is removed from the timeline as well because no attributes are keyframed.
Fig 4: All keys attached to non-changing attributes are also deleted however the keyframe is noted on the timeline because at least one attribute is keyed.