我想将动画值写入文本文件中并读出来,所以我需要一种方法来链接这些值以旋转开始并旋转到导出和导入功能。
def animation():
selectionList = cmds.ls(selection=True , l = True )
#slider = cmds.intSliderGrp("frames", visible=True, field=True, min=1, max=5000, value=200, step=1 )
if len(selectionList) >=1:
startTime = cmds.playbackOptions( query=True, minTime=True)
endTime = cmds.playbackOptions( query=True, maxTime=True)
startValue = cmds.intSliderGrp(rotStart, q= True,value =True)
endValue = cmds.intSliderGrp(rotEnd, q= True,value =True)
for objectName in selectionList:
#objectTypeResult = cmds.objectType( objectName )
#print '%s type: %s' % ( objectName, objectTypeResult )
cmds.cutKey( objectName, time=(startTime, endTime), attribute='rotateY')# remove prevouis Keys and gets rotatye Y
cmds.setKeyframe( objectName, time=startTime, attribute='rotateY', value=startValue)#value of key startime
cmds.setKeyframe( objectName, time=endTime, attribute='rotateY', value=endValue)
def exportToTxtFile (): # export function
global presetNameStartStr
global presetNameEndStr
filePath = cmds.fileDialog2(dialogStyle=2, fm=1)
print filePath
fileHandle = open(filePath[0],'w')
fileHandle.write(presetNameStartStr)
fileHandle.write("This is a .txt file")
fileHandle.write(presetNameEndStr)
fileHandle.close()
def importFromTxtFile (): # import function
global presetNameStartStr
global presetNameEndStr
extensionLimitation = "Maya Files (*.txt)"
filePath = cmds.fileDialog2(fileFilter = extensionLimitation, dialogStyle=2, fm=4)
fileHandle = open(filePath[0],'r')
presetString = fileHandle.read()
fileHandle.close()
print presetString
splitPresetList = string.split(s = presetString, sep = presetNameStartStr)
del splitPresetList[0]
for i in splitPresetList:
endPos = string.find(i,presetNameEndStr)
namePreset = i[:endPos]
print namePreset
答案 0 :(得分:1)
我有一个json解决方案,我认为它比使用txt文件更容易。
import maya.cmds as cmds
import json
#store
def saveJson(path, data):
with open(path, 'w') as outfile:
json.dump(data, outfile)
#load
def loadJson(path):
with open(path, 'rb') as fp:
data = json.load(fp)
return data
# list of objects
objectNames = ['pSphere1']
# list of attributes
attributes = ['rotateY']
# dictionnary to store data
data = {}
# this maya path for data defined by your setProject
path = cmds.workspace(expandName = 'data')
# i like to put json as extension but you can put any extension you want or even none
filename = 'attributes.json'
fullpath = path + '/' + filename
# beginning the storage
for o in objectNames:
for attr in attributes:
# all the time value
frames = cmds.keyframe(o, q=1, at=attr)
# all the attribute value associated
values = cmds.keyframe(o, q=1, at=attr, valueChange=True)
# store it in the dictionnary with key 'objectName.attribute'
data['{}.{}'.format(o, attr)]=zip(frames, values)
# clear the keys on the object
cmds.cutKey( o, attribute=attr, option="keys")
# save the file data
saveJson(fullpath, data)
# ============================================================
# LOAD DATA BACK
# =============================================================
# load the dictionnary back
new_data = loadJson(fullpath)
# set values again
for k, v in new_data.iteritems():
for t, x in v:
o, a = k.split('.')
cmds.setKeyframe( o, time=t, attribute=a, value=x)