Python import/export Maya keyframes script

These are fairly rudimentary python scripts, to export keys from Maya and import them back into Maya. Creates a key at every frame. May be interesting to people learning Python.

Export Maya Keys
[py]
import maya.cmds as mc
# Select all nodes with keys to export
# Customize the path below so that it points to, or creates, the file where you want to store the keyframe data
myFileObject=open(‘/mydataDir/data.txt’, ‘w’)
obs = mc.ls(sl=True)
theData = []
minTime = mc.playbackOptions(query=True, minTime=True)
maxTime = mc.playbackOptions(query=True, maxTime=True)
attributes = [‘translateX’, ‘translateY’, ‘translateZ’, ‘rotateX’, ‘rotateY’, ‘rotateZ’, ‘scaleX’, ‘scaleY’, ‘scaleZ’, ‘visibility’]
for time in range(minTime -1, maxTime +1):
mc.currentTime(time)
count = 0
for selection in obs:
name = obs[count]
count +=1
for theAttribute in attributes:
myAtF = mc.getAttr(selection + ‘.’ + theAttribute)
myAt = str(myAtF)
myTime = str(time)
theData.append(myAt + ‘ ‘ + myTime + ‘ ‘ + theAttribute + ‘ ‘ + name + ‘ \n’)
for lines in theData:
myFileObject.writelines(lines)
myFileObject.close()
[/py]

Import Maya Keys
This will import keys to nodes which are named identically to the nodes that were selected when the keys were exported.
[py]
import maya.cmds as mc
# Customize the path below so that it points to the file where you have exported the keyframe data
myFileObject=open(‘/mydataDir/data.txt’, ‘r’)
theLines = myFileObject.readlines()
count = 0
for line in theLines:
theLine = theLines[count]
theSplit = str.split(theLine)
theValue = theSplit[0]
theFrame = theSplit[1]
theAttribute = theSplit[2]
theName = theSplit[3]
mc.setKeyframe( theName, v=float(theValue), at=theAttribute, t =float(theFrame))
count +=1
myFileObject.close()
[/py]

Post a comment

You may use the following HTML:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>