簡體   English   中英

Python Maya 導出動畫值腳本

[英]Python Maya export Animation Values Script

我想在文本文件中寫入我的動畫值並讀出它們,所以我需要一種方法來鏈接這些值以旋轉開始和旋轉到我的導出和導入功能。

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

我有一個使用 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']
# dictionary 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 dictionary 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 dictionary 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM