UKDP
UKDP

Reputation: 226

Find the last window created in Maya?

I was wondering if there is any way to find the name of the last window created in Maya, knowing that I can't add any information to the window itself before that... I checked in both the cmds and API but couldn't find anything. Maybe in PyQt but I don't know much about it.

I'm looking for any solution. Thanks

Upvotes: 1

Views: 1850

Answers (3)

theodox
theodox

Reputation: 12218

If you create a window with the window command, you'll get back the name of the window you just created:

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)

If for some reason you don't own the code that creates the window:

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

Upvotes: 0

UKDP
UKDP

Reputation: 226

Here is what I came up with, it's surely not the "cleanest" solution but it works!

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

Upvotes: 0

Ari Gold
Ari Gold

Reputation: 1548

you can work with something like a close callback, save the needed information and restore it again

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

a simple case/idea to save tha main win_id and a 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"])

Upvotes: 1

Related Questions