3D Designer
Modules
At some point, things are going to get complex. You are going to have objects twisted around other objects, modifying other objects that are used in combination with yet more objects.
It helps then that openSCAD supports modules to organize stuff. Modules are simply a bunch of instructions that go together.
To illustrate how they work, let's make a funnel as shown in Figure 7.
A funnel is made up by two parts: a hollow, cut-off conical object that I am going to call the hat (I couldn't find formal names for the parts of a funnel, so I am improvising here), and a hollow cylinder I am calling the pipe.
Turns out both the hat and the pipe can be made entirely from cylinders, since the hat, for openSCAD, is a cylinder with two different diameters at each end. As you need it to be hollow, you make another cylinder with a slightly smaller radius at both ends and subtract that from the bigger one. The module for that would look like Listing 3.
Listing 3
Hat Module
module hat () { difference () { cylinder (r1=8, r2=2, h=10, center=true, $fn=50); cylinder (r1=7.5, r2=1.5, h=10, center=true, $fn=50); } }
The pipe module is even simpler (Listing 4), but you do have to remember to translate the lower end of the pipe to the top of the hat (line 2).
Listing 4
Pipe Module
01 module pipe () { 02 translate ([0,0,10]) { 03 difference () { 04 cylinder (r=2, h=10, center=true, $fn=50); 05 cylinder (r=1.5, h=10, center=true, $fn=50); 06 } 07 } 08 }
With these two modules, you can create a funnel ()
module that calls the other two (Listing 5, lines 17 to 20). Then call the funnel ()
module to actually show something in the preview pane (Listing 5, line 22).
Listing 5
funnel.scad
01 module hat () { 02 difference () { 03 cylinder (r1=8, r2=2, h=10, center=true, $fn=50); 04 cylinder (r1=7.5, r2=1.5, h=10, center=true, $fn=50); 05 } 06 } 07 08 module pipe () { 09 translate ([0,0,10]) { 10 difference () { 11 cylinder (r=2, h=10, center=true, $fn=50); 12 cylinder (r=1.5, h=10, center=true, $fn=50); 13 } 14 } 15 } 16 17 module funnel () { 18 hat (); 19 pipe (); 20 } 21 22 funnel ();
You can take this modularity further and save a set of modules into a separate .scad file altogether. You can then import them back into other files using either the include
or the use
methods.
The include </path/to/file.scad>
method imports the whole thing, modules and commands that are not in modules, integrating the whole imported file into your current file. The use </path/to/file.scad>
method, on the other hand, only imports the modules so you can use them as library modules with the code in your current file.
This means that, to get a funnel with include
, all you have to do is:
include <funnel.scad>;
To do the same with use
, you would also have to call the funnel ()
method like this:
use <funnel.scad>; funnel ();
Conclusion
This article has covered the most essential bits and pieces of the OpenSCAD language. With this and the user manual's excellent documentation [5], you should be able to start creating your first 3D pieces.
That said, there is quite a lot more to learn regarding OpenSCAD. We'll be looking at more advanced things in next month's issue. We will also be creating a real piece to print and showing how you can go all the way from concept to real physical object.
Until then, have fun!
Infos
- Thingiverse: https://www.thingiverse.com/
- Atelier: https://atelier.kde.org/
- OpenSCAD: http://www.openscad.org
- FreeCAD: https://www.freecadweb.org/
- OpenSCAD User Manual: https://en.wikibooks.org/wiki/OpenSCAD_User_Manual
« Previous 1 2 3
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Budgie 10.10 Scheduled for Q1 2025 with a Surprising Desktop Update
If Budgie is your desktop environment of choice, 2025 is going to be a great year for you.
-
Firefox 134 Offers Improvements for Linux Version
Fans of Linux and Firefox rejoice, as there's a new version available that includes some handy updates.
-
Serpent OS Arrives with a New Alpha Release
After months of silence, Ikey Doherty has released a new alpha for his Serpent OS.
-
HashiCorp Cofounder Unveils Ghostty, a Linux Terminal App
Ghostty is a new Linux terminal app that's fast, feature-rich, and offers a platform-native GUI while remaining cross-platform.
-
Fedora Asahi Remix 41 Available for Apple Silicon
If you have an Apple Silicon Mac and you're hoping to install Fedora, you're in luck because the latest release supports the M1 and M2 chips.
-
Systemd Fixes Bug While Facing New Challenger in GNU Shepherd
The systemd developers have fixed a really nasty bug amid the release of the new GNU Shepherd init system.
-
AlmaLinux 10.0 Beta Released
The AlmaLinux OS Foundation has announced the availability of AlmaLinux 10.0 Beta ("Purple Lion") for all supported devices with significant changes.
-
Gnome 47.2 Now Available
Gnome 47.2 is now available for general use but don't expect much in the way of newness, as this is all about improvements and bug fixes.
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.
-
Armbian 24.11 Released with Expanded Hardware Support
If you've been waiting for Armbian to support OrangePi 5 Max and Radxa ROCK 5B+, the wait is over.