diff --git a/api/routers/agent.py b/api/routers/agent.py index e8b1fd8b..711eeb5f 100644 --- a/api/routers/agent.py +++ b/api/routers/agent.py @@ -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", diff --git a/api/routers/export.py b/api/routers/export.py index 854d8bf2..def92cfb 100644 --- a/api/routers/export.py +++ b/api/routers/export.py @@ -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 @@ -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]: @@ -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 @@ -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): @@ -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": diff --git a/api/routers/optimize.py b/api/routers/optimize.py index c6d70a1c..573bc871 100644 --- a/api/routers/optimize.py +++ b/api/routers/optimize.py @@ -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 @@ -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 = { @@ -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 diff --git a/api/services/imported_sources.py b/api/services/imported_sources.py index a21dded5..3cddef29 100644 --- a/api/services/imported_sources.py +++ b/api/services/imported_sources.py @@ -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 diff --git a/api/services/mesh_ops/builtin.py b/api/services/mesh_ops/builtin.py index 68bc1530..32533934 100644 --- a/api/services/mesh_ops/builtin.py +++ b/api/services/mesh_ops/builtin.py @@ -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 @@ -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", @@ -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", @@ -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", diff --git a/api/services/mesh_ops/geometry.py b/api/services/mesh_ops/geometry.py index 3fa3c60f..95a53b00 100644 --- a/api/services/mesh_ops/geometry.py +++ b/api/services/mesh_ops/geometry.py @@ -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.""" @@ -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. diff --git a/api/services/mesh_ops/operations.py b/api/services/mesh_ops/operations.py index 7fae3845..96651c58 100644 --- a/api/services/mesh_ops/operations.py +++ b/api/services/mesh_ops/operations.py @@ -10,6 +10,7 @@ from pathlib import Path from typing import Any, Mapping +from .geometry import single_mesh from .types import ( MeshOpContext, MeshOpExecutionError, @@ -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): diff --git a/api/services/mesh_ops/presets.py b/api/services/mesh_ops/presets.py index 13d3cfbf..f08c2881 100644 --- a/api/services/mesh_ops/presets.py +++ b/api/services/mesh_ops/presets.py @@ -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, diff --git a/api/services/mesh_ops/real_scale.py b/api/services/mesh_ops/real_scale.py new file mode 100644 index 00000000..c0989f29 --- /dev/null +++ b/api/services/mesh_ops/real_scale.py @@ -0,0 +1,129 @@ +"""Real-world scale: whether a GLB's numbers really are metres. + +glTF's unit is the metre, but generators emit meshes at an arbitrary size, so +a GLB's numbers only mean metres once someone has said how big the object is +(set_real_scale, normalize_scale, the prepare_for_* presets). That declaration +is stored in the file itself, in the default scene's extras: + + scenes[scene].extras.modly.real_scale = {"unit": "m"} + +so it travels with the asset (library, export, re-import) and the viewer reads +it from the loaded scene's userData. Ops that rebuild the mesh drop the extras; +the registry carries the marker over to their output (see carry_real_scale). +""" + +import json +import struct +from pathlib import Path +from typing import Any, Mapping, Optional + +from .glb_patch import GlbPatchError, _build_glb, _parse_glb + + +EXTRAS_KEY = "modly" +REAL_SCALE_KEY = "real_scale" +CALIBRATED = {"unit": "m"} +# STL carries no unit and every slicer reads it as millimetres. +MM_PER_M = 1000.0 + +_GLB_MAGIC = 0x46546C67 # "glTF" +_CHUNK_JSON = 0x4E4F534A # "JSON" + + +def _default_scene(gltf: dict) -> Optional[dict]: + scenes = gltf.get("scenes") + if not isinstance(scenes, list) or not scenes: + return None + index = gltf.get("scene", 0) + if not isinstance(index, int) or not 0 <= index < len(scenes): + return None + scene = scenes[index] + return scene if isinstance(scene, dict) else None + + +def _read_gltf_json(path: Path) -> Optional[dict]: + """The JSON chunk of a GLB, reading only the header and that chunk.""" + with open(path, "rb") as handle: + header = handle.read(20) + if len(header) < 20: + return None + magic, version, _length, json_length, chunk_type = struct.unpack(" Optional[dict[str, Any]]: + """The asset's real-scale declaration, or None when it is not calibrated.""" + path = Path(path) + if path.suffix.lower() != ".glb" or not path.is_file(): + return None + try: + gltf = _read_gltf_json(path) + except (OSError, UnicodeDecodeError, ValueError): + return None + scene = _default_scene(gltf) if gltf else None + extras = scene.get("extras") if scene else None + modly = extras.get(EXTRAS_KEY) if isinstance(extras, dict) else None + declaration = modly.get(REAL_SCALE_KEY) if isinstance(modly, dict) else None + if isinstance(declaration, dict) and declaration.get("unit") == "m": + return dict(declaration) + return None + + +def is_calibrated(path: Path) -> bool: + return read_real_scale(path) is not None + + +def write_real_scale(path: Path, declaration: Mapping[str, Any] = CALIBRATED) -> None: + """Stamp the declaration into a GLB in place, leaving everything else intact.""" + path = Path(path) + gltf, binary = _parse_glb(path.read_bytes()) + scene = _default_scene(gltf) + if scene is None: + raise GlbPatchError("GLB has no default scene to hold the real-scale declaration") + extras = scene.get("extras") + if not isinstance(extras, dict): + extras = scene["extras"] = {} + modly = extras.get(EXTRAS_KEY) + if not isinstance(modly, dict): + modly = extras[EXTRAS_KEY] = {} + modly[REAL_SCALE_KEY] = dict(declaration) + path.write_bytes(_build_glb(gltf, binary or b"")) + + +def carry_real_scale(input_path: Path, output_path: Optional[Path]) -> None: + """Keep a calibrated input's declaration on an op's output GLB. + + Every op keeps the coordinates in metres, so a calibrated input yields a + calibrated output, even when the op rebuilt the file and lost its extras. + """ + if output_path is None or Path(output_path).suffix.lower() != ".glb": + return + # The input first: most meshes are not calibrated, and then the output + # is never read. + declaration = read_real_scale(input_path) + if declaration is None or is_calibrated(output_path): + return + try: + write_real_scale(output_path, declaration) + except (GlbPatchError, OSError): + pass + + +# Formats with no unit of their own: by convention they hold millimetres +# (slicers, CAD), the same convention imports read them with. +MM_FORMATS = {"stl", "obj", "ply"} + + +def export_scale(path: Path, fmt: str) -> float: + """Factor from the GLB's units to an export format's units. + + A calibrated asset is written in millimetres to STL, OBJ and PLY, like the + slicer hand-off; GLB keeps metres, and an uncalibrated asset its own units. + """ + return MM_PER_M if fmt.lower() in MM_FORMATS and is_calibrated(path) else 1.0 diff --git a/api/services/mesh_ops/registry.py b/api/services/mesh_ops/registry.py index 614b54bf..be29ed6e 100644 --- a/api/services/mesh_ops/registry.py +++ b/api/services/mesh_ops/registry.py @@ -6,6 +6,7 @@ from pathlib import Path from typing import Any, Iterable, Iterator, Mapping, Optional +from .real_scale import carry_real_scale from .types import MeshOp, MeshOpContext, MeshOpNotFoundError, MeshOpResult @@ -98,7 +99,9 @@ def run( if not path.is_file(): raise FileNotFoundError(f"Input mesh not found: {path}") - return operation.fn(path, self.resolve_params(operation_id, params), context) + result = operation.fn(path, self.resolve_params(operation_id, params), context) + carry_real_scale(path, result.file_path) + return result def resolve_params( self, diff --git a/api/services/mesh_ops/transforms.py b/api/services/mesh_ops/transforms.py index 12266f11..a138344d 100644 --- a/api/services/mesh_ops/transforms.py +++ b/api/services/mesh_ops/transforms.py @@ -3,8 +3,9 @@ from pathlib import Path from typing import Any, Mapping, Sequence -from .geometry import merged_geometry, scene_meshes +from .geometry import bake_transforms, merged_geometry, scene_meshes from .operations import _output_path +from .real_scale import CALIBRATED, MM_PER_M, write_real_scale from .types import ( MeshOpContext, MeshOpExecutionError, @@ -14,8 +15,11 @@ # glTF's unit is the metre. -UNIT_SCALE = {"m": 1.0, "cm": 0.01, "mm": 0.001} +UNIT_SCALE = {"m": 1.0, "cm": 0.01, "mm": 0.001, "units": 1.0} +# "units" sizes the mesh in its own units: numerically metres, but no real size. +REAL_UNITS = ("m", "cm", "mm") DIMENSION_AXES = {"x": 0, "y": 1, "z": 2} +DIMENSION_NAMES = {"longest": "longest side", "x": "width", "y": "height", "z": "depth"} # Steps always run in this order, whatever order they were requested in: # orienting or scaling after grounding would lift the mesh off the ground again. CANONICAL_ORDER = ("auto_orient", "normalize_scale", "recenter", "drop_to_ground") @@ -116,7 +120,7 @@ def transform_file(input_path: Path, matrix, output_path: Path) -> Path: loaded = trimesh.load(str(input_path)) loaded.apply_transform(matrix) output_path.parent.mkdir(parents=True, exist_ok=True) - loaded.export(str(output_path)) + bake_transforms(loaded, trimesh).export(str(output_path)) return output_path @@ -154,7 +158,14 @@ def apply_transforms( context.progress(85, "Exporting…") output_path.parent.mkdir(parents=True, exist_ok=True) + # Scene.apply_transform only moves the base frame: bake it into the + # vertices so every reader sees the transformed geometry. + loaded = bake_transforms(loaded, trimesh) loaded.export(str(output_path)) + # Scaling to a size in a real unit is a statement of the real size. + scale = requested.get("normalize_scale") + if scale and scale.get("unit") in REAL_UNITS and output_path.suffix.lower() == ".glb": + write_real_scale(output_path) result = _world_mesh(loaded, trimesh) context.log(f"Applied {', '.join(ordered)} → {output_path}") @@ -193,3 +204,31 @@ def run( drop_to_ground = _single_step_op("drop_to_ground") auto_orient = _single_step_op("auto_orient") normalize_scale = _single_step_op("normalize_scale") + + +def set_real_scale( + input_path: Path, + params: Mapping[str, Any], + context: MeshOpContext, +) -> MeshOpResult: + """"This object is 30 cm": rescale the mesh to that size and mark it calibrated.""" + # normalize_scale turns "size in a unit along a dimension" into metres and + # writes the real-scale declaration. + result = apply_transforms( + input_path, + [("normalize_scale", params)], + _output_path(context, "set-real-scale"), + context, + ) + dimensions = [float(value) for value in result.details["dimensions"]] + side = DIMENSION_NAMES[params["dimension"]] + summary = f"Declared the {side} as {params['size']:g} {params['unit']}: the mesh is now at real size." + return MeshOpResult( + file_path=result.file_path, + details={ + "steps": [{"op": "set_real_scale", **params, "summary": summary}], + "real_scale": dict(CALIBRATED), + "dimensions": dimensions, + "dimensions_mm": [value * MM_PER_M for value in dimensions], + }, + ) diff --git a/api/tests/test_mesh_ops_real_scale.py b/api/tests/test_mesh_ops_real_scale.py new file mode 100644 index 00000000..a3a1b92b --- /dev/null +++ b/api/tests/test_mesh_ops_real_scale.py @@ -0,0 +1,380 @@ +import asyncio +import base64 +import io +import tempfile +import unittest +from pathlib import Path +from unittest import mock +from urllib.parse import quote, unquote + +import numpy as np +import trimesh + +import routers.export as export_router +import routers.optimize as optimize_router +from services.mesh_ops import MeshOp, MeshOpContext, MeshOpResult, MeshOpsRegistry, mesh_ops_registry +from services import imported_sources +from services.mesh_ops import real_scale +from services.mesh_ops.real_scale import ( + is_calibrated, + read_real_scale, + write_real_scale, +) + + +def _extents(path: Path) -> np.ndarray: + return trimesh.load(str(path), force="mesh").extents + + +def _stl_extents(body: bytes) -> np.ndarray: + return trimesh.load(io.BytesIO(body), file_type="stl").extents + + +class SetRealScaleTests(unittest.TestCase): + def setUp(self) -> None: + self._directory = tempfile.TemporaryDirectory() + self.workspace = Path(self._directory.name) + self.context = MeshOpContext(workspace_dir=self.workspace, temp_dir=self.workspace) + self.mesh = self.workspace / "mesh.glb" + # Generator output: about one unit, no real size. + trimesh.creation.box(extents=[2.0, 1.0, 0.5]).export(str(self.mesh)) + + def tearDown(self) -> None: + self._directory.cleanup() + + def _set(self, path: Path, **params) -> MeshOpResult: + output = self.workspace / "out" / f"{len(list(self.workspace.rglob('*')))}.glb" + context = MeshOpContext(self.workspace, self.workspace, output_path=output) + return mesh_ops_registry.run("set_real_scale", path, params, context) + + def test_a_generated_mesh_is_not_calibrated(self) -> None: + self.assertIsNone(read_real_scale(self.mesh)) + + def test_declaring_a_size_rescales_the_mesh_in_metres_and_marks_it(self) -> None: + result = self._set(self.mesh, size=30, unit="cm") + + np.testing.assert_allclose(_extents(result.file_path), [0.3, 0.15, 0.075], atol=1e-6) + self.assertEqual(read_real_scale(result.file_path), {"unit": "m"}) + np.testing.assert_allclose(result.details["dimensions_mm"], [300, 150, 75], atol=1e-3) + self.assertEqual( + result.details["steps"][0]["summary"], + "Declared the longest side as 30 cm: the mesh is now at real size.", + ) + # The input is left as it was. + self.assertFalse(is_calibrated(self.mesh)) + + def test_a_dimension_other_than_the_longest_can_be_declared(self) -> None: + result = self._set(self.mesh, size=50, unit="mm", dimension="y") + + np.testing.assert_allclose(_extents(result.file_path), [0.1, 0.05, 0.025], atol=1e-6) + + def test_the_declaration_survives_later_ops(self) -> None: + calibrated = self._set(self.mesh, size=30, unit="cm").file_path + + # An op that rebuilds the file and loses its extras, like a repair. + def rebuild(input_path, params, context): + output = self.workspace / "rebuilt.glb" + trimesh.load(str(input_path), force="mesh").export(str(output)) + return MeshOpResult(output) + + registry = MeshOpsRegistry([MeshOp("rebuild", "Rebuild", (), rebuild, "repair")]) + rebuilt = registry.run("rebuild", calibrated, {}, self.context).file_path + self.assertTrue(is_calibrated(rebuilt)) + + # ...but an uncalibrated input never comes out calibrated. + rebuilt = registry.run("rebuild", self.mesh, {}, self.context).file_path + self.assertFalse(is_calibrated(rebuilt)) + + def test_rescaling_a_calibrated_mesh_keeps_it_calibrated(self) -> None: + calibrated = self._set(self.mesh, size=30, unit="cm").file_path + output = self.workspace / "scaled.glb" + context = MeshOpContext(self.workspace, self.workspace, output_path=output) + + mesh_ops_registry.run("normalize_scale", calibrated, {"size": 12, "unit": "cm"}, context) + + self.assertTrue(is_calibrated(output)) + self.assertAlmostEqual(float(max(_extents(output))), 0.12, places=6) + + def test_writing_the_declaration_keeps_the_rest_of_the_file(self) -> None: + scene = trimesh.Scene(trimesh.creation.box(extents=[1.0, 2.0, 3.0])) + scene.metadata["author"] = "someone" + path = self.workspace / "extras.glb" + scene.export(str(path)) + + write_real_scale(path) + + reloaded = trimesh.load(str(path)) + self.assertEqual(reloaded.metadata["author"], "someone") + self.assertEqual(reloaded.metadata["modly"], {"real_scale": {"unit": "m"}}) + np.testing.assert_allclose(_extents(path), [1.0, 2.0, 3.0], atol=1e-6) + + def test_files_that_are_not_glb_are_never_calibrated(self) -> None: + stl = self.workspace / "mesh.stl" + trimesh.creation.box().export(str(stl)) + junk = self.workspace / "junk.glb" + junk.write_bytes(b"not a glb") + + self.assertIsNone(read_real_scale(stl)) + self.assertIsNone(read_real_scale(junk)) + self.assertIsNone(read_real_scale(self.workspace / "missing.glb")) + + def test_reading_the_declaration_stops_after_the_json_chunk(self) -> None: + path = self.workspace / "real.glb" + trimesh.creation.box().export(str(path)) + write_real_scale(path) + data = path.read_bytes() + json_length = int.from_bytes(data[12:16], "little") + # Keep the header and JSON chunk only: the binary data is never needed. + path.write_bytes(data[:20 + json_length]) + + self.assertEqual(read_real_scale(path), {"unit": "m"}) + + def test_carrying_never_reads_the_output_of_an_uncalibrated_input(self) -> None: + output = self.workspace / "out.glb" + trimesh.creation.box().export(str(output)) + + with mock.patch.object(real_scale, "read_real_scale", wraps=real_scale.read_real_scale) as read: + real_scale.carry_real_scale(self.mesh, output) + + self.assertEqual([call.args[0] for call in read.call_args_list], [self.mesh]) + + def test_the_op_is_offered_in_the_registry(self) -> None: + described = {op["id"]: op for op in mesh_ops_registry.describe()}["set_real_scale"] + + self.assertEqual(described["category"], "transform") + self.assertEqual( + [schema["id"] for schema in described["params_schema"]], + ["size", "unit", "dimension"], + ) + with self.assertRaises(ValueError): + mesh_ops_registry.resolve_params("set_real_scale", {"unit": "inch"}) + + +class RealScaleExportTests(unittest.TestCase): + def setUp(self) -> None: + self._directory = tempfile.TemporaryDirectory() + self.workspace = Path(self._directory.name).resolve() + self._original = export_router.registry.WORKSPACE_DIR + export_router.registry.WORKSPACE_DIR = self.workspace + # 0.3 m tall: a calibrated 30 cm figurine, or 0.3 arbitrary units. + trimesh.creation.box(extents=[0.1, 0.3, 0.2]).export(str(self.workspace / "raw.glb")) + trimesh.creation.box(extents=[0.1, 0.3, 0.2]).export(str(self.workspace / "real.glb")) + write_real_scale(self.workspace / "real.glb") + + def tearDown(self) -> None: + export_router.registry.WORKSPACE_DIR = self._original + self._directory.cleanup() + + def test_a_calibrated_stl_export_is_in_millimetres(self) -> None: + body = export_router.export_mesh("stl", "real.glb").body + np.testing.assert_allclose(_stl_extents(body), [100, 300, 200], atol=1e-3) + + body = optimize_router.export_mesh(path="real.glb", format="stl").body + np.testing.assert_allclose(_stl_extents(body), [100, 300, 200], atol=1e-3) + + def test_an_uncalibrated_export_keeps_its_units(self) -> None: + body = export_router.export_mesh("stl", "raw.glb").body + np.testing.assert_allclose(_stl_extents(body), [0.1, 0.3, 0.2], atol=1e-6) + + def test_the_slicer_gets_the_real_size_instead_of_the_default(self) -> None: + token = base64.urlsafe_b64encode(b"real.glb").decode("ascii").rstrip("=") + + body = export_router.export_for_slicer("stl", token, "model.stl").body + + # Y-up to Z-up, in millimetres, not resized to the 50 mm default. + np.testing.assert_allclose(_stl_extents(body), [100, 200, 300], atol=1e-3) + + +def _stored_extents(path: Path) -> np.ndarray: + """Extents of the vertices as stored, ignoring any node transform.""" + loaded = trimesh.load(str(path)) + geometries = loaded.geometry.values() if isinstance(loaded, trimesh.Scene) else [loaded] + return np.ptp(np.vstack([g.vertices for g in geometries]), axis=0) + + +class RealScaleEndToEndTests(unittest.TestCase): + """set_real_scale, then the ops and exports a user runs next, on real files.""" + + def setUp(self) -> None: + self._directory = tempfile.TemporaryDirectory() + self.workspace = Path(self._directory.name).resolve() + self._original = export_router.registry.WORKSPACE_DIR + export_router.registry.WORKSPACE_DIR = self.workspace + # Generator output: 2 × 1 × 0.5 units, no real size. + trimesh.creation.box(extents=[2.0, 1.0, 0.5]).export(str(self.workspace / "raw.glb")) + + def tearDown(self) -> None: + export_router.registry.WORKSPACE_DIR = self._original + self._directory.cleanup() + + def _run(self, op_id: str, input_name: str, output_name: str, **params) -> Path: + output = self.workspace / output_name + context = MeshOpContext(self.workspace, self.workspace, output_path=output) + result = mesh_ops_registry.run(op_id, self.workspace / input_name, params, context) + return result.file_path + + def _slicer_extents(self, name: str) -> np.ndarray: + token = base64.urlsafe_b64encode(name.encode()).decode("ascii").rstrip("=") + return _stl_extents(export_router.export_for_slicer("stl", token, "model.stl").body) + + def test_the_declared_size_is_baked_into_the_vertices(self) -> None: + self._run("set_real_scale", "raw.glb", "real.glb", size=30, unit="cm") + + np.testing.assert_allclose(_stored_extents(self.workspace / "real.glb"), [0.3, 0.15, 0.075], atol=1e-6) + + def test_an_stl_export_after_set_real_scale_measures_the_declared_size(self) -> None: + self._run("set_real_scale", "raw.glb", "real.glb", size=30, unit="cm") + + body = export_router.export_mesh("stl", "real.glb").body + np.testing.assert_allclose(_stl_extents(body), [300, 150, 75], atol=1e-3) + body = optimize_router.export_mesh(path="real.glb", format="stl").body + np.testing.assert_allclose(_stl_extents(body), [300, 150, 75], atol=1e-3) + # Y-up to Z-up for the slicer. + np.testing.assert_allclose(self._slicer_extents("real.glb"), [300, 75, 150], atol=1e-3) + + def test_repair_and_smooth_keep_the_declared_size(self) -> None: + # A sphere: smoothing a box would round its corners off. + trimesh.creation.icosphere(subdivisions=3).export(str(self.workspace / "sphere.glb")) + self._run("set_real_scale", "sphere.glb", "real.glb", size=30, unit="cm") + + for op_id in ("repair", "smooth"): + with self.subTest(op=op_id): + self._run(op_id, "real.glb", f"{op_id}.glb") + self.assertTrue(is_calibrated(self.workspace / f"{op_id}.glb")) + body = export_router.export_mesh("stl", f"{op_id}.glb").body + self.assertAlmostEqual(float(max(_stl_extents(body))), 300.0, delta=10.0) + + def test_ops_read_node_transforms_of_any_glb(self) -> None: + # A GLB from elsewhere whose size lives in a node matrix. + scene = trimesh.Scene() + scene.add_geometry(trimesh.creation.box(extents=[2.0, 1.0, 0.5]), transform=np.diag([0.1, 0.1, 0.1, 1.0])) + scene.export(str(self.workspace / "nodes.glb")) + + self._run("repair", "nodes.glb", "nodes-repaired.glb") + + np.testing.assert_allclose(_stored_extents(self.workspace / "nodes-repaired.glb"), [0.2, 0.1, 0.05], atol=1e-6) + body = export_router.export_mesh("stl", "nodes.glb").body + np.testing.assert_allclose(_stl_extents(body), [0.2, 0.1, 0.05], atol=1e-6) + + def test_normalize_scale_declares_a_real_size(self) -> None: + self._run("normalize_scale", "raw.glb", "scaled.glb", size=12, unit="cm") + + self.assertTrue(is_calibrated(self.workspace / "scaled.glb")) + self.assertAlmostEqual(float(max(self._slicer_extents("scaled.glb"))), 120.0, places=3) + + def test_normalize_scale_in_model_units_declares_nothing(self) -> None: + # The default unit, as the Edit panel and the agent send it. + self._run("normalize_scale", "raw.glb", "units.glb", size=3) + self.assertFalse(is_calibrated(self.workspace / "units.glb")) + self.assertAlmostEqual(float(max(_extents(self.workspace / "units.glb"))), 3.0, places=6) + + def test_arrange_marks_only_a_real_unit(self) -> None: + for params, calibrated in (({"size": 3}, False), ({"size": 30, "unit": "cm"}, True)): + with self.subTest(params=params): + result = optimize_router.arrange_mesh(optimize_router.ArrangeRequest( + path="raw.glb", steps=["normalize_scale"], params={"normalize_scale": params}, + )) + self.assertEqual(is_calibrated(self.workspace / result["path"]), calibrated) + + def test_prepare_for_printing_reaches_the_slicer_at_the_requested_size(self) -> None: + self._run("prepare_for_printing", "raw.glb", "print.glb", size_mm=100) + + self.assertTrue(is_calibrated(self.workspace / "print.glb")) + self.assertAlmostEqual(float(max(self._slicer_extents("print.glb"))), 100.0, places=3) + + +class PrintScaleAndImportTests(unittest.TestCase): + def setUp(self) -> None: + self._directory = tempfile.TemporaryDirectory() + self.workspace = Path(self._directory.name).resolve() + self._original = export_router.registry.WORKSPACE_DIR + export_router.registry.WORKSPACE_DIR = self.workspace + imported_sources.clear() + + def tearDown(self) -> None: + export_router.registry.WORKSPACE_DIR = self._original + imported_sources.clear() + self._directory.cleanup() + + @staticmethod + def _token(path: str) -> str: + return base64.urlsafe_b64encode(path.encode()).decode("ascii").rstrip("=") + + def _slicer_extents(self, path: str) -> np.ndarray: + return _stl_extents(export_router.export_for_slicer("stl", self._token(path), "model.stl").body) + + def test_the_print_page_scale_is_the_slicer_scale(self) -> None: + cases = { + "ai.glb": (trimesh.creation.box(extents=[2.0, 1.0, 0.5]), False), # unit-sized AI output + "part.glb": (trimesh.creation.box(extents=[40.0, 180.0, 40.0]), False), # already in mm + "real.glb": (trimesh.creation.box(extents=[0.1, 0.3, 0.2]), True), # calibrated + } + for name, (box, calibrated) in cases.items(): + with self.subTest(name=name): + box.export(str(self.workspace / name)) + if calibrated: + write_real_scale(self.workspace / name) + + scale = export_router.print_scale(self._token(name)) + + self.assertEqual(scale["calibrated"], calibrated) + # The page multiplies the model's size by mm_per_unit: the slicer + # must receive exactly that size. + expected = float(max(box.extents)) * scale["mm_per_unit"] + self.assertAlmostEqual(float(max(self._slicer_extents(name))), expected, places=3) + self.assertAlmostEqual(export_router.print_scale(self._token("ai.glb"))["mm_per_unit"], 25.0) + + def test_an_imported_stl_is_converted_to_metres_y_up_and_calibrated(self) -> None: + # 10 × 20 mm footprint, 30 mm tall along Z, as a slicer would save it. + source = self.workspace / "bracket.stl" + trimesh.creation.box(extents=[10.0, 20.0, 30.0]).export(str(source)) + + result = asyncio.run(optimize_router.import_mesh_by_path(optimize_router.ImportByPathRequest(path=str(source)))) + + served = Path(unquote(result["url"].split("path=", 1)[1])) + self.assertTrue(is_calibrated(served)) + # Metres, and the height is now Y. + np.testing.assert_allclose(_stored_extents(served), [0.01, 0.03, 0.02], atol=1e-9) + # Back to the slicer: millimetres and Z-up again, at its real size. + np.testing.assert_allclose(self._slicer_extents(str(served)), [10, 20, 30], atol=1e-3) + self.assertEqual(export_router.print_scale(self._token(str(served)))["mm_per_unit"], 1000.0) + + def test_an_imported_obj_and_ply_are_converted_too(self) -> None: + for suffix in ("obj", "ply"): + with self.subTest(suffix=suffix): + source = self.workspace / f"part.{suffix}" + trimesh.creation.box(extents=[10.0, 20.0, 30.0]).export(str(source)) + result = asyncio.run(optimize_router.import_mesh_by_path(optimize_router.ImportByPathRequest(path=str(source)))) + served = Path(unquote(result["url"].split("path=", 1)[1])) + self.assertTrue(is_calibrated(served)) + np.testing.assert_allclose(_stored_extents(served), [0.01, 0.03, 0.02], atol=1e-9) + + def test_an_imported_glb_is_left_alone(self) -> None: + source = self.workspace / "model.glb" + trimesh.creation.box(extents=[2.0, 1.0, 0.5]).export(str(source)) + + result = asyncio.run(optimize_router.import_mesh_by_path(optimize_router.ImportByPathRequest(path=str(source)))) + + self.assertIn(quote(str(source)), result["url"]) + self.assertFalse(is_calibrated(source)) + + def test_a_calibrated_asset_exports_obj_and_ply_in_millimetres(self) -> None: + trimesh.creation.box(extents=[0.1, 0.3, 0.2]).export(str(self.workspace / "real.glb")) + write_real_scale(self.workspace / "real.glb") + trimesh.creation.box(extents=[0.1, 0.3, 0.2]).export(str(self.workspace / "raw.glb")) + + for fmt in ("obj", "ply"): + with self.subTest(fmt=fmt): + for body in ( + export_router.export_mesh(fmt, "real.glb").body, + optimize_router.export_mesh(path="real.glb", format=fmt).body, + ): + extents = trimesh.load(io.BytesIO(body), file_type=fmt, force="mesh").extents + np.testing.assert_allclose(extents, [100, 300, 200], atol=1e-3) + raw = export_router.export_mesh(fmt, "raw.glb").body + extents = trimesh.load(io.BytesIO(raw), file_type=fmt, force="mesh").extents + np.testing.assert_allclose(extents, [0.1, 0.3, 0.2], atol=1e-6) + + +if __name__ == "__main__": + unittest.main() diff --git a/api/tests/test_mesh_ops_registry.py b/api/tests/test_mesh_ops_registry.py index bc2e5733..ce014b93 100644 --- a/api/tests/test_mesh_ops_registry.py +++ b/api/tests/test_mesh_ops_registry.py @@ -30,6 +30,7 @@ def test_builtin_metadata_is_serializable_and_complete(self) -> None: "drop_to_ground", "auto_orient", "normalize_scale", + "set_real_scale", "prepare_for_printing", "prepare_for_game", "prepare_for_web", diff --git a/package.json b/package.json index 239b3007..6048f9c7 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "prepare-resources": "node scripts/download-python-embed.js", "test": "npm run test:py && npm run test:node", "test:py": "node scripts/run-pytests.mjs", - "test:node": "node --test --experimental-strip-types --experimental-loader ./scripts/node-ts-extensionless-loader.mjs src/shared/types/assetLibrary.test.ts src/areas/generate/assetLibraryProjection.test.ts src/areas/generate/assetLibraryService.test.ts src/areas/generate/assetLibraryUi.test.ts src/areas/library/libraryView.test.ts src/areas/library/gridWindow.test.ts src/areas/generate/orcaSlicerLink.test.ts src/shared/utils/agentStream.test.ts src/shared/thumbnails/assetThumbnailQueue.test.ts src/shared/thumbnails/assetThumbnailStore.test.ts src/shared/components/agent/clientTools.test.ts src/shared/components/agent/agentContext.test.ts src/shared/utils/ollamaHealth.test.ts src/shared/utils/notification.test.ts src/shared/utils/agentConversations.test.ts electron/main/artifact-registry-service.test.ts electron/main/asset-thumbnail-cache.test.ts electron/main/extension-path-guard.test.ts electron/main/storage-directory-guard.test.ts electron/main/storage-ownership.test.ts electron/main/startup-error.test.ts electron/main/api-port.test.ts electron/main/agent-conversations.test.ts electron/main/gpu-memory.test.ts electron/preload/artifact-registry-preload.test.ts src/shared/stores/meshHistory.test.ts src/areas/generate/meshEditPanel.test.ts src/areas/generate/sculpt/SculptMesh.test.ts src/areas/generate/sculpt/SculptMesh.perf.test.ts src/areas/generate/sculpt/sculptLogic.test.ts src/shared/pages/pageRegistry.test.ts src/shared/pages/startPage.test.ts src/areas/home/recents.test.ts src/areas/workflows/workflowRunRecord.test.ts src/areas/queue/queueModel.test.ts electron/main/workspace-images.test.ts src/areas/image-studio/imageStudioStore.test.ts src/areas/image-studio/imageOps.test.ts src/shared/utils/imageInput.test.ts && node --test electron/main/*.test.mjs src/**/*.test.mjs", + "test:node": "node --test --experimental-strip-types --experimental-loader ./scripts/node-ts-extensionless-loader.mjs src/shared/types/assetLibrary.test.ts src/areas/generate/assetLibraryProjection.test.ts src/areas/generate/assetLibraryService.test.ts src/areas/generate/assetLibraryUi.test.ts src/areas/library/libraryView.test.ts src/areas/library/gridWindow.test.ts src/areas/generate/orcaSlicerLink.test.ts src/shared/utils/agentStream.test.ts src/shared/thumbnails/assetThumbnailQueue.test.ts src/shared/thumbnails/assetThumbnailStore.test.ts src/shared/components/agent/clientTools.test.ts src/shared/components/agent/agentContext.test.ts src/shared/utils/ollamaHealth.test.ts src/shared/utils/notification.test.ts src/shared/utils/agentConversations.test.ts electron/main/artifact-registry-service.test.ts electron/main/asset-thumbnail-cache.test.ts electron/main/extension-path-guard.test.ts electron/main/storage-directory-guard.test.ts electron/main/storage-ownership.test.ts electron/main/startup-error.test.ts electron/main/api-port.test.ts electron/main/agent-conversations.test.ts electron/main/gpu-memory.test.ts electron/preload/artifact-registry-preload.test.ts src/shared/stores/meshHistory.test.ts src/areas/generate/meshEditPanel.test.ts src/areas/generate/sculpt/SculptMesh.test.ts src/areas/generate/sculpt/SculptMesh.perf.test.ts src/areas/generate/sculpt/sculptLogic.test.ts src/shared/pages/pageRegistry.test.ts src/shared/pages/startPage.test.ts src/areas/home/recents.test.ts src/areas/workflows/workflowRunRecord.test.ts src/areas/queue/queueModel.test.ts electron/main/workspace-images.test.ts src/areas/image-studio/imageStudioStore.test.ts src/areas/image-studio/imageOps.test.ts src/shared/utils/imageInput.test.ts src/areas/print/printBed.test.ts src/shared/three/viewScale.test.ts && node --test electron/main/*.test.mjs src/**/*.test.mjs", "package": "cross-env CSC_IDENTITY_AUTO_DISCOVERY=false npm run build && npm run prepare-resources && electron-builder", "package:mac": "cross-env CSC_IDENTITY_AUTO_DISCOVERY=false npm run build && npm run prepare-resources && electron-builder --mac --arm64", "lint": "eslint ." diff --git a/src/areas/generate/components/Viewer3D.tsx b/src/areas/generate/components/Viewer3D.tsx index 71d4a321..4243e934 100644 --- a/src/areas/generate/components/Viewer3D.tsx +++ b/src/areas/generate/components/Viewer3D.tsx @@ -15,6 +15,8 @@ THREE.Mesh.prototype.raycast = acceleratedRaycast import SplatViewer, { type SplatViewerHandle } from './SplatViewer' import { useGeneration } from '@shared/hooks/useGeneration' import { useAppStore } from '@shared/stores/appStore' +import { disposeObject, groundPlacement } from '@shared/three/meshScene' +import { viewScaleFor } from '@shared/three/viewScale' import { ViewerToolbar, type ViewMode } from './ViewerToolbar' import type { LightSettings, PointLight } from '@shared/stores/appStore' import { DEFAULT_LIGHT_SETTINGS } from '@shared/stores/appStore' @@ -137,6 +139,35 @@ function CanvasCapture({ return null } +// --------------------------------------------------------------------------- +// CameraRig — reframes the default view when the model's scale changes +// --------------------------------------------------------------------------- + +const DEFAULT_CAMERA_POSITION = new THREE.Vector3(0, 1.5, 4) +const DEFAULT_NEAR = 0.1 +const DEFAULT_FAR = 1000 + +function CameraRig({ viewScale }: { viewScale: number }): null { + const camera = useThree((s) => s.camera) + const controls = useThree((s) => s.controls) as { target?: THREE.Vector3, update?: () => void } | null + const applied = useRef(1) + + useEffect(() => { + // Only on a change of scale: swapping between unit-sized meshes keeps the + // user's camera, as before. + if (viewScale === applied.current) return + applied.current = viewScale + camera.position.copy(DEFAULT_CAMERA_POSITION).multiplyScalar(viewScale) + camera.near = DEFAULT_NEAR * viewScale + camera.far = DEFAULT_FAR * viewScale + camera.updateProjectionMatrix() + controls?.target?.set(0, 0, 0) + controls?.update?.() + }, [viewScale, camera, controls]) + + return null +} + // --------------------------------------------------------------------------- // ModelErrorBoundary — catches useGLTF load failures (e.g. 404) // --------------------------------------------------------------------------- @@ -200,6 +231,8 @@ interface MeshModelProps { /** Sculpt mode: clicks paint instead of selecting. */ sculpting: boolean onStats: (stats: { vertices: number; triangles: number }) => void + /** Longest side of the model, in scene units: frames the view. */ + onSize: (longestSide: number) => void onSelect: () => void onObject: (obj: THREE.Object3D | null) => void } @@ -231,6 +264,7 @@ function SceneMeshModel({ selected, sculpting, onStats, + onSize, onSelect, onObject, scene, @@ -256,13 +290,7 @@ function SceneMeshModel({ } else { useGLTF.clear(url) } - scene.traverse((child) => { - if (child instanceof THREE.Mesh) { - child.geometry.dispose() - const materials = Array.isArray(child.material) ? child.material : [child.material] - materials.forEach((m: THREE.Material) => m.dispose()) - } - }) + disposeObject(scene) } }, [loaderType, scene, url]) @@ -289,15 +317,11 @@ function SceneMeshModel({ // Centre the mesh on the grid. Runs only on first load / model change — never // on plain re-renders, so a live gizmo transform is not silently overwritten. useEffect(() => { - // Clear any cached transform before measuring (useGLTF may reuse a scene + // Clears any cached transform before measuring (useGLTF may reuse a scene // that still carries an earlier gizmo pose). - scene.position.set(0, 0, 0) - scene.rotation.set(0, 0, 0) - scene.scale.set(1, 1, 1) - const box = new THREE.Box3().setFromObject(scene) - const center = new THREE.Vector3() - box.getCenter(center) - scene.position.set(-center.x, -box.min.y, -center.z) + const placement = groundPlacement(scene) + scene.position.copy(placement.offset) + onSize(Math.max(placement.size.x, placement.size.y, placement.size.z)) // Compute stats let vertices = 0 @@ -956,6 +980,9 @@ export default function Viewer3D({ const splatRef = useRef(null) const [meshObject, setMeshObject] = useState(null) + // Camera, grid and zoom limits follow the model's size (see viewScaleFor). + const [viewScale, setViewScale] = useState(1) + const handleModelSize = useCallback((longestSide: number) => setViewScale(viewScaleFor(longestSide)), []) const sculpting = useSculptStore((s) => s.active) const sculptStatus = useSculptStore((s) => s.status) @@ -1138,7 +1165,8 @@ export default function Viewer3D({ - + + {modelUrl && currentJob ? ( @@ -1154,6 +1182,7 @@ export default function Viewer3D({ selected={selected} sculpting={sculpting} onStats={setStoreMeshStats} + onSize={handleModelSize} onSelect={handleMeshSelect} onObject={setMeshObject} /> @@ -1193,8 +1222,8 @@ export default function Viewer3D({ enablePan enableZoom enableRotate - minDistance={0.5} - maxDistance={20} + minDistance={0.5 * viewScale} + maxDistance={20 * viewScale} autoRotate={autoRotate} autoRotateSpeed={1.5} enableDamping diff --git a/src/areas/generate/orcaSlicerLink.test.ts b/src/areas/generate/orcaSlicerLink.test.ts index 514618eb..813b7401 100644 --- a/src/areas/generate/orcaSlicerLink.test.ts +++ b/src/areas/generate/orcaSlicerLink.test.ts @@ -6,6 +6,7 @@ import { buildOrcaSlicerDeepLink, canOpenInOrcaSlicer, encodeWorkspacePathToken, + slicerSourceToken, } from './orcaSlicerLink.ts' test('builds an orcaslicer://open deeplink whose file= is a percent-encoded, query-less URL ending in model.stl', () => { @@ -80,3 +81,11 @@ test('buildOrcaSlicerDeepLink refuses an output it cannot slice', () => { test('SLICER_FORMAT is a format OrcaSlicer can import', () => { assert.equal(SLICER_FORMAT, 'stl') }) + +test('the Print page sizes a mesh with the same source token as the slicer link', () => { + const imported = '/optimize/serve-file?path=' + encodeURIComponent('C:\\models\\part.glb') + assert.equal(slicerSourceToken('/workspace/Workflows/a.glb'), encodeWorkspacePathToken('Workflows/a.glb')) + assert.equal(slicerSourceToken(imported), encodeWorkspacePathToken('C:\\models\\part.glb')) + assert.equal(slicerSourceToken('/workspace/Workflows/splat.ply'), undefined) + assert.equal(slicerSourceToken(undefined), undefined) +}) diff --git a/src/areas/generate/orcaSlicerLink.ts b/src/areas/generate/orcaSlicerLink.ts index d5a76c9b..0764ff95 100644 --- a/src/areas/generate/orcaSlicerLink.ts +++ b/src/areas/generate/orcaSlicerLink.ts @@ -53,6 +53,15 @@ function sliceableSourcePath(outputUrl: string | undefined): string | undefined return undefined } +/** + * The source token the slicer routes take (`/export/slicer/…`, `/export/print-scale/…`) + * for a viewer mesh URL, or `undefined` when it cannot be sliced. + */ +export function slicerSourceToken(outputUrl: string | undefined): string | undefined { + const sourcePath = sliceableSourcePath(outputUrl) + return sourcePath ? encodeWorkspacePathToken(sourcePath) : undefined +} + /** * Whether a generation output can be opened in OrcaSlicer: it must be a mesh in * a format the slicer route converts, served either from the workspace or as a @@ -70,9 +79,8 @@ export function canOpenInOrcaSlicer(outputUrl: string | undefined): boolean { * @throws if `outputUrl` is not sliceable — guard with {@link canOpenInOrcaSlicer} */ export function buildOrcaSlicerDeepLink(apiUrl: string, outputUrl: string): string { - const sourcePath = sliceableSourcePath(outputUrl) - if (!sourcePath) throw new Error(`Not sliceable: ${outputUrl}`) - const token = encodeWorkspacePathToken(sourcePath) + const token = slicerSourceToken(outputUrl) + if (!token) throw new Error(`Not sliceable: ${outputUrl}`) const base = apiUrl.replace(/\/+$/, '') const modelUrl = `${base}/export/slicer/${SLICER_FORMAT}/${token}/model.${SLICER_FORMAT}` return `orcaslicer://open?file=${encodeURIComponent(modelUrl)}` diff --git a/src/areas/print/PrintPage.tsx b/src/areas/print/PrintPage.tsx new file mode 100644 index 00000000..e9039140 --- /dev/null +++ b/src/areas/print/PrintPage.tsx @@ -0,0 +1,410 @@ +import { Component, Suspense, useCallback, useEffect, useMemo, useState } from 'react' +import type { ReactNode } from 'react' +import { Canvas } from '@react-three/fiber' +import { Environment, Lightformer, OrbitControls, useGLTF } from '@react-three/drei' +import * as THREE from 'three' +import { useAppStore } from '@shared/stores/appStore' +import { useNavStore } from '@shared/stores/navStore' +import type { ProjectedAssetLibraryEntry } from '@areas/generate/assetLibraryProjection' +import { getDefaultAssetLibraryService } from '@areas/generate/assetLibraryService' +import { + filterVisibleAssetLibraryEntries, + isAssetLibraryEntryOpenable, + openAssetLibraryEntry, + sortAssetLibraryEntries, +} from '@areas/generate/assetLibraryUi' +import { + CUSTOM_PRINTER_ID, + MAX_VOLUME_MM, + MIN_VOLUME_MM, + PRINTER_PRESETS, + checkFit, + createLatestGuard, + describeOverflow, + errorForModel, + isGlbUrl, + formatVolume, + modelSizeMm, + parsePrintScale, + parsePrinterChoice, + resolveBuildVolume, + type BuildVolume, + type ModelError, + type PrintScale, + type PrinterChoice, +} from './printBed' +import { slicerSourceToken } from '@areas/generate/orcaSlicerLink' +import { usePrintStore } from './printStore' +import { disposeObject, groundPlacement } from '@shared/three/meshScene' + +const PRINTER_STORAGE_KEY = 'modly.print.printer' + +function readStoredPrinter(): PrinterChoice { + try { return parsePrinterChoice(localStorage.getItem(PRINTER_STORAGE_KEY)) } catch { return parsePrinterChoice(null) } +} + +/** A model's bounding-box size, in its own (glTF) units. */ +type Size3 = { x: number, y: number, z: number } + +// ─── 3D scene (millimetres) ────────────────────────────────────────────────── + +/** Plate grid: a line every 10 mm, a brighter one every 50 mm. */ +function PlateGrid({ volume }: { volume: BuildVolume }): JSX.Element { + const [minor, major] = useMemo(() => { + const minorPoints: number[] = [] + const majorPoints: number[] = [] + const w = volume.width / 2 + const d = volume.depth / 2 + for (let x = 0; x <= volume.width + 1e-6; x += 10) { + const target = Math.round(x) % 50 === 0 ? majorPoints : minorPoints + target.push(x - w, 0, -d, x - w, 0, d) + } + for (let z = 0; z <= volume.depth + 1e-6; z += 10) { + const target = Math.round(z) % 50 === 0 ? majorPoints : minorPoints + target.push(-w, 0, z - d, w, 0, z - d) + } + const geometry = (points: number[]) => { + const g = new THREE.BufferGeometry() + g.setAttribute('position', new THREE.Float32BufferAttribute(points, 3)) + return g + } + return [geometry(minorPoints), geometry(majorPoints)] + }, [volume.width, volume.depth]) + + useEffect(() => () => { minor.dispose(); major.dispose() }, [minor, major]) + + return ( + + + + + ) +} + +function BuildPlate({ volume, fits }: { volume: BuildVolume, fits: boolean }): JSX.Element { + const edges = useMemo( + () => new THREE.EdgesGeometry(new THREE.BoxGeometry(volume.width, volume.height, volume.depth)), + [volume.width, volume.height, volume.depth], + ) + useEffect(() => () => edges.dispose(), [edges]) + + return ( + + + + + + + + + + + ) +} + +/** The model at its printed size, centred on the plate and resting on it. */ +function PlacedModel({ url, mmPerUnit, onSize }: { + url: string + mmPerUnit: number + onSize: (size: Size3) => void +}): JSX.Element { + const { scene } = useGLTF(url) + + const placement = useMemo(() => groundPlacement(scene), [scene]) + + useEffect(() => { + const { x, y, z } = placement.size + onSize({ x, y, z }) + }, [placement, onSize]) + + // Free the loader cache and GPU memory when the model changes, as Generate does. + useEffect(() => () => { + useGLTF.clear(url) + disposeObject(scene) + }, [url, scene]) + + return ( + + + + + + ) +} + +class ModelBoundary extends Component<{ children: ReactNode, resetKey: string, onError: () => void }, { failed: boolean }> { + state = { failed: false } + static getDerivedStateFromError(): { failed: boolean } { return { failed: true } } + componentDidCatch(error: Error): void { + console.warn('[PrintPage] Failed to load model:', error.message) + this.props.onError() + } + componentDidUpdate(prev: { resetKey: string }): void { + if (prev.resetKey !== this.props.resetKey && this.state.failed) this.setState({ failed: false }) + } + render(): ReactNode { return this.state.failed ? null : this.props.children } +} + +function PlateScene({ volume, modelUrl, mmPerUnit, fits, onSize, onError }: { + volume: BuildVolume + modelUrl: string | null + /** Null until the print scale is known: the model is not placed before. */ + mmPerUnit: number | null + fits: boolean + onSize: (size: Size3) => void + onError: () => void +}): JSX.Element { + const span = Math.max(volume.width, volume.depth, volume.height) + return ( + + + + + + + + + + + + {modelUrl && mmPerUnit !== null && ( + + + + + + )} + + + + ) +} + +// ─── Side panel ────────────────────────────────────────────────────────────── + +const FIELD = 'h-8 w-full rounded-lg border border-zinc-800 bg-zinc-900/70 px-2.5 text-xs text-zinc-200 focus:outline-none focus-visible:border-accent/50' + +function Section({ title, children }: { title: string, children: ReactNode }): JSX.Element { + return ( +
+

{title}

+ {children} +
+ ) +} + +function VolumeInput({ label, value, onChange }: { label: string, value: number, onChange: (value: number) => void }): JSX.Element { + return ( + + ) +} + +export default function PrintPage(): JSX.Element { + const apiUrl = useAppStore((s) => s.apiUrl) + const currentJob = useAppStore((s) => s.currentJob) + const navigate = useNavStore((s) => s.navigate) + const source = usePrintStore((s) => s.source) + const setSource = usePrintStore((s) => s.setSource) + const service = useMemo(() => getDefaultAssetLibraryService(), []) + + const [printer, setPrinter] = useState(readStoredPrinter) + const [assets, setAssets] = useState([]) + // Both are kept with the model they belong to, so a stale one never shows. + const [modelSize, setModelSize] = useState<{ modelUrl: string, size: Size3 } | null>(null) + const [scale, setScale] = useState<{ modelUrl: string, value: PrintScale } | null>(null) + const [error, setError] = useState(null) + // A library asset opens asynchronously: only the latest choice may apply. + const [sourceGuard] = useState(createLatestGuard) + + useEffect(() => { + let cancelled = false + service.list() + .then((result) => { + if (cancelled || !result.success) return + const meshes = filterVisibleAssetLibraryEntries(result.entries).filter(isAssetLibraryEntryOpenable) + setAssets(sortAssetLibraryEntries(meshes, 'date')) + }) + .catch(() => { /* the picker just lists fewer assets */ }) + return () => { cancelled = true } + }, [service]) + + function changePrinter(next: PrinterChoice) { + setPrinter(next) + try { localStorage.setItem(PRINTER_STORAGE_KEY, JSON.stringify(next)) } catch { /* private mode / quota */ } + } + + // GLB only: splats have no plate size, and imports are converted to GLB. + const currentUrl = currentJob?.status === 'done' && currentJob.outputUrl && isGlbUrl(currentJob.outputUrl) + ? currentJob.outputUrl + : null + const sourceUrl = source.kind === 'library' ? source.url : currentUrl + const modelUrl = sourceUrl ? `${apiUrl}${sourceUrl}` : null + const loadError = errorForModel(error, modelUrl) + + async function chooseSource(value: string) { + const ticket = sourceGuard.take() + setError(null) + if (value === 'current') { setSource({ kind: 'current' }); return } + const entry = assets.find((a) => a.id === value) + if (!entry) return + const result = await openAssetLibraryEntry(service, entry) + if (!sourceGuard.isLatest(ticket)) return + if (!result.success) { setError({ modelUrl, message: result.message }); return } + setSource({ kind: 'library', id: entry.id, name: entry.displayName, url: result.selection.job.outputUrl ?? '' }) + } + + // The print scale comes from the backend's slicer rule, so the size on the + // plate is the size OrcaSlicer receives, calibrated or not. + useEffect(() => { + if (!modelUrl) return + const token = slicerSourceToken(sourceUrl ?? undefined) + if (!token) { + setError({ modelUrl, message: 'This model cannot be sized for printing.' }) + return + } + let cancelled = false + fetch(`${apiUrl}/export/print-scale/${token}`) + .then(async (response) => (response.ok ? parsePrintScale(await response.json()) : null)) + .catch(() => null) + .then((value) => { + if (cancelled) return + if (value) setScale({ modelUrl, value }) + else setError({ modelUrl, message: 'The print size of this model could not be read.' }) + }) + return () => { cancelled = true } + }, [apiUrl, sourceUrl, modelUrl]) + + const onSize = useCallback((size: Size3) => { + if (modelUrl) setModelSize({ modelUrl, size }) + }, [modelUrl]) + + const printScale = scale?.modelUrl === modelUrl ? scale.value : null + const sizeUnits = modelSize?.modelUrl === modelUrl ? modelSize.size : null + const measured = printScale && sizeUnits + ? { size: modelSizeMm(sizeUnits, printScale.mmPerUnit), calibrated: printScale.calibrated } + : null + + const volume = resolveBuildVolume(printer) + const fit = measured ? checkFit(measured.size, volume) : null + + return ( +
+ + +
+ setError({ modelUrl, message: 'The model could not be loaded.' })} + /> + {!modelUrl && ( +
+

No model to print

+

Generate or open a mesh, or pick one from the Library on the left.

+ +
+ )} + {modelUrl && ( +

Drag to rotate • Scroll to zoom

+ )} +
+
+ ) +} diff --git a/src/areas/print/printBed.test.ts b/src/areas/print/printBed.test.ts new file mode 100644 index 00000000..76575c80 --- /dev/null +++ b/src/areas/print/printBed.test.ts @@ -0,0 +1,114 @@ +import test from 'node:test' +import assert from 'node:assert/strict' +import { + CUSTOM_PRINTER_ID, + DEFAULT_PRINTER_CHOICE, + MAX_VOLUME_MM, + MIN_VOLUME_MM, + checkFit, + createLatestGuard, + describeOverflow, + errorForModel, + formatVolume, + isGlbUrl, + modelSizeMm, + parsePrintScale, + parsePrinterChoice, + resolveBuildVolume, +} from './printBed' + +const PLATE_256 = { width: 256, depth: 256, height: 256 } + +test('the default printer is a 256 mm cube', () => { + assert.deepEqual(resolveBuildVolume(DEFAULT_PRINTER_CHOICE), PLATE_256) + assert.deepEqual(resolveBuildVolume(parsePrinterChoice(null)), PLATE_256) +}) + +test('a calibrated asset is shown at its real size and fits the plate', () => { + // A 30 cm × 15 cm × 7.5 cm GLB (metres, Y up): 300 wide, 75 deep, 150 tall. + const size = modelSizeMm({ x: 0.3, y: 0.15, z: 0.075 }) + assert.deepEqual(size, { width: 300, depth: 75, height: 150 }) + + const small = modelSizeMm({ x: 0.2, y: 0.25, z: 0.1 }) + assert.deepEqual(checkFit(small, PLATE_256), { fits: true, over: [] }) +}) + +test('a model larger than the build volume is flagged, side by side', () => { + const size = modelSizeMm({ x: 0.3, y: 0.3, z: 0.1 }) + const fit = checkFit(size, PLATE_256) + assert.deepEqual(fit, { fits: false, over: ['width', 'height'] }) + assert.equal( + describeOverflow(size, PLATE_256, fit.over), + 'Too large for this printer: 300 mm wide (max 256), 300 mm tall (max 256).', + ) +}) + +test('a model sized exactly to the plate fits despite float noise', () => { + const size = modelSizeMm({ x: 0.256, y: 0.256, z: 0.256 }) + assert.ok(checkFit(size, PLATE_256).fits) +}) + +test('an uncalibrated model is shown, and checked, at the provisional size the slicer gets', () => { + // GET /export/print-scale for unit-sized AI output: longest side to 50 mm. + const scale = parsePrintScale({ calibrated: false, mm_per_unit: 25 }) + assert.deepEqual(scale, { calibrated: false, mmPerUnit: 25 }) + const size = modelSizeMm({ x: 2, y: 1, z: 0.5 }, scale!.mmPerUnit) + assert.deepEqual(size, { width: 50, depth: 12.5, height: 25 }) + assert.ok(checkFit(size, PLATE_256).fits) + // The same mesh read as metres would wrongly be flagged as 2 m wide. + assert.ok(!checkFit(modelSizeMm({ x: 2, y: 1, z: 0.5 }), PLATE_256).fits) +}) + +test('an unusable print scale answer is rejected', () => { + assert.deepEqual(parsePrintScale({ calibrated: true, mm_per_unit: 1000 }), { calibrated: true, mmPerUnit: 1000 }) + assert.equal(parsePrintScale(null), null) + assert.equal(parsePrintScale({ calibrated: true }), null) + assert.equal(parsePrintScale({ calibrated: false, mm_per_unit: 0 }), null) + assert.equal(parsePrintScale({ mm_per_unit: 25 }), null) +}) + +test('a stored custom printer is restored and clamped', () => { + const choice = parsePrinterChoice(JSON.stringify({ + presetId: CUSTOM_PRINTER_ID, + custom: { width: 5, depth: 300, height: 99999 }, + })) + assert.deepEqual(resolveBuildVolume(choice), { width: MIN_VOLUME_MM, depth: 300, height: MAX_VOLUME_MM }) +}) + +test('an unknown or unreadable stored printer falls back to the default', () => { + assert.deepEqual(resolveBuildVolume(parsePrinterChoice('{"presetId":"gone"}')), PLATE_256) + assert.deepEqual(resolveBuildVolume(parsePrinterChoice('not json')), PLATE_256) +}) + +test('presets and formatting', () => { + assert.deepEqual(resolveBuildVolume({ ...DEFAULT_PRINTER_CHOICE, presetId: 'prusa-mk4' }), { width: 250, depth: 210, height: 220 }) + assert.equal(formatVolume({ width: 256, depth: 42.25, height: 7.5 }), '256 × 42.3 × 7.50 mm') +}) + +test('a load error disappears once another model is shown', () => { + const error = { modelUrl: 'http://api/workspace/a.glb', message: 'The model could not be loaded.' } + assert.equal(errorForModel(error, 'http://api/workspace/a.glb'), 'The model could not be loaded.') + assert.equal(errorForModel(error, 'http://api/workspace/b.glb'), null) + assert.equal(errorForModel(error, null), null) + assert.equal(errorForModel(null, 'http://api/workspace/a.glb'), null) +}) + +test('a slow source choice never overrides a newer one', async () => { + const guard = createLatestGuard() + let shown = 'current' + const choose = async (name: string, delay: number) => { + const ticket = guard.take() + await new Promise((resolve) => setTimeout(resolve, delay)) + if (guard.isLatest(ticket)) shown = name + } + // The first library asset answers after the user picked a second one. + await Promise.all([choose('slow', 30), choose('fast', 0)]) + assert.equal(shown, 'fast') +}) + +test('only GLB meshes are shown on the plate', () => { + assert.ok(isGlbUrl('/workspace/Workflows/a.glb')) + assert.ok(isGlbUrl('/optimize/serve-file?path=C%3A%5Cmodels%5Cpart.glb')) + assert.ok(!isGlbUrl('/workspace/Workflows/splat.ply')) + assert.ok(!isGlbUrl('/optimize/serve-file?path=%2Ftmp%2Fsplat.splat')) +}) diff --git a/src/areas/print/printBed.ts b/src/areas/print/printBed.ts new file mode 100644 index 00000000..2b154297 --- /dev/null +++ b/src/areas/print/printBed.ts @@ -0,0 +1,166 @@ +// Pure logic behind the Print page: printer volumes, the model's real size and +// whether it fits. Kept free of React and three.js so node --test can load it. + +/** A printer's build volume, in millimetres. Height is the printer's Z (up). */ +export interface BuildVolume { + width: number + depth: number + height: number +} + +export interface PrinterPreset { + id: string + label: string + volume: BuildVolume +} + +export const PRINTER_PRESETS: readonly PrinterPreset[] = [ + { id: 'bambu-x1', label: 'Bambu Lab X1 / P1 / A1', volume: { width: 256, depth: 256, height: 256 } }, + { id: 'bambu-a1-mini', label: 'Bambu Lab A1 mini', volume: { width: 180, depth: 180, height: 180 } }, + { id: 'prusa-mk4', label: 'Prusa MK4', volume: { width: 250, depth: 210, height: 220 } }, + { id: 'prusa-mini', label: 'Prusa MINI+', volume: { width: 180, depth: 180, height: 180 } }, + { id: 'ender-3', label: 'Creality Ender-3', volume: { width: 220, depth: 220, height: 250 } }, + { id: 'voron-350', label: 'Voron 2.4 (350)', volume: { width: 350, depth: 350, height: 340 } }, +] + +export const CUSTOM_PRINTER_ID = 'custom' +export const DEFAULT_PRINTER_ID = 'bambu-x1' + +/** Bounds of a custom build volume side, in millimetres. */ +export const MIN_VOLUME_MM = 10 +export const MAX_VOLUME_MM = 2000 + +export interface PrinterChoice { + /** A PRINTER_PRESETS id, or CUSTOM_PRINTER_ID. */ + presetId: string + /** The custom volume, kept while a preset is selected. */ + custom: BuildVolume +} + +export const DEFAULT_PRINTER_CHOICE: PrinterChoice = { + presetId: DEFAULT_PRINTER_ID, + custom: { width: 200, depth: 200, height: 200 }, +} + +function clampSide(value: unknown, fallback: number): number { + if (typeof value !== 'number' || !Number.isFinite(value)) return fallback + return Math.min(MAX_VOLUME_MM, Math.max(MIN_VOLUME_MM, value)) +} + +export function sanitizeVolume(value: unknown, fallback: BuildVolume = DEFAULT_PRINTER_CHOICE.custom): BuildVolume { + const v = (value && typeof value === 'object' ? value : {}) as Partial> + return { + width: clampSide(v.width, fallback.width), + depth: clampSide(v.depth, fallback.depth), + height: clampSide(v.height, fallback.height), + } +} + +/** A stored choice (JSON), falling back to the default for anything unreadable. */ +export function parsePrinterChoice(raw: string | null): PrinterChoice { + if (!raw) return DEFAULT_PRINTER_CHOICE + try { + const parsed = JSON.parse(raw) as { presetId?: unknown, custom?: unknown } + const known = parsed.presetId === CUSTOM_PRINTER_ID || PRINTER_PRESETS.some((p) => p.id === parsed.presetId) + return { + presetId: known ? String(parsed.presetId) : DEFAULT_PRINTER_ID, + custom: sanitizeVolume(parsed.custom), + } + } catch { + return DEFAULT_PRINTER_CHOICE + } +} + +export function resolveBuildVolume(choice: PrinterChoice): BuildVolume { + if (choice.presetId === CUSTOM_PRINTER_ID) return sanitizeVolume(choice.custom) + return (PRINTER_PRESETS.find((p) => p.id === choice.presetId) ?? PRINTER_PRESETS[0]).volume +} + +/** A viewer mesh URL (/workspace/… or /optimize/serve-file?path=…) that points at a GLB. */ +export function isGlbUrl(url: string): boolean { + let path = url + try { path = decodeURIComponent(url) } catch { /* keep it as is */ } + return /\.(glb|gltf)$/i.test(path) +} + +/** glTF units are metres; the plate is drawn in millimetres. */ +export const MM_PER_M = 1000 + +/** A load error, kept with the model it is about. */ +export interface ModelError { + modelUrl: string | null + message: string +} + +/** The error to show for the model on screen: one about another model is stale. */ +export function errorForModel(error: ModelError | null, modelUrl: string | null): string | null { + return error && error.modelUrl === modelUrl ? error.message : null +} + +/** + * Orders async choices: each one takes a ticket, and only the latest ticket + * may apply its result, so a slow answer never overrides a newer choice. + */ +export function createLatestGuard(): { take: () => number, isLatest: (ticket: number) => boolean } { + let latest = 0 + return { take: () => ++latest, isLatest: (ticket) => ticket === latest } +} + +/** + * A model's printed size in millimetres, from its glTF bounding box (Y up). + * `mmPerUnit` is the backend's print scale (GET /export/print-scale): 1000 for + * a calibrated asset (metres), and for an uncalibrated one the provisional + * scale the slicer hand-off applies, so the plate and the slicer agree. + */ +export function modelSizeMm(size: { x: number, y: number, z: number }, mmPerUnit: number = MM_PER_M): BuildVolume { + return { width: size.x * mmPerUnit, depth: size.z * mmPerUnit, height: size.y * mmPerUnit } +} + +/** GET /export/print-scale/{token}: how the slicer hand-off sizes a mesh. */ +export interface PrintScale { + calibrated: boolean + mmPerUnit: number +} + +/** The backend's answer, or null when it is not a usable scale. */ +export function parsePrintScale(body: unknown): PrintScale | null { + const value = body as { calibrated?: unknown, mm_per_unit?: unknown } | null + if (!value || typeof value.calibrated !== 'boolean') return null + const mmPerUnit = value.mm_per_unit + if (typeof mmPerUnit !== 'number' || !Number.isFinite(mmPerUnit) || mmPerUnit <= 0) return null + return { calibrated: value.calibrated, mmPerUnit } +} + +export type VolumeSide = keyof BuildVolume + +export interface FitResult { + fits: boolean + /** The sides the model is larger than the build volume on. */ + over: VolumeSide[] +} + +/** Sub-micron slack, so a model sized exactly to the plate still fits. */ +const FIT_EPSILON_MM = 1e-3 + +export function checkFit(model: BuildVolume, volume: BuildVolume): FitResult { + const sides: VolumeSide[] = ['width', 'depth', 'height'] + const over = sides.filter((side) => model[side] > volume[side] + FIT_EPSILON_MM) + return { fits: over.length === 0, over } +} + +export function formatMm(value: number): string { + if (value >= 100) return value.toFixed(0) + if (value >= 10) return value.toFixed(1) + return value.toFixed(2) +} + +export function formatVolume(volume: BuildVolume): string { + return `${formatMm(volume.width)} × ${formatMm(volume.depth)} × ${formatMm(volume.height)} mm` +} + +/** "Too large: 312 mm wide for a 256 mm plate." */ +export function describeOverflow(model: BuildVolume, volume: BuildVolume, over: VolumeSide[]): string { + const words: Record = { width: 'wide', depth: 'deep', height: 'tall' } + const parts = over.map((side) => `${formatMm(model[side])} mm ${words[side]} (max ${formatMm(volume[side])})`) + return `Too large for this printer: ${parts.join(', ')}.` +} diff --git a/src/areas/print/printStore.ts b/src/areas/print/printStore.ts new file mode 100644 index 00000000..249203d1 --- /dev/null +++ b/src/areas/print/printStore.ts @@ -0,0 +1,17 @@ +import { create } from 'zustand' + +/** What the Print page shows: Generate's current mesh, or a Library asset. */ +export type PrintSource = + | { kind: 'current' } + | { kind: 'library', id: string, name: string, url: string } + +interface PrintState { + source: PrintSource + setSource: (source: PrintSource) => void +} + +// Kept across page switches, like Image Studio's history. +export const usePrintStore = create((set) => ({ + source: { kind: 'current' }, + setSource: (source) => set({ source }), +})) diff --git a/src/areas/workflows/meshExporter.test.mjs b/src/areas/workflows/meshExporter.test.mjs index b5f5ff65..09a154a9 100644 --- a/src/areas/workflows/meshExporter.test.mjs +++ b/src/areas/workflows/meshExporter.test.mjs @@ -18,12 +18,12 @@ const compiled = buildSync({ packages: 'external', }).outputFiles[0].text -function loadProcessor() { +function loadProcessor(doc = {}) { // Exercise the real destination creation and file writing without needing // glTF dependencies or a generated mesh for these export-path regressions. class NodeIO { registerExtensions() { return this } - async read() { return {} } + async read() { return doc } async write(filePath) { writeFileSync(filePath, 'exported mesh') } } const module = { exports: {} } @@ -91,3 +91,48 @@ test('a filename cannot silently use a different export format', async () => { rmSync(root, { recursive: true, force: true }) } }) + +// One triangle, 0.1 m along X and 0.2 m along Y, with or without the real-size marker. +function triangleDoc(extras) { + const prim = { + getAttribute: (name) => (name === 'POSITION' ? { getArray: () => new Float32Array([0, 0, 0, 0.1, 0, 0, 0, 0.2, 0]) } : null), + getIndices: () => null, + } + return { + getRoot: () => ({ + listMeshes: () => [{ listPrimitives: () => [prim] }], + getDefaultScene: () => ({ getExtras: () => extras }), + }), + } +} + +function objVertices(text) { + return text.split('\n').filter((line) => line.startsWith('v ')).map((line) => line.split(' ').slice(1).map(Number)) +} + +for (const scenario of [ + { name: 'a real-size asset is written in millimetres', extras: { modly: { real_scale: { unit: 'm' } } }, factor: 1000 }, + { name: 'an asset without a real size keeps its units', extras: {}, factor: 1 }, +]) { + test(scenario.name, async () => { + const root = mkdtempSync(join(tmpdir(), 'modly-export-')) + try { + const context = { workspaceDir: join(root, 'workspace'), log() {}, progress() {} } + const obj = await loadProcessor(triangleDoc(scenario.extras))( + { filePath: join(root, 'input.glb') }, { export_format: 'obj', output_path: 'part.obj' }, context, + ) + const vertices = objVertices(readFileSync(obj.filePath, 'utf8')) + assert.deepEqual(vertices[1].map((v) => Number(v.toFixed(3))), [0.1 * scenario.factor, 0, 0].map((v) => Number(v.toFixed(3)))) + assert.equal(Number(vertices[2][1].toFixed(3)), Number((0.2 * scenario.factor).toFixed(3))) + + const stl = await loadProcessor(triangleDoc(scenario.extras))( + { filePath: join(root, 'input.glb') }, { export_format: 'stl', output_path: 'part.stl' }, context, + ) + const data = readFileSync(stl.filePath) + // Binary STL: 84-byte header, then normal (12 bytes) and three vertices. + assert.ok(Math.abs(data.readFloatLE(84 + 12 + 12) - 0.1 * scenario.factor) < 1e-3) + } finally { + rmSync(root, { recursive: true, force: true }) + } + }) +} \ No newline at end of file diff --git a/src/areas/workflows/nodes/mesh-exporter/processor.ts b/src/areas/workflows/nodes/mesh-exporter/processor.ts index 19d819f7..5a91fc34 100644 --- a/src/areas/workflows/nodes/mesh-exporter/processor.ts +++ b/src/areas/workflows/nodes/mesh-exporter/processor.ts @@ -38,6 +38,27 @@ function extractPrimitives(doc: any): PrimGeometry[] { return result } +// ─── Real-world scale ────────────────────────────────────────────────────── + +/** STL, OBJ and PLY carry no unit: by convention they hold millimetres. */ +const MM_PER_M = 1000 + +/** + * Whether the GLB declares its real size: Modly's set_real_scale writes + * `scenes[n].extras.modly.real_scale = { unit: 'm' }` (api/services/mesh_ops/real_scale.py). + */ +function isCalibrated(doc: any): boolean { + const scene = doc.getRoot().getDefaultScene?.() ?? doc.getRoot().listScenes?.()[0] + const extras = scene?.getExtras?.() as { modly?: { real_scale?: { unit?: unknown } } } | undefined + return extras?.modly?.real_scale?.unit === 'm' +} + +/** A calibrated asset is written in millimetres, like the API's exports. */ +function scalePrimitives(prims: PrimGeometry[], factor: number): PrimGeometry[] { + if (factor === 1) return prims + return prims.map((p) => ({ ...p, positions: p.positions.map((v) => v * factor) })) +} + // ─── STL (binary) ───────────────────────────────────────────────────────────── function faceNormal(p: Float32Array, i0: number, i1: number, i2: number): [number, number, number] { @@ -225,8 +246,10 @@ const processor = async ( if (format === 'glb') { await io.write(outPath, doc) } else { - const prims = extractPrimitives(doc) + const calibrated = isCalibrated(doc) + const prims = scalePrimitives(extractPrimitives(doc), calibrated ? MM_PER_M : 1) if (prims.length === 0) throw new Error('mesh-exporter: no mesh data found in input') + if (calibrated) context.log('Real-size asset: written in millimetres') if (format === 'stl') writeSTL(prims, outPath) else if (format === 'obj') writeOBJ(prims, outPath) else if (format === 'ply') writePLY(prims, outPath) diff --git a/src/shared/components/agent/clientTools.test.ts b/src/shared/components/agent/clientTools.test.ts index ef6754bb..53384f13 100644 --- a/src/shared/components/agent/clientTools.test.ts +++ b/src/shared/components/agent/clientTools.test.ts @@ -339,7 +339,7 @@ test('navigate changes page and validates it', async () => { const f = fake() assert.equal(await run('navigate', { page: 'models' }, f), 'Opened the Extensions page.') assert.deepEqual(f.calls, [['navigate', 'models']]) - assert.match(await run('navigate', { page: 'nope' }, f), /^Error: page must be one of: home, generate, image-studio, workflows, library, queue, models, settings/) + assert.match(await run('navigate', { page: 'nope' }, f), /^Error: page must be one of: home, generate, image-studio, workflows, print, library, queue, models, settings/) }) test('a throwing dependency comes back as an error text', async () => { diff --git a/src/shared/pages/pageRegistry.test.ts b/src/shared/pages/pageRegistry.test.ts index 5b970af3..66eda1b5 100644 --- a/src/shared/pages/pageRegistry.test.ts +++ b/src/shared/pages/pageRegistry.test.ts @@ -22,7 +22,7 @@ test('the pages keep their ids, labels and sidebar order', () => { pagesByGroup().map((s) => [s.group, s.pages.map((p) => `${p.id}:${p.label}`)]), [ ['home', ['home:Home']], - ['create', ['generate:Generate', 'image-studio:Image Studio', 'workflows:Workflows']], + ['create', ['generate:Generate', 'image-studio:Image Studio', 'workflows:Workflows', 'print:Print']], ['manage', ['library:Library', 'queue:Queue']], ['system', ['models:Extensions', 'settings:Settings']], ], diff --git a/src/shared/pages/pageRegistry.ts b/src/shared/pages/pageRegistry.ts index 726a6f99..b8154240 100644 --- a/src/shared/pages/pageRegistry.ts +++ b/src/shared/pages/pageRegistry.ts @@ -75,6 +75,18 @@ export const PAGES = [ load: () => import('@areas/workflows/WorkflowsPage'), wrapperClass: 'flex flex-1 overflow-hidden', }, + { + id: 'print', + label: 'Print', + group: 'create', + icon: [ + ['path', { d: 'M6 9V3h12v6' }], + ['rect', { x: 3, y: 9, width: 18, height: 8, rx: 1.5 }], + ['rect', { x: 7, y: 14, width: 10, height: 7, rx: 1 }], + ], + load: () => import('@areas/print/PrintPage'), + wrapperClass: 'flex flex-1 overflow-hidden', + }, { id: 'library', label: 'Library', diff --git a/src/shared/three/meshScene.ts b/src/shared/three/meshScene.ts new file mode 100644 index 00000000..0a719460 --- /dev/null +++ b/src/shared/three/meshScene.ts @@ -0,0 +1,32 @@ +import * as THREE from 'three' + +// Helpers shared by the viewers that show a loaded mesh file (Generate, Print). + +/** Free a loaded model's GPU memory: every mesh's geometry and materials. */ +export function disposeObject(object: THREE.Object3D): void { + object.traverse((child) => { + if (child instanceof THREE.Mesh) { + child.geometry.dispose() + const materials = Array.isArray(child.material) ? child.material : [child.material] + materials.forEach((m: THREE.Material) => m.dispose()) + } + }) +} + +/** + * Clear any pose left on a loaded scene (the loader cache may hand back one an + * earlier viewer moved), then measure it. `offset` centres it on the origin + * and rests its lowest point on the ground (y = 0). + */ +export function groundPlacement(scene: THREE.Object3D): { offset: THREE.Vector3, size: THREE.Vector3 } { + scene.position.set(0, 0, 0) + scene.rotation.set(0, 0, 0) + scene.scale.set(1, 1, 1) + scene.updateMatrixWorld(true) + const box = new THREE.Box3().setFromObject(scene) + const center = box.getCenter(new THREE.Vector3()) + return { + offset: new THREE.Vector3(-center.x, -box.min.y, -center.z), + size: box.getSize(new THREE.Vector3()), + } +} diff --git a/src/shared/three/viewScale.test.ts b/src/shared/three/viewScale.test.ts new file mode 100644 index 00000000..bbb2bba9 --- /dev/null +++ b/src/shared/three/viewScale.test.ts @@ -0,0 +1,22 @@ +import test from 'node:test' +import assert from 'node:assert/strict' +import { viewScaleFor } from './viewScale' + +test('a generated, unit-sized mesh keeps the default view', () => { + assert.equal(viewScaleFor(1), 1) + assert.equal(viewScaleFor(2), 1) + assert.equal(viewScaleFor(0.3), 1) +}) + +test('an imported 30 mm part (0.03 m) is framed instead of being a speck', () => { + assert.equal(viewScaleFor(0.03), 0.015) +}) + +test('a very large model is framed from further away', () => { + assert.equal(viewScaleFor(180), 90) +}) + +test('an empty or broken size keeps the default view', () => { + assert.equal(viewScaleFor(0), 1) + assert.equal(viewScaleFor(Number.NaN), 1) +}) diff --git a/src/shared/three/viewScale.ts b/src/shared/three/viewScale.ts new file mode 100644 index 00000000..5aede971 --- /dev/null +++ b/src/shared/three/viewScale.ts @@ -0,0 +1,17 @@ +// How far the Generate viewer's camera, grid and zoom limits are scaled for a +// model. Generated meshes are about a unit across and keep the default view; +// a model at its real size can be a few centimetres (an imported STL, a +// calibrated asset) or many metres, and would otherwise be a speck or swallow +// the camera. + +/** Longest sides the default view frames well, in scene units. */ +export const DEFAULT_VIEW_RANGE = { min: 0.25, max: 10 } as const + +/** The longest side the default view is built around. */ +const DEFAULT_VIEW_SIZE = 2 + +export function viewScaleFor(longestSide: number): number { + if (!Number.isFinite(longestSide) || longestSide <= 0) return 1 + if (longestSide >= DEFAULT_VIEW_RANGE.min && longestSide <= DEFAULT_VIEW_RANGE.max) return 1 + return longestSide / DEFAULT_VIEW_SIZE +}