Loops and Arrays

To manipulate various objects or properties you will use arrays. Arrays are variables that contain several other variables indexed.
An array is created using:

variable = #()

Its values are accessed this way:

variable[x], where x is an integer number indicating an element in the array

Inside MAX we have several predefined arrays, such as object classes:

Lights, geometry, shapes, cameras, helpers, spacewarps

To access each object in these arrays, simply add [1], [2], etc. after the class name. This makes it easier to manipulate all scene lights in a script.

Among array properties you can use .count, which indicates how many elements are in that array. An example is light.count, which indicates how many lights exist in the scene.

To manipulate arrays you use Loop functions. These functions allow you to step through each element of an array using a single command.

The main loop function is FOR. It repeats the function between two specified numbers of an interval, always adding 1 unit to the latest value.

  • Type for i in 1 to 10 do box height:i width:i length:i pos:[i,0,0]

This command draws a box with the actual value of the loop, stepping from 1 to 10. This value is assigned to the I variable, and can be used by the commands inside the loop.

Using Arrays and Loops, you can create a script that will create several copies of an Omnilight, positioning each of them in an object's vertex, simulating an area light.

The interface needs only a single button, which will select the object. You will add a spinned to adjust the power (multiplier) of the lights.

  • Create a new script with the following commands:
    utility area "Area Light"
    (
    pickbutton pickobj "Select Object"
    spinner mult "Multiplier" range:[0,100,1.0]
    on pickobj picked obj do
    (
    addmodifier obj (mesh_select())
    nvert = getnumverts obj
    l = #()
    for i in 1 to nvert do
    (
    vt = getvert obj i
    l[i] = omnilight pos:vt multiplier:(mult.value/nvert)
    )
    )
    on mult changed val do
    (
    for i in 1 to l.count do l[i].multiplier = (mult.value/l.count)
    )
    )

You can download this script here, and this sample MAX file here.

Notice the soft shadows on the rendered image.



The Mesh Select modifier was added to allow the commands getnumvert and getvert to work, since they require an Editable Mesh object to work.

Notice that without loops and arrays, you would not be able to use this script in any object, cause you would not know how many vertices that object would have, making it impossible to know how many lights to create, etc.

Execute the Enhanced_area_light.ms script, and observe how many more features can be added to a script.

Alexander Esppeschit Bicalho © 1999