Parametric Spinning Star

How to use OpenSCAD to design for 3D printing

Pioneer Dr. Joshua Pearce is an academic engineer known for his work on photovoltaic technology, open-source-appropriate technology, and open-source hardware. In this post, he shares how you can use OpenSCAD, open-source software, and coding to generate parametric designs.

There are many 3D solid modeling programs available, but few have the power and potential of OpenSCAD. OpenSCAD has several nice attributes that make it particularly good for schools and for those starting in 3D printing but would still like to make professional quality models to 3D print.

First, it is free and open source software, which means you are free to download it, use it, change it, etc (under General Public License version 2). The software is available for Linux/UNIX, Windows and Mac OS X. Unlike most CAD models it does not have an intimidating, highly sophisticated interface, it is not interactive and you do not need to draw. Instead, it is something like a 3D-compiler that reads in a computer script that describes the object. For anyone that is even a little familiar with computer coding, it is easy to learn. It is also extremely helpful for proving to young people why math is so important. You literally write equations for the objects you want to 3D print. As you are writing the equation the designs are exact, you have full control. If you design in OpenSCAD properly, as I hope to show you below, you can design in such a way to make not just the product you want to print, but ALL of the products of the same class that anyone might want to print. This is called making your model “parametric” and that is the first step.

Step 1: Make everything about your design parametric

Making your design parametric allows later scaling of any aspect of your design. It allows you to make quick changes as you print prototypes to ensure, for example, a good snap fit. Perhaps most importantly it allows newbies to make custom objects from your design easily. To make your model parametric all numbers should be made into variables. For simple designs, it easiest to use letters — but when you do that make sure you add:

// comments like this

Example:

a=5; // I have just defined the letter “a” to be 5 mm (all measurements in OpenSCAD are in mm)
b=10; // the letter “b” is now 10 mm 
c=20; // note that you end each line of OpenSCAD code with a semicolon

This makes equations easy to type but can become a disadvantage for large projects with lots of variables. For those cases you can make your variable descriptive.

Example:

box_length=5; 

Step 2: Build your design from 3D primitive objects

When you think about your design try to break it down into your head into basic shapes. So, for example, a simple snowman is made up of three roughly spherical shapes stacked upon one another. OpenSCAD provides you with 3 core primitives:

Examples:

cube([a,b,c], center=true); // note you can make rectangular prisms with the cube command as below

   

01_openscad

sphere(a, $fn=c);  //$fn is the resolution the higher the number the smoother the surface

   

02__openscad

cylinder(h = c, r1 = b, r2 = a, center = true);

   

cylinder

If you type the examples into OpenSCAD you can see them by hitting F5 for a quick look but then you can render them with F6. After you render them you can export them to STL and then use the STL file to print on your favorite open source 3D printer.

Step 3: Create Interactions between Primitives

After you have basic primitive designs you can begin to create interactions between them to get the exact shape you want for your design. OpenSCAD has the basics of adding shapes together, subtracting them, intersecting them and hulling them. When I do this I like to “try before you buy,” by adding one object at a time to a transparent rendering of the other object.

For example to add two objects together you use the union command. Anything that is inbetween two { } is combined

Example:

union(){
%cube([a,b,c], center=true);
sphere(a, $fn=c);  
}

   

04__openscad

The cube is shown in see-through grey. If I am happy with it I delete the “%” and get this:

   

05__openscad

You can subtract objects by using the difference command.

Example:

difference(){
cube([a,b,c], center=true);
sphere(a, $fn=c);  
}

   

06__openscad

In the difference command any objects after the first will be subtracted from it. In the example the sphere is subtracted from the cube. Similarly you can look at where the two objects intersect by using the intersection command.

Example:

intersection(){
cube([a,b,c], center=true);
sphere(a, $fn=c);  
}

   

Intersection

Finally you can merge two objects together by doing a convex hull of child nodes.

Example:

hull(){
cube([a,b,c], center=true);
sphere(a, $fn=c);  
}

   

08__openscad

Note how with all these interactions with our example cube and sphere the syntax is the same.

Step 4: Move and orient primitives where you want them

Now that you know how to combine objects you might also want to position them in 3D space exactly where you want them. The best command for that is the translate command.

Example:

union(){
cube([a,b,c], center=true);
translate([0,0,b])sphere(a, $fn=c); }

   

09__openscad

The translate move things in the x, y and z coordinates. In the example we left x and y alone but moved the sphere up b (or 10 mm).

Step 5: Make each completed component a module

By making groups of your objects into a module you essentially create your own new “primitive” no matter how complex it is. This allows you to clear your workspace. The syntax is like this to create a module called example:

module example(){ put your module scad here }

Here is an example:

module example(){
union(){
cube([a,b,c], center=true);
translate([0,0,b])sphere(a, $fn=c);  
}
}

If you render this nothing will show up. To see it, call it by:

example();

   

10__openscad

You can manipulate your module the same way you would with any primitive -- so you can add or subtract them. Or you can rotate them like this:

rotate([45,0,0])example();

   

11__openscad

Note the rotation is in the x plane.

Or you can hull() them like this:

hull() {
example();
}

   

12__openscad

Step 6: Use coding to make repetitive tasks easy

One place OpenSCAD really shines above other CAD tools is in automating repetitive tasks - because you essentially have the power of normal programming. So for example you can set up a for loop:

for (i = [1:12])
{
assign (angle = i*30)
{
    rotate(angle, [1,0,0])
 example();
}
}

   

13__openscad

Step 7: Make something you want (while making it easier for others to make exactly what they want)

My lab often makes 3D printable scientific equipment. So for example we might want to make a new plate that allows for faster separation in a Buckner Funnel (piece of laboratory equipment used in filtration). To do that we made this OpenSCAD code and we did it in such a way that it would be easy for others to customize it. Note how the comment comes before each variable:

   

code

You can make any kind of plate you want by simply changing the values of the variables. Without changing parameters in OpenSCAD the code returns this:

   

15__openscad

Step 8: Share!

Lastly, do not forget to share your creation with everyone else.

You should always share both your STL file as well as the SCAD file of your original source with everyone. This makes it much easier for people to build on your work (and perhaps they will make your creation better - so the next time you need this model you can print the improved version).

But what about people that find OpenSCAD too tricky? For them, we created an open source customizer that can work for any OpenSCAD script. You can download the customizer from github. And read all about it in a paper for free at academia.edu.

You can then either upload your OpenSCAD to a repository that has a customizer or make your own website to make it easy for others to make a custom creation.

   

16__openscad

   

17__openscad

Now it is easy even for someone that does not understand scripting at all to make exactly the type of perforated plate they want.

Here is another example using only free software - all the features and all the control over your designs:

   

18__openscad

Now anyone can move the sliders or change the numbers in the webpage and immediately make a new version of your design. See:

   

19__openscad

You can also make more complex designs by building on others’ past work. My group keeps libraries of useful OpenSCAD functions for us on github, but there are many others.

Hopefully, this was enough to get you interested and started in using OpenSCAD to make your next 3D printing project. If you need more help see the OpenSCAD documentation for the manual, tutorials, and videos and there is also a cheat sheet for the syntax you can 3D print and post above your computer until you get used to using OpenSCAD.

   

cheat sheet

Happy creating!

Support