Guest User

maya_child_windows.py

a guest
Jul 10th, 2013
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.25 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. """
  4. Showing a test, inside and outside of Maya,
  5. of child windows still remaining alive after being closed.
  6.  
  7. Test can toggle the WA_DeleteOnClose attribute on each child widget:
  8.  
  9. # Dont delete child windows
  10. start()
  11.  
  12. # Delete child windows when closed
  13. start(delete=True)
  14.  
  15. """
  16. from PySide import QtCore
  17. from PySide import QtGui
  18.  
  19. IS_MAYA = QtGui.qApp is not None
  20.  
  21. if IS_MAYA:
  22.     import shiboken
  23.     import maya.OpenMayaUI as mui
  24.  
  25.  
  26. def getMayaWindow():
  27.     ptr = mui.MQtUtil.mainWindow()
  28.     return shiboken.wrapInstance(long(ptr), QtGui.QWidget)
  29.  
  30.  
  31. def checkChildren(parent, timer):
  32.     count = len([c for c in parent.children() if isinstance(c, TestWindow)])
  33.     print "Num. of TestWindows:", count
  34.  
  35.     if not count:
  36.         timer.stop()
  37.  
  38.  
  39. class TestWindow(QtGui.QMainWindow):
  40.    
  41.     def __init__ (self, parent=None) :
  42.         QtGui.QMainWindow.__init__(self, parent=parent)
  43.         self.setObjectName("testWindow")
  44.         self.layout = QtGui.QHBoxLayout()
  45.         self.label = QtGui.QLabel("test panel")
  46.         self.layout.addWidget(self.label)
  47.         self.setLayout(self.layout)
  48.  
  49.  
  50. def start(delete=False):
  51.  
  52.     # Create a main window if we are running outside Maya
  53.     if IS_MAYA:
  54.         mainWindow = getMayaWindow()
  55.     else:
  56.         app = QtGui.QApplication([])
  57.         mainWindow = QtGui.QMainWindow()
  58.         mainWindow.setWindowTitle("I am the Maya Main Window")
  59.         mainWindow.resize(600,400)
  60.         mainWindow.show()
  61.         mainWindow.raise_()
  62.  
  63.     # Make some child windows
  64.     for i in xrange(5):
  65.         window = TestWindow(parent=mainWindow)
  66.         window.setAttribute(QtCore.Qt.WA_DeleteOnClose, delete)
  67.         window.setWindowTitle("I am a test window")
  68.         window.resize(300,200)
  69.         window.show()
  70.         window.raise_()
  71.  
  72.     # Check the children every 1.5 seconds
  73.     timer = QtCore.QTimer(mainWindow)
  74.     timer.timeout.connect(lambda: checkChildren(mainWindow, timer))
  75.     timer.start(1500)
  76.  
  77.     # Stop the check timer after 15 seconds
  78.     QtCore.QTimer.singleShot(15 * 1000, timer.stop)
  79.  
  80.     if not IS_MAYA:
  81.         app.exec_()
  82.  
  83.  
  84. if __name__ == "__main__":
  85.     import sys
  86.  
  87.     args = sys.argv[1:]
  88.     delete = '-delete' in args
  89.     start(delete)
Advertisement
Add Comment
Please, Sign In to add comment