You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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 used long() to handle pointer values, but Python 3 merged int and long into a single integer type. Calling long(pointer) is invalid here.
  • Incorrect argument for wrapInstance: The second parameter needs to be a specific Qt widget class (like QMainWindow), not the entire QtWidgets module 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) for int(pointer) (in most Maya Python 3 environments, you can even skip the int() cast entirely since pointer returns as an integer already).
  • Replaced QtWidgets with QtWidgets.QMainWindow to tell wrapInstance exactly 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:29:00