Files
scripts_scad/weave_planter.py
T
2026-10-07 16:33:26 +02:00

97 lines
2.1 KiB
Python
Executable File

#!/bin/python3
from __future__ import annotations
import shutil
import subprocess
import sys
from pathlib import Path
from utils import ask_float
BASE_DIR = Path(__file__).resolve().parent
SCAD_DIR = BASE_DIR / "flower_pot"
OUT_DIR = BASE_DIR / "stl"
def render_scad(
scad_file: Path,
out_file: Path,
outer_d: float,
planter_h: float,
) -> None:
cmd = [
"openscad",
"-D",
f"outer_d={outer_d}",
"-D",
f"planter_h={planter_h}",
"-D",
'part="both"',
"-o",
str(out_file),
str(scad_file),
]
print("\nRendering:")
print(" ".join(f'"{arg}"' if " " in arg else arg for arg in cmd))
result = subprocess.run(
cmd,
cwd=BASE_DIR,
text=True,
capture_output=True,
)
if result.returncode != 0:
print(f"\nFailed to render {scad_file.name}")
if result.stdout:
print(result.stdout)
if result.stderr:
print(result.stderr, file=sys.stderr)
raise SystemExit(result.returncode)
print(f"Generated {out_file}")
def main() -> None:
if shutil.which("openscad") is None:
raise SystemExit(
"OpenSCAD was not found in PATH. "
"Install OpenSCAD or make sure the 'openscad' command is available."
)
scad_file = SCAD_DIR / "weave_planter.scad"
if not scad_file.exists():
raise SystemExit(f"Missing SCAD file: {scad_file}")
print("Weave planter generator")
print("The SCAD model has additional geometric constraints, so very small dimensions may fail OpenSCAD's assertions.\n")
outer_d = ask_float("Outer diameter (mm): ")
planter_h = ask_float("Planter height (mm): ")
OUT_DIR.mkdir(parents=True, exist_ok=True)
diameter_str = f"{outer_d:g}"
height_str = f"{planter_h:g}"
out_file = OUT_DIR / f"weave_planter_{diameter_str}x{height_str}mm.stl"
render_scad(
scad_file,
out_file,
outer_d,
planter_h,
)
print("\nDone.")
if __name__ == "__main__":
main()