Creating a simple pyqt window in maya

 

How to create a simple pyQt window inside of maya:

This the easiest way to get started with creating maya UIs using pyQt, and if your goal is create a super simple window, just paste the code below into your script editor and execute it. This will of coarse require that you have already installed PyQt for your version of maya.

The window created will have a single button, that will print ‘hello world’ when pressed.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import maya.OpenMayaUI as mui
import sip
from PyQt4 import QtGui, QtCore, uic, Qt


def getMayaWindow():
    """Returns as a PyQt Object, the Maya main window"""
    ptr = mui.MQtUtil.mainWindow()
    return sip.wrapinstance(long(ptr), QtCore.QObject)



class MySimpleWindow(QtGui.QMainWindow):
    """This class represents our window"""

    def __init__(self,):
        windowParent = getMayaWindow()
        super(QtGui.QMainWindow, self).__init__(windowParent)

        self.setWindowTitle('Groovy Window')

        ## Make Hello World Button
        # creates button
        self.toggleExpandButton = QtGui.QPushButton('Print "hello world" button', self)

        # move button to x pos, y pos
        self.toggleExpandButton.move(12, 22)

        # size button to width, height
        self.toggleExpandButton.setFixedSize(160, 30)

        # connect the clicking of the button to a function or method
        self.connect(self.toggleExpandButton, QtCore.SIGNAL('clicked()'), self.printHelloWorld)


    def printHelloWorld(self):
        print "hello world :P"



def openUI():
    """main function to launch the window"""

    # attempt to close the last UI before opening the
    # new one. If UI is not a window yet, it will just pass
    global UI
    try: UI.close()
    except: pass

    # create the window from the class
    UI = MySimpleWindow()

    UI.show()


# actually run the function to open the UI
openUI()

About 3dkris@gmail.com

One comment

  1. Hey, great post, I have a question for you. I want to make this a ssrhsapcleen and incorporate a modelEditor there. Is it possible? Have a look at my code and tell me if it’s possible to make this normal window into a ssrhsapcleen since i cant remove the frame from the window just using this code(setWindowFlags(Qt.Tool | Qt.FramelessWindowHint))Thank you in advance(My .ui file is just an empty widget named QWdgt .)import maya.cmds as cmdsif ( cmds.window( QWdgt’, exists=True) ): cmds.deleteUI(dialog1)dialog1 = cmds.loadUI(f=’C:/Users/Anderson/Documents/sample4.ui’)form = cmds.formLayout( parent = QWdgt )myeditor = cmds.modelEditor( CustomModelEditor , parent = form )cmds.formLayout(form, e=True, af=[(myeditor,’left’,0), (myeditor,’right’,0), (myeditor,’top’,0), (myeditor,’bottom’,0)])cmds.showWindow(dialog1)

Leave a Reply

Your email address will not be published. Required fields are marked *

*