为什么在此脚本中deleteUI无法在Maya Python中工作?

问题描述:

我在使用Maya Python编写的脚本中遇到了一些问题!我希望能够通过按下按钮来调用一个函数来关闭用户界面。我环顾了这里和其他网站,我似乎无法使其工作。按下“关闭”按钮时没有任何反应。为什么在此脚本中deleteUI无法在Maya Python中工作?

def closeUI(*args): 

    if (cmds.window('mainWindow', exists=True)): 
     cmds.deleteUI('mainWindow') 


def mainWindow(*args): 

    closeUI() 

    mainWindow = cmds.window(title="Auto-rig", widthHeight=(300, 300), s=False, vis=True, toolbox=True) 
    cmds.columnLayout(adjustableColumn=True) 

    cmds.button(label='Create Joints (1 of 3)', command=createJoints, en=True) 

    cmds.button(label='Create IK (2 of 3)', command=createIk, en=True) 

    cmds.button(label='Create Controls (3 of 3)', command=createControls, en=True) 

    cmds.button(label='Close', command=closeUI, en=True) 

mainWindow() 

def del_win(win_id, *args) 
    if cmds.window(win_id, query=True, exists=True): 
     # add type of UI element, window, layout, control 
     cmds.deleteUI(win_id, window=True) 
window = cmds.window("main_win", title="Auto-rig") 

del_win("main_win") 

del_win(window) 

回到你的代码

from functools import partial 
def closeUI(win_id, *args): 
    if (cmds.window(win_id, exists=True)): 
     cmds.deleteUI(win_id, window=True) 

def main_Window(*args): 

    cmds.window("mainWindow", title="Auto-rig", widthHeight=(300, 300), s=False, vis=True, toolbox=True) 
    cmds.columnLayout(adjustableColumn=True) 
    cmds.button(label='Create Joints (1 of 3)', command=createJoints, en=True) 
    cmds.button(label='Create IK (2 of 3)', command=createIk, en=True) 
    cmds.button(label='Create Controls (3 of 3)', command=createControls, en=True) 
    cmds.button(label='Close', command=partial(closeUI, "mainWindow"), en=True) 
    cmds.showWindow("mainWindow") 
main_Window() 
+0

你检查了你的代码吗?它根本不起作用... – andy

+0

如果您只是删除不存在的调用按钮命令的函数,代码将起作用。它实际上回答了这个问题。 – UKDP

这里是WORKING MAYA Python脚本。

enter image description here

你不应该对当前的脚本中使用def main_Window(*args)

import maya.cmds as maya 

def createJoints(*args): 
    print 'createJoints button was pushed.' 

def createIk(*args): 
    print 'createIK button was pushed.' 

def createControls(*args): 
    print 'createControls button was pushed.' 

def closeWindow(*args):  
    print 'mayaWindow was closed.' 

    if (maya.window(mayaWindow, exists=True)): 
    maya.deleteUI(mayaWindow, control=True) 

mayaWindow = maya.window(title="Auto-rig", widthHeight=(300, 300), s=False, vis=True, toolbox=True) 
maya.columnLayout(adjustableColumn=True) 
maya.button(label='Create Joints...(1 of 3)', command=createJoints, en=True) 
maya.button(label='Create IK...(2 of 3)', command=createIk, en=True) 
maya.button(label='Create Controls...(3 of 3)', command=createControls, en=True) 
maya.button(label='Close', command=closeWindow, en=True) 
maya.showWindow(mayaWindow) 

您必须小心使用Maya gui小部件的字符串引用 - 您不能保证获得您要求的名称。因此,即使您的代码试图创建一个名为“X”的窗口,您可能会得到一个名为“X1”的窗口,如果您有硬编码参考,则永远找不到“X1”

正确的方法在这种情况下做的是在创建变量时捕获窗口的名称,然后使用该保存的名称。您可以使用闭包非常优雅地执行此操作:Python函数允许函数“捕捉”声明时出现的变量的值。这里有一个非常简单的例子,使用关闭和忽略窗口的ID:

import maya.cmds as cmds 

def create_window(): 
    window = cmds.window(title='Main Window') 
    column = cmds.columnLayout() 
    button = cmds.button("Delete me") 

    def close_handler(*_): 
     cmds.deleteUI(window) # 'window' is captured by the closure 

    cmds.button(button, e=True, command = close_handler) 

    cmds.showWindow(window) 
    return window 

create_window() 

如果你要记住主窗口的实际的ID是,只是存储“create_window”的结果。鉴于确保您始终知道硬编码UI项目的正确路径名称所涉及的麻烦,它很少值得一提。

您可以也应该扩展闭包的使用来处理您的用户界面的各个部分之间的其他通信。大多数情况下,与使用硬编码字符串管理全部内容相比,它更容易且更容易出错。

+0

谢谢!这绝对有帮助! – RasmusJBjork