I have placed this Instructable in the category of 3D printing. Because it is about OpenSCAD and that is how I use it. OpenSCAD is almost exclusively the program I use to create 3D objects for printing. To me it is a very powerful program that is easy to use. I teach classes in OpenSCAD at Tampa Hackerspace and I have a lot of tips to using OpenSCAD. Here I will share with you some of the tips about animating in OpenSCAD
Step 1: Downloading OpenSCAD
We are going to create some simple animations and I would like for you to follow along, so if you don't have OpenSCAD already, download it here.
Install it according to your system's requirements.
This Instructable will have more meaning for people who are already using OpenSCAD and I am expecting that most people who are reading this are, but I plan on writing it so that one doesn't need to know a thing about OpenSCAD to follow along. I will give simple explanations as we go along.
Step 2: Run OpenSCAD
When you first start up OpenSCAD it will open with three windows. If it appears to have only one window (Macintosh!), check that the "Hide editor" and "Hide console" boxes are unchecked under the "View" menu. Also slide your cursor along the edges. If the cursor turns into an icon with two parallel lines with arrows pointing out click and slide the window open. The editor window is on the left side. The console window is the bottom right. And the 3D display window is the upper right. I have seen the Macintosh open with the 3D display window only. The other two windows needed to be slid open as described.
Step 3: Creating a Simple Object
We are going to start with the simplest of objects, a sphere. In the editor type the following
Then under the "Design" drop down menu, choose "Preview". One should see a sphere as shown in the image. OpenSCAD uses a system called Solid Constructive Geometry (CSG). In this system, one combines simple shapes called primitives into more complex shapes. In my class, I say there are three parts to the system. Shapes, transformations and combinations. The phrase I use is "Transformed Shapes are Combined". Animation is the point of this Instructable, so we will not explore the meaning of that phrase to its fullest. In this simple case, we only need to concern ourselves with shapes and transformations. These two parts are required for an animation. Obviously we need a shape to see and animate. There are a number of transformations, but the two one will use most of the time are translate and rotate. These two transformations change the position of the shape. Animation is simply the position of a shape changing with time.
Step 4: Transforming (moving) Your Simple Object
Now edit your line to the following.
Choose "Preview" under the "Design" drop down. Your shape moved! OpenSCAD has a unique syntax. I have found that I must say this over and over to my students. Work from the end of the line forward. Each line ends with a ";", The function immediately in front of that ";" on every line (except lines that do math) creates a shape. Typically it will be a primitive shape, but it could also be a more complex shape created by a module. In any case, that shape will travel through the various transformations from the end of the line to the beginning. In our case, the sphere created at the end of the line travels toward the front of the line through the translate operation. This translate has the vector [50,0,0] which causes the sphere to be moved 50 units in the positive X direction.
Step 5: Doing a Rotation on Our Translated Shape
Now edit your line to say the following.
rotate([0,0,45]) translate([50,0,0]) sphere(10);
Choose "Preview" under the "Design" drop down. Your shape moved again. What happened was that the translated shape was now transformed by a rotation operation. The [0,0,45] vector caused the translated shape to be rotated 45 degrees around the Z axis. Note that the order is important here. If we switched the rotate and translate, the rotate would happen first.
translate([50,0,0]) rotate([0,0,45]) sphere(10);
The sphere starts at the origin, rotations happen around the origin, rotated spheres look the same as the original. If we switched the order it would look like we only translated the sphere. We would not see the effect of the rotate. Of course, this would be different if we had almost any other shape than a sphere. We would see that the shape itself rotated.
Step 6: Understanding Animation in OpenSCAD
I know, I have been very boring so far. What about the animation?
OK, here we go! Animations in OpenSCAD are controlled by the system variable $t. During an animation session $t will start at 0 and based on some inputs will take equal sized steps to 1; For example, if we tell OpenSCAD that our animation has 10 steps, $t would take on the values 0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9 and maybe 1 (it seems that sometimes it does and sometimes it doesn't go all the way to 1). At each step OpenSCAD will do the equivalent of the "Preview" menu. So if we make a transformation that is dependent on $t the transformed shape appears to move.
Now edit your line to the following.
rotate([0,0,$t*360]) translate([50,0,0]) sphere(10);
Now we have made the rotation around Z dependent on $t. At each step $t will be multiplied by 360 and the translated shape will rotate by that many degrees. So if we did an animation of 720 steps, each step would "move" by 1/2 degree and the sphere would make a complete revolution. In fact in doesn't matter how many steps we do, the sphere will make one revolution, the $t*360 will go from 0 to 360 regardless, the question is simply how many steps it takes. Now how do we get this to happen? We choose "Animate" from the "View" drop down menu as shown.
Step 7: 1,2,3, Animate!
When you chose "Animate" the 3D display window changed. There are now three boxes at the bottom of the window. This is how we control the animation. The first box "Time" is an output, it shows the values of the $t variable. The important box is "Steps", in this box we enter the number of steps in our animation. I have entered 360. Now $t will start at 0 and add 1/360 for each step. That means that each frame changes by 1 degree ($t*360 => 360 *1/360). The next box for us is "FPS" which stands for Frames Per Second. That controls our animation speed. I show it set to 20. If one sets it to 10, the animation will slow to 1/2 the speed. In reality, this value is less important then "Steps". This is a very simple animation. OpenSCAD can keep up with the real-time processing. For complicated shapes/systems, the FPS becomes irrelevant. OpenSCAD can't keep up and one can't see real time. I have had animations where each frame took 20 or 30 seconds to render. But all is not lost! One can select the "Dump Pictures" box. OpenSCAD will render each frame and store it in a file. It will give the files sequential names, one can use a program like VirtualDub to create a real-time video from those images. I will be writing an Instructable on how to do animations using this technique.
Step 8: More Complicated Animations and Using the Lookup Function
One can do highly complicated animations with OpenSCAD. Multiple variables can depend on $t. I have uploaded a file where I did a quick little animation of how I would design a crane system that could 3D print a house. There are lots of comments in the file. So I am not going to repeat the discussion found in the file here. The bottom line is that the lookup function allows one to define a few points for the animation. The lookup interpolates between points allowing one to give values to a lot more $t values that the few points defined. In the crane example, the interpolations result in movement along straight lines. But one can take the interpolated values and use them as parameters to equations that result in almost any kind of movement. For example the link below shows an animation I made where there are extensive calculations to determine the positions of the carriages on the threaded rods and the positions of the arms. The motions of those parts are not linear with the interpolated values.