|
|
|||||
Recursive functions with MAXScript
Summary: This document demonstrates recursion in MAXScript using a simple recursive function.
The tips, tricks, examples and suggestions
outlined in Autodesk Product Support technical documents are suggested
for use at your own risk. Document contents are subject to change without
notice. Autodesk is not responsible or liable for damage or events that
may occur as a result of following suggestions from any Autodesk Product
Support technical document.
© 1998 Autodesk. Autodesk and
the Autodesk logo are registered trademarks of Autodesk Inc. All other
brand names, product names or trademarks belong to their respective holders.
Overview
A recursive function calls itself within its own definition. This means that one of the lines in the MAXScript function definition calls the function again; creating a "picture within a picture within a picture" effect.
Recursive functions are appropriate for working with hierarchies and groups, or any collection whose contents might have additional elements beyond the first collection loop. For arrays and pathname collections, iterative functions are usually more appropriate. In this document both iterative and recursive functions will be defined, but the discussion focuses on recursive functions.
Create objects
Note: For more information on "mapped" or iterative functions, search the MAXScript Reference, index tab for "Function Definition."
Before creating the recursive function, you must create some objects for the script to work with. Go to the Utilities panel, push the MAXScript button, and then push the ‘New Script’ button from the MAXScript rollout. Copy and paste the following text into the new MAXScript window:
resetMaxFile #noPrompt --Make some scene objects spheres = for i in 1 to 10 collect sphere radius: (i*10) position: [(10*(1.0*i)^2) - 10, 0, 0] --A function to link all the objects in an already defined array: fn SimpleLinkArray Array = ( for i = 1 to Array.count do ( if Array[ (i+1)] != undefined then Array[ (i+1) ].parent = Array[i] ) ) --Call the function SimpleLinkArray spheresSave the new Script as "Simple Recursion.MS." Then, in the file menu of the new Script, select Evaluate all.
A series of spheres are in the viewport. They have been linked using the SimpleLinkArray function, (an iterative function). Push the ‘select by name’ button and check Display Subtree to see the hierarchy structure. This is the scene with which we will study recursive functions. If it becomes necessary to recreate this scene, it is recommended that you evaluate the script up to this point.
Tip: Evaluation will run the entire script. To evaluate one line of the script at a time, place the cursor at the end of that line and hold the <SHIFT> key while pressing <ENTER>. To evaluate multiple lines, highlight the lines and press and hold <SHIFT> while pressing <ENTER>.
Define a recursive script
Note: While the recursive functions defined below can be evaluated on any linked objects, it is recommended that you use the objects created by the script to avoid confusion. To create fresh objects at a later point, evaluate the code above by highlighting the block of code and pressing <SHIFT> and <ENTER> simultaneously.
Next, you will make a script that randomly "squashes" the spheres. To do this, copy and paste the following text into "Simple Recursion" window, after the existing code:
--Now make the recursive Script fn Stretcher arg = ( addModifier arg (stretch stretch:0.0) animate on ( at time 0 (arg.stretch.stretch = random 0.0 -.25; arg.stretch.amplify = 0.0) at time 100 (arg.stretch.stretch = random -1.0 -2.0; arg.stretch.amplify = -20.0) ) for i =1 to arg.children.count do ( Stretcher arg.children[i] ) ) Stretcher $sphere01Evaluate the newly pasted code by selecting it and pressing <SHIFT> and <ENTER> simultaneously. The linked hierarchy now has an animated squash, in different amounts. The file recursion occurs when the Stretcher function calls itself. See the discussion below for details.
resetMaxFile #noPromptThis line resets the current session without further prompting, to ensure a fresh start. This line is optional, and can be commented out.
The script that makes the spheres is not covered in this document, but you can look at the linking function defined in the first part:
Tip: "Commenting out" is a method of including text in code that the program will ignore. This is useful for commenting on lines of the code as you write it, and for disabling parts of a routine without deleting the code. In MAXScript, the comment symbol is two hyphens. [ -- ]. MAXScript ignores whatever follows the two dashes as it evaluates that line of code. Comments will only apply to one line of code.
fn SimpleLinkArray Array = ( for i = 1 to Array.count do ( if Array[ (i+1)] != undefined then Array[ (i+1) ].parent = Array[i] ) )The script lines above represent an iterative function. This function can be distinguished from the recursive function below, because it works serially through an array, not operating on anything outside the current collection of objects.
fn Stretcher arg = ( addModifier arg (stretch stretch:0.0) animate on ( at time 0 (arg.stretch.stretch = random 0.0 -.25; arg.stretch.amplify = 0.0) at time 100 (arg.stretch.stretch = random -1.0 -2.0; arg.stretch.amplify = -20.0) )These lines create the main picture, a simple function.
for i =1 to arg.children.count do ( Stretcher arg.children[i] ) )And this section of the script is the "picture within the picture." At this point, after evaluating itself on the first collection, it searches for more objects and evaluates itself again upon the new collection. This is recursion, as compared to the serial evaluation found in the iterative function of the first part.
Stretcher $sphere01This line evaluates the recursive function on the parent of the sphere hierarchy.
| Was this article helpful?
(very helpful) 5 43 21 (not helpful) |
Home | Products | Tech Assist | Autodesk | Prof Net | Search | Sitemap | Purchase
©Copyright
1998 Autodesk, Inc. All rights reserved.
Reproduction or copying
of images is prohibited.
Autodesk
Privacy Policy