查找在 Maya 中创建的最后一个 window?

Find the last window created in Maya?

我想知道是否有任何方法可以找到在 Maya 中创建的最后一个 window 的名称,我知道在此之前我无法向 window 本身添加任何信息.. . 我检查了 cmds 和 API 但找不到任何东西。也许在 PyQt 中,但我对此了解不多。

我正在寻找任何解决方案。谢谢

您可以使用关闭回调之类的东西,保存所需的信息并再次恢复它

def restoreLayout(self):
    """
    Restore the layout of each widget
    """
    settings=self.settings
    try:
        self.restoreGeometry(settings.value("geometry").toByteArray())
        self.restoreState(settings.value("windowState").toByteArray())
        size=settings.value('fontSize').toFloat()[0]
        self.setFontSize(size)
    except:
        pass

def saveLayout(self):
    """
    Save the layout of each widget
    Save the main window id to your data base
    """
    settings=self.settings
    settings.setValue("geometry", self.saveGeometry())
    settings.setValue("windowState", self.saveState())
    settings.setValue("fontSize", app.font().pointSize())

def closeEvent(self, event):
    QtGui.QMainWindow.closeEvent(self, event)
    self.saveLayout()

一个简单的 case/idea 来保存主 win_id 和一个 child button_id:

from functools import partial
import json
def close_ui(*args):
    win_id = args[0]
    if cmds.window(win_id, exists=True):
       cmds.deleteUI(win_id, window=True)
       with open('dataBase/ui/uidata.json', 'w') as outfile:
            json.dump(args, outfile)
win = {}
win["main_win"] = cmds.window()
cmds.columnLayout()
cmds.text( label='closing it' )
win["btn"] = cmds.button( label='Close')
cmds.button(win["btn"],e=True, command=partial(close_ui, win["main_win"], win["btn"]))
cmds.showWindow(win["main_win"])

这是我想到的,它肯定不是 "cleanest" 解决方案,但它有效!

# List all the currently opened windows
uisBefore = cmds.lsUI (wnd = True)

# Execute the function which may or may not create a window
func(*args, **kwargs)

# List all the opened windows again
uisAfter = cmds.lsUI (wnd = True)

# Find all the windows that were opened after executing func()
newUIs = [ui for ui in uisAfter if ui not in uisBefore]

如果您使用 window 命令创建 window,您将取回刚刚创建的 window 的名称:

import maya.cmds as cmds
w = cmds.window()
c= cmds.columnLayout()

def who_am_i(*_):
    print "window is", w

b = cmds.button('push', c=who_am_i)
cmds.showWindow(w)

如果由于某种原因您不拥有创建 window 的代码:

existing_windows = set(cmds.lsUI(type = 'window'))
// make your window here
new_windows = list(set(cmds.lsUI(type = 'window') - existing_windows))