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()