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)
Filed under: Python, TechArt No Comments

Model Viewer in PyOpenGL

Posted on

In the last few months, I have been work with Python and PyQt a lot, so I thought why not do a personal project at home to improve my python skills and also learn some OpenGL. I started learning OpenGL and to make a Model Viewer was my realistic goal. This goal was to keep me on track and avoid getting lost in the huge world of OpenGL. I choose the following frame works:

  • Python 2.7
  • PyQt4 - for UI
  • PyOpenGl - OpenGL binding for python
  • FreeImage - for loading Images

Initially I used PIL for loading images in python, but quickly changed to FreeImage as PIL did not have support for loading DDS and HDR Images. FreeImage library was loaded using CTypes in python, which proved to be great for sending image file data to OpenGL. I had a small list of features to be implemented in the viewer and Marmoset Toolbag(link) has been a great inspiration for me during the development. The following features are partly/fully implemented:

  • Loading OBJ file format (WIP).
  • Simple Phong Shading with One Light support.
  • Diffuse, Normal, Specular and Illumination Map support.
  • Image Based Lighting with Vertical Cross Cubemap.
  • DOF, HDR and Bloom (WIP/Basic implementation).
Imrod model by Dmitriy Parkin(www.parkparkin.com)
Image Probes from Humus.name
Filed under: Python, TechArt 5 Comments