Deploying Qt based application is quite easy and flexible according to our needs. When it comes to an animation studio, there will be lots of legacy tools used inside maya. Sometimes it is better to add a few features and make it more user-friendly; for example by pumping up the interface related features. But Maya’s built-in UI framework doesn’t really allow much modifications. Considering the fact that Maya ships with underlying Qt framework, we can wrap or access every Maya UI elements as Qt objects. A few lines of code can increase the functionality of your legacy tools. Is what I am talking not making any sense? Well then why don’t we go through a small working example,
The following will add type suggestion to the Maya’s textField.
# This is a basic UI with Maya's native framework import maya.cmds as mc from PySide.QtGui import * # This is safe because Qt elements starts with 'Q' from PySide.QtCore import * import maya.OpenMayaUI as omui import shiboken # Needed to wrap ui elements as Qt object win = "SampleWin" if mc.window(win, ex=1): mc.deleteUI(win) mc.window(win, t="Native UI", wh=(400, 400), s=0, rtf=1) mc.columnLayout(adj=1) myField = mc.textField("sampleField", pht="Search") myList = mc.textScrollList("sampleScrollList", ams=0) mc.setParent("..") mc.showWindow(win) # lets add few items to the textScrollList myContent = ["Maya", "Autodesk", "Qt", "Riverbank", "This", "Is", "Awesome"] for each in myContent: mc.textScrollList(myList, e=1, append=each) # lets start adding our new feature # get the pointer to the ui element using MQtUtil ptr = omui.MQtUtil.findControl(myField) # wrap our textField as QLineEdit (Equivalent Qt widget) qfield = shiboken.wrapInstance(long(ptr), QLineEdit) # lets start adding type suggestion feature completer = QCompleter() qfield.setCompleter(completer) # add the actual content of the textScrollList tot suggestion list model = QStringListModel(myContent) completer.setModel(model) # lets make the suggestion to ignore case sensitivity completer.setCaseSensitivity(Qt.CaseInsensitive)
Now that’s just 7 line of code! Isn’t it cool?
Here is how it will look:
Make sure you don’t wrap a wrong widget. For example, don’t try to wrap textScrollList as QLineEdit. If you do, most likely you would end up with a fatal error.
There are few special scenarios while wrapping widgets to Qt. Maya’s native widget like
textFieldButtonGrp, floatFieldGrp (Widgets which ends which Grp) which are not a single widget but a collection or combination of widgets.
With an additional line of code, you can easily wrap these combo widgets.
Wrap such widgets as a QWidget and you can access its children directly.
ptr = omui.MQtUtil.findControl(mc.textFieldButtonGrp()) combo = shiboken.wrapInstance(long(ptr), QWidget) qfield = combo.children() print combo.children()
In this case, you will get a collection of layout and widgets like:
Now you find the same QLineEdit at index one.
This is just a simple example, you can do just about anything with Qt. Happy coding 🙂
P.S. I know it is too late for such a post about Qt and Maya; but I sincerely hope that this would atleast help someone who is new to the Maya world. Besides, I will be completing 5 years in the animation industry by 19th of December 2016. So I thought it is high time I gave back something to the community.
Thank you for your time. See you guys later with more updates. Ciao.
May 12, 2017 at 10:15 pm
This is a cool tutorial on using Qt widgets in Python!