Akram Parvez Technical Artist

Catching and debugging errors in Maya Python

Posted on

Working as a TA for an outsourcing company means that you have to support different project at the same time. This includes writing small or big python scripts for the artist and sometimes these script could be buggy as you would not have a proper testing environment and you depend on the artist to send you error reports that help you to understand/analyze the bug. But Maya tries to act smart and mask error output in the script output window. So artist end up sending only part of the information.
Here is a super simple example:

import maya.cmds as mc
def getAllObjs():
    objs = mc.ls()
    for obj in objs:
        mc.setAttr('%s.doubleSided' % obj, False)

If you call the above function, the artist would get an error and he forwards you this.

# Error: RuntimeError: setAttr: No object matches name: time1.doubleSided #

This does not help you if the script is big and finding out where this error occurred would take some time if you were not the author. Instead you could use the traceback module from python's standard library to get some useful/proper info.

Wrap your code as follows and now you would get much more meaningful tracebacks as except from python.

import traceback
try:
    getAllObjs()
except RuntimeError:
    print traceback.format_exc()

# Outputs:
# Error: setAttr: No object matches name: time1.doubleSided
# Traceback (most recent call last):
#   File "", line 1, in
#   File "", line 7, in getAllObjs
# RuntimeError: setAttr: No object matches name: time1.doubleSided #

I know this can also be achieved by enabling Script Editor->History->Show Stack Trace, but usually this is not setup on an artist's work stations.

You can also use this in the userSetup.py if you have one used by everyone in the company:

gScriptEditor = mel.eval('$tmp = $gCommandReporter');
mc.cmdScrollFieldReporter(gScriptEditor, edit=True, stackTrace=True)
Comments (0) Trackbacks (0)

No comments yet.


Leave a Reply

No trackbacks yet.

%d bloggers like this: