Parametric design with OpenSCAD

Every time I design something with a 3D editor like Blender, my inner programmer whispers repeatedly: "If you later have to modify what you're doing, adjusting everything will be a nightmare."

And it's not without reason. If we're not designing something purely artistic, an early design error implies a lot of correction effort later on and many mouse clicks. That's why parametric design programming languages like OpenSCAD exist.

3D render of the coronadados

This coronavirus dice set I designed recently is composed of 81,454 vertices, but its source code consists of just 140 lines. And if I wanted to modify a parameter, like the size of the legs or the text on one of its faces, I would only need to change the value of a variable.

Parametric languages are not new, and almost all 3D editors allow you to program models in one way or another. What makes OpenSCAD special is the simplicity of its syntax and the speed with which you can design any piece.

This language consists of a few basic commands to create volumes and apply operations to them. In fact, the complete specification of OpenSCAD fits in this handy cheatsheet with links to usage examples of each function in the official wiki.

As a demonstration of its power, I'm going to explain how to design a simple model step by step. If you want to try programming it at the same time, you can install OpenSCAD, which takes up about 20MB. The model I've chosen is a pear, which, although not very useful, is quite illustrative.

We'll start by creating a sphere with a 20mm radius. Calling sphere(20) would be enough, but it's better to store all measurements in variables at the beginning of the code to make adjustments more comfortable. All lines must end in ; or they will be considered part of the next line.

r = 20;

sphere(r);
First step: a simple sphere.

Pressing F5 will render the sphere on the right. Now we're going to create a second smaller sphere to which we'll apply a translate([x, y, z]) transformation to raise it a bit. Transformations apply to the next object on the same line. In this case, with no ;, it will apply to the sphere on the next line.

r = 20;

sphere(r);

translate([0, 0, r*1.5])
sphere(r/4);
Second step: a second small sphere above.

Thus, we can concatenate several transformations to the same object that can be read as "translate to this position the sphere with this radius." Besides transformations, we can also perform operations on more than one object at the same time, like the hull operation, which we'll apply to obtain the hull of our two spheres.

r = 20;

hull() {
    sphere(r);

    translate([0, 0, r * 1.5])
    sphere(r / 4);
}
Third step: hull operation to give pear shape.

By using braces, we have applied the hull operation to a set of bodies, resulting in another object to which we can also apply transformations. In this case, we'll apply the color transformation to give it a pear color. And while we're at it, we're going to modify the special variable $fn —fragments number— to increase the quality of the rendering.

$fn = 64;
r = 20;

color("#de3") hull() {
    sphere(r);
    translate([0, 0, r * 1.5])
    sphere(r / 4);
}
Fourth step: More resolution and color.

This way of programming volumes shapes our way of thinking in design. By reducing everything to basic shapes and gradually adding operations and transformations, we build our models incrementally. The next increment will be using the difference operation to subtract a sphere from the entire pear, serving as a bite mark.

$fn = 64;
r = 20;
difference() {
    // color("#de3") hull()...
    color("#cea")
    translate([r * 3/4, 0, r / 2])
    sphere(r * 3/4);
}
Fifth step: Difference with another sphere to make a bite.

All that's left is the pear's stem. We can define a few variables to create a cylinder, tilt it a bit, move it to the tip of the pear, and give it a brown color. As we haven't used cylinder until now, we can take a look at its syntax in the cheatsheet.

sa = 10;
sr = 2;
sh = 10;

color("#632")
translate([0, 0, r * 7/4])
rotate([sa, sa, 0])
cylinder(r=sr, h=sh);
Sixth step: pear tail.

And we could consider this pear finished. However, the variables we have used do not have very meaningful names. OpenSCAD allows us to use comments to give them a name and a valid range of values without making our code more complex.

// Model quality (fragments number)
$fn = 64;  // [32,64,128,256]
// Pear radius
r = 20;  // [1:.1:100]
// Stalk angle
sa = 10;  // [0:360]
// Stalk radius
sr = 2;  // [1:.1:10]
// Stalk height
sh = 10;  // [1:20]

Using this syntax, the software itself will generate an adjustment panel that only lets us modify the variables within the defined ranges. This is also useful to tell other users what are the safest ranges to adjust a parameter without breaking the model.

Customizable parameters

I've uploaded the complete code of the example to the web. It has many magic numbers to keep it simple, although in a real design, it's advisable not to use any number that is not defined in a variable, to make the model as parametric and easy to adjust as possible.

If you want to see some real examples, you can take a look at some of my creations, like these minimalist spice jars or this grass base for pots.

Comentarios

bonu Reply
Que grande, lo use para arreglar la funda de mis Airpods. Tremendo el tutorial!!
Juan C. Roldán Reply

Replying to bonu:

Me alegro de que te haya sido de utilidad. Igual puedes colgar tu diseño en Thingiverse, seguro que hay más gente con el mismo problema!

Leave a comment

Get a mail
4d8cd43bbbfbbd2b7aed08d9a2b0ef251cebfd3e2603b74b710a2d38b7f8ec39