如何使用PySide获取maya主窗口指针?

nin*_*o64 2 python pyqt maya pyside

我在maya中使用了PyQt4,通常我发现切换到PySide很容易,但是我无法获得指向主窗口的指针.也许有人可以理解出了什么问题.

这是我在PyQt4中所做的:

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

这很好用.当我在PySide中尝试相同时:

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

我收到以下错误:

# 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 # 
Run Code Online (Sandbox Code Playgroud)

谁知道出了什么问题?

qur*_*ban 12

您需要导入shiboken而不是sip传递QWidgetwrapInstanceQObject而不是QObject

编辑: Maya2017包含shiboken2PySide2替代shiboken,并PySide在下面的评论中指出.

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)
Run Code Online (Sandbox Code Playgroud)

请注意,sipwrapinstance是不是资本,但在shiboken.wrapInstance 的资本.

shiboken.wrapInstance()需要包装类型作为第二个参数,因此您可以QWidget作为第二个参数传递.

  • shiboken在Maya 2017上不可用.人们现在使用它来获取主窗口链接:对于QtWidgets.qApp.topLevelWidgets()中的obj ...然后检查是否有objectName()=='MayaWindow'.不要忘记PySide现在是PySide2 (5认同)
  • shiboken现在是shiboken2。目前,在Maya2017官方文档中没有记录shiboken2。 (2认同)