|
Maxscript fun: A couple of examples
Maxscript provides two main things for a max user. The ability to create new features in max, and a way to perform repetitive, or difficult tasks. Even if some of this makes no sense to you, I hope some of it teaches you something new
Take the example of a light which flickers. This would be tedious to animate by hand, but a parametric light flickering tool can be made quickly in maxscript, which can be used over and over again.
Here's a couple of examples of using of maxscript to reduce repetitive or difficult tasks. The more you see of maxscript, the more you can imagine what you can do with it. This is less of a tutorial than the Dub's excellent work, but I hope the experience of this will help you write maxscripts in the future.

This picture illustrates the use of maxscript to alter the color of a group of objects. As you can see, the colors blend nicely. Coloring the objects this way manually would be possible, but would be time consuming.
Here is the complete script.
color_01 = color 0 0 80
color_02 = color 255 255 255
stepsize_r = (color_02.r - color_01.r)/$.count
stepsize_g = (color_02.g - color_01.g)/$.count
stepsize_b = (color_02.b - color_01.b)/$.count
for i = 1 to $.count do
(
newColor = (color ((i*stepsize_r)+ color_01.r-stepsize_r) ((i*stepsize_g)+color_01.g-stepsize_g) ((i*stepsize_b)+color_01.b-stepsize_b))
$[i].wirecolor = newColor
)
To use it, select a row of objects, and run the script. If there are no errors, your objects will be given a new wire color. There is supposed to be 2 lines of code between the brackets above, but the forum is making it wrap. I hope this doesn't confuse people.
---- Explanation-----
color_01 = color 0 0 80
color_02 = color 255 255 255
color_01 and color_02 are the start and end colors to use for the color fade
This is how you would define pure white in maxscript
color 255 255 255
Pretty obvious, right?
stepsize_r = (color_02.r - color_01.r)/$.count
stepsize_g = (color_02.g - color_01.g)/$.count
stepsize_b = (color_02.b - color_01.b)/$.count
This bit divides the difference between the start and end color divided by the number of objects. One for red, green and blue. This gives us the amount of change of color in each step for use later in the script. Don't worry if this makes no sense, it's the technique rather than the math I want you to experience.
A "for" loop repeats a number of times, and gives an incrementing value for each cycle in the loop
For i = 1 to 10 do (print i) would print all the numbers from one to ten in the listener. Using Print is a great way of having feedback from your calculations, like a debugger would be used by a programmer (if you are doing this tutorial, you are a programmer!)
In the code
for i = 1 to $.count do
(
blah blah blah
)
Any code in the brackets (blah blah blah) will be executed a number of times. In this case, the number ($.count) equals the number of selected objects. "i" will be incremented every time the loop repeats, until it reaches the ($.count) last number.
$ in max means "the selected object"
$.count gives a number of how many selected objects you have.
When working on a selection of objects, FOR and $ are really useful.
The next lines of code do the actual work of coloring the objects. First, I calculate the new color of the object.
newColor = (color ((i*stepsize_r)+ color_01.r-stepsize_r) ((i*stepsize_g)+color_01.g-stepsize_g) ((i*stepsize_b)+color_01.b-stepsize_b))
This defines a new color I have called newColor. The code here calculates the new color based on i (the step in the loop) and the step sizes calculated above. Don't worry if this bit is gibberish to you right now, all experience is good.
$[i].wirecolor = newColor
This line applies the "newColor" to the objects.
$[1] would be the first object in the selection
$[2] would be the second object in the selection
$[3] would be the third object in the selection
When we use $[i], we can use i to define which object in the selection we are referring to. This is where the i in the "FOR loop" comes in.
You might want to use something other than the wire color. You can create a material and apply the color to the diffuse channel with script instead. To find out what code to use. Manually make a material in the material editor, and watch the code that appears in the listener window. Use this code to create your material
This method can be used to do almost anything to a group of selected objects. You could randomly scale objects, rotate them, adjust the material, apply modifiers. The possibilities are almost endless.
Here's a slightly more advanced example of something I could not do without maxscript.

The script can be found HERE.
This is one I made years ago, so please ignore the horrible, awful formatting, and lengthy calculations in the code. Select your grid of objects (has to be an even square), select the four colors by clicking on them and using the color picker, then click on the button.
I don't expect you to understand this script, but there may be something of use to you in it. Part of the problem with maxscript is not knowing what it can do, and the more exposure to various scripts, the better. Steal ideas (even code) from other scripts, use the listener and the help to work out what you need to do. Select a word in the Maxscript editor, and press F1. The help will go straight to the relevent article (mostly).
Happy scripting, and please ask any questions you may have.
-
Nik
read 13561 times 12/1/2006 9:44:42 AM (last edit: 12/1/2006 10:02:41 AM)
|