OpenSCAD Intermediates: Modularity
You didn’t think I’d let you off that easily did you? There’s so much more to learn about OpenSCAD! I’ve put together a series of tutorials on using this amazing powerful program. If you haven’t scanned the tutorials or just tried using the program you’re really missing out. The commands and interface are simple and straight forward and the benefit of being able to generate a printable STL each and every time is a huge benefit.
In this OpenSCAD tutorial series so far we’ve covered the basics of the OpenSCAD interface, how to make 2D forms, how to make some basic 3D forms, how to position those forms in 3D space, the different ways to combine forms, and how to create mashups of one or more existing STL’s and OpenSCAD forms. Although I described the last two tutorials as “intermediate” levels, that’s really only because you learned the basics so quickly from the first few tutorials. With just the basics you can literally design anything you can imagine. The “intermediate” lessons will help you do everything you’ve already learned – but easier, more efficiently, and more reliably.
Before we get started, the image is from johnbentcope‘s OpenSCAD tutorial homework. I’d like to include a picture of your homework next time. So, practice making something in OpenSCAD, upload it to Thingiverse with an open license, and tag it with “openscadtutorial.”
Today we’ll cover the “module” command in OpenSCAD.
More after the jump!
- Module.
- There are a lot of benefits to using modules. They let you easily reuse commands over and over, they let you manipulate everything inside a module at the same time, and they let you easily make variations on a design without having to re-write tons of commands.
- Declaring a Module
- Using a module is almost as easy as just typing the word. Let’s suppose I want to create a module named, “box” that is just a “cube” of with certain dimensions. Here’s a very simple example:
- “module box()
- {
- cube([5,10,15]);
- }”
- Using a module is almost as easy as just typing the word. Let’s suppose I want to create a module named, “box” that is just a “cube” of with certain dimensions. Here’s a very simple example:
- Running a Module
- If you copy and paste that code into OpenSCAD and hit F5 to render it, you will see… nothing! All you’ve really done is define the module, which is essentially creating a new command for OpenSCAD called “box.” So, let’s try this instead:
- “module box()
- {
- cube([5,10,15]);
- }
- box();”
- If you copy and paste that code into OpenSCAD and hit F5 to render it, you will see… nothing! All you’ve really done is define the module, which is essentially creating a new command for OpenSCAD called “box.” So, let’s try this instead:
-
- This will create a box with the dimensions of 5x10x15. Here’s how you make two such cubes:
- “module box()
- {
- cube([5,10,15]);
- }
- box();
- box();”
- Of course, doing this will make both boxes occupy the same space. So, let’s just move one of them over, like so:
- “module box()
- {
- cube([5,10,15]);
- }
- translate([20,0,0]) box();
- box();”
- Here, you’ll notice that changes to the first box don’t affect changes to the second box. That means we can do all kinds of wacky things:
- “module box()
- {
- cube([5,10,15]);
- }
- scale([2,2,2]) translate([20,0,0]) rotate([0,45,45]) box();
- box();”
- This will create a box with the dimensions of 5x10x15. Here’s how you make two such cubes:
-
- But, why stop there? You can combine modules together into other modules. Let’s try this:
- “module leaves()
- {
- cylinder(20,5,0);
- }
- module box()
- {
- cube([5,10,15]);
- }
- module tree()
- {
- leaves();
- scale([0.5,0.5,0.5]) translate([-2.5,-5,-15]) box();
- }
- tree();”
- But, why stop there? You can combine modules together into other modules. Let’s try this:
-
- And, here’s how you make a forest:
- “module leaves()
- {
- cylinder(20,5,0);
- }
- module box()
- {
- cube([5,10,15]);
- }
- module tree()
- {
- leaves();
- scale([0.5,0.5,0.5]) translate([-2.5,-5,-15]) box();
- }
- module treeline()
- {
- translate([20,0,0]) tree();
- translate([10,0,0]) tree();
- tree();
- translate([-10,0,0]) tree();
- translate([-20,0,0]) tree();
- }
- module forest()
- {
- translate([0,20,0]) treeline();
- translate([0,10,0]) treeline();
- treeline();
- translate([0,-10,0]) treeline();
- translate([0,-20,0]) treeline();
- };
- forest();”
- And, here’s how you make a forest:
- Variables in Modules
- Let’s do something a little more interesting than just make a pile of trees. Do you remember back when we talked about cylinders and how it was good form to describe a cylinderlike this?
- “cylinder(h = 20, r1 = 10, r2 = 5);”
- Well, just like when your mom used to sneak some veggies in your oatmeal1 , I actually taught you about variables without you knowing it. When we use words (or just a few characters) in place of numbers, these are called, “variables.” If you can get the concept of making the letter “h” equal to “20,” then you’re ready to do some really amazing things with modules.
- Before we get started with variables, let’s take a closer look at this longer format for describing a cylinder. First, if you define the “h”2 , “r1″3 , and “r2″4 then the order in which you define them doesn’t matter. Secondly, this good habit will help you make better use of modules. Suppose you’re one of those people who hates that the OpenSCAD command for cube is really a command for making various kinds of boxes or that the cylinder command also makes cones. 5 Well, fret no more! You’re free to create modules like this:
- module box(width,length,height)
- {
- cube([width,length,height]);
- }
- box(5,10,15);
- translate([-40,0,0]) box(20,25,30);
- I’ve colored a few key parts above to help show what we’re doing. If you look at line #5 the first number (5) will correspond to “width,” the second number (10) will correspond to “length,” and the third number (15) will correspond to “height” when you run that module. Thus, line #5 will create a “cube” with the dimensions of 5mm x 10mm x 15mm. Interestingly, line #6 will create a “cube” with the dimensions of 20mm x 25mm x 30mm. So, by creating one little module, we can create two very different objects.
- But, what if you knew that you always wanted a box with certain proportions? Say a box that always has a length that’s 2mm longer than the width and a height that’s always 4mm longer than the width. Well, then we can take a little shortcut:
- module wackybox(width)
- {
- cube([width, width + 2, width + 4]);
- }
- wackybox(3);
- translate([10,0,0]) wackybox(6);
- The nifty thing about the example above is that we only need to specify one variable (the width) and the module takes care of everything else for us.
- Let’s do something a little more interesting than just make a pile of trees. Do you remember back when we talked about cylinders and how it was good form to describe a cylinderlike this?
- Defaults in Modules
- Sometimes you just want a module to work (like the “tree” module above), but still have the flexibility of using a variable (like the “wackybox” module). If you specify a “default” in the module, you can have the best of both worlds.
-
- module reallywackybox(width = 3)
- {
- cube([width, width + 2, width + 4]);
- }
- reallywackybox();
- translate([10,0,0]) reallywackybox(5);
- In this example just running the “reallywackybox” module without specifying the width will use the default value of 3. However, you can easily override this default value by just including a value when you use the module, as we did on line #6.
- More Variables in Modules
- Just because we’ve only used variables in objects so far doesn’t mean we’re limited to using them in 3D forms. We can use them in any of the commands we’ve learned. Check this out:
- module superwacky(funky = 10)
- {
- translate([1,2,funky]) scale([1,funky,1]) rotate([funky, funky,funky]) cylinder(funky,funky + 2, funky +4);
- scale([funky,2,1]) translate([1,funky,3]) rotate([-funky, funky + funky,-funky]) sphere(funky);
- }
- superwacky(4);
- Just because we’ve only used variables in objects so far doesn’t mean we’re limited to using them in 3D forms. We can use them in any of the commands we’ve learned. Check this out:
- Conclusion
- Modules give you ability to reuse just one piece of code repeatedly. Combining modules with variables will let you reuse the same code in totally different ways. By taking something you’ve designed in OpenSCAD and putting it into a module, you can create multiple instances of it and manipulate those instances in lots of different ways.6
Homework assignment
Now that you’ve learned how to use modules and variables, how about showing everyone what you can do? Use what you’ve learned today to create a module with variables and run the module at least twice to create two different objects, then upload your OpenSCAD file and the STL to Thingiverse. As always, to make me extra proud be sure and tag it with “openscadtutorial.” As if basking in my affection wasn’t enough, I’ll pick one someone’s OpenSCAD homework and use their designs as part of the next tutorial.
Bonus Section 1: The Tutorials So Far
- OpenSCAD Basics: The Setup
- OpenSCAD Basics: 2D Forms
- OpenSCAD Basics: 3D Forms
- OpenSCAD Basics: Manipulating Forms
- OpenSCAD Intermediates: Combining Forms
- OpenSCAD Intermediates: Mashups
- OpenSCAD Intermediates: Modularity
- OpenSCAD Intermediates: Extruding 2D Objects
- OpenSCAD Intermediates: Fixing Design Problems
- OpenSCAD Intermediates: How to Make Organic Shapes
- OpenSCAD Design Tips
- OpenSCAD Design Tips: How to Make a Customizable Thing
Bonus Section 2: Other sources
If you like reading ahead or want more information about OpenSCAD, I’ve found these three websites to be very helpful. A word of warning, as much useful information is on these sites, I found the presentation to be confusing.
- Official OpenSCAD website
- OpenSCAD User’s Manual
- OpenSCAD beginner’s tutorial
- OpenSCAD tutorial roundup on the Thingiverse blog
Bonus Section 3: What’s next???
The topic of the next tutorial is up to you. What would you like to learn next? Is there something you’d like to learn how to make? Is there something more you’d like to learn about some of the topics we’ve covered?
| Tagged with | openscad, openscad tutorials, tutorial, tutorials | 2 comments |







2 Comments so far
Syvwlch returns with a vengance! Experiments in printable clocks - MakerBot Industries
[...] back and posting parts for a printable clock. He’s making copious use of math, science, and OpenSCAD in the process. I’d also point out that in the span of about 24 hours he went from just one [...]
Syvwlch’s Printable Clock – ready for printing! - MakerBot Industries
[...] obstacles. Syvwlch explained some of the benefits to designing such a complex mechanism in OpenSCAD: There are none of the usual frustrations. If you made a mistake a few steps back, it’s not [...]