Wednesday, 8 April 2015

Python, Qt and Maya!

One very interesting combination with Maya is Python and Qt!
Qt offers a huge range of tools to help us develop and create softwares.
http://www.qt.io/
Up to now, all I could do was create some interfaces for Maya... Qt Designer allows us to have some nice interfaces with simple drag'n drop and resize commands. With the help of PyQt libraries, we can quickly call those interfaces from Maya's shelf using Python!
This week I decided to start my AutoRig as a way to learn a little bit of rigging and scripting in Maya!

Baby steps to an AutoRig in Maya!

Here are some steps to get things working!

1) Install Qt, Python and PyQt (check if you're downloading for the same version);
2) Create a new Form->Widget in QtDesigner;
3) Set an objectName for each widget (eg: objectName: ObjTest);
Creating a basic form with a pushButton widget.

4) Save the ui file (eg: test.ui);
5) Create a python script with the following content:

# -*- coding: utf-8 -*-
import maya.cmds as cmds
from PyQt4 import QtGui, QtCore, uic

ui_filename = '%PATH_TO_FILE/test.ui'
form_class,base_class = uic.loadUiType(ui_filename)

class testing(base_class, form_class):
    def __init__(self):
        super(base_class,self).__init__()
        self.setupUi(self)
self.setObjectName('ObjTest')
self.connectInterface()

    def connectInterface(self):
QtCore.QObject.connect(self.ObjTest, QtCore.SIGNAL("clicked()"),self.ObjTestCmds)

    def ObjTestCmds(self):
        print "It is working!"
        
def main():
    global ui
    ui=testing()
    ui.show()

if __name__ == "__main__":
    main()

In this case, when I click on the pushButton, it calls the ObjTestCmds function.
Save the file (eg: testing.py)

6) In Maya, go to the Script Editor and type:
import sys
Dir = "%PATH_TO_FILE_FOLDER%"
if Dir not in sys.path:
    sys.path.append(Dir)
import testing
testando.main()

I'm just appending the script directory to the system's Path variable and calling the python's class testing.
If everything goes ok, you should see something like this:

Qt Form in Maya!

And that's it!... Now you can do anything with Python, Qt and Maya!... \o/


No comments:

Post a Comment