3D Printing

Laurent software Tutorials and 3D models

Modelize your first 3D shape with openSCAD #4

4. Good practice

Like in all programing languages there is good practice you must do to ensure that everyone can understand your code and also to be easier to reread. In openSCAD it’s not so essential to do some of this good practice because the code is quite easy to understand but I definitely recommend you to do it.

a. Commentary

To do a commentary in openSCAD you can use // to comment 1 line or /* at the start and */ at end to comment an entire block of code.

//this is a commentary

Cube(10) //this is a cube

hull() {


   translate([0,0,20]) cylinder(1,8,8);


Commentary can be used to document your code or to comment code you don’t need for the moment.

b. Indentation

Indentation is when you do space in your code. Indentation is essential to have a clear code:

hull() {


……………….translate([0,0,20]) cylinder(1,8,8);




      difference() {

            difference() {


                   union() {

                          translate([0,2,0]) circle(r=14);

                          translate([-60,0,0]) square([60,60]);

                          translate([-40,-40,0]) rotate([0,0,55]) square([60,60]);

                          translate([-9,-14.2,0]) rotate([0,0,-20]) square([7,3]);




c. Global variables

Global variables are in programming when you give at the beginning of the program a value that can be reused everywhere in your code. To do this you need to use the “=” operator with the syntax: name = value; .


my_height = 20;

my_radius = 5;



Global variables are very useful when you want for example the same height for your cylinder in your code and if one day you want to change it because your cylinders are to small, you just need to change the value of the variable and not of each cylinder.

d. Colors

One last good practice I like to do is to put colors on each of my objects to see them clearly in my view. The syntax for color is color(“your color”). Basic color like black, green, red… exist yet in the openSCAD kit but if you need more colors you can use RGB with this syntax : color([r,g,b,a]).

Leave a Reply

Your email address will not be published. Required fields are marked *