Maya中PySide2的wrapInstance参数错误问题求助
Fixing the
wrapInstance TypeError in Maya PySide2 Hey there! Let's get that TypeError sorted out for your Maya PySide2 setup. The error you're seeing comes from two key mismatches between your code and how PySide2 works with Python 3 in Maya:
What's Causing the Error?
- Python 3 doesn't support
long: Back in Python 2, we usedlong()to handle pointer values, but Python 3 mergedintandlonginto a single integer type. Callinglong(pointer)is invalid here. - Incorrect argument for
wrapInstance: The second parameter needs to be a specific Qt widget class (likeQMainWindow), not the entireQtWidgetsmodule itself.
Corrected Code
from PySide2 import QtWidgets, QtGui import maya.cmds as cmds import maya.OpenMayaUI as mui import shiboken2 def getMayaWindow(): pointer = mui.MQtUtil.mainWindow() if pointer is not None: # Remove long() cast and target the specific QMainWindow class return shiboken2.wrapInstance(int(pointer), QtWidgets.QMainWindow)
Quick Breakdown of Changes
- We swapped
long(pointer)forint(pointer)(in most Maya Python 3 environments, you can even skip theint()cast entirely sincepointerreturns as an integer already). - Replaced
QtWidgetswithQtWidgets.QMainWindowto tellwrapInstanceexactly what type of Qt object we're wrapping the Maya main window into.
This should resolve the TypeError and give you a valid reference to the Maya main window for your PySide2 UI development.
内容的提问来源于stack exchange,提问作者Vlad
相关产品推荐
相关产品推荐

