我在使用Maya Python编写的这个脚本时遇到了一些问题!我希望能够通过按下按钮来调用函数来关闭UI。我在这里和其他网站上都环顾四周,似乎无法让它发挥作用。当我按下“关闭”时没有任何反应。按钮。
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()
答案 0 :(得分:2)
您必须小心使用对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项目的正确路径名称所涉及的麻烦,它很少值得麻烦。
您可以而且应该扩展闭包的使用,以处理UI的各个部分之间的其他类型的通信。大多数时候,它比使用硬编码字符串管理它更容易,更容易出错。
答案 1 :(得分:1)
这是工作的玛雅抒情诗。
您不应将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 )
答案 2 :(得分:0)
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()