Welcome to CADVANTAGE’s No Nonsense, Quick Start, Step by Step tutorial for MAX Script.

This tutorial is part of the CAD-CAM @ Home scheme currently available in the city of Aurangabad in India.

The tutorial is a Goodwill Gesture for all MAX users who take pains to go out of way and learn new things.

To learn more about computers in general and CAD CAM CAE in particular as detailed below, you may enroll for the
CAD++ [ CAD @ Home ] program at :

CADVANTAGE ( where AutoCAD is taught FREE )
C 2 - 6, N 7, CIDCO,
Opp. Onkar Gas, Behind Swamikripa STD,
Aurangabad 431003
India
Ph : 0240 - 480767
Email : CadGuruCool@yahoo.com
Webpage : www.geocities.com/CadGuruCool



MAX Script Tutorial


Step 1


It is assumed that you are familiar with the basics of 3D Studio MAX or 3D Studio VIZ.

In case of any doubts with the basics, complete the 3D Studio MAX or 3D Studio VIZ tutorial first.

To avail these tutorials, go to :

www.geocities.com/CadGuruCool

and click Tutorials 4U


Step 2


You must understand what a spinner is and what you eat a rollout with.

As shown in the figure on the right, the item circled red is called a spinner while the one circled blue is the rollout.

Observe the - sign to the left of the word Parameters on the rollout.

The minus sign indicates that the roll out is currently expanded.

When you click on the rollout title or the minus sign, the rollout collapses and its contents are hidden.

The minus sign changes to a + sign indicating the rollout can be again expanded.

Lastly, the etching line that surrounds the spinners and the text is called a Group.
dialog box

Step 3


We want to write a program in MAX Script that will draw the external wall as shown in step 1.

The program will run via a dilaog box as shown in the figure to the right.

An external wall has a width of 0.23 meters (9"), so the program will ask for length and height of the wall.

Step 4


1) To begin with, fire up 3D Studio MAX and Select the Utilities command panel (hammer) as shown in figure besides.

2) Click the MAXScript button.

3) Click the New Script button.

A notepad like window will appear. Browse the various pulldown menus in this window. They are all similar to those found in Notepad.
The only additional menuitem is the last one in the File menu and we will discuss it in this tutorial.

4) By clicking the Open Script button you can open an existing MAXScript file, but don't click it right now.

5) Similarly, clicking the Run Script button will load an existing MAX Script.

When you click this button, the standard File Open dialog box will appear and you can select a file with ms extension.

The script will be added in the list below and you can select the script to run from the list.

6) Note the list below the word Utilities. This means all programs we write in MAX Script are utilities that we are adding to MAX.

Now onwards, say we are going to write a utility called Wall. Our utility will also appear in the list and users can run it by selecting it from the list.

Click the list and observe the utilities available.

Don't select any item from the list - press escape at the keyboard.

Step 5


In the MAXScript window, type the first few lines as shown in the figure (bottom).

In the first line :

utility MyUtil "Wall"

the word utility is part of the syntax.

Syntax is like grammar of that language and cannot be challenged. If it is required to be written, you just can't help it.

When you run the script, MAX reads it line by line.

When MAX reads the word utility, it understands that a new utility is to be created and adds it to the Utilities list as shown in figure (top).

The word MyUtil is the name of the utility and we are going to use it internally in the program to identify our utility. This never comes into picture.

The last word enclosed in double quotes is the title of our utility and will appear in the list as shown with pink line with green dots in the figure.

Step 6


The second line is a single parentheses or opening bracket ( you get this one by pressing Shift and 9 in that sequence).

Again, this is part of the syntax - so no questions.

The third line reads : Local wal

Anything that you see on the screen comes from the computer's memory, even MAX itself is in the computer's memory and the computer displays contents from the memory to the screen.

When you create a Teapot in MAX, it is first created and stored in the computer's memory and then sent to the monitor screen where we see it.
After we delete the teapot, MAX removes the teapot from the memory of computer and the memory space becomes empty so that other programs can use the empty memory space.

In our utility we are going to create a new wall object. So we must also store it in the computer's memory and display it in the MAX screen.

Later in this tutorial, we will create the wall and also store and display it.

The question is where do we store our new wall in the memory and further how to retrieve it from the memory and display on the MAX screen.

This purpose can be solved by reserving a space in memory and giving it a name, like we reserve a seat in the railway before we travel.

You can safely assume that the word local does the work of reserving a space in the computer's memory, giving the memory a name - Wal and also specifies the type of memory. We won't go into the details of types of memory.

Step 7


The third line reads :

rollout createWall "Draw Wall"

The first word rollout is part of the syntax that tells MAX to create a rollout.

The second word createWall is the internally used name for the rollout. .

The third word "Draw Wall" is the title of the rollout, which will appear as shown with the red line with blue dots in figure.

Add one more bracket on the next line as shown in figure.

Step 8


