Contents
- Learning Objectives
- Understanding Modules in OpenSCAD
- Defining Your First Module
- Calling a Module
- Note on Module Arguments
- Applying Transformations to Modules
- Practice Problems
- Review
Learning Objectives
At the end of this lesson, you will be able to:
- Understand the concept of modules in OpenSCAD.
- Define and call your own modules.
- Pass arguments to modules.
- Apply transformations to modules.
Understanding Modules in OpenSCAD
In OpenSCAD, a module is a portion of code that you can define so you don’t have to type it out every time. It can be seen as a custom primitive shape. OpenSCAD’s default primitive shapes, like cube, sphere, and cylinder, are all modules that have been pre-defined for you.
Defining Your First Module
You can define a module in OpenSCAD using the module
keyword, followed by a name and optional arguments. For example:
module my_first_module_name(an_argument, like_size, etc){
cube([an_argument, like_size, etc], center=true);
}
After defining a module, you won’t see any immediate change in your preview because you need to call the module to actually create the shape.
Calling a Module
You can call a module just like you would call any other OpenSCAD shape. For example, to call the module you defined earlier, you would use the following line of code:
my_first_module_name(1, 1, 1);
Note that the numbers in the parentheses are arguments that match the ones you defined in the module. The argument order matters!
Note on Module Arguments
If you do not include the correct arguments when you call your module, you will receive a warning. Always ensure that the arguments in your module definition match with the ones that you use to call the module. You can check the error log (press alt > w > l and tab around) to hear the warnings and find out what is wrong.
Applying Transformations to Modules
You can treat modules just like shapes, meaning that transformation commands such as translate
will work with them. For example:
translate([1, 1, 1])
my_first_module_name(1, 1, 1);
Practice Problems
- Create and call a module that makes a cylinder with arguments
h
for height andw
for width. Translate your shape in any direction(s) after calling it. - Create and call a module that makes a cone with arguments for the cone’s width and height. Translate your shape in any direction(s) after calling it.
- Create and call a bead module with arguments for the hole width and the bead size.
- Create and call a soda can module.
Review
In this lesson you learned:
- What modules are in OpenSCAD and why they are useful.
- How to define and call your own modules.
- The importance of correctly using arguments in your modules.
- That you can apply transformations to your modules