Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion api/routers/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,10 @@ def registry_tool_lines(registry: MeshOpsRegistry) -> str:
"unit": {
"type": "string",
"enum": ["m", "cm", "mm"],
"description": "normalize_scale only: unit of `size`. Default m.",
"description": (
"normalize_scale only: unit of `size`, when the user gave a real size. "
"Omit it for a size in model units: the mesh is then not marked as real-sized."
),
},
"dimension": {
"type": "string",
Expand Down
58 changes: 41 additions & 17 deletions api/routers/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from fastapi.responses import Response, FileResponse

from services import imported_sources
from services.mesh_ops.geometry import single_mesh
from services.mesh_ops.real_scale import MM_PER_M, export_scale, is_calibrated
# Import the module (not the name) so WORKSPACE_DIR is read at call time: the
# settings endpoint rebinds it when the user moves the workspace.
import services.generator_registry as registry
Expand Down Expand Up @@ -77,18 +79,24 @@ def _scale_to_print_size(mesh: "trimesh.Trimesh", longest_mm: float = DEFAULT_PR
mesh.apply_scale(longest_mm / longest)


def _normalize_print_scale(mesh: "trimesh.Trimesh") -> bool:
"""Rescale ``mesh`` only if it looks unit-sized; return whether it was rescaled.
def print_mm_per_unit(mesh: "trimesh.Trimesh | None", calibrated: bool) -> float:
"""Millimetres per mesh unit when the mesh is printed.

A mesh whose longest edge already exceeds ``UNIT_SCALE_MAX`` is assumed to
carry a real-world size the user chose, and is left untouched.
The single rule behind the slicer hand-off and the Print page, so the size
on the plate is the size the slicer gets:

* a calibrated asset is in metres: 1000 mm per unit;
* an unit-sized one (longest edge up to ``UNIT_SCALE_MAX``) is AI output
with no real size: its longest edge becomes ``DEFAULT_PRINT_LONGEST_MM``;
* anything larger is assumed to be in millimetres already.
"""
extents = mesh.extents
if calibrated:
return MM_PER_M
extents = mesh.extents if mesh is not None else None
longest = float(max(extents)) if extents is not None and len(extents) else 0.0
if not math.isfinite(longest) or not (1e-9 < longest <= UNIT_SCALE_MAX):
return False
_scale_to_print_size(mesh)
return True
if math.isfinite(longest) and 1e-9 < longest <= UNIT_SCALE_MAX:
return DEFAULT_PRINT_LONGEST_MM / longest
return 1.0


def _resolve_slicer_source(token: str) -> tuple[Path, str]:
Expand Down Expand Up @@ -133,6 +141,18 @@ def _resolve_slicer_source(token: str) -> tuple[Path, str]:
return full_path, full_path.suffix.lower()


@router.get("/print-scale/{token}")
def print_scale(token: str):
"""How the slicer hand-off sizes this mesh: the Print page shows the same.

``token`` is the same source token as the slicer route's.
"""
full_path, _ = _resolve_slicer_source(token)
calibrated = is_calibrated(full_path)
mesh = None if calibrated else _to_single_mesh(trimesh.load(str(full_path)))
return {"calibrated": calibrated, "mm_per_unit": print_mm_per_unit(mesh, calibrated)}


@router.get("/slicer/{fmt}/{token}/{filename}")
def export_for_slicer(fmt: str, token: str, filename: str):
"""Serve a generated GLB converted to a slicer-importable mesh, at a URL
Expand Down Expand Up @@ -164,7 +184,10 @@ def export_for_slicer(fmt: str, token: str, filename: str):
# thing this corrects — lay an upright model on its side.
if source_suffix in GLTF_SUFFIXES:
mesh.apply_transform(trimesh.transformations.rotation_matrix(math.pi / 2, [1, 0, 0]))
_normalize_print_scale(mesh)
# Slicers read millimetres (see print_mm_per_unit).
scale = print_mm_per_unit(mesh, is_calibrated(full_path))
if scale != 1.0:
mesh.apply_scale(scale)

data = mesh.export(file_type=fmt)
if isinstance(data, str):
Expand Down Expand Up @@ -193,13 +216,14 @@ def export_mesh(fmt: str, path: str):
if fmt == "glb":
return FileResponse(str(full_path), media_type="model/gltf-binary")

# Load and flatten scene to a single mesh
loaded = trimesh.load(str(full_path))
if isinstance(loaded, trimesh.Scene):
geoms = list(loaded.geometry.values())
mesh = trimesh.util.concatenate(geoms) if len(geoms) > 1 else geoms[0]
else:
mesh = loaded
# Flatten to a single mesh in world space (node transforms applied).
try:
mesh = single_mesh(trimesh.load(str(full_path)), trimesh)
except ValueError as exc:
raise HTTPException(422, str(exc)) from exc
scale = export_scale(full_path, fmt)
if scale != 1.0:
mesh.apply_scale(scale)

buf = io.BytesIO()
if fmt == "stl":
Expand Down
49 changes: 35 additions & 14 deletions api/routers/optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
mesh_ops_registry,
)
from services.mesh_ops.glb_patch import GlbPatchError, VertexPatch, patch_glb_file
from services.mesh_ops.geometry import bake_transforms, single_mesh
from services.mesh_ops.real_scale import MM_PER_M, export_scale, write_real_scale
from services.mesh_ops.transforms import apply_transforms, transform_file
from schemas.runs import RunRecord
from services.run_history import run_history, workspace_relative
Expand Down Expand Up @@ -465,14 +467,31 @@ async def import_mesh_by_path(body: ImportByPathRequest):

# Mesh ply / obj / stl: convert to GLB in a temp directory (not the workspace)
tmp_dir = tempfile.mkdtemp(prefix="modly_import_")
output_path = os.path.join(tmp_dir, "mesh.glb")
loaded = trimesh.load(str(file_path))
loaded.export(output_path)
# Remember the ORIGINAL extension: this conversion changes the container but
# not the axes, so a .stl imported here still holds Z-up data despite the
# .glb suffix, and must not be rotated as if it were glTF.
imported_sources.register(output_path, file_path)
return {"url": f"/optimize/serve-file?path={quote(output_path)}"}
output_path = Path(tmp_dir) / "mesh.glb"
convert_print_mesh_to_gltf(file_path, output_path)
# The result is a real glTF now (Y-up, metres): the slicer route rotates it
# back to Z-up and sends it in millimetres, like any calibrated GLB.
imported_sources.register(output_path, output_path)
return {"url": f"/optimize/serve-file?path={quote(str(output_path))}"}


# STL, OBJ and PLY carry no unit and no up axis; by convention (slicers, CAD)
# they are millimetres and Z-up.
_Z_UP_TO_Y_UP = trimesh.transformations.rotation_matrix(-np.pi / 2, [1, 0, 0])
_MM_TO_M = trimesh.transformations.scale_matrix(1.0 / MM_PER_M)


def convert_print_mesh_to_gltf(source: Path, output_path: Path) -> Path:
"""Convert an STL/OBJ/PLY to a GLB in metres and Y-up, marked at real size.

Their size is known (millimetres), so the GLB is calibrated: the Print page
shows it at its real size and exports give it back in millimetres.
"""
loaded = trimesh.load(str(source))
loaded.apply_transform(_MM_TO_M @ _Z_UP_TO_Y_UP)
bake_transforms(loaded, trimesh).export(str(output_path))
write_real_scale(output_path)
return output_path


_SERVE_MEDIA_TYPES = {
Expand Down Expand Up @@ -531,12 +550,14 @@ def export_mesh(path: str, format: str):
if not input_path.exists():
raise HTTPException(404, f"File not found: {path}")

loaded = trimesh.load(str(input_path))
if isinstance(loaded, trimesh.Scene):
geoms = list(loaded.geometry.values())
mesh = trimesh.util.concatenate(geoms) if len(geoms) > 1 else geoms[0]
else:
mesh = loaded
# Flatten to a single mesh in world space (node transforms applied).
try:
mesh = single_mesh(trimesh.load(str(input_path)), trimesh)
except ValueError as exc:
raise HTTPException(422, str(exc)) from exc
scale = export_scale(input_path, format)
if scale != 1.0:
mesh.apply_scale(scale)

data = mesh.export(file_type=format)
stem = input_path.stem
Expand Down
8 changes: 4 additions & 4 deletions api/services/imported_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
this session. Membership is an equality test, never a prefix test, so it grants
no traversal.
It also remembers each file's ORIGINAL suffix. An imported `.stl` is converted
to GLB on import without any axis change, so the container extension alone would
mislead a consumer into applying the glTF Y-up -> Z-up rotation to data that is
already Z-up.
It also remembers each file's source suffix, which decides the up axis. An
imported `.stl`/`.obj`/`.ply` is converted on import to a real glTF (Y-up,
metres) and registered as `.glb`; the suffix still matters for any file
registered in its own format.
Session-scoped and in-memory: it is emptied when the backend restarts, so a
deeplink replayed after a restart is rejected rather than silently served. Links
Expand Down
70 changes: 67 additions & 3 deletions api/services/mesh_ops/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from .operations import decimate_mesh, repair_mesh, smooth_mesh
from .presets import prepare_for_game, prepare_for_printing, prepare_for_web
from .repair import auto_repair_mesh
from .transforms import auto_orient, drop_to_ground, normalize_scale, recenter
from .transforms import auto_orient, drop_to_ground, normalize_scale, recenter, set_real_scale
from .types import AgentTool, MeshOp


Expand Down Expand Up @@ -139,13 +139,17 @@
"id": "unit",
"label": "Unit",
"type": "select",
"default": "m",
"default": "units",
"options": [
{"value": "units", "label": "Model units (no real size)"},
{"value": "m", "label": "Metres"},
{"value": "cm", "label": "Centimetres"},
{"value": "mm", "label": "Millimetres"},
],
"tooltip": "glTF stores metres: 20 cm is written as 0.2 units.",
"tooltip": (
"glTF stores metres: 20 cm is written as 0.2 units. A real unit also "
"marks the mesh as real-sized, for the Print page and exports."
),
},
{
"id": "dimension",
Expand All @@ -163,6 +167,45 @@
)


SET_REAL_SCALE_PARAMS = (
{
"id": "size",
"label": "Real Size",
"type": "float",
"default": 10.0,
"min": 0.001,
"max": 100000.0,
"step": 0.1,
"tooltip": "How big the measured dimension really is, in the chosen unit.",
},
{
"id": "unit",
"label": "Unit",
"type": "select",
"default": "cm",
"options": [
{"value": "mm", "label": "Millimetres"},
{"value": "cm", "label": "Centimetres"},
{"value": "m", "label": "Metres"},
],
"tooltip": "Unit of the real size.",
},
{
"id": "dimension",
"label": "Dimension",
"type": "select",
"default": "longest",
"options": [
{"value": "longest", "label": "Longest side"},
{"value": "x", "label": "Width (X)"},
{"value": "y", "label": "Height (Y)"},
{"value": "z", "label": "Depth (Z)"},
],
"tooltip": "Which bounding-box dimension has the real size.",
},
)


REPAIR_MESH_PARAMS = (
{
"id": "fill_holes",
Expand Down Expand Up @@ -355,6 +398,27 @@
fn=normalize_scale,
category="transform",
),
MeshOp(
id="set_real_scale",
label="Set Real Size",
params_schema=SET_REAL_SCALE_PARAMS,
fn=set_real_scale,
category="transform",
agent=AgentTool(
description=(
"Declare how big the object really is (\"this is 30 cm tall\"): rescales the "
"mesh to that size and marks it calibrated, so the Print page and STL "
"exports use its real size."
),
params={
"size": "Real size of `dimension`, in `unit`.",
"unit": "mm, cm or m.",
"dimension": DIMENSION_HINT,
},
required=("size", "unit"),
format_result=format_preset,
),
),
MeshOp(
id="prepare_for_printing",
label="Prepare for 3D Printing",
Expand Down
47 changes: 47 additions & 0 deletions api/services/mesh_ops/geometry.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""World-space geometry helpers shared by the analysis and transform ops."""

from copy import deepcopy


def scene_meshes(loaded, trimesh_module) -> list[tuple]:
"""Return (geometry, world transform or None) for every non-empty triangle mesh."""
Expand All @@ -17,6 +19,51 @@ def scene_meshes(loaded, trimesh_module) -> list[tuple]:
]


def baked_parts(loaded, trimesh_module) -> list:
"""A copy of every triangle mesh with its world transform baked into the vertices."""
parts = []
for geometry, transform in scene_meshes(loaded, trimesh_module):
part = geometry.copy()
if transform is not None:
part.apply_transform(transform)
parts.append(part)
return parts


def single_mesh(loaded, trimesh_module):
"""One mesh in world space, keeping visuals: what the node transforms place.

Concatenating scene.geometry directly would drop the node transforms, and a
mesh scaled or moved at node level would come out at its stored size.
"""
parts = baked_parts(loaded, trimesh_module)
if not parts:
raise ValueError("The file contains no triangle mesh")
return trimesh_module.util.concatenate(parts) if len(parts) > 1 else parts[0]


def bake_transforms(loaded, trimesh_module):
"""The same scene with every node transform applied to its geometry.

trimesh's Scene.apply_transform only moves the base frame, so the vertices
keep their old coordinates. Baking makes the stored geometry what the scene
shows, for readers that ignore nodes (STL/OBJ writers, the mesh exporter).
"""
if not isinstance(loaded, trimesh_module.Scene):
return loaded
baked = trimesh_module.Scene()
baked.metadata.update(deepcopy(loaded.metadata))
for node in loaded.graph.nodes_geometry:
transform, geometry_name = loaded.graph[node]
geometry = loaded.geometry.get(geometry_name)
if geometry is None:
continue
part = geometry.copy()
part.apply_transform(transform)
baked.add_geometry(part, node_name=node, geom_name=node)
return baked


def merged_geometry(parts, trimesh_module, process: bool = True):
"""Concatenate the parts into one world-space mesh, without visuals.

Expand Down
12 changes: 3 additions & 9 deletions api/services/mesh_ops/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from pathlib import Path
from typing import Any, Mapping

from .geometry import single_mesh
from .types import (
MeshOpContext,
MeshOpExecutionError,
Expand All @@ -32,15 +33,8 @@ def _output_path(context: MeshOpContext, prefix: str) -> Path:


def _load_single_mesh(input_path: Path, trimesh_module):
loaded = trimesh_module.load(str(input_path))
if isinstance(loaded, trimesh_module.Scene):
geometries = list(loaded.geometry.values())
return (
trimesh_module.util.concatenate(geometries)
if len(geometries) > 1
else geometries[0]
)
return loaded
# In world space: a mesh scaled at node level must keep its size.
return single_mesh(trimesh_module.load(str(input_path)), trimesh_module)


def _raw_geometry(mesh, trimesh_module):
Expand Down
5 changes: 1 addition & 4 deletions api/services/mesh_ops/presets.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,10 @@
from .geometry import merged_geometry, scene_meshes
from .operations import _output_path, decimate_mesh
from .repair import auto_repair_mesh
from .transforms import UNIT_SCALE, _trimesh, apply_transforms
from .transforms import DIMENSION_NAMES, UNIT_SCALE, _trimesh, apply_transforms
from .types import MeshOpContext, MeshOpExecutionError, MeshOpResult


DIMENSION_NAMES = {"longest": "longest side", "x": "width", "y": "height", "z": "depth"}


def _stage(
context: MeshOpContext,
start: int,
Expand Down
Loading
Loading