If you just happened to stumble on this page, maxscript is the built-in scripting language for 3DStudio Max (3dsmax). It allows users to automate many of the features of 3dsmax, even conceive of new features. While it's not the most user friendly language, its a very powerful language in the max environment that can be used for a variety of tasks from simple to the extremely complex. The language has gone through and continues to go through evolutions in order to better suit the artists who use 3dsmax.
Learning maxscript can seem challenging and somewhat time consuming. Unfortunately, like most things in life it takes practice and dedication to become proficient. Have no fear though, if you make your maxscript experience fun, it can seem quite easy, at times.
This is the first post in a what should be a series of posts explaining maxscript in simple, easy, fun steps to help you get aquainted with my general process in developing scripts. I'll attempt to keep the discussion simple and direct, so as not to confuse you.
Workflow
Generally, I take a problem and boil it down to the most fundamental steps. Then I'll write script that validates the possibility of each step, note that not everything is possible in maxscript. I'll continue by planning out the structure of my final code and eventually I'll combine all the small steps into one unified script using my plan. This takes the overwhelming feeling out of something that may sound complicated while also giving you a set of milestones you can use to track your progress. With experience you'll be able to fly through these steps with relative ease.
In this first lesson I'm going to cover the basics of the maxscript language structure. Once you understand how a language works, you can generally apply that knowledge to any language out there. Before we get started you'll need to know a little about the Maxscript Listener window.
The listener window (F11): This window allows you do several things including, testing single lines of code, viewing script output and general error information, and recording max actions. I generally always have this window open while I work.
With the listener active you can do several things easily:
Ctrl-N: New script
Ctrl-O: Open script
Ctrl-D: Clear window
Shift-Enter: Execute the active line, regardless of it's position in the listener window
I'll point out more shortcuts as I think they're necessary. Take a look at the drop downs and help file for more information.
Maxscript Help
Probably the most important tool in the maxscript arsenal. I would not have a job now if it weren't for the help documentation, memorizing every function in maxscript is probably impossible. An important, but hopefully obvious note is that the maxscript help is not the same as the general max help documentation. This documentation is something I strongly urge you to explore and attempt to understand how to navigate. I've found that with enough practice I can gather information relatively quickly, though it can take some patience with more complicated searches. I'll attempt to explain some of my searches in examples I give as we progress.
Getting Started
The most basic definition I can think of for maxscript (or any language for that matter) is that it's a set of instructions, much like a to-do list. In maxscript the instructions are read top-down so for example, 3Dsmax takes the first item in the list and executes it, then the second and executes it and then the third and so on. With this in mind, scripting and programming really just mean translating your commands into a language an application or an operating system understand.
Example 1. Making a sphere move
In this example you'll learn how to create a sphere and animate the spheres' position. All with maxscript and in only a few lines of code. So lets write ourselves a small to-do list so we know the steps and the order in which they're necessary.
the To-do list:
1. Create Sphere
2. Animate the sphere at frame 25 to move up 100 units (on the Z axis)
3. Animate the sphere at frame 50 to move down 100 units
Maxscript:
(
mySphere = sphere() -- step 1. Create the sphere
with animate on -- turn animate on
(
-- step 2. Move the sphere and set a key at frame 25
at time 25 mySphere.pos = [0,0,100]
-- step 3. Move the sphere and set a key at frame 50
at time 100 mySphere.pos = [0,0,0]
)
)
Copy and paste this code into a new script (ctrl-n in the listener window). Then execute the script by evaluating it (ctrl-e while in the scripting window, or by accessing the Tools drop down menu).
You must note that there are many ways in maxscript to do the same thing, this just happens to be the quickest method I know. You could for example set keys to the spheres position controller at different times and then assigned the keys different values. Knowing that there may, and generally is, a different way to do one thing is crucial in finding information on different techniques. Try and keep your mind open to different possibilities.
What Happened here?
The first line of the script assigned a sphere to a variable (which I will explain in the next lesson). In this line the sphere was actually created in the 3d environment, as is apparent in the viewports. The second line 'with animate on' basically tells 3dsmax to add keys for any code within the preceeding parens '()'. The third and fourth line tell max to move the sphere up and down at different times. Thats all there is to it!
Conclusion
While this first example is extremely simple, you can see how the language is executed in a top-down fashion, much like the way we read any normal document. In the future I will explain the use of functions (also referred to as methods) and structures (similar to classes in other languages) which will allow you to control the way the script is executed. Even though you will be able to control the flow, the top-down execution will still hold true.
You should now have a basic understanding of the fundamental flow of maxscript. This is essentially how most languages work in their simplest forms. I hope you enjoyed this lesson and will come back for a seconds.
Homework
1. Create a scene with a plane on the ground thats 100x100
2. Create a Box on the ground anywhere.
3. Create a Sphere that starts at 0,0,0 (on top of the ground)
4. Animate the sphere to roll towards the box, then hop over the box in 100 frames. (Give the sphere a secondary bounce when it lands, a bounce thats 1/3 the height and distance of the first jump)