I had several people ask me this and usually I say. Install PyQt … on every machine that needs it and voilla. However this can not always be done and if you want to share your tools online then you just want your tools to work out of the box.
So hereby a short tutorial on how to build a QT interface with Designer that work in Maya. Since maya 2012 QT Designer is shipped with the installation of maya. You can find it here “C:\Program Files\Autodesk\<maya version>\bin\designer.exe”. This opens QT designer default interface and you can start creating your own UI’s with simple drag and drop actions. I will not go in depth on how to actually use QT designer in itself as I’ll leave that for a later time.
There are a few standard QT widgets(buttons, sliders etc) that Maya recognizes. As soon as you load the UI in maya, maya will detect these and will replace the widgets with maya’s own version. This means that you can edit, query and delete most of these detectable widgets just by using maya mel or python or pymel. No need for pyqt (even though that does make it a 100 times easier).
Someone on the creative crash website wrote a great introduction to using the QT interface. http://www.creativecrash.com/tutorials/using-qt-designer-for-mel-interfaces Especially the list of controls is very useful to keep handy when you are getting started with QT. Now I will try to skip most of the things that are covered there but some stuff might overlap.
1) Giving commands to button:
In QT Designer you can add a Dynamic Property that gets called every time the button is clicked. You can do this by going into the properties , click the plus icon and click “other… ”
Then in the “Create Dynamic Property” window, write “-c” without the quotes in the property name section. “-c” resembles the maya mel command for a button. You can add almost all commands like this. But most of the time you will only need “-c” or “-cc” for sliders etc. (*Note if you want to use python commands use a + instead of – )
Now in your property list you will see a new dynamic property at the end of the list. Here you can type any Maya Mel !!! command. However make sure its encapsulated in a string like this.
As you can see the command in itself needs to be in a string. This might cause some confusion as you will have to “escape” any other quotation marks by using a \. Do a search on escaping strings and you will find loads of information on this. As for python code just encapsulate it in a
"python(\" your python code goes here\")"
or use a +c instead of -c.
If you have multiple lines of python code you want to put in one line use the always dreaded and nightmarish ; (semi colon).
They still give me nightmares from the time I was using mel. haha
import myModule as mm; var = mm.myFunction(); print var;
2) Loading the UI in maya.
This is as straightforward as it can get. Make ourselves a simple window. Also added a dock control to it so its dock able everywhere.
## Define maya commands
import maya.cmds as cmds
## Define path to ui file
pathToFile = '<pathToUiFile>'
## Load our window and put it into a variable.
qtWin = cmds.loadUI(uiFile=pathToFile)
## Open our window
## Create dock layout and tell it where it can go
dockLayout = cmds.paneLayout(configuration='single', parent=qtWin)
cmds.dockControl(allowedArea='all', area='right', floating=True, content=dockLayout, label='Custom Dock')
## parent our window underneath the dock layout
cmds.control(qtWin, e=True, parent=dockLayout)
3) Getting values from widgets that are not supported by maya.
Now lets do something that we are not supposed to. Now this is where it starts to get interesting for me. Every time I tried to use QT I always ran into this problem. HOW THE HECK DO YOU GET A VALUE FROM A SPINBOX or a DIAL or any other cool custom downloaded widget etc. It always bugged the hell out of me for not being able to find a stable way. But now I seem to have found a solution and the answer is “signals and hidden line edits”.
You can connect any widget to a line edit. To do this create a “line edit” anywhere in the UI. Preferably where it does not edit your layout but you can easily access it.
Go into “Edit Signal / Slots” Mode (F4) and click and drag from your widget to the “line edit”. This opens up a new window that allows you to link a value to the text field (providing the widget support exporting a Qstring)
Now on change of the original widget (a double spin box in this case) the “line edit ” in itself will be updated with the value. However if the user does not change the value the line edit will be empty. Which is why its a good idea to give it the same default value as the widget.
4) interacting with the UI from within maya WITHOUT USING PYQT.
So now when we load our QT Ui inside of maya we can query the text of our line edit every time we want to by using the following line of code:
pm.textField('textFieldName', query = True, text = True)