TOP SEARCH TERMS
CANCEL
TOP SEARCH TERMS
At this time users are unable to login to MakerBot properties. To complete your purchase, please call us at 347-334-6800.

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:
        1. “module box()
        2. {
        3. cube([5,10,15]);
        4. }”
    • 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:
        1. “module box()
        2. {
        3. cube([5,10,15]);
        4. }
        5. box();”
      • This will create a box with the dimensions of 5x10x15.  Here’s how you make two such cubes:
        1. “module box()
        2. {
        3. cube([5,10,15]);
        4. }
        5. box();
        6. box();”
      • Of course, doing this will make both boxes occupy the same space.  So, let’s just move one of them over, like so:
        1. “module box()
        2. {
        3. cube([5,10,15]);
        4. }
        5. translate([20,0,0]) box();
        6. 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:
        1. “module box()
        2. {
        3. cube([5,10,15]);
        4. }
        5. scale([2,2,2]) translate([20,0,0]) rotate([0,45,45]) box();
        6. box();”
      • But, why stop there?  You can combine modules together into other modules.  Let’s try this:
        1. “module leaves()
        2. {
        3. cylinder(20,5,0);
        4. }
        5. module box()
        6. {
        7. cube([5,10,15]);
        8. }
        9. module tree()
        10. {
        11. leaves();
        12. scale([0.5,0.5,0.5]) translate([-2.5,-5,-15]) box();
        13. }
        14. tree();”
      • And, here’s how you make a forest:
        1. “module leaves()
        2. {
        3. cylinder(20,5,0);
        4. }
        5. module box()
        6. {
        7. cube([5,10,15]);
        8. }
        9. module tree()
        10. {
        11. leaves();
        12. scale([0.5,0.5,0.5]) translate([-2.5,-5,-15]) box();
        13. }
        14. module treeline()
        15. {
        16. translate([20,0,0]) tree();
        17. translate([10,0,0]) tree();
        18. tree();
        19. translate([-10,0,0]) tree();
        20. translate([-20,0,0]) tree();
        21. }
        22. module forest()
        23. {
        24. translate([0,20,0]) treeline();
        25. translate([0,10,0]) treeline();
        26. treeline();
        27. translate([0,-10,0]) treeline();
        28. translate([0,-20,0]) treeline();
        29. };
        30. forest();”
    • Variables in Modules
      • 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:
        1. module box(width,length,height)
        2. {
        3. cube([width,length,height]);
        4. }
        5. box(5,10,15);
        6. 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:
        1. module wackybox(width)
        2. {
        3. cube([width, width + 2, width + 4]);
        4. }
        5. wackybox(3);
        6. 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.
    • 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.
        1. module reallywackybox(width = 3)
        2. {
        3. cube([width, width + 2, width + 4]);
        4. }
        5. reallywackybox();
        6. 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:
        1. module superwacky(funky = 10)
        2. {
        3. translate([1,2,funky]) scale([1,funky,1]) rotate([funky, funky,funky]) cylinder(funky,funky + 2, funky +4);
        4. scale([funky,2,1]) translate([1,funky,3]) rotate([-funky, funky + funky,-funky]) sphere(funky);
        5. }
        6. superwacky(4);
  • 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

 

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.

  1. Official OpenSCAD website
  2. OpenSCAD User’s Manual
  3. OpenSCAD beginner’s tutorial
  4. 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?

  1. Wait, yours didn’t? []
  2. For height []
  3. For radius 1 []
  4. For radius 2 []
  5. Hey, look, I’m not judging you.  But, they had to call these commands something. []
  6. It really beats the heck out of retyping code too! []
Tagged with , , , 2 comments
 

2 Comments so far

 

Leave your comment

 
 
 

xhtml: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 

 
 
Chat
What can we help you with today?
I want to chat with Sales.
I have a question about an existing order.
I have a technical question about my device.
Open Support Case
Existing Orders
For faster service, enter your order number
(found in your confirmation e-mail)
Skip
Submit