The next few lines are shown in bold below:
 
 utility MyUtil "Wall"
 (
  local wal
  rollout createWall "Draw Wall"
  (
  group "Wall Parameters" 
   (
   spinner spnwidth  "Wall Width:" range:[1,10,5] type:#float enabled:false 
   spinner spnwidth  "Wall Width:" range:[0.01,1,0.23] type:#float enabled:false
   spinner spnheight  "Wall Height:" range:[1,10,5] type:#float enabled:false
   colorpicker WallCol "Wall Color:" color:[30,30,30] align:#center enabled:false
   button btnDraw "Draw"
  ) 
Here, group "Wall Parameters" creates a group with "Wall Parameters" as the title as shown in figure below :



Next come the spinners.

The line

spinner spnwidth "Wall Width:" range:[1,10,5] type:#float enabled:false

creates a spinner with an internal name spnwidth and label Wall Width:

The user can change the wall width within the range 1 to 10 and the number 5 is the starting value when the dialog box box appears for the first time.

type:#float means we can use fractions as wall width, for example 5.4

You can also use type:#integer if you want to restrict users to input numbers without a decimal point in certain utilities.

enabled:false tells MAX that when the dialog box appears for the first time, the spinners are to be disabled. This is done because the spinners are used to change the length, height, etc of the wall but the wall is not yet created.

The wall will appear on the screen after we click the Draw button.

Later in this tutorial, we will create the Draw button and write code to enable the spinners when the button is clicked.

You can try the functionality of the utility by running the wall.ms file that is contained in the zip file that contained this tutorial. The detials on how to run the wall.ms utility is given in the last step of this tutorial.

Take the next line

colorpicker WallCol "Wall Color:" color:[30,30,30] align:#center enabled:false

Here, colorpicker creates a rectangle on the dialog box, clicking which will open the color picker dialog of MAX.

The internal name of the color picker is WallCol and the label is Wall Color:.

color:[30,30,30] indicate the RGB ie. Red, Green and Blue components of the initial color. The colorpicker is aligned in the center of the dialog and is also disabled initially like the sipiners.

Finally

button btnDraw "Draw"

creates the hero - the draw button. Here, button is the syntax, btnDraw is the internal name and Draw is the title that appears on the button in the dialog box.

The meaty part of the code is over.

Refer the complete script code at the end of the tutorial and note the label code which creates text lines on the dialog to display your name etc.

Step 9


The next lines are as below:
 
 on btnDraw pressed do
(
wal=box()
wal.length=spnlength.value
wal.width=spnwidth.value
wal.height=spnheight.value
wal.position=[0,0,0]
Wallcol.enabled=true
spnheight.enabled=true
spnlength.enabled=true
spnwidth.enabled=false
) 
on btnDraw pressed do
tells MAX what to do when the Draw button is pressed.

Wal = Box()
creates a box (our wall) and stores it in the Wal memory that we created in line 2 of the code.

wal.length=spnlength.value
Here, the value entered by the user in the length spinner is given to the wall as length.
spnlength.value is the nummeric entry in the length spinner.

wal.position=[0,0,0]
The wall is created with its pivot at the global origin.

Wallcol.enabled=true
Now that the Draw button is clicked, enable the color picker and also the spinner as shown in the next line :
spnheight.enabled=true

on spnlength changed value do
tells MAX what to do when the spinner value is changed.

In that case,

( wal.length=spnlength.value )
means - assign the changed ie. new value in the spinner to the wall length.

on WallCol changed val do wal.wirecolor = val
tells MAX what to do when the current color is changed. Here, wal.wirecolor is the color of the wall which is assigned the val ie. the color value from the color picker.

Finally,

 
on MyUtil open do
(
RollOutWall = NewRolloutFloater "Wall" 220 265
addRollOut CreateWall RollOutWall
)  
When the utility with internal name MyUtil is selected from the Utilities list, the following things are to be done :

RollOutWall=NewRolloutFloater "Wall" 220 265
Here, RollOutWall is the intername of the dialog box which in MAX Script is called a floater because it can float anywhere in the MAX Screen.

You float the floater on the screen, press and drag the title bar of the floater.

NewRolloutFloater is the syntax for creating the floater with the title Wall
The width of floater is 220 and height is 265.

addRollOut CreateWall RollOutWall
adds the rollout to the floater.

Step 10


The complete code is given below:
 
 
utility MyUtil "Wall"
(
local wal

rollout createWall "Draw Wall"
(
 group "Wall Parameters"
  (
   spinner spnlength  "Wall Length:" range:[1,10,5] type:#integer enabled:false
   spinner spnwidth  "Wall Width:" range:[0.01,1,0.23] type:#float enabled:false
   spinner spnheight  "Wall Height:" range:[1,10,4] type:#integer enabled:false
   colorpicker WallCol "Wall Color:" color:[30,30,30] align:#center enabled:false
   button btnDraw "Draw"
  )

  label lab0 " "
  label lab1 "Wall Script" align:center
  label lab2 "Written by Tushar Suradkar" align:center

on btnDraw pressed do
(
wal=box()
wal.length=spnlength.value
wal.width=spnwidth.value
wal.height=spnheight.value
wal.position=[0,0,0]
Wallcol.enabled=true
spnheight.enabled=true
spnlength.enabled=true
spnwidth.enabled=false
)

on spnlength changed value do
( wal.length=spnlength.value )

on spnwidth changed value do
( wal.width=spnwidth.value )

on spnheight changed value do
( wal.height=spnheight.value )

on WallCol changed val do wal.wirecolor = val
) -- rollout

on MyUtil open do
(
RollOutWall=NewRolloutFloater "Wall" 220 265
addRollOut CreateWall RollOutWall
)

) -- utility


To use the script,

In MAX or VIZ, click the Utilities command panel (the one with the hammer icon) and select the MAX script button below.

More butons will appear.

Click the New Script button. A notepad like window will appear.

Copy and paste the entire code above into the MAX Script editor. Save & Close the editor.

Click the Run Script button and select the wall.ms file.

From the Utitlities list below, select Wall and the floater will appear.

Click on Draw and move the floater away from the center of the screen.

Adjust the length and height of the wall and try changing its color.
Step 18

Want to dig deeper
into MAXScript ?

Call CADVANTAGE Now !!! Ph : 0240 - 480767 in Aurangabad.
Dig deep till you reach the other side of the globe - Only at CADVANTAGE



FREE - Basic Course on 3D Studio MAX or VIZ

FREE - Tons of free MAX Script code on CD

FREE - 400 pages spiral bound indeth study material