Python 对于 Maya:"Object's name is not unique." 从 class 调用对象构建 UI

Python for Maya: "Object's name is not unique." when calling object from class to build UI

问题:

在我尝试构建 UI 之前,当我 运行 脚本时,我没有收到任何语法错误。一切似乎都很好,直到我 运行 最后两行代码。

我收到以下错误: 错误:运行时错误:文件第 41 行:对象名称 'mst_txtfld_x_value' 不唯一。

我确保在第 8~9 行 deleteUI,所以我假设 textField 是被创建两次。

或者我对 类 的工作方式有什么不了解的地方吗? 我是 类 的新手,希望能解释一下为什么会出现此错误。

代码:

import maya.cmds as mc
from functools import partial

class MoveSelTool(object):
    def __init__(self, *args):
        pass       
    if(mc.window("ak_move_sel_tool_window", query=True, exists=True)):
        mc.deleteUI("ak_move_sel_tool_window")
        
    def build_window_UI(self):   
        self.window = mc.window("ak_move_sel_tool_window", 
                                title="Move Selection Tool")
        self.columnLayout = mc.columnLayout()
        self.txt_directions = mc.text(align="left", 
                                    label="Directions: Input translation increment.\n")
        self.rowColumn = mc.rowColumnLayout(numberOfColumns=8)
        self.txt_x = mc.text(label=" X: ", 
                            align="right")         
        self.txtfld_x = mc.textField("mst_txtfld_x_value", 
                                    ann='input units to move X', 
                                    width=50)
        self.txt_y = mc.text(label=" Y: ", 
                            align="right")         
        self.txtfld_y = mc.textField("mst_txtfld_y_value", 
                                    ann='input units to move Y', 
                                    width=50)
        self.txt_z = mc.text(label=" Z: ", 
                            align="right")         
        self.txtfld_z = mc.textField("mst_txtfld_z_value", 
                                    ann='input units to move Z', 
                                    width=50)
        self.txt_space = mc.text(label="  ")    
        self.move_btn = mc.button(label="Move") 
    
        #ui commands
        mc.button(self.move_btn, 
                edit=True, 
                command=partial(self.move_selection))
        mc.textField("mst_txtfld_x_value", 
                    enterCommand=partial(self.move_selection))
        mc.textField("mst_txtfld_y_value", 
                    enterCommand=partial(self.move_selection))             
        mc.textField("mst_txtfld_z_value", 
                    enterCommand=partial(self.move_selection))
                     
        #show ui
        mc.showWindow(self.window)
            
    def query_mst_user_input(self):

        self.x_value = mc.textField("mst_txtfld_x_value", 
                                    query=True,
                                    text=True)

        self.y_value = mc.textField("mst_txtfld_y_value", 
                                    query=True,
                                    text=True)
                           
        self.z_value = mc.textField("mst_txtfld_z_value", 
                                    query=True,
                                    text=True)
                               
        return (self.x_value, self.y_value, self.z_value) 
                                                     
    def move_selection(self):
        self.mst_user_selection = mc.ls(selection=True)    
        self.mst_user_inputs = query_mst_user_input()
        mc.move(self.mst_user_selection, 
                self.mst_user_inputs[0], 
                self.mst_user_inputs[1], 
                self.mst_user_inputs[2], 
                relative=True)
    def show(self):
        self.build_window_UI()
mst=MoveSelTool()
mst.show()

您忘记了设置 ui 命令的编辑标志。

#ui commands
mc.button(self.move_btn, 
        edit=True, 
        command=partial(self.move_selection))
mc.textField("mst_txtfld_x_value", edit=True,
            enterCommand=partial(self.move_selection))
mc.textField("mst_txtfld_y_value", edit=True,
            enterCommand=partial(self.move_selection))             
mc.textField("mst_txtfld_z_value", edit=True,
            enterCommand=partial(self.move_selection))