OK, let's dive in!
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.
Let's start by looking at some variables.
myBox = box()
This will create a box at the origin, a simple a cube with length, width, and height set to the default dimensions in whatever units Max is set to. It's then 'saving' the box into the variable "myBox". Let's take a closer look. Why does "myBox" look the way it does? Why not "My Box"? First off, variable names cannot have spaces. Maxscript would think you were moving on to your next word of code. Why is the "B" in caps and not the "m"? The "B" is capitalized simply so that we can quickly see that it begins a new word. We can't have a space so we use a capital letter. Why not use an underscore? You could. And I won't tell you that you can't. The important things here are legibility and consistency. You would be amazed how quickly you forget what your code does, and so when you revisit a script after a few weeks, you need to read through it all again, and depending on how long it's been, it might seem like the first time you're looking at it! (Not to mention somebody else should be able to read your code too!) This begs a mention of comments, but we'll talk about that a bit later.
And how about the other side of the equals sign? Why are there "()" after "box"? The short answer is because this is a function. 3ds Max has a number of internal functions for core operations within the program. A function, as I described in the previous blog post, if called with no arguments, will end with a pair of parenthesis.
myBox = box width:15 length:6 height: 4
In the example above, the box dimensions are the arguments of the function, so the parenthesis are omitted.
Using variables allows us to control objects or entities in an efficient manner, but more importantly because computers are not humans and we need to provide the computers with a way to identify these objects or entities. (I should take a second to clarify what I mean when I refer to "objects or entities" because you may be wondering about that. Sometimes in Maxscript we're operating on mesh objects. We could store those objects in variables. Sometimes a variable may contain an object property, like it's width or it's wireframe color. Sometimes a variable is storing a numerical value or text string. These aren't necessarily 'objects', so I'm using the word 'entity' to describe some thing without getting too specific.)
So in the example above, our variable is 'myBox'. With this variable we can do some pretty cool stuff!
myBox.width = 3
myBox.wirecolor = red
myBox.pos.x = 100
myBox.name = "The BEST box in the world"
You probably took notice of the period that follows "myBox". You will see these periods all throughout Maxscript. Basically, the purpose of the period is to signify a property of the object that is stored in the variable. For example, if "myBox" is a Box Primitive, the properties associated with that object would be length, width, height, position, wireframe color, material, and so on. Another example, if "mat" is a Max Standard Material, it's properties would be diffuse color, specular amount, opacity amount, bump texture map, reflection map, etc. And each of these properties will be accessible by typing out the variable name, followed by a period, followed by the property.
One very cool variable is the Max System variable "$". This is the shorthand version of the system variable "selection", and they can be used interchangeably. The "$" simply refers to the current selection in the viewport! It doesn't matter if it's 5 lights, 2 cameras, a box, spline, or all of the above. Using the "$" variable is a very simple way of applying commands to an object you already have selected. It's worth mentioning up front, that as convenient as the "$" variable is, best practice states that the "$" should really only be used in the Listener or for quick tests in the editor. Ideally, you'd store the selection into a unique variable to help avoid confusion and mistakes.
In the next post, we'll take a look at this and actually begin typing some commands into the Listener and seeing Maxscript in action. Stay tuned!!