Files
POPS/app/stl_analysis.py
Martin Hohenberg ab52721c7f Fix 3MF analysis: concatenate scene geometries instead of force="mesh"
trimesh loads 3MF files as a Scene; force="mesh" fails on those.
Detect Scene and concatenate all geometries before computing extents/volume.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-19 10:56:21 +02:00

52 lines
1.7 KiB
Python

import logging
from pathlib import Path
from sqlalchemy.orm import Session
from app.models.stl_cache import StlCache
logger = logging.getLogger("pops")
_ANALYSABLE = {".stl", ".3mf"}
def analyse(filename: str, db: Session) -> StlCache | None:
"""Return cached analysis for filename, computing it if missing."""
existing = db.query(StlCache).filter(StlCache.filename == filename).first()
if existing:
return existing
from app.stl import get_stl_dir
filepath = get_stl_dir() / filename
if Path(filename).suffix.lower() not in _ANALYSABLE or not filepath.exists():
return None
try:
import trimesh
loaded = trimesh.load(str(filepath))
if isinstance(loaded, trimesh.Scene):
mesh = trimesh.util.concatenate(list(loaded.geometry.values()))
else:
mesh = loaded
extents = mesh.extents # [size_x, size_y, size_z] in mm
volume_mm3 = abs(mesh.volume) # mm³ (negative for inverted normals)
entry = StlCache(
filename=filename,
cube_x=round(float(extents[0]), 2),
cube_y=round(float(extents[1]), 2),
cube_z=round(float(extents[2]), 2),
volume_ccm=round(volume_mm3 / 1000, 3),
)
db.add(entry)
db.commit()
return entry
except Exception:
logger.exception("STL analysis failed for %s", filename)
return None
def get_cache_map(filenames: list[str], db: Session) -> dict[str, StlCache]:
"""Bulk-fetch cached entries for a list of filenames."""
rows = db.query(StlCache).filter(StlCache.filename.in_(filenames)).all()
return {r.filename: r for r in rows}