简体   繁体   中英

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

The Problem:

I don't get any syntax errors when i run the script up until i try to build the UI. Everything seems fine until i run the last 2 lines of code.

I get the following error : Error: RuntimeError: file line 41: Object's name 'mst_txtfld_x_value' is not unique.

I made sure to deleteUI in line 8~9, so I'm assuming that the textField is being created twice.

Or is there something that I'm not understanding about how classes should work? I am new to classes and would appreciate an explanation why I'm getting this error.

The Code:

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()

You forgot the edit flag to set ui command.

#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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM