I’ve been messing around this week getting PySide for Maya 2013 Win7 x64 up and running. Using a bunch of great guides on the net, I feel like I’ve pieced it together. First, I wanted to post the PySide version of Nathan’s PyQt tutorial for anyone else looking for this information. This is how I got it working on my end:
import shiboken
from PySide import QtGui, QtCore
import maya.OpenMayaUI as apiUI
from cStringIO import StringIO
import pysideuic
import xml.etree.ElementTree as xml
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.QMainWindow)
def loadUiType(uiFile):
"""
Pyside lacks the "loadUiType" command, so we have to convert the ui file to py code in-memory first
and then execute it in a special frame to retrieve the form_class.
"""
parsed = xml.parse(uiFile)
widget_class = parsed.find('widget').get('class')
form_class = parsed.find('class').text
with open(uiFile, '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 designer
form_class = frame['Ui_%s'%form_class]
base_class = eval('QtGui.%s'%widget_class)
return form_class, base_class
uiFile = 'c:/users/cadams/desktop/example1.ui'
listExample_form, listExample_base = loadUiType(uiFile)
class ListExample(listExample_form, listExample_base):
def __init__(self, parent=getMayaWindow()):
super(ListExample, self).__init__(parent)
self.setupUi(self)
#The names "addItemBtn" and "removeItemBtn"
#come from the "objectName" attribute in Qt Designer
#the attributes to access them are automatically created
#for us when we call setupUi()
#Designer ensures that the names are unique for us.
self.addItemBtn.clicked.connect(self.addItem)
self.removeItemBtn.clicked.connect(self.removeItem)
def addItem(self):
"""
Add a new item to the end of the listWidget
"""
item = QtGui.QListWidgetItem(self.listWidget)
item.setText('Item #%s!'%self.listWidget.count())
def removeItem(self):
"""
Remove the last item from the listWidget
"""
count = self.listWidget.count()
if count:
self.listWidget.takeItem(count-1)
le = ListExample()
le.show()
Everything is largely the same, minus the obvious change of module imports. I did find that I had to change getMayaWindow() to return an object of type QtGui.QMainWindow instead of QtCore.QObject. Perhaps this is a nuance of the shiboken.wrapInstance() implementation, which is supposed to mirror sip.wrapinstance() in PyQt.
I’d love to hear from anyone else who’s got PySide to compile and work for 2013 to share experiences.
-csa