Animator Tool: Delete Redundant Keys

 

 

Hi all! Here’s a python script I wrote for Maya that will delete the redundant “flat” keys in your graph editor for the selected object. Download it for free here.

How it works:

  1. I check the neighboring values of each key
  2. If they are the same, I add the middle key to a “trash” list to be later deleted.

Pretty simple concept, but the execution required some creative thinking. For one, I had a hard time with maya’s method of getting a key’s frame number and value, so I decided to make an object instead. So instead of cycling through all the keys, I cycle through all the objects I defined from the values of the keys and evaluate things that way.

It’s a simple enough class:

class keyframeObj(object):
    def __init__(self,frame,value):
        self.frame = frame
        self.value = value

    def getFrame(self):
        return self.frame
    def getValue(self):
        return self.value

I then define its values here:

classArray = []
    i = 0

    # For every keyframe on the object, create a keyframeObj object and give it the values of the frame num and the keyframe value
    while i < len(frames):
        newKey = keyframeObj(frames[i],values[i])
        classArray.append(newKey)     
        i += 1</pre>

Then I check all the key values:

# the array that holds the keyframes that will be cut (hehe)
    crushKeys = []
    # simple check to see if key B is the same as its neighbors A and C. If so, add it to the array to be cut. 
    while i &lt; (len(classArray)-1):
        keyA = classArray[i-1].getValue()
        keyB = classArray[i].getValue()
        keyC = classArray[i+1].getValue()
        if keyB == keyA and keyB == keyC:
            #print("Redundant key at frame: " + str(classArray[i].getFrame()))
            crushKeys.append(classArray[i].getFrame())
        i += 1

Then finally  use cutKey to delete those values.

for frame in crushKeys:
cmds.cutKey(activeObj,at=att,cl=1,t=(frame,frame))
#print("Key Obliterated")

I then just cycle that method on every keyable attribute on the selected object and I’m done!

def deleteAllRedundant():
    selObj = cmds.ls(sl=1,o=1)
    if len(selObj) &gt; 1:
        cmds.confirmDialog(m="You can only run this on one object at a time.")
        return
    else:
        keyable = cmds.listAttr(selObj,k=1)
        for attr in keyable:
            deleteRedundant(attr)
Advertisements
Image

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s