I've been using a bit of PyQt in Maya lately and felt like writing up some of my thoughts and solutions to problems I've come across in learning this module. I'm going to assume who ever is reading this has Maya 2011+ installed and intermediate knowledge of Python. If you're reading this as a beginner, I suggest checking out the following sites:
Python in Maya:
http://www.chadvernon.com/blog/resources/python-scripting-for-maya-artists/
PyQt examples:
http://diotavelli.net/PyQtWiki/Tutorials - A collection of PyQt tutorial websites.
http://zetcode.com/tutorials/pyqt4/ - One of my favourites.
Note: These are written as standalone applications so won't work in Maya if you copy/paste. Just run these examples in IDLE or interpreter of your choice. This is what causes Maya to stall:
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
Maya already has it's own QApplication running when it opens, so creating a new one isn't necessary.
Maya PyQt window:
First up, let's start with a simple PyQt window in Maya that has a button which prints out a message!
Paste this into Maya:
from PyQt4 import QtGui
# inherit QtGui.QMainWindow class
class Main(QtGui.QMainWindow):
def __init__(self, parent=None):
# runs __init__ in QMainWindow first, then overrides with Main
super(Main, self).__init__(parent)
# make a central widget
self.centralWidget = QtGui.QWidget(self)
# attach widget to the PyQt window
self.setCentralWidget(self.centralWidget)
# set the window title
self.setWindowTitle("PyQt window")
# setup UI method
self.setup_UI()
# this method add things to the QMainWindow
def setup_UI(self):
# make a vertical box layout and set it's parent to the central widget
self.mainLayout=QtGui.QVBoxLayout(self.centralWidget)
# make a button
helloButton=QtGui.QPushButton("Hello")
# connect button's click slot to say_hello method
helloButton.clicked.connect(self.say_hello)
# add the button to the main layout
self.mainLayout.addWidget(helloButton)
# set the QMainWindow's height and width to be fixed with given integers
self.setFixedWidth(200)
self.setFixedHeight(40)
# bind this layout to QMainWindow
self.setLayout(self.mainLayout)
# make a print method
def say_hello(self, *args):
print "Hello, Maya!"
# show the window!
myWindow=Main()
myWindow.show()
There you have it! An example of displaying a simple PyQt window in Maya. Over the next couple of weeks I'd like write about some of the following topics:
- How to work with a QTreeWidget
- Mixing PyQt and Maya UI (reparenting windows and scripted panels)
- Custom right click menus on QWidgets
- Adding popup windows
- Parenting to Maya's window explanation