'catch'ing Maya script errors.

Recently I’ve come across a problem with using Maya for our pipeline development; in some cases (especially with 3rd party MLL’s) a script that must be run from a mayabatch call will fail without causing an exception, and Maya fails to give a non-zero return when called from mayabatch. This is a major problem if, like me, your trying to build a repeatable dependency correct build system. How to fix this?

My hack was to use an expect style system with the ‘catch’ keyword in MEL. An example is easier to see.

mayabatch -command "someProblemCommand \"param1\" \"param2\" \"etc\" "

This might fail inside the ‘someProblemCommand’ function provided by a ‘3rd Party Inc.TM’ MLL.

All I did was wrap the call in a catch with special prints.

mayabatch -command "if ( 
  catch( `someProblemCommand \"param1\" \"param2\" \"etc\"`)) 
  { print \"!@#$ failure $#@!\"; } 
  { print \"!@#$ success $#@!\";} "

Now you can capture the output of the mayabatch command in a simple perl/python script and look for the result with a regular expression (/!@#$ (.*) $#@!/) and test the result.

This works because catch acts like a catch to a try (or error within MEL) and returns 1 on failure, 0 on success.

You have no excuse to not error out in your build scripts now.

blog comments powered by Disqus