Skip to content

Instantly share code, notes, and snippets.

@ali1234
Last active April 12, 2026 02:19
Show Gist options
  • Select an option

  • Save ali1234/5d8f3a9f1954c1dc37cc82bced0afdff to your computer and use it in GitHub Desktop.

Select an option

Save ali1234/5d8f3a9f1954c1dc37cc82bced0afdff to your computer and use it in GitHub Desktop.
OpenSCAD parts system...
import subprocess
import sys
f = sys.argv[1]
openscad = 'openscad-nightly'
result = subprocess.check_output([openscad, '--export-format', 'echo', '-o', '-', f])
exported = set()
for line in result.decode().split('\n'):
if line.startswith("ECHO: ") and "PART:" in line:
part = line[13:-1]
if part not in exported:
subprocess.check_call([openscad, '-D', f'$part="{part}"', '-o', f'{part}.stl', f])
exported.add(part)
use <part.scad>
# Wrap each separate part in the "part" module like this:
part("a") cube(10);
part("b") sphere(5);
# Running the export.py on this file will export a.stl and b.stl
# If you define $part at the top of this file, only that part will be previewed.
# If $part is undefined or doesn't match anything, everything will be previewed.
# The part module eachoes the name of the part so that the exporter
# script can parse a list of all parts. Then it applies ! to children
# iff $part = this part.
module part(name) {
echo(str("PART: ", name));
if($part == name) {
!children();
} else {
children();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment