Maya PySide

From wikinotes

qt objects from maya ui primitives

# mainwindow
ptr = OpenMayaUI.MQtUtil.mainWindow()
wid = shiboken2.wrapInstance(long(ptr), QtWidgets.QWidget)

# misc widget
    # find using:
    # cmds.lsUI(type='helpLine')
ptr = OpenMayaUI.MQtUtil.findControl('helpLine1')
statusbar = shiboken2.wrapInstance(long(ptr), QtWidgets.QStatusBar)


Docking QWidgets to Maya

As of maya2017, the docking that users are used is performed relative to maya windows (or more specifically, workspaceControls).

  • you can horizontall/vertically split a dock-cell
  • you can add a widget to a new tab within a dock-cell
  • you can add a widget to maya itself

Here is an example (specific to maya2018's docking implementation) on how to create and dock a widget.

class MyWindow(mayaMixin.MayaQWidgetDockableMixin, QtWidgets.QMainWindow):
    def __init__(self):

        ptr = OpenMayaUI.MQtUtil.mainWindow()
        mayawin = shiboken2.wrapInstance(long(ptr), QtWidgets.QWidget)
        super(MyWindow, self).__init__(mayawin)

        btn = QtWidgets.QPushButton('test window')

        btn.setText('test window')

        btn.setSizePolicy(
            QtWidgets.QSizePolicy.Expanding,
            QtWidgets.QSizePolicy.Expanding,
        )

        self.setCentralWidget(btn)

    def dock(self, area):
        # ===============
        # SET DOCK PARAMS
        # ===============
        ws_controlname = win.objectName() + 'WorkspaceControl'
        ws_controlname = cmds.workspaceControl(
            ws_controlname,
            label=win.windowTitle(),
            retain=True, # default
            loadImmediately=True,
            initialWidth=win.width(),
            tabPosition=['north', False],        # where does the tab-bar appear for this widget only (default 'north'), and if it exists
            #dockToControl=['Outliner', 'top'],  # cmds.lsUI(type='workspaceControl'), (left, right, top, bottom)
            tabToControl=['Outliner', 0],
        )

        currParent = omui.MQtUtil.getCurrentParent()
        mixinPtr = omui.MQtUtil.findControl(self.objectName())
        omui.MQtUtil.addWidgetToMayaLayout(long(mixinPtr), long(currParent))

		# Add this control to the list of controls created in Python
        mayaMixin.mixinWorkspaceControls[ws_controlname] = self

        # ====
        # SHOW
        # ====
        QtWidgets.QWidget.setVisible(self, True) # NOTE: Explicitly calling QWidget.setVisible() as using super() breaks in PySide: super(self.__class__, self).show()

        # Handle special case if the parent is a QDockWidget (dockControl)
        parent = self.parent()
        if parent:
            parentName = parent.objectName()
            if parentName and len(parentName) and cmds.workspaceControl(parentName, q=True, exists=True):
                if cmds.workspaceControl(parentName, q=True, visible=True):
                    cmds.workspaceControl(parentName, e=True, restore=True)
                else:
                    cmds.workspaceControl(parentName, e=True, visible=True)