Beginning Maxscript

Variables

So, open 3ds Max and open up the Script Editor as well as the Listener. You can do this quickly by right clicking the little box in the lower left corner of the Max interface. Now, before we start anything I'm going to introduce you to your new best friend. Click in either the Editor or the Listener and then press F1. This brings up the online Maxscript Reference. If you're going to learn Maxscript, you're going to spend a fair amount of time here. One of the best ways to use the help system is to go to the search tab. You will find your answers faster here than sifting through the contents or index.

When I first got into 3D I don't think I knew what a script was. In my life I can recall taking two very basic programming classes, but by the time I was finally exposed to Maxscript, I could only remember that there were such things as variables, loops, if statements, and that you could 'print' stuff. I knew nothing of common syntax, nothing about functions... it was all Greek to me. And there's STILL a LOT that I don't know. That puts me in the unique position to help out a lot of people, because I'm not the guy who assumes you already know something about programming and writes a post with all kinds of code and leaves you completely flustered. I will be writing this article in a language that just about anyone can understand.

3ds Max Tips and Tricks

When I first got into 3D I don't think I knew what a script was. In my life I can recall taking two very basic programming classes, but by the time I was finally exposed to Maxscript, I could only remember that there were such things as variables, loops, if statements, and that you could 'print' stuff. I knew nothing of common syntax, nothing about functions... it was all Greek to me. And there's STILL a LOT that I don't know. That puts me in the unique position to help out a lot of people, because I'm not the guy who assumes you already know something about programming and writes a post with all kinds of code and leaves you completely flustered. I will be writing this article in a language that just about anyone can understand.

Let's get started.

So there are a few very basic things to know about scripting. Though much of this will apply to numerous programming environments, to keep things simple, I'm only going to refer to the context within 3ds Max and Maxscript. Here's a short list of things to permanently store in your brain-

Variables

Variables are letters or words (which can also contain numbers) that store some kind of value. Some variables will be created by you, and some are "system" variables created by and reserved for 3ds Max. Variables can act as numbers or text, or other groups of information. An couple examples would be

numPolys = 10 objName = "John's Sphere"

Strings

A string is a simple group of characters that you want to treat as text. You can modify it, but you won't be able to use a string in a mathematical command. Strings are contained in quotes, for example

"Maxscript can add 1 + 1"

Maxscript will not treat the 1's as numbers that it can add or subtract. Nor will the plus symbol have any mathematical effect.

Arrays

Think of an array as a group of values or objects enclosed in a container which Maxscript can open, inspect, and apply commands to one element at a time. In Maxscript, an array looks like this-

#(element1, element2, element3, ....final element)

Those elements could be numbers, object names, materials, maps... virtually any type of element in 3ds Max.

Loops

It's very common that you'll want to apply a command to multiple entities. You create a section of code, called a 'Loop', that defines which entities you want to affect and then specify WHAT you want to happen to those entities. 3ds Max will apply that section of code to the first entity in the group and when it's complete it will begin the 2nd entity, the 3rd, and so on, until it has finished all the objects you specified.

for i = 1 to 10 do ( your code here )

If Statements

This is how you 'test' for a certain condition, sort of like coming to a fork in the road. If I go left, then I end up here. If I go right, I end up there. A more specific example might be something like- if this object has more than 10 polygons, then color it red. If it has 10 or fewer polygons, color it blue. If we store the object in the variable "obj", the statement would look like this-

if polyop.getNumFaces obj > 10 then obj.wirecolor = red

Functions

Functions are a bit similar to loops in that they are a block of code that you intend to apply to a lot of objects or entities. However, a function operates a bit differently. You send a few variables to your function, called arguments, and it returns a value. For example, suppose you need to have Maxscript rename a file. Specifically, it needs to remove spaces or dashes and replace them with underscores. You would write your function in a few lines of code. You give your function a name (called 'defining' your function) and place it toward the beginning of your script. Then, later on in your script where it becomes necessary, you write a line of code that 'calls' your function. You send it the filename you want to rename. The function then computes the new filename and stores it in a variable that you can use later on.

We'll look at some simple examples of Strings, Arrays, Loops, If Statements, and Functions over the next few blog posts.

That's great John, now what?

For now, I'd just like to discuss how you can use Maxscripts in your typical workflow. Once you get the hang of scripting, it's pretty amazing how many opportunities you will find to create quick little time saving tools. Here are a few examples of tools I've created over the last few years (in addition to the ones available on this website):

a script that adds vertical blinds behind windows that matches the height of the window, but randomizes the width.

numerous scripts for use in preparing models and collections for Digitalxmodels.com and Turbosquid

a script that copies all the texture maps used in a scene to a specified folder

a script that converts a Group into a single editable poly

numerous file renaming tools

a roof building script that automatically takes a flat roof object and projects a specified pitch

numerous batch rendering tools

scripts to aid in landscaping scenes with CAD symbols

Who's got time for this?

Some of these are less than 10 lines of code and some of them are a few hundred. And while creating scripts can be frustrating when you're having trouble solving errors, it can be INCREDIBLY addictive. If you're someone who likes puzzles, or just a good challenge, you may find yourself skipping lunch or working late while you wrestle with code. When you finally get things working as you wish it's an incredibly rewarding feeling. However, depending on how ambitious your script idea is, it may require a significant amount of time to develop. And it's always worth asking yourself, "If I spend a lot of time creating this, will it get much use in the future? Or is this something I'd only use once or twice? How much time will it save me even if it is only used once?" It may be something you intend to use once, and will take 3 or 4 hours to write, but may save you 3 days of work!

Something else to consider is that you don't always have to make a pretty interface for your scripts. If you know you will be the only one using them (or just a few people), it may not be worth the time to design and develop a user interface. It's wonderfully simple to just open a script in the editor and execute it from there. The really nice thing about that is that if you need to make a quick tweak to your code, you can do it right there in an instant.

In part 2, we'll take a closer look at variables and then in part 3 we'll see Maxscript in action!