Files
plants_scad/flower_pot.py
T

80 lines
2.2 KiB
Python

#!/bin/python3
from __future__ import annotations
import subprocess
import sys
from pathlib import Path
BASE_DIR = Path(__file__).resolve().parent
SCAD_DIR = BASE_DIR / "flower_pot"
OUT_DIR = BASE_DIR / "stl"
def ask_float(prompt: str) -> float:
while True:
try:
value = float(input(prompt).strip())
if value <= 0:
print("Please enter a positive number.")
continue
return value
except ValueError:
print("Please enter a valid number.")
def render_scad(scad_file: Path, out_file: Path, base_radius: float, wall_height: float, wall_height_tray: float) -> None:
cmd = (
f'openscad '
f'-D base_radius={base_radius} '
f'-D wall_height={wall_height} '
f'-D wall_height_tray={wall_height_tray} '
f'-o "{out_file}" '
f'"{scad_file}"'
)
result = subprocess.run(
["bash", "-lc", 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 not SCAD_DIR.exists():
raise SystemExit(f"Missing SCAD directory: {SCAD_DIR}")
base_radius = ask_float("Flower pot radius (base_radius) in mm: ")
wall_height = ask_float("Flower pot height (wall_height) in mm: ")
wall_height_tray = ask_float("Tray height (wall_height_tray) in mm: ")
OUT_DIR.mkdir(parents=True, exist_ok=True)
pot_scad = SCAD_DIR / "pot.scad"
tray_scad = SCAD_DIR / "tray.scad"
if not pot_scad.exists():
raise SystemExit(f"Missing file: {pot_scad}")
if not tray_scad.exists():
raise SystemExit(f"Missing file: {tray_scad}")
render_scad(pot_scad, OUT_DIR / "pot.stl", base_radius, wall_height, wall_height_tray)
render_scad(tray_scad, OUT_DIR / "tray.stl", base_radius, wall_height, wall_height_tray)
print("\nDone.")
if __name__ == "__main__":
main()