Tip:
Highlight text to annotate it
X
MaxScript is a rich scripting language which you can use to automate many tasks in 3ds Max.
In this movie, we'll introduce how to move, rotate, and scale objects in MaxScript by building and animating a voxel tree.
Under the MaxScript menu, click on MaxScript Listener.
In the MaxScript Listener window, select "MacroRecorder > Enable".
Now, when we manipulate Max's user interface, the corresponding MaxScript commands will be echoed in the listener.
This will help us discover the commands we need as we build our script.
Let's get an intuition on how to build our script by manually stacking two boxes.
Notice that in the MaxScript Listener, the "Box" command is echoed twice along with several parameters including each box's length, width and height.
The "move" MaxScript command moves the selected item.
The currently selected item is denoted by the dollar sign ($).
The three bracketed numbers define the movement vector from the box's current position.
Parent, or link, the top box to the bottom box.
In the MaxScript Listener, the name of the bottom box is assigned to the parent property of the top box.
Given what we've just seen from the MaxScript Listener's output, we've got a good starting point for our own script.
Under the MaxScript menu, open the MaxScript Editor.
We could type our new script in the interactive tab of the MaxScript Listener, however the MaxScript Editor is a more convenient place to test and run more complex scripts.
We're going to incrementally build on our script, so let's begin by clearing the scene with "delete objects".
On the next line, type b1 = Box().
The empty parentheses will create a default box, since we're not specifying any parameters.
To run the script, press CTRL+E.
Notice that the scene has been cleared, and that the two previous boxes have been replaced by a single default-sized box.
Back in the script editor, type b2 = Box() to create the second box.
Type the following line to place the second box on top of the first box.
Type b2.parent = b1 to assign the bottom box as the parent of the top box.
Run the script again using CTRL+E.
We now have two default boxes stacked and linked in a parent/child relationship.
Now, suppose we wanted to stack twenty boxes.
Manually creating each box and parenting it to the previous box would be quite tedious.
Likewise, it would be time-consuming to type out Box() twenty times and move each one.
Instead, we can use a for-loop to create and stack as many boxes as we want.
Let's comment our previous code out, in case we want to refer to it later.
Two dashes will create a single-line comment, while a paired slash and a star will comment multiple lines out.
We'll leave the "delete objects" line, since we want to run our script on a fresh scene every time.
Type the following for-loop.
Since "i" is assigned to 0 on the first loop, the z-component of the move vector is multiplied by 0, effectively keeping the first box at the scene origin.
On the second loop, "i" is valued at 1, so the new box will be moved up by 30 units along the z axis.
On the third loop, "i" is valued at 2, so the new box will be moved up by 60 units, and so on.
Notice that we haven't added any parent/child relationships in the for-loop.
To add these relationships, we'll add the following code.
The first box created won't have a parent, however the following boxes will be linked in a parent/child hierarchy.
The move command operates in world coordinates, but we'd like to move our boxes up by 30 units relative to their parent.
To do that, we'll replace the "move" command with the following line.
The "in coordsys parent" context expression tells Max that the new box's position property should be modified relative to its parent's coordinate system.
By moving the box relative to its parent, we've gotten rid of the multiplication by "i".
Next, we'll cover how to randomly rotate our boxes around the x y and z axes of their parents.
Use the "random" expression to sample a decimal number between -10 and 10 degrees.
Instead of "in coordsys", we'll use the "about" context expression with the "rotate" command to rotate the new box around its parent.
We want our tree to get narrower as it grows. To do that, we'll scale each new box to be 0.95 times the size of its parent.
Our tree is taking shape, but it doesn't have any branches yet - we've only got a tree trunk.
In the next part, we'll cover how to add branches to our voxel tree.