Phone: 905 409-1589
Email: info@penproductions.ca
RSS LinkedIn Twitter Twitter
Tutorials:
GDC Max Scripting Class 2005

Overview:

Topics:

Script types:
Variables and Arrays:

For Loops:
Functions:
Interfaces:
If / Else Statements:

Description:

- .ms, .mcr, .mse, .mzp
- A brief overview on creating and using variables and arrays.
- Covers indexed and iterative loops.
- Over view on functions with and without parameters.
- Create custom UI's for your scripts.
- Decision making in you code.


Script types:

.ms: This is a regular max script extension. These scripts can be run from the Max Script/Run Script drop down menu or be called by another script.

.mcr: This is a macro script and with the appropriate header in the file you can hot key, add them to button bars or call them from quad menus.

.mse: An mse file is an encrypted max script. This allows developers to keep their code private from others so that it can't be read. You create an encrypted a script with the use of encryptScript <filePath> command.

.mzp: An mzp file uses winZip to compress many script files into one. When the script is run in Max a run file is executed which can call and run other scripts. I use this for creating install scripts for script packages for clients.

Back to Top


Variables and Arrays:

Variables in Max script can take any type of value and don't need to be declared as a specific type. This can make managing them more difficult if you let you code get sloppy. Variables can be either local or global but again don't need to be declared in every instance. If not declared as global a variable will be local to the set of parenthesies that they are within. In some instances Max will throw and error if the variable is not declared as local.

Declaration of variables.
local foo
global bar
baz = 10
local foo = "a string"
global baz = #("A,","array","of","strings")

Arrays are a collection of objects, strings values or all of the above. Max will allow all diferent data types to be stored in the same array. You can even nest arrays of varring types. Max script arrays are 1 based which is unusual for scripting and programing languages. This means the first item in the array is accessed with [1] and not [0] as in other languages.

Declaration of Arrays:

myArray = #() --This is an empty array.
myStringArray = #("test", "another test")


Access arrays:

myStringArray[2]
--Will return "another test"


Built in Arrays:
Max Script has a number of built in arrays for accessing many things. Some of the most used are below.

$selection --returns all selected objects in the scene
$objects --returns all objects in the scene
$geometry --returns all geometry objects in the scene
$shapes --returns all shape objects in the scene
$helpers --returns all helpers in the scene
$cameras --returns all cameras in the scene

Consult the help files for a full list of built in arrays.

Back to Top


For Loops:

For loops are used in conjuction with arrays, it is way to iterate through each item in the array and preform a task on that item. An array can be thought of a bucket of items, each item has a number in the array and can be accessed in any order that you need them.
There are two ways to access arrays with for loops, the first is by index in the array.

Indexed for loop:

myArray = #("foo","baz","bar")

for x = 1 to myArray.count do
(
   print myArray[x]
)

--result
"foo"
"baz"
"bar"
OK

The above example creates an array of string values and prints them to the listener and then returns the value OK to let you know that the code exited without errors. In this example x becomes equal to the indexed value of the array.

This next example uses as iterative loop.

Iterative for loop:

myArray = #("foo","baz","bar")

for x in myArray do
(
   print x
)

--result
"foo"
"baz"
"bar"
OK

Notice that the result is the same as the previous one. In this example x becomes equal to each object in the array and not the index of the array. This is often used as a short had to the previous how ever isn't as flexable in some coding situations.

Example #1:
--Select several objects and then run this code
--Each object will recieve a turboSmooth modifier and have the options set.


for x in selection do
(
   addModifier x (turboSmooth())
   x.turboSmooth.iterations = 0
   x.turboSmooth.useRenderIterations = true
   x.turboSmooth.renderIterations = 2
   x.turboSmooth.isolineDisplay = true
)

Back to Top


Functions:

Functions are a way of creating a reusable block of code. This block of code can be called from just about any where in the script and can be called from other functions. Functions give us a way to create none linear code. Arguments can be passed in to functions so that variables can passed on to them.

Function with no arguments:

--Define the function
function myFunction =
(
  "test"
)

--Call the function and pass the return value to a variable
myFnVariable = myFunction()

--Print the value of the variable
print myFnVariable

--Returns
"test"

The value that is returned from a function is defined is the last line of the expression in the function. If no definable value is returned OK will be returned in it's place. Not having a value returned from a variable can make it hard to error check your code so it is always best to have your functions return a known value of some kind.

Function that is passed one argument:

--Define the function and the arguments. This uses the sort cut of fn instead of function.
fn myFn obj =
(
   local myAr = #()
   append myAr obj.name
   append myAr obj.pos
   append myAr (classOf obj)
   myAr
)

--Loop through all selected objects and run the function on each one.
for x in selection do
(
   local myAr = myFn x
   --Uses the format command instead of print. % will be replaced with each item in the array.
   format "The object name: %\nThe position: %\nThe class: %\n" myAr[1] myAr[2] myAr[3]
)

--Return
The object name: Sphere01
The position: [1.33333,-5.77778,0]
The class: Sphere

The variable x is passed from the for loop to the function and holds the one of the objects that is selected. The function sorts three bits of data and appends them to an empty array then passes that array back to the for loop. The format command prints that data to the listener and then the for loop continues with the next object.

Example #1: 1

Back to Top


Interfaces:

Creating interfaces for scripts is an easy task and makes any script easier to use.

Create a rollout and display it:

--Check to see if the dialog already exists
--The try/catch is simple error checking to see if the rollout already exists and if it does it will remove it. If it --   doesn't it will not do anything at all.
try(destroyDialog myRollout)catch()

--Create a rollout
rollout myRollout "This is a rollout"
(
   
)

--Create a dialog box and add the rollout to it.
createDialog myRollout


Rollout with UI items added:

try(destroyDialog myRollout)catch()

rollout myRollout "My Rollout"
(
   button myButton "Button"
   spinner mySpinner "This is a spinner:"
)

createDialog myRollout