Sweeping mathematical curves in OpenSCAD

From Wikistix

Searching around, I didn't see a whole heap of guidance here, but after some thought and experimentation, I found a perfectly workable solution.

OpenSCAD doesn't have a sweep function or operator; however, the hull transformation can be used for this purpose.

Trefoil Knot

My first attempt was the "trefoil knot", a relatively simple trigonometric curve represented by the parametric equations:

[math]\displaystyle{ \begin{align} x & = \cos t + 2\cos 2t\\ y & = \sin t - 2\sin 2t\\ z & = -\sin 3t\\ \end{align} }[/math]

The OpenSCAD solution was to break the curve into short steps, and "sweep" a hull between spheres translated to successive points on the curve. The rendering is very clean, and generates a clean stl file read for slicing and 3D-printing.

Trefoil knot rendered in OpenSCAD
// Trefoil knot
// stix@stix.id.au 2022-08-30

// 10 for testing, 50 for final - renders in ~8m
$fn=50;
stepsize = 180 / $fn;

function trefoil(t) = [
	cos(t) + 2 * cos(2 * t),
	sin(t) - 2 * sin(2 * t),
	-sin(3 * t)
];

union() {
	for(a = [0 : stepsize : 360]) {
		hull() {
			translate(trefoil(a)) sphere(1);
			translate(trefoil(a + stepsize)) sphere(1);
		}
	}
}

Möbius Strip

Another famous object, the Möbius strip, can be rendered similarly, starting with a simple slice, some clever rotations, and the hull transformation.

Möbius strip rendered in OpenSCAD
// Möbius strip
// stix@stix.id.au 2026-05-09

// 10 for testing, 100 for final - renders in ~8m
$fn=100;
stepsize = 180 / $fn;

module mobius_slice(a) {
		rotate(a, [0, 0, 1])
		translate([20, 0, 0])
		rotate([0, a / 2, 0])
		cube([10, 0.1, 1], center = true);
}

union() {
	for(a = [0 : stepsize : 360])
	hull() {
		mobius_slice(a);
		mobius_slice(a + stepsize);
	}
}