简体   繁体   English

为什么deleteUI在使用此脚本的Maya Python中不起作用?

[英]Why does deleteUI not work in Maya Python with this script?

I'm having some issues with this script I've written in Maya Python! 我在使用Maya Python编写的这个脚本时遇到了一些问题! I want to be able to call a function to close the UI by pushing a button. 我希望能够通过按下按钮来调用函数来关闭UI。 I've looked around both here and on other sites and I can't seem to make it work. 我在这里和其他网站上都环顾四周,似乎无法让它发挥作用。 Nothing happens when I press the 'Close' button. 按“关闭”按钮时没有任何反应。

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

You have to be careful with using string references to Maya gui widgets -- you are not guaranteed to get the name you asked for. 您必须小心使用对Maya gui小部件的字符串引用 - 您无法保证获得您要求的名称。 So even if your code tries to create a window named "X", you may get a window called "X1", and if you've got the reference hard coded you'll never find "X1" 因此,即使您的代码尝试创建一个名为“X”的窗口,您也可能会得到一个名为“X1”的窗口,如果您的参考文件是硬编码的,那么您将永远找不到“X1”

The right way to do it in this case is to capture the name of the window in a variable when it's created, and then use that saved name. 在这种情况下,正确的方法是在创建变量时捕获变量窗口的名称,然后使用该保存的名称。 You can do this quite elegantly using a closure: the python feature that allows a function to 'capture' the value of a variable that was present at declaration time. 您可以使用闭包非常优雅地执行此操作:python功能允许函数“捕获”在声明时出现的变量的值。 here's a very basic example, that uses closures and ignores window ids: 这是一个非常基本的例子,它使用闭包并忽略窗口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()

If you want to remember what the actual id of the main window is, just store the result of 'create_window'. 如果你想记住主窗口的实际id是什么,只需存储'create_window'的结果。 Given the hassles involved in making sure that you always know the proper path name of hard-coded UI items, its rarely worth the hassle. 鉴于确保您始终知道硬编码UI项目的正确路径名称所涉及的麻烦,它很少值得麻烦。

You can and should extend the use of the closures to deal with other kinds of communication between bits and pieces of your UI. 您可以而且应该扩展闭包的使用,以处理UI的各个部分之间的其他类型的通信。 Most of the time it's easier and less bug prone than trying to manage it all with hard coded strings. 大多数时候,它比使用硬编码字符串管理它更容易,更容易出错。

HERE IS WORKING MAYA PYTHON SCRIPT. 这是工作玛雅PYTHON SCRIPT。

在此输入图像描述

You should not use def main_Window(*args) for current script. 您不应该将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 )
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")

or 要么

del_win(window)

back to your code 回到你的代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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