The for-construct
Suppose you want to create a series of 20 spheres, one partially above the other. To keep track of the number of spheres created, you must declare a variable whose value will change from, for example, 0 to 19. Each time the loop is executed the value of this variable is increased by one. As long as this value remains below 20, the loop is repeated. You can als use this variable's value in the specifation of the position of the new sphere.
int $i;
for ($i = 0; $i < 20; $i++) {
sphere -pivot 0 $i 0;
}
Explanation:
A helix
This is the same basic loop, but the position of each sphere also varies in the X and Z-axes, using the sine and cosine function to create a helix shape.
int $i;
for ($i = 0; $i < 20; $i++) {
float $sin = sin($i);
float $cos = cos($i);
sphere -pivot $sin $i $cos;
}
A spiral
This is the same basic loop extended to 100 spheres, but the position of each sphere only varies in the X and Z-axes, using $i also as a multiplication factor creating a spiral.
int $i;
for ($i = 0; $i < 100; $i++) {
float $sin = sin($i) * $i / 4;
float $cos = cos($i) * $i / 4;
sphere -pivot $cos 0 $sin;
}
A spiral surface
Instead of creating each time a new object from scratch, it is also possible to duplicate the last created object and move it relative to this position. Here a series of circles are created and lofted into a single surface.
select -all;
delete; // delete all existing objects
circle -center 4 0 0;
int $i;
for ($i = 0; $i < 60; $i++) {
duplicate;
rotate -r 0 30 0;
move -r 0 .3 0;
}
select -all;
loft;