Maya 保存菜单信息(帧范围)

maya saving menu info (frame range)

enter image description here我有一个菜单,其中有多个帧范围供不同的动画对象使用。每次关闭和重新打开时,我都需要该菜单来保存该帧范围。是否可以将数据保存到外部文件。

有很多方法可以将数据保存到外部。可能最简单的方法之一是使用 json 模块:

import os
import json


path = "PATH/TO/YOUR/FILE/data.json"


def save_data(frame_range):
    with open(path, "w") as f:
        f.write(json.dumps(frame_range))


def load_data():
    if os.path.exists(path):
        with open(path, "r") as f:
            return json.loads(f.read())


save_data([1, 100])

stored_range = load_data()

print stored_range
# Output: [1, 100]

在本例中它转储 list,但它支持更多(字典、嵌套数据结构)

另一种方法是使用 pickle 模块保存数据:

import pickle


path = "PATH/TO/YOUR/FILE/data.p"


def save_data(frame_range):
    with open(path, "w") as f:
        f.write(pickle.dumps(frame_range))


save_data([1, 100])

您还可以使用 cpickle 导出为二进制格式。

在 Maya 本身中,您可以将设置直接保存到用户的首选项中:

cmds.optionVar(iv=("frameStart", 1))
cmds.optionVar(iv=("frameEnd", 100))

对于更复杂的数据结构,您也可以直接将 json 字符串直接存储在 cmds.optionVar 中。

import json
def saveS ():
    startFrame =  cmds.textField ('one', q= True, text = True)
    endFrame = cmds.textField ('two', q= True, text = True)
    frame= {}
    frame["start"] = startFrame
    frame["end"] = endFrame
    file2 = open ("maya/2018/scripts/test_pickle/dataCopy.json", "w")
    json.dump (frame, file2)
    file2.close()
def helpMenu():
    if(cmds.window('window1_ui',q=True,ex=True)):cmds.deleteUI('window1_ui')
    cmds.window('window1_ui')
    cmds.columnLayout(adj=True)
    cmds.checkBox( label='Default' )
    cmds.textField('one', cc= 'saveS ()')
    cmds.textField('two', cc= 'saveS ()')
    json_file = open ("maya/2018/scripts/test_pickle/dataCopy.json", "r")
    frame = json.load(json_file)
    json_file.close()
    ted=frame["start"]
    cmds.textField ('one', edit= True, text = ted)
    fred=frame["end"]
    cmds.textField ('two', edit= True, text = fred)
    cmds.showWindow('window1_ui')
 helpMenu()

这是我想到的。它有效感谢您的帮助@GreenCell。