OpenSCAD examples

Runnable OpenSCAD examples covering primitives, boolean operations, loops, reusable modules, parametric shapes, and practical 3D models. Edit and compile every example in the browser.

// The three core 3D primitives
$fn = 64;

cube([20, 20, 20], center = true);

translate([35, 0, 0])
  sphere(r = 12);

translate([70, 0, -15])
  cylinder(h = 30, r = 10);

Build a scene from OpenSCAD's core cube, sphere, and cylinder primitives.

Open in OpenSCAD playground
// union, difference and intersection
$fn = 48;

module blocks() {
  cube(20, center = true);
  sphere(13);
}

// union: the two shapes fused together
translate([-30, 0, 0]) union() blocks();

// difference: sphere carved out of the cube
difference() blocks();

// intersection: only where they overlap
translate([30, 0, 0]) intersection() blocks();

Compare OpenSCAD union, difference, and intersection operations in one model.

Open in OpenSCAD playground
// Nested loops build a grid of pillars
$fn = 32;

for (x = [0:4])
  for (y = [0:4]) {
    h = 6 + 4 * (x + y);
    translate([x * 12, y * 12, 0])
      cylinder(h = h, r = 4);
  }

Generate a parametric grid of pillars with nested OpenSCAD for loops.

Open in OpenSCAD playground
// Hex head bolt with a round shaft
$fn = 64;

module hex_head(across_flats, thickness) {
  // a cylinder with 6 facets is a perfect hexagon
  cylinder(h = thickness, r = across_flats / sqrt(3), $fn = 6);
}

union() {
  hex_head(16, 7);
  translate([0, 0, 7])
    cylinder(h = 30, r = 5);
}

Model a reusable hex-head bolt from an OpenSCAD module and cylinders.

Open in OpenSCAD playground
// Hollowed cylinder body with a hull() handle
$fn = 96;

module body() {
  difference() {
    cylinder(h = 60, r = 30);
    translate([0, 0, 4])
      cylinder(h = 60, r = 26);
  }
}

module handle() {
  difference() {
    hull() {
      translate([0, 0, 15]) rotate([90, 0, 0]) cylinder(h = 8, r = 10, center = true);
      translate([0, 0, 45]) rotate([90, 0, 0]) cylinder(h = 8, r = 10, center = true);
    }
    hull() {
      translate([0, 0, 15]) rotate([90, 0, 0]) cylinder(h = 10, r = 5, center = true);
      translate([0, 0, 45]) rotate([90, 0, 0]) cylinder(h = 10, r = 5, center = true);
    }
  }
}

union() {
  body();
  translate([30, 0, 0]) rotate([0, 0, -90]) handle();
}

Create a hollow OpenSCAD coffee mug with difference, hull, and translated cylinders.

Open in OpenSCAD playground
// Spin a 2D profile around the Z axis
$fn = 120;

rotate_extrude()
  polygon(points = [
    [0, 0],
    [30, 0],
    [30, 4],
    [16, 4],
    [22, 40],
    [30, 70],
    [26, 74],
    [16, 44],
    [12, 8],
    [0, 8],
  ]);

Turn a 2D profile into a parametric OpenSCAD vase with rotate_extrude.

Open in OpenSCAD playground
// minkowski() rounds every edge with a sphere
$fn = 32;

difference() {
  minkowski() {
    cube([40, 60, 15], center = true);
    sphere(r = 4);
  }
  // hollow out the inside
  translate([0, 0, 6])
    cube([40, 60, 15], center = true);
}

Make a rounded OpenSCAD enclosure by applying minkowski to a box and sphere.

Open in OpenSCAD playground
// A 2D star swept into 3D with linear_extrude
points = 6;
outer = 30;
inner = 13;

module star(points, outer, inner) {
  poly = [
    for (i = [0 : 2 * points - 1])
      let (r = (i % 2 == 0) ? outer : inner, a = i * 180 / points)
        [r * cos(a), r * sin(a)]
  ];
  polygon(poly);
}

linear_extrude(height = 10, twist = 40, slices = 40)
  star(points, outer, inner);

Generate an adjustable star polygon and extrude it into a 3D OpenSCAD part.

Open in OpenSCAD playground
// Angled desk stand for a phone
$fn = 48;

module base() {
  hull() {
    translate([-35, 25, 0]) cylinder(h = 6, r = 6);
    translate([35, 25, 0]) cylinder(h = 6, r = 6);
    translate([-35, -25, 0]) cylinder(h = 6, r = 6);
    translate([35, -25, 0]) cylinder(h = 6, r = 6);
  }
}

module back() {
  translate([0, -12, 3])
    rotate([65, 0, 0])
      cube([70, 70, 6], center = true);
}

module lip() {
  translate([0, 22, 8])
    cube([70, 8, 16], center = true);
}

difference() {
  union() {
    base();
    back();
    lip();
  }
  // trim everything below the base
  translate([0, 0, -50]) cube([200, 200, 100], center = true);
}

Build a practical parametric phone stand with angled supports and a retaining lip.

Open in OpenSCAD playground