Thursday, 21 July 2011

PyQT maya widgets

Some thing I have been fightig with for some time is putting maya widgets (modelEditors,outliners etc) in to a pyqt ui so thought I'd share this:


import maya.cmds as cmds

import sip

from pymel.core import *
from pymel.all import *

from PyQt4 import QtGui, QtCore, uic


def getMayaWindow():
    'Get the maya main window as a QMainWindow instance'
    ptr = mui.MQtUtil.mainWindow()
    return sip.wrapinstance(long(ptr), QtCore.QObject)

def getControl(controlName):
    ptr = mui.MQtUtil.findControl(controlName)
    return sip.wrapinstance(long(ptr), QtCore.QObject)


uiFile = '/path/to/mainwindow.ui'

form_class, base_class = uic.loadUiType(uiFile)
class Window(base_class, form_class):
    def __init__(self, parent=getMayaWindow()):
        '''A custom window with a demo set of ui widgets'''
        #init our ui using the MayaWindow as parent
        super(base_class, self).__init__(parent)
        #uic adds a function to our class called setupUi, calling this creates all the widgets from the .ui file
        self.setupUi(self)

        modeditor=modelEditor()

        self.qtEditor=getControl(modeditor)
        self.qtEditor.setParent(self.viewFrame)
        self.viewFrame.layout().addWidget(self.qtEditor)
        #works in 2012+
        self.modeditor=modeditor.shortName()
        mel.eval('modelEditor -e -da "smoothShaded" '+myWindow.modeditor.shortName())
        self.qtEditor

    def mycommand(self):
        print "HAHAHAHAHA the POWER!!!!!!!!!"

def main():
        global myWindow
        myWindow = Window()
        myWindow.show()


main()

1 comment:

  1. Hi!
    Great example, but...
    Can you advise me please, what is wrong in mine modified version? I see just white layout in my window. Thank you in advance!

    from PyQt4 import QtCore, QtGui

    import maya.cmds as maya
    import maya.OpenMayaUI as mui

    import sip

    def getMayaWindow():
    'Get the maya main window as a QMainWindow instance'
    ptr = mui.MQtUtil.mainWindow()
    return sip.wrapinstance(long(ptr), QtCore.QObject)

    def getControl(controlName):
    ptr = mui.MQtUtil.findControl(controlName)
    return sip.wrapinstance(long(ptr), QtCore.QObject)

    class MyDialog(QtGui.QDialog):

    def __init__(self, parent=getMayaWindow()):
    super(MyDialog, self).__init__(parent)

    self.setObjectName("MyWindow")
    self.resize(800, 600)
    self.setWindowTitle("PyQt ModelPanel Test")

    modeditor=modelEditor()
    self.qtEditor=getControl(modeditor)

    self.verticalLayout = QtGui.QVBoxLayout(self)
    self.verticalLayout.setObjectName("verticalLayout")
    self.verticalLayout.addWidget(self.qtEditor)

    self.modeditor=modeditor.shortName()
    #mel.eval('modelEditor -e -da "smoothShaded" '+myWindow.modeditor.shortName())
    self.qtEditor

    def show():
    global myWindow
    myWindow = MyDialog()
    myWindow.show()

    ReplyDelete