Search code examples
pythongraphics3dmayamaya-api

Maya Python: Check to see if the attribute has any keyframes


I am trying to write a python script for Maya that will copy keyframes from one rig to another. I have found objects and matched them up. What I am trying to do now is copy the keys from the original objects if the original objects have keys to be copied. I was hoping to use the Keyframe command to check to see if the object has keys.

Example: if cmds.keyframe(oldObjPath attribute=oldAttr,sl=True, q=True, tc=True ) > 0:

This however always returned false. When I print out the attributes of oldObjPath I do get all the attributes printed out. Any idea what I am doing wrong here? Full code is below

Documentation on Keyframe Command: http://download.autodesk.com/global/docs/maya2014/en_us/index.html?url=files/Python_Python_in_Maya.htm,topicNumber=d30e813275

#create a decunary of the object names and paths for faster searching
#[search_name:path]

originalObjectDic = {}
newObjectDic = {}

for obj in originalObjects:

    #First remove the full path to give us somthing to search the new object with
    subStrLoc = 0
    index = 0
    for char in obj:
        if char == ':':
            subStrLoc = index
        index=index+1

    searchName = obj[subStrLoc+1:]

    originalObjectDic.update({searchName:obj})


    #next look at all the names of the new object and see if they match up    
for nObj in newObjects:
    #correct the new objects name
    subStrLoc=0
    index=0
    for char in nObj:
        if index != 0:
            if char == '_' and nObj[index-1] == 'r' and nObj[index-2] == 'u' and nObj[index-3] == 'F':
                    subStrLoc = index
        index = index + 1  

    if subStrLoc == 0:
        index = 0
        for char in obj:
            if char == ':':
                subStrLoc = index
        index=index+1 

    searchName = nObj[subStrLoc+1:]
    newObjectDic.update({searchName:nObj})   


#now that we have to dicunaries to check agaenst we will match up the two obj paths
# and copy the keys on all attributes on each node

for key in newObjectDic:

    newObjPath = newObjectDic.get(key)
    oldObjPath = originalObjectDic.get(key)
    #if there is a match between the two dics
    if newObjPath != None and oldObjPath != None:
        #get a list of all the attributes
        newObjAttributes = cmds.listAttr(newObjPath,v=True,r=True, w=True)
        oldObjAttributes = cmds.listAttr(oldObjPath,v=True,r=True, w=True)

        for x in range(len(newObjAttributes)-1):
            newAttr = newObjAttributes[x]
            oldAttr = oldObjAttributes[x]
            if cmds.keyframe(oldObjPath attribute=oldAttr,sl=True, q=True, tc=True ) > 0:
                print oldObjPath
                print oldAttr
                print 'Has Key'
                print '----------------------------'

Solution

  • Got help from a freands. Had the wrong option on. sl which stands for selection should be false or not there at all so...

    if cmds.keyframe(oldObjPath, attribute=oldAttr, sl=False, q=True, tc=True):