Forum Groups
  All forums
    Help & Feedback
      Work in progress
      Finished Art
      Non-Max related

Maxunderground news unavailable

Very introductory Maxscript tutorial 3
show user profile  mrgrotey
--page recreated on behalf of Dub.--

For some reason this page (the original with the same name) no longer exists, so I thought I'd recreate if from the .pdf version.

Dub, can you link to this page now on your index page? Cheers.


Part 3– Making stuff random
This tutorial should take around 10 minutes to complete
or this tut, we'll have a look at the random function, object positioning and also
do some more loops for practice.

For this tut, we'll have a look at the random function, object positioning and also
do some more loops for practice.

Here is our hypothetical scenario. We want to build a fence across a
field. We have made an array of tall boxes - 20 of them - for fence
posts unfortunately they look really uniform and CG like. What to
do? Maxscript to the rescue.

The Random Function
Open the listener and type in the following command:

random 1.0 35.0

Maxscript will return a float number between 1 and 35. Notice how putting .0 after the number
made maxscript realise that we wanted it to generate floats and not integers.

Now we'll make a variable with a random integer between 1 and 6

diceThrow = random 1 6

Now whenever we access the diceThrow variable we made, it will contain the number that it
randomly chose when we first created the variable.
Randomizing post heights
Create an array of about 20 boxes. They should have their pivot on the ground and be 10 x 10 by
100 high. Make the spacing about 150 units.

I'm sure that you have a rough idea in your head by now of how we will go about this. Here is the

for obj in $ do
randHeight = random 90.0 110.0
obj.height = randHeight

We have the same loop as last time. For all the objects in the selection, execute the following code:

randHeight = random 90.0 110.0 - Make a variable with a random float between 90 and 110 in it.
obj.height = randHeight - Assign that variable to the objects height.

Select all your fencepost boxes and run the code. Now all the posts should have slightly different

Create a sphere in the scene and select it.

The way Maxscript accesses the position of objects is with the .position (or .pos) property.

If you type:


Maxscript should return: [12.3434,34.6446,0] or similar. You exact results will depend on where
you made the sphere in your scene.
Point 3 Data type
This result is a special type of data type called a Point3 data type. It contains X Y and Z values
always in that order. If we assign one to a variable:

ourPoint3variable = [5,4,1]

and then type this:


Max will return 4 which is the Y component of our Point3. If we just type:


Maxscript will return all the values - [5,4,1]
Position (and many other things) in 3D often needs 3 values, X, Y and Z so a Point3 is a very
useful data type.

Moving Objects

Back to setting an objects position.
We could type the following:

$.pos = [0,0,0]

that will move our selected sphere to the center of the world. We are giving the sphere explicit co-
ordinates to go to. What if we want to move it relative to where it is now?
maxscript provides a move function to do just that.

move $ [3,2,0]

That is simply saying "Move the selected object +3 units in X, +2 units in Y and 0 units in Z"


Now maxscript will return the position as [3,2,0]
Execute the move line again:

move $ [3,2,0]

Then ask the position:


You'll see that it is now at [6,4,0]
Delete the sphere. (if you want to be fancy, type: delete $)

Now we want to randomise the position of all the posts slightly.

Randomizing Position

Select all the posts again.

Here is the code we are going to use.

for obj in $ do
randX = random -5.0 5.0
randY = random -5.0 5.0
move obj [randX,randY,0]

Lets step through it.
For each object in our current selection do:
Create a random float between -5 and 5 and assign it to the variable randX
Create a random float between -5 and 5 and assign it to the variable randY
move the current object according to the following Point3. Where it says randX in the point3,
maxscript will substitute in the random number it generated earlier. The same goes for the randY.
we don't want the posts to move vertically so we have just put in a 0.

Highlight the code and run it.

We now have fence posts that vary randomly in height and x,y position. In the next tut, we'll also
randomize their rotation so they all lean in slightly different directions.
Save the max file as we'll use the same one next time.

read 21543 times
2/11/2009 11:53:50 AM (last edit: 2/11/2009 12:00:39 PM)
show user profile  Dub.
Thanks Mr G

read 21498 times
2/11/2009 1:22:44 PM (last edit: 2/11/2009 1:22:58 PM)
#Maxforums IRC
Open chat window