Chapter 7
Note: This chapter is based on the wonderful and inspiring work of Malcolm
Kesson. Kudos to Malcolm for letting us use his excellent example of recursion. You
can find Malcom’s extremely informative site at:
I would also like to explicitly thank Craig “bring on the radians”
Davies for his invaluable input on this chapter.
7.1 Introduction
This chapter is fairly heavy stuff. If the previous chapters were
strong, dark Belgian ale, then this chapter is a 30-year-old, single-
barrel malt whiskey. It takes a refined, educated, and well-traveled
man with a good palate to appreciate all the nuances of such fine
liquor. This chapter, well, requires none of those characteristics, but
a background in trigonometry (or at least a willingness to refresh)
will come in handy.
7.1.1 What Is Recursion?
For more information about recursion, see the chapter about recursion.
You are probably wondering what that was all about. This is the
chapter about recursion, yet I am referring to it for information
about recursion. This geeky little joke illustrates exactly what
recursion is. Recursion is a slightly more advanced programming
technique that we will use in this and the later chapters to do a vari-
ety of things. As such, it is a good idea to clearly understand what
recursion is and how it can be used.
In terms of MEL, recursion is when a function directly (or indi-
rectly) calls itself, as in the following:
global proc int factorial(int $n)
if ($n <= 1)
return 1;
return $n*factorial($n-1);
In the example above, you can clearly see (in bold) that the proce-
dure is calling itself in the procedure.
In Plain English
(Borrowing from here):
Recursion is the process a procedure goes through when one of
the steps of the procedure involves rerunning the procedure. A pro-
cedure that goes through recursion is said to be recursive.
Something is also said to be recursive when it is the result of a
recursive procedure.
An everyday example would be shampoo directions:
1. Lather
2. Rinse
3. Repeat
To understand recursion, one must recognize the distinction
between a procedure and the running of a procedure. A procedure
is a set of steps that are to be taken based on a set of rules. The
running of a procedure involves actually following the rules and per-
forming the steps. An analogy might be that a procedure is like a
restaurant menu because the procedure lists all possible steps,
while running a procedure is actually choosing the steps (or courses
for the meal) from the menu.
A procedure is recursive if one of the steps that makes up the
procedure calls for a new running of the procedure. Therefore, a
recursive four-course meal would be a meal in which one of the
choices of appetizer, salad, entrée, or dessert was an entire meal
Chapter 7
unto itself. So a recursive meal might be potato skins, baby greens
salad, chicken Parmesan, and for dessert, a four-course meal con-
sisting of crab cakes, Caesar salad, a four-course meal, and chocolate
cake for dessert. And so on until each of the meals within the meals
is completed.
Recursions as Loops
Throughout this book you have been introduced to various exam-
ples and uses of loops. What separates recursion from other loops is
that an entire procedure is being looped. Like loops, there is an
inherent danger to recursive procedures: triggering an infinite loop.
You may have seen examples in the past of for loops that looked like
the following one.
Warning: Maya will crash if you try the code below!
print "oops\n";
If you were to run this, you would get an infinite loop because $i
will always be at least equal to (at least on the first iteration) and
greater than 0. If you were to run this script with the script editor
open, you might see an infinite printing of the word “oops” before
Maya locks up (a.k.a. “hangs”).
// and so on!!!!
A technique I use to demonstrate why loops, even ones that only
print strings, can quickly crash Maya is to have people repeat the
same word over and over until they begin to run out of breath. Infi-
nite loops essentially suck the “breath” out of Maya until no air is
The same mistake can be made with recursions. For example,
the following procedure would infinitely loop over and over again,
creating one NURBs sphere after another on top of each other. It is
Chapter 7

Get Professional MEL Solutions for Production now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.