How to get maya main window pointer using PySide?

10,652

Solution 1

You need to import shiboken instead of sip and pass QWidget to wrapInstance instead of QObject

Edit: Maya2017 contains shiboken2 and PySide2 instead of shiboken and PySide as pointed out in comments below.

import shiboken
from PySide import QtGui, QtCore
import maya.OpenMayaUI as apiUI

def getMayaWindow():
    """
    Get the main Maya window as a QtGui.QMainWindow instance
    @return: QtGui.QMainWindow instance of the top level Maya windows
    """
    ptr = apiUI.MQtUtil.mainWindow()
    if ptr is not None:
        return shiboken.wrapInstance(long(ptr), QtGui.QWidget)

Please note that sip has wrapinstance in which i is not capital but in shiboken.wrapInstance i is capital.

shiboken.wrapInstance() requires wrapertype as a second argument, so you can pass QWidget as a second argument.

Solution 2

Because the previous answer has become somewhat out-of-date, here is a more current version to save someone the trouble of having to update it themselves.

Two approaches for getting the maya main window:

  • using PySide2:
from PySide2 import QtWidgets

global app
app = QtWidgets.QApplication.instance() #get the qApp instance if it exists.
if not app:
    app = QtWidgets.QApplication(sys.argv)


def getMayaMainWindow():
    mayaWin = next(w for w in app.topLevelWidgets() if w.objectName()=='MayaWindow')

    return mayaWin

print(getMayaMainWindow())
  • Using shiboken2 and the maya api:
import maya.OpenMayaUI as apiUI
from PySide2 import QtWidgets

try:
    import shiboken2
except:
    from PySide2 import shiboken2


def getMayaMainWindow():
    ptr = apiUI.MQtUtil.mainWindow()
    mayaWin = shiboken2.wrapInstance(long(ptr), QtWidgets.QWidget)

    return mayaWin

print(getMayaMainWindow())
Share:
10,652
ninhenzo64
Author by

ninhenzo64

I <3 python

Updated on June 07, 2022

Comments

  • ninhenzo64
    ninhenzo64 about 2 years

    I've used PyQt4 in maya quite a bit, and generally I've found it quite easy to switch to PySide, but I'm having trouble getting the pointer to the main window. Perhaps someone can understand what's going wrong.

    Here's what I do in PyQt4:

    import sip, PyQt4.QtCore
    import maya.OpenMayaUI as mui
    ptr = mui.MQtUtil.mainWindow()
    parent = sip.wrapinstance(long(ptr), PyQt4.QtCore.QObject)
    

    This works fine. When I try the same in PySide:

    import sip, PySide.QtCore
    import maya.OpenMayaUI as mui
    ptr = mui.MQtUtil.mainWindow()
    parent = sip.wrapinstance(long(ptr), PySide.QtCore.QObject)
    

    I get the following error:

    # Error: wrapinstance() argument 2 must be sip.wrappertype, not Shiboken.ObjectType
    # Traceback (most recent call last):
    #   File "<maya console>", line 4, in <module>
    # TypeError: wrapinstance() argument 2 must be sip.wrappertype, not Shiboken.ObjectType # 
    

    Anyone know what's going wrong?

  • Darkgaze
    Darkgaze over 7 years
    shiboken is not available on Maya 2017. People use this to get the main window link now: for obj in QtWidgets.qApp.topLevelWidgets()... then check if any has objectName() == 'MayaWindow' . Don't forget that PySide is now PySide2
  • Darkgaze
    Darkgaze over 7 years
    shiboken is now shiboken2. Right now, shiboken2 is not documented on Maya2017 official docs.