【问题标题】:Python Maya referencePython Maya 参考
【发布时间】:2023-02-02 05:36:51
【问题描述】:

我正在尝试创建一个脚本来引用不同的灯光装置并在同一窗口中清理它们。因为我有不止一个,所以我想在它们之间切换,这就是脚本中断的部分。

这是我尝试过的:

import maya.cmds as cmds

class ReferenceUI(object):
    def __init__(self):
        self.window_name = "template"
        self.file_paths = ["D:\\Estudos\\Cenas\\fred\\fred.0001.mb", "D:\\Estudos\\Cenas\\fred\\fred.0002.mb", "D:\\Estudos\\Cenas\\fred\\fred.0003.mb"]
        self.ref_name = "template"
        self.current_file = self.file_paths[0]

    def create(self):
        if cmds.window(self.window_name, exists=True):
            cmds.deleteUI(self.window_name)

        self.window = cmds.window(self.window_name, title=self.window_name)

        self.main_layout = cmds.columnLayout()
        self.file_path_selector = cmds.optionMenuGrp(label="File Path")
        for file_path in self.file_paths:
            cmds.menuItem(label=file_path)

        self.reference_button = cmds.button(label="Add Reference", 
                            command=lambda x: self.add_reference())

        self.switch_button = cmds.button(label="Switch Reference",
                            command=lambda x: self.switch_reference())

        self.clean_button = cmds.button(label="Clean All References",
                            command=lambda x: self.clean_references())

        cmds.showWindow(self.window)

    def add_reference(self):
        file_path = cmds.optionMenuGrp(self.file_path_selector, query=True, value=True)
        cmds.file(file_path, reference=True, namespace=self.ref_name)
        self.current_file = file_path

    def switch_reference(self):
        file_path = cmds.optionMenuGrp(self.file_path_selector, query=True, value=True)
        if file_path != self.current_file:
            cmds.file(file_path, edit=True, force=True, options="v=0;", loadReferenceDepth="all", namespace=self.ref_name)
            self.current_file = file_path
        else:
            cmds.warning("The selected file is already loaded")

    def clean_references(self):
        references = cmds.ls(references=True)
        for ref in references:
            cmds.file(removeReference=True, referenceNode=ref, type='mayaAscii')

reference_manager = ReferenceUI()
reference_manager.create()

【问题讨论】:

  • “脚本中断”是什么意思?
  • 它不会切换文件,有时会使 Maya 崩溃

标签: python maya


【解决方案1】:

我试过你的脚本,我发现了两个问题

  • 当您删除引用时,您有一个 mayaAscii 类型,而我看到您的引用文件具有扩展名 .mb,因此我假设它们是二进制文件。我试过使用 ascii 参考文件进行测试
  • 为了交换引用,有一个技巧可以使用带有参数的文件 cmd 从命名空间(名称应该有 RN 后缀)加载你的引用并指定一个新的引用文件路径

这是一个更新的脚本

import maya.cmds as cmds

class ReferenceUI(object):
    def __init__(self):
        self.window_name = "template"
        self.file_paths = ["C:\work\box01.ma", "C:\work\cone01.ma"]
        self.ref_name = "template"
        self.current_file = self.file_paths[0]

    def create(self):
        if cmds.window(self.window_name, exists=True):
            cmds.deleteUI(self.window_name)

        self.window = cmds.window(self.window_name, title=self.window_name)

        self.main_layout = cmds.columnLayout()
        self.file_path_selector = cmds.optionMenuGrp(label="File Path")
        for file_path in self.file_paths:
            cmds.menuItem(label=file_path)

        self.reference_button = cmds.button(label="Add Reference", 
                            command=lambda x: self.add_reference())

        self.switch_button = cmds.button(label="Switch Reference",
                            command=lambda x: self.switch_reference())

        self.clean_button = cmds.button(label="Clean All References",
                            command=lambda x: self.clean_references())

        cmds.showWindow(self.window)

    def add_reference(self):
        file_path = cmds.optionMenuGrp(self.file_path_selector, query=True, value=True)
        cmds.file(file_path, reference=True, namespace=self.ref_name)
        self.current_file = file_path

    def switch_reference(self):
        file_path = cmds.optionMenuGrp(self.file_path_selector, query=True, value=True)
        if file_path != self.current_file:
            cmds.file(file_path, options="v=0;", type="mayaAscii", loadReference="templateRN")
            self.current_file = file_path
        else:
            cmds.warning("The selected file is already loaded")

    def clean_references(self):
        references = cmds.ls(references=True)
        for ref in references:
            cmds.file(removeReference=True, referenceNode=ref, type='mayaAscii')

reference_manager = ReferenceUI()
reference_manager.create()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-31
    • 2011-01-21
    • 2011-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-17
    相关资源
    最近更新 更多