Showing posts with label pyQT in Maya. Show all posts
Showing posts with label pyQT in Maya. Show all posts

4 November 2011

Our First Window - 01 -Creating the Window

First thing we are going to want to do now that we have PyQt installed on our machine is to create a bare bones window. But before we do, it is important to talk about qt designer and what it can do for you if you choose to use it. Qt designer is an application made by Nokia that greatly speeds up the process of designing a UI.



It basically turns much of the process of making and positioning button into drag an drop operations, and while you can not do everything you may want using this program, it does give you a very big head start. Aside from time saved in the initial layout of the UI, Qt Designer also makes it very easy to edit the layout of your UI down the road I do this quite often at work, as the tools we use evolve and grow over time. That said, if you do go the route of QT Designer (which I usually do), do keep a couple of things in mind.

1) you can't do everything in Qt Desiner, there will be certain things you would like to do, that just require you to script them, and for some tasks it is probably simpler than try to do it in designer.

2) you will likely have an extra .ui file that needs to travel with your python file. Although you can avoid this by converting it into python code, and then added to your python script, you would then need to reconvert it each time you wanted to make a change (no matter how small) in Qt Desiner. In general I prefer to keep it as a .ui file.

you can download Qt Designer from:
http://qt.nokia.com
it comes bundled with QtCreator

----------------------------------------------------------------------------------------------------------

ok, lets get started. So the first thing we are going to want to do to is to open QT Designer, and create a MainWindow.
Then feel free to populate it with some buttons (drag and drop them from the widget box column on the left, to your UI window in the middle of the work area)
Once you have a few buttons, save it in your mayaScripts directory (C:\Users\****\Documents\maya\scripts) as testWindow.ui


Next, we will create a python file in that same folder. To do this, create a text file, and rename it to testWindow.py
Now lets open the .py file and paste the following inside:


#!/usr/bin/env python

import sys
import os
import sip

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

from PyQt4 import QtGui, QtCore, uic
import PyQt4

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



selfDirectory = os.path.dirname(__file__)    #returns the folder THIS file is in
uiFile= selfDirectory+'/testWindow.ui'    #only works if the .ui file is in the same folder

#Load the ui file, and create my class
form_class, base_class = uic.loadUiType(uiFile)
    #form_class will be <class 'Ui_MainWindow'>
    #base_class will be <class 'PyQt4.QtGui.QMainWindow'>


class BaseClass():
   
    def __init__(self):
        ptr = long(long(OpenMayaUI.MQtUtil.findWindow(newMayaWindow)))
        newQtWin = sip.wrapinstance(ptr, base_class)


class UI(base_class, form_class):

   
    title = 'My wicked window'


    def __init__(self, parent=getMayaWindow()):
        #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)
        self.setWindowTitle(self.title)

      
      
      
      

   
def open():
    '''will attempt to close the window (in case there is one already open)
    and then open a new one'''

    global testWindow
    try:
        testWindow.close()
    except:
        pass
   
    testWindow = UI()
    testWindow.show()


 And once we have that, just paste the next chunk of code into your maya script editor:


import testWindow
reload(testWindow)
testWindow.open()


...And run it. This should pop up the window you created in Qt Designer.
Next post, I will go over how to add some functionality to the window.

21 August 2011

pyQT in Maya - getting started

Ok, so you probably have heard that maya switched its interface to QT in 2011, which opens up a world of possibility's for those of us who want to make UI's that can go above and beyond what MEL used to offer us.

So how do we get started?

Well if your like me, you probably are not too keen about in programming UIs in C++ to run commands we are scripting with python, so our first step is going to be to go and get 'pyQT'. pyQT is developed by Riverbank Computing (where as QT itself is developed by Nokia). PyQT allows us to write QT UI's with python.

What if I am writing code in MEL?

Well then your shit out of luck. perhaps this is the push you need to make the switch to python (having programmed in MEL for 3-4 years, I can tell you, once you learn python, you will never want to look at MEL again... ever)

Does it come with maya?

Nope. This is largely due to licensing issues.
see:    http://images.autodesk.com/adsk/files/pyqtmaya2011.pdf

So we get it from Riverbank's website? Not really. First thing is that it needs to be compiled. Furthermore from what I understand the version shipped with maya, does not line up with the way riverbanks versions (and this list of complications goes on)... but lucky for us, other TDs have done this leg work for us. For this you can thank Nathan Horne.

Now we download pre compiled file from his website:
maya 2011:   http://nathanhorne.com/?p=229
maya 2012:   http://nathanhorne.com/?p=322

and  install to C:\Program Files\Autodesk\Maya2011\Python    (or where ever your maya is installed to).

This should get you up and running (it did for me anyways, I am running on windows 7, with maya 2011). Stay tuned for our first simple window


Cheers,
Kris Andrews