Advertisement
gmendieta

Maya dock window with pyside2

Mar 7th, 2018
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.72 KB | None | 0 0
  1. """
  2. File: baseWindow.py
  3. Author: Gorka Mendieta
  4. Implementation of a Base Window using PySide2
  5. """
  6. import PySide2.QtCore as qc
  7. import PySide2.QtWidgets as qw
  8. import maya.OpenMayaUI as omui
  9. from maya.app.general.mayaMixin import MayaQWidgetDockableMixin
  10. from shiboken2 import wrapInstance
  11.  
  12. import maya.cmds as mc
  13.  
  14.  
  15. def GetMayaWindow():
  16.     '''Get Main Maya Window'''
  17.     ptr = omui.MQtUtil.mainWindow()
  18.     return wrapInstance(long(ptr), qw.QWidget)
  19.  
  20.  
  21. class BaseWindow(MayaQWidgetDockableMixin, qw.QMainWindow):
  22.     _windowName = 'BaseUI'
  23.     _windowTitle = 'Base UI'
  24.     _windowSize = (300, 600)
  25.     _minSize = (400, 600)
  26.  
  27.     def __init__(self, parent=GetMayaWindow()):
  28.         """"""
  29.         super(BaseWindow, self).__init__(parent)
  30.  
  31.         self._dockable = False
  32.         self.widgets = {}
  33.         self.BuildUI()
  34.  
  35.     def BuildUI(self):
  36.         '''
  37.  
  38.        :param name:
  39.        :param width:
  40.        :param height:
  41.        :return:
  42.        '''
  43.         self.widgets = {}
  44.  
  45.         if mc.window(self._windowName, exists=True):
  46.             mc.deleteUI(self._windowName)
  47.  
  48.         self.setObjectName(self._windowName)
  49.         self.setWindowTitle(self._windowTitle)
  50.         self.setMinimumSize(self._minSize[0], self._minSize[1])
  51.         self.resize(self._windowSize[0], self._windowSize[1])
  52.  
  53.         self.mainWidget = qw.QWidget()
  54.  
  55.         self.widgets['mainWidget'] = qw.QWidget()
  56.         self.mainLayout = qw.QVBoxLayout()
  57.         self.widgets['mainLayout'] = self.mainLayout
  58.         self.mainWidget.setLayout(self.mainLayout)
  59.         self.setCentralWidget(self.mainWidget)
  60.  
  61.         self.BuildCustomUI()
  62.         self.BuildMenu()
  63.         self.BuildConnections()
  64.  
  65.     def BuildCustomUI(self):
  66.         """
  67.        To be overriden
  68.        :return: None
  69.        """
  70.         print ('BaseWindow.BuildCustomUI')
  71.  
  72.     def BuildMenu(self):
  73.         """
  74.        To be overriden
  75.        :return: None
  76.        """
  77.         print ('BaseWindow.BuildMenu')
  78.         self.windowMenu = self.menuBar().addMenu('Window')
  79.         self.dockableAct = qw.QAction('Dockable Window', self)
  80.         self.dockableAct.setCheckable(True)
  81.         self.dockableAct.triggered.connect(self.UpdateDockable)
  82.         self.windowMenu.addAction(self.dockableAct)
  83.  
  84.     def BuildConnections(self):
  85.         """"""
  86.         print ('BaseWindow.BuildConnections')
  87.  
  88.     def Show(self, dockable=False):
  89.         """"""
  90.         self._dockable = dockable
  91.         self.dockableAct.setChecked(self._dockable)
  92.         self.show(dockable=self._dockable)
  93.  
  94.     def UpdateDockable(self):
  95.         """"""
  96.         self._dockable = self.dockableAct.isChecked()
  97.         self.Show(self._dockable)
  98.        
  99. window = BaseWindow()
  100. window.Show(dockable=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement