28 August 2011

Python in Maya - How to make commands repeatable and undoable

Two of the problems you may have noticed using python in Maya are:

1) that your function calls are not repeatable with the "G" hotkey the way they are with MEL commands and shelf buttons.

2) python functions are not by default undoable with a single undo. If you make 10 connections between 10 nodes, you will need to undo 10 times to get back to the state your scene was in before you ran the function.

One way you can get around this is to build a decorator function to wrap your regular functions and add that functionality to them. So that when you are done, it will look like:


@undoable
@repeatable
def myAwesomeFunction(valueA='totalyWickedMode'):
    ....doAwesomeStuff


The nice thing about the format of the code above is that it is easy to add or remove from any function, and we can edit the functionality of these two decorators in 1 place. So lets define our decorators starting with the easiest of the two, undoable.




def undoable(function):
    '''A decorator that will make commands undoable in maya'''


    def decoratorCode(*args, **kwargs):
        cmds.undoInfo(openChunk=True)
        functionReturn = None
        try:
            functionReturn = function(*args, **kwargs)
           
        except:
            print sys.exc_info()[1]

        finally:
            cmds.undoInfo(closeChunk=True)
            return functionReturn
           
    return decoratorCode




The above function will automaticly be called in place of the function its decorating (in this case myAwesomeFunction), passing as a variable the function being called (again, in this case it is myAwesomeFunction). Now the function inside the function 'undoable', can be named whatever you want, and it will be the function collecting our arguments and keyword arguments (if there are any). In this inner function is where we can openChunck in maya's undoInfo (which defines the start of our undo "chunk"). Then, we run the original function in a try statement, followed by a finally statement so that we can close the undo chunk regardless of whether or not our original command produces and error. Now if we run myAwesomeFunction() it should undo the whole function in a single undo.


Next is the somewhat tricky problem of making our functions repeatable. The reason this is harder is because we have to use a command "cmds.repeatLast()" that is undocumented. luckily if you do a help() on the mel version of the command, we can see the list of flags for this command. The two flags we will need are: "ac" and "acl". 
"ac" being the command (which even tho we are using the python version of repeat last, we will need to feed a MEL command to it".
"acl" is just the label of the command, in this case we will label it with the name of our command.




def repeatable(function):
    '''A decorator that will make commands repeatable in maya'''
    def decoratorCode(*args, **kwargs):
        functionReturn = None
        argString = ''
        if args:
            for each in args:
                argString += str(each)+', '
               
        if kwargs:
            for key, item in kwargs.iteritems():
                argString += str(key)+'='+str(item)+', '

        commandToRepeat = 'python("'+__name__+'.'+function.__name__+'('+argString+')")'
       
        functionReturn = function(*args, **kwargs)
        try:
            cmds.repeatLast(ac=commandToRepeat, acl=function.__name__)
        except:
            pass
       
        return functionReturn
       
    return decoratorCode





The main difference here is that we need to construct our command into a mel command, understanding that our python command could be named anything (because many different commands will be using this decorator) . When we are done, our command string should look like (in this case):

python("awesomeTools.myAwesomeFunction(valueA=\"totalyWickedMode\"")

So the first thing we do is to turn our arguments and keyword arguments into a string in the same format as we would type to call it. Then we need to find the name of the module that contains this function (assuming we are not defining it in the script editor), the __name__ attribute in this case will give us that info, as it is inherited from the module. Now we just need the name of the function being decorated, again, we can use the __name__ attribute of the function (function.__name__) to get that info. Now we just stick them all together, and feed it as the mel command arg in cmds.repeatLast().


Hope this answers more questions than it creates for you,
Cheers,
Kris Andrews

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

20 August 2011

First Post

First Post! Ok, so having picked up loads of code, tricks, and general knowledge from other forums, blogs and websites, its only fair that I contribute back in some way. Since there is plenty of info on things like general python scripting, I will try to post some information that is more specific to 3d animation (specifically Maya, since that is what I am currently working in).

Cheers,
Kris