Maya PySide Cheatsheet 2017

This is a working document where I write down my notes about using PySide for Maya, which may prove to be useful to others as well. I plan on adding more examples in the future.

Examples assume that the required packages are imported.

Import PySide2 or PySide?

# use PySide2 if available (Maya 2017+) otherwise PySide
try:
    from PySide2.QtCore import *
    from PySide2.QtGui import *
    from PySide2.QtWidgets import *
    from PySide2 import __version__
    from shiboken2 import wrapInstance
except ImportError:
    from PySide.QtCore import *
    from PySide.QtGui import *
    from PySide import __version__
    from shiboken import wrapInstance

Common Methods

QMainWindow()  # create a window
QHBoxLayout()  # horizontal layout
QVBoxLayout()  # vertical layout
QPushButton()  # create button
QCheckBox()  # create checkbox
QSpacerItem()  # create spacer
QFont()  # create font
import OpenMayaUI as omui
omui.MQtUtil.findControl()  # find Qt control object

Example: Create a simple window parented to Maya

# import wrapInstance
try:
    from shiboken2 import wrapInstance
except ImportError:
    from shiboken import wrapInstance

# get Maya's main window
def getMayaMainWindow():
    mayaMainWindowPtr = omui.MQtUtil.mainWindow()
    return wrapInstance(long(mayaMainWindowPtr), QWidget)


mainWindow = getMayaMainWindow()

# create a new window parented to Maya's window
window = QMainWindow(mainWindow)
window.setObjectName("MyWindow")
window.setWindowTitle("My Window")

# create and attach widget
mainWidget = QWidget()
window.setCentralWidget(mainWidget)

# create and add layout
layout = QVBoxLayout(mainWidget)

# create button
button = QPushButton("Click me")
layout.addWidget(button)

# finally, show the window
window.show()

Example: Finding a control widget

# create a checkbox...
layout = QHBoxLayout()
checkbox = QCheckBox("Label")
checkbox.setObjectName("MyCheckbox")
checkbox.setChecked(True)
layout.addWidget(checkbox)

# ... and find it again
ptr = omui.MQtUtil.findControl("MyCheckbox")
checkbox = shiboken.wrapInstance(long(ptr), QtGui.QCheckBox)
value = checkBox.isChecked()

Example: Boilerplate code for window

"""
Use the following code to load the script after saving it to Maya's scripts folder.
import maya_pyside_boilerplate as min_ui
# min_ui.delete()  # try to delete it before reloading to avoid duplicates, should only be needed when testing
# reload(min_ui)  # only reload when testing
min_ui.create()
"""

import maya.OpenMayaUI as omui
import os
from functools import partial

# PySide2 is for Maya 2017+, PySide is for Maya 2016-(2011 ?)
try:
    from PySide2.QtCore import *
    from PySide2.QtGui import *
    from PySide2.QtWidgets import *
    from shiboken2 import wrapInstance
except ImportError:
    from PySide.QtCore import *
    from PySide.QtGui import *
    from shiboken import wrapInstance

basic_window = None


def get_main_maya_window():
    ptr = omui.MQtUtil.mainWindow()
    return wrapInstance(long(ptr), QMainWindow)


def create():
    global basic_window
    
    if basic_window is None:
        basic_window = BasicUI(parent=get_main_maya_window())
        print '// Created %s' % basic_window.objectName()
        
    basic_window.show()  # show the window
    basic_window.raise_()  # raise it on top of others
    basic_window.activateWindow()  # set focus to it


def delete():
    global basic_window
    if basic_window is not None:
        print '// Deleting %s' % basic_window.objectName()
        basic_window.deleteLater()
        basic_window = None


class BasicUI(QMainWindow):
    def __init__(self, parent):
        super(BasicUI, self).__init__(parent)
        
        self.parent = parent
        self.window_name = 'BasicWindowObj'
        
        # Set basic window properties
        self.setWindowTitle('Basic UI')
        self.setObjectName(self.window_name)
        
        if os.name == 'nt':  # windows platform
            self.setWindowFlags(Qt.Window)
        else:
            self.setWindowFlags(Qt.Window | Qt.WindowStaysOnTopHint)
            
        self.setProperty("saveWindowPref", True)  # maya's automatic window management
        
        # Define window dimensions
        self.setMinimumWidth(200)
        self.setMaximumWidth(400)
        self.setMinimumHeight(42)
        self.setMaximumHeight(200)
        
        # Setup central widget and layout
        self.central_widget = QWidget()
        self.central_layout = QVBoxLayout(self.central_widget)
        self.central_layout.setContentsMargins(8, 8, 8, 8)
        self.setCentralWidget(self.central_widget)
        
        self.example_layout()
    
    def example_layout(self):
        layout = QHBoxLayout()
        layout.setAlignment(Qt.AlignTop)
        layout.setSpacing(8)
        
        label = QLabel('Fancy')
        label.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum)
        label.setAlignment(Qt.AlignLeft | Qt.AlignVCenter)
        layout.addWidget(label)
        
        self.button = QPushButton('Button')
        self.button.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Minimum)
        self.connect(self.button, SIGNAL("clicked()"), partial(self.button_clicked, self.button))
        layout.addWidget(self.button)
        
        self.central_layout.addLayout(layout)
    
    def button_clicked(self, obj):
        print '// Clicked on %s' % obj.text()
        self.button.setText('Clicked')
    
    def hideEvent(self, event):
        # Close the window instead of hiding it when clicking on 'x' in the title bar
        if not self.isMinimized():
            self.close()
            delete()
    
    def keyPressEvent(self, event):
        # Prevents keyboard press events from being passed onto Maya
        pass
Write a comment