Skip to content

Instantly share code, notes, and snippets.

@zewt
Created October 19, 2021 18:06
Show Gist options
  • Save zewt/59757a1b32c56f1f46a38a54cc00d129 to your computer and use it in GitHub Desktop.
Save zewt/59757a1b32c56f1f46a38a54cc00d129 to your computer and use it in GitHub Desktop.

Revisions

  1. zewt created this gist Oct 19, 2021.
    40 changes: 40 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    from PySide2 import QtCore, QtGui, QtWidgets
    from maya import OpenMayaUI as omui
    from shiboken2 import wrapInstance

    class ClosingWindow(QtGui.QWindow):
    def eventFilter(self, object, event):
    # Prevent this window from being closed. It should stay alive until Maya exits.
    if object is self and event.type() == QtCore.QEvent.Close:
    return True

    return False

    def __init__(self):
    super().__init__()

    # Try to stay hidden and at the end of alt-tab, but make sure we have an alt-tab entry.
    self.setTitle('Closing...')
    self.setFlags(QtCore.Qt.Dialog|QtCore.Qt.Window|QtCore.Qt.WindowStaysOnBottomHint)
    self.installEventFilter(self)

    class MainWindowFilter(QtCore.QObject):
    def eventFilter(self, object, event):
    # Open CloseWindow when the main Maya window is closed.
    global window
    if event.type() == QtCore.QEvent.Close:
    window = ClosingWindow()
    window.show()
    window.setVisibility(QtGui.QWindow.Minimized)

    return False

    def __init__(self):
    super().__init__()
    main_window = omui.MQtUtil.mainWindow()
    window = wrapInstance(int(main_window), QtWidgets.QMainWindow)
    window.window().installEventFilter(self)

    def go():
    global filter
    filter = MainWindowFilter()