MaxScript Beginner - 01 Basics, variables
MaxScript Beginner - 01 Basics
The 3ds max maxscript helpfile is probably the best place to look for your scripting questions. Since 3dsmax 2012 it is now available online… Make a bookmark of this… it is your best friend in the scripting djungle 3dsmax 2024 maxscript Help File
https://help.autodesk.com/view/MAXDEV/2024/ENU/?guid=GUID-F039181A-C072-4469-A329-AE60FF7535E7
Comments
-- The two -- is used to comment out a single line
-- this line will be ignored
/*
You can also do this if you want to comment
out many lines of code...
*/
Variables
myVarWithMyOwnNameString = "test"
myVarWithMyOwnNameInteger = 34
myVarWithMyOwnNameFloat = 3.14523
myVarWithMyOwnNameVector = [0,23,1]
myVarWithMyOwnNameBoolean = true
Debug - Print Variables
To print out a value you can use the Print() command
print myVarWithMyOwnNameString -- prints out "test"
print myVarWithMyOwnNameInteger -- prints out 34
print myVarWithMyOwnNameFloat -- prints out 3.14523
print myVarWithMyOwnNameVector -- prints out [0,23,1]
print myVarWithMyOwnNameBoolean --prints out true
Properties
myBoxObject = box() -- create a box and store it in
-- the myBoxObject variable
-- show what properties that is available on the object
showProperties myBoxObject
Edit some properties
myBoxObject.length --get the length of the box
myBoxObject.height = 150 --set the height of the box
myBoxObject.width = 34.54
myBoxObject.lengthsegs = 4
myBoxObject.pos = [0,10,20]
For Loop, biggest benefit you get a number/iterator you can use…
for x = 1 to 100 do (
print x --this prints the values from 1 to 100
)
You can create nested for loops inside other loops
-- first we create a box and store it inside variable b
b = box width:1 length:1 height:1 name:"OS3D_exampleBox"
-- now create 100 instace copies of that box and set its position..
for x = 1 to 10 do (
for y = 1 to 10 do (
tmp = instance b
tmp.pos = [x*10,y*10,0]
)
)
Arrays, lists of xx
this creates a new array "myListOfStrings" with three strings in it...
myListOfStrings = #("name1", "name2", "name3" )
--for you who program other languages, yes arrays starts "usually" on 1
--there is some cases like bitArrays where it is 0 based
myListOfStrings[1] -- get the first name in the list
Loop through all values in an array
-- To do a for loop you need to know how many
-- items that are in the list/array..
myListOfStrings = #("name1", "name2", "name3" )
-- this gets the number of items in the array
nmbr_of_items = myListOfStrings.count
--We can now do a for loop from 1 to "nmbr_of_items"
--to loop through all items...
for i = 1 to nmbr_of_items do (
--this prints all the items in the array
print myListOfStrings[i]
)
“For each” loop
--It is very common that you want to loop through all items in an array
--and do something and therefor you have a another way of writing a loop
--called "for each" loop...
--First we need a fresh array again
myListOfStrings = #("name1", "name2", "name3" )
-- Second lets loop through them all and print out the values
for item in myListOfStrings do ( print item )
--"item" is a name I choose...
for myVarName in myListOfStrings do ( print myVarName)
--"myVarName" is another name I choose..., it does the same thing
Exercise 01 - Box array Version 1.0
This is a small exercise to test what you just have learned… Lets create an array of boxes and add some random height values to them… To use the random command in maxscript you do myValue = random 1 200 this creates a random value between 1 and 200
--Start of script
delete $Exercise01* --this is for testing purpose
--It is always good practice to store variables
NmbrOfCopies = 10 --how many copies to make
DistanceBetweenBoxes = 50.0
for x = 1 to NmbrOfCopies do (
mybox = box name:"Exercise01"
mybox.pos = [x * DistanceBetweenBoxes,0,0]
mybox.height = random 10.0 170.0
)
Exercise 01 - Box array Version 2.0
Here is a different version of the script that does X and Y copies and also changes the color of the boxes depending on distance from the camera… Btw you need to create a target-cam for this to work!
--Start of script
delete $Exercise01* --this is for testing purpose
--It is always good practice to store variables
NmbrOfCopies = 30 --how many copies to make
DistanceBetweenBoxes = 50.0
--you need a camera in the scene
myCam = $camera001
distanceFromCam = 2100.0
for x = 1 to NmbrOfCopies do (
for y = 1 to NmbrOfCopies do (
DoABox = random 0 1
if DoABox == 1 then (
mybox = box name:"Exercise01"
mybox.pos = [x * DistanceBetweenBoxes, y * DistanceBetweenBoxes,0]
mybox.height = random 10.0 170.0
theMod = (taper())
theMod.amount = random 0.1 -.5
addModifier myBox theMod
if (distance myCam.pos myBox.pos) > distanceFromCam then (
mybox.wirecolor = [255,0,0]
) else (
mybox.wirecolor = [0,255,0]
)
)
)
)