One of the most powerful features of OpenSCAD is the use of scripting to generate arrays or groups of objects. Until now, in order to create 12 similar objects you would have to copy and paste the object multiple times (11 times to be exact). A better way to do this is with the help of a “for” loop. A “for” loop is a piece of code that is executed a designated number of times. This can be used to create multiple copies or versions of an object. First we have to tell the computer how many times to execute the loop. We will use the common index variable, i . We will use i because it is the most common used in most programming languages and examples. The for loop is not exclusive to OpenSCAD. All programming languages have their own version of various types of loops. Defining an interval in OpenSCAD (and other similar programming languages) is easy. To define the integers [0,1,2,3,4,5,6,7,8,9,10,11] we only need to say i = [ 0 : 11 ]. OpenSCAD knows that you mean to say “let i equal the integers from 0 to 11.” Notice that zero counts as one iteration so to perform 12 iterations we need only go up to 11. The interval defined by i = [ 1 : 12 ] also produces a set of 12 numbers and will cause the loop to iterate twelve times. The difference is in how it will interact with the code. Let us consider the simple case of two iterations. Suppose I want to create two spheres, one right next to the other. The intervals [ 0 : 1 ] and [ 1 : 2 ] will both produce two iterations but with an important difference. Consider the following for loops and their outputs. Can you spot the difference? Notice the position of the spheres relative to the origin.
for (i=[0:1]) { translate([20*i,0,0]) sphere(r=10); }
for (i=[1:2]) { translate([20*i,0,0]) sphere(r=10); }
In the example on the left the code is executed once for i =0 and once for i =1. When i =0 translate([20*i,0,0]) is the same as translate([0,0,0]). Therefore the first sphere created is not translated anywhere and remains at the origin. In the second example the first sphere that is created is translated 20 in the positive xdirection and the second sphere is translated 20*2 or 40 in the positive xdirection. Both examples produce 2 spheres but this small detail should not be overlooked. The previous example is one way to use a for loop: to create identical copies of an object and translate them along a straight line. i is not limited to integers. If we give OpenSCAD three numbers such as [ 0 : 0.1 : 1 ] we are saying “let i go from zero to one in increments of 0.1.” This produces the following set [ 0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1]. Similarly, [ 0 : 5 : 25 ] produces [ 0, 5, 10, 15, 20, 25 ]. Let’s see how a for loop can be combined with rotate([x,y,z]) to arrange objects in a circle. There are 360 degrees in a circle. To arrange 12 objects evenly around a circle they would each need to be separated by 30 degrees (360 divided by 12). First we need to decide how large our circle is going to be. I want to arrange 12 spheres of radius 10 in a circular pattern of radius 50. This can be done with the following for loop.
for (i=[0:11]) { rotate([0,0,i*30]) translate([50,0,0]) sphere(r=10); } Notice that i only appears in the parameters for rotate. Translate must be done “first” in order for this to work. “first” in this case means it is executed before rotate. OpenSCAD works backwards from the semicolon. A sphere is created, then translated, and finally rotated. The code inside the curly brackets {} is executed twelve times. The first when i=0, the second when i=1, and lastly when i=11. What happens if we translate and rotate with respect to i at the same time?
for (i=[0:29]) { rotate([0,0,i*15]) translate([5*i,0,0]) sphere(r=10); } Can you guess what happens if we apply the scale transformation in terms of i?
Why is this useful? Let us return to our variable staircase with 5 stairs. In that example the width, height, and depth of the individual stairs were easily adjusted by the changing the value of the variable. We were, however, stuck with 5 stairs and the copypaste procedure for making them was somewhat clumsy. We can now create a variable staircase where the number of stairs is, in itself, a variable and the code is much shorter. We start with the same variables stair_width, stair_height, stair_depth and add another variable called stair_count. By using a for loop where the index i, is determined by the variable stair_count we can truly create a variable staircase.