我正在尝试将旧的Maya python脚本转换为Maya 2017. 2017年他们做了一些更改,包括从PySide切换到PySide 2和Qt4切换到Qt5。我没有任何这些库甚至python的经验。
我做的第一件事是尝试通过pyqt4topyqt5运行它,没有检测到必要的更改。
我认为脚本的核心功能在两个版本中是相同的,但是由于这些更改,GUI加载失败。导入库的原始脚本如下:
import shiboken
from PySide import QtGui
import maya.OpenMayaUI as apiUI
from cStringIO import StringIO
import pysideuic
import xml.etree.ElementTree as xml
def get_maya_window():
ptr = apiUI.MQtUtil.mainWindow()
if ptr is not None:
return shiboken.wrapInstance(long(ptr), QtGui.QMainWindow)
def load_ui_type(ui_file):
parsed = xml.parse(ui_file)
widget_class = parsed.find('widget').get('class')
form_class = parsed.find('class').text
with open(ui_file,'r') as f:
o = StringIO()
frame = {}
pysideuic.compileUi(f, o, indent = 0)
pyc = compile(o.getvalue(), '<string>', 'exec')
exec pyc in frame
# Fetch the base_class and form class based on their type in the xml from design
form_class = frame['Ui_{0}'.format(form_class)]
base_class = eval('QtGui.{0}'.format(widget_class))
return form_class, base_class
我将PySide的所有实例更改为PySide2,shiboken更改为shiboken2(2017年maya中的另一个更改),并将pysideuic更改为pyside2uic。在测试脚本时,我收到了错误
Error: line 1: AttributeError: file <string> line 1: 'module' object has no attribute 'QMainWindow' #
(第1行引用另一个脚本中的行:
from JUM.core.loadUIFile import get_maya_window, load_ui_type
调用此文件)
在查看Qt5文档后,我确定QMainWindow现在是QtWidgets的一部分,包含在PyQt5中,而不是QtGui,所以我也替换了它。目前脚本代码是
import shiboken2
from PyQt5 import QtWidgets
import maya.OpenMayaUI as apiUI
from cStringIO import StringIO
import pyside2uic
import xml.etree.ElementTree as xml
def get_maya_window():
ptr = apiUI.MQtUtil.mainWindow()
if ptr is not None:
return shiboken2.wrapInstance(long(ptr), QtWidgets.QMainWindow)
def load_ui_type(ui_file):
parsed = xml.parse(ui_file)
widget_class = parsed.find('widget').get('class')
form_class = parsed.find('class').text
with open(ui_file,'r') as f:
o = StringIO()
frame = {}
pyside2uic.compileUi(f, o, indent = 0)
pyc = compile(o.getvalue(), '<string>', 'exec')
exec pyc in frame
# Fetch the base_class and form class based on their type in the xml from design
form_class = frame['Ui_{0}'.format(form_class)]
base_class = eval('QtWidgets.{0}'.format(widget_class))
return form_class, base_class
然而我仍然得到完全相同的错误,所以我认为我的模块导入有问题。知道Qt5的人可以在python chime中使用吗?
答案 0 :(得分:0)
简单的答案就是这样。 QTWidgets
不是QTGui
。在Pyside2中,你不仅要导入shiboken2和wrapinstance2,而且所有好的东西都进入了QTWidgets
。
def get_maya_window():
ptr = apiUI.MQtUtil.mainWindow()
if ptr is not None:
return shiboken2.wrapInstance2(long(ptr), QtWidgets.QWidget)