From a7e2a799969a15fc4905613ec05ffb89cb1455d4 Mon Sep 17 00:00:00 2001 From: Lightning Pixel Date: Tue, 17 Nov 2026 17:44:39 +0100 Subject: [PATCH 1/8] feat(mesh-ops): declare an asset's real-world scale with set_real_scale "This object is 30 cm": the new set_real_scale op rescales the mesh with normalize_scale and marks the GLB as calibrated, in the default scene's extras (modly.real_scale = {unit: "m"}). The declaration travels with the file, and the registry carries it over to the output of any later op that rebuilds the mesh. Exports use it: a calibrated asset's STL is written in millimetres, and the slicer hand-off sends its real size instead of the 50 mm default. Closes #323 --- api/routers/export.py | 11 +- api/routers/optimize.py | 4 + api/services/mesh_ops/builtin.py | 61 ++++++++++ api/services/mesh_ops/real_scale.py | 129 ++++++++++++++++++++ api/services/mesh_ops/registry.py | 5 +- api/tests/test_mesh_ops_real_scale.py | 164 ++++++++++++++++++++++++++ api/tests/test_mesh_ops_registry.py | 1 + 7 files changed, 373 insertions(+), 2 deletions(-) create mode 100644 api/services/mesh_ops/real_scale.py create mode 100644 api/tests/test_mesh_ops_real_scale.py diff --git a/api/routers/export.py b/api/routers/export.py index 854d8bf21..c7d34a213 100644 --- a/api/routers/export.py +++ b/api/routers/export.py @@ -9,6 +9,7 @@ from fastapi.responses import Response, FileResponse from services import imported_sources +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 @@ -164,7 +165,12 @@ 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) + # A calibrated asset is in metres and slicers read millimetres; only an + # uncalibrated one needs the unit-size guess. + if is_calibrated(full_path): + mesh.apply_scale(MM_PER_M) + else: + _normalize_print_scale(mesh) data = mesh.export(file_type=fmt) if isinstance(data, str): @@ -200,6 +206,9 @@ def export_mesh(fmt: str, path: str): mesh = trimesh.util.concatenate(geoms) if len(geoms) > 1 else geoms[0] else: mesh = loaded + 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 c6d70a1c0..7b6fadb55 100644 --- a/api/routers/optimize.py +++ b/api/routers/optimize.py @@ -26,6 +26,7 @@ mesh_ops_registry, ) from services.mesh_ops.glb_patch import GlbPatchError, VertexPatch, patch_glb_file +from services.mesh_ops.real_scale import export_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 @@ -537,6 +538,9 @@ def export_mesh(path: str, format: str): mesh = trimesh.util.concatenate(geoms) if len(geoms) > 1 else geoms[0] else: mesh = loaded + 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/mesh_ops/builtin.py b/api/services/mesh_ops/builtin.py index 68bc1530d..4775254bb 100644 --- a/api/services/mesh_ops/builtin.py +++ b/api/services/mesh_ops/builtin.py @@ -4,6 +4,7 @@ from .inspection import inspect_mesh, suggest_fixes from .operations import decimate_mesh, repair_mesh, smooth_mesh from .presets import prepare_for_game, prepare_for_printing, prepare_for_web +from .real_scale import set_real_scale from .repair import auto_repair_mesh from .transforms import auto_orient, drop_to_ground, normalize_scale, recenter from .types import AgentTool, MeshOp @@ -163,6 +164,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 +395,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/real_scale.py b/api/services/mesh_ops/real_scale.py new file mode 100644 index 000000000..3d950c300 --- /dev/null +++ b/api/services/mesh_ops/real_scale.py @@ -0,0 +1,129 @@ +"""Real-world scale: declare how big an asset really is, and remember it. + +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. +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). +""" + +from pathlib import Path +from typing import Any, Mapping, Optional + +from .glb_patch import GlbPatchError, _build_glb, _parse_glb +from .operations import _output_path +from .presets import DIMENSION_NAMES +from .transforms import apply_transforms +from .types import MeshOpContext, MeshOpResult + + +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 + + +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_real_scale(path: Path) -> 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, _ = _parse_glb(path.read_bytes()) + except (GlbPatchError, OSError): + return None + scene = _default_scene(gltf) + 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 + 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 + + +def export_scale(path: Path, fmt: str) -> float: + """Factor from the GLB's units to an export format's units. + + A calibrated asset's STL is written in millimetres; everything else keeps + the file's own units (metres for a calibrated GLB, arbitrary otherwise). + """ + return MM_PER_M if fmt.lower() == "stl" and is_calibrated(path) else 1.0 + + +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.""" + output_path = _output_path(context, "set-real-scale") + # normalize_scale already turns "size in a unit along a dimension" into + # metres; the declaration is what makes the result trustworthy downstream. + result = apply_transforms(input_path, [("normalize_scale", params)], output_path, context) + write_real_scale(output_path) + 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=output_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/services/mesh_ops/registry.py b/api/services/mesh_ops/registry.py index 614b54bf4..be29ed6e0 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/tests/test_mesh_ops_real_scale.py b/api/tests/test_mesh_ops_real_scale.py new file mode 100644 index 000000000..dd95af9b0 --- /dev/null +++ b/api/tests/test_mesh_ops_real_scale.py @@ -0,0 +1,164 @@ +import base64 +import io +import tempfile +import unittest +from pathlib import Path + +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.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_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) + + +if __name__ == "__main__": + unittest.main() diff --git a/api/tests/test_mesh_ops_registry.py b/api/tests/test_mesh_ops_registry.py index bc2e57334..ce014b93d 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", From 47f88a9164c4bf941d61c7ae95338d10a4c86f25 Mon Sep 17 00:00:00 2001 From: Lightning Pixel Date: Tue, 17 Nov 2026 17:53:32 +0100 Subject: [PATCH 2/8] feat(print): add a Print page with a build-plate view A new Print page (Create, after Workflows) shows a model on a printer's build plate at its real size: - the model is Generate's current mesh or any mesh from the Library - printer presets (Bambu Lab, Prusa, Creality, Voron) or a custom volume, remembered between sessions; the default is 256 x 256 x 256 mm - the model is read in metres (glTF), drawn in millimetres, centred on the plate and resting on it; its size is shown in mm - a model larger than the build volume is flagged, side by side, and the volume outline turns red - a model without a declared real size (set_real_scale) says so and points to Set Real Size Closes #425 --- package.json | 2 +- src/areas/print/PrintPage.tsx | 379 ++++++++++++++++++ src/areas/print/printBed.test.ts | 79 ++++ src/areas/print/printBed.ts | 135 +++++++ src/areas/print/printStore.ts | 17 + .../components/agent/clientTools.test.ts | 2 +- src/shared/pages/pageRegistry.test.ts | 2 +- src/shared/pages/pageRegistry.ts | 12 + 8 files changed, 625 insertions(+), 3 deletions(-) create mode 100644 src/areas/print/PrintPage.tsx create mode 100644 src/areas/print/printBed.test.ts create mode 100644 src/areas/print/printBed.ts create mode 100644 src/areas/print/printStore.ts diff --git a/package.json b/package.json index 239b30074..00096849c 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 && 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/print/PrintPage.tsx b/src/areas/print/PrintPage.tsx new file mode 100644 index 000000000..ecbd4c865 --- /dev/null +++ b/src/areas/print/PrintPage.tsx @@ -0,0 +1,379 @@ +import { Component, Suspense, 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, + describeOverflow, + isGlbUrl, + formatVolume, + isCalibrated, + modelSizeMm, + parsePrinterChoice, + resolveBuildVolume, + type BuildVolume, + type PrinterChoice, +} from './printBed' +import { usePrintStore } from './printStore' + +const PRINTER_STORAGE_KEY = 'modly.print.printer' +const MM_PER_M = 1000 + +function readStoredPrinter(): PrinterChoice { + try { return parsePrinterChoice(localStorage.getItem(PRINTER_STORAGE_KEY)) } catch { return parsePrinterChoice(null) } +} + +interface Measured { + /** Size in millimetres, glTF units read as metres. */ + size: BuildVolume + calibrated: boolean +} + +// ─── 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 real size, centred on the plate and resting on it. */ +function PlacedModel({ url, onMeasured }: { url: string, onMeasured: (measured: Measured) => void }): JSX.Element { + const { scene } = useGLTF(url) + + const offset = useMemo(() => { + 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()) + const size = box.getSize(new THREE.Vector3()) + return { center, minY: box.min.y, size } + }, [scene]) + + useEffect(() => { + onMeasured({ size: modelSizeMm(offset.size), calibrated: isCalibrated(scene.userData) }) + }, [offset, scene, onMeasured]) + + // Free the loader cache and GPU memory when the model changes, as Generate does. + useEffect(() => () => { + 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()) + } + }) + }, [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, fits, onMeasured, onError }: { + volume: BuildVolume + modelUrl: string | null + fits: boolean + onMeasured: (measured: Measured) => void + onError: () => void +}): JSX.Element { + const span = Math.max(volume.width, volume.depth, volume.height) + return ( + + + + + + + + + + + + {modelUrl && ( + + + + + + )} + + + + ) +} + +// ─── 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([]) + const [measured, setMeasured] = useState(null) + const [loadError, setLoadError] = useState(null) + + 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 */ } + } + + async function chooseSource(value: string) { + setLoadError(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 (!result.success) { setLoadError(result.message); return } + setSource({ kind: 'library', id: entry.id, name: entry.displayName, url: result.selection.job.outputUrl ?? '' }) + } + + // 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 + + useEffect(() => { setMeasured(null) }, [modelUrl]) + + const volume = resolveBuildVolume(printer) + const fit = measured ? checkFit(measured.size, volume) : null + + return ( +
+ + +
+ setLoadError('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 000000000..ec9900a8e --- /dev/null +++ b/src/areas/print/printBed.test.ts @@ -0,0 +1,79 @@ +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, + describeOverflow, + formatVolume, + isCalibrated, + isGlbUrl, + modelSizeMm, + 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('the calibration marker is read from the scene userData', () => { + assert.ok(isCalibrated({ modly: { real_scale: { unit: 'm' } } })) + assert.ok(!isCalibrated({})) + assert.ok(!isCalibrated(null)) + assert.ok(!isCalibrated({ modly: { real_scale: { unit: 'cm' } } })) +}) + +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('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 000000000..be4602fd9 --- /dev/null +++ b/src/areas/print/printBed.ts @@ -0,0 +1,135 @@ +// 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 +} + +/** + * Whether a loaded GLB declares its real size (T-H1): the backend's + * set_real_scale op writes `scenes[n].extras.modly.real_scale = { unit: 'm' }`, + * which three's GLTFLoader puts on the scene's userData. + */ +export function isCalibrated(userData: unknown): boolean { + const modly = (userData as { modly?: { real_scale?: { unit?: unknown } } } | null)?.modly + return modly?.real_scale?.unit === 'm' +} + +/** 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) +} + +const MM_PER_M = 1000 + +/** A model's size in millimetres, from its glTF bounding box (metres, Y up). */ +export function modelSizeMm(size: { x: number, y: number, z: number }): BuildVolume { + return { width: size.x * MM_PER_M, depth: size.z * MM_PER_M, height: size.y * MM_PER_M } +} + +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 000000000..249203d1b --- /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/shared/components/agent/clientTools.test.ts b/src/shared/components/agent/clientTools.test.ts index ef6754bb9..53384f13f 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 5b970af37..66eda1b55 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 726a6f998..b8154240a 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', From a0a643fa532973b4a53ba4374f863db6f0488c36 Mon Sep 17 00:00:00 2001 From: Lightning Pixel Date: Tue, 17 Nov 2026 18:14:08 +0100 Subject: [PATCH 3/8] fix(mesh-ops): keep the declared real size through ops and exports trimesh's Scene.apply_transform only moves the scene's base frame, so set_real_scale stored the new size in a node matrix and left the vertices as they were. Readers that join the geometry without the nodes then saw the old size: a 2 x 1 x 0.5 mesh declared as 30 cm exported as a 2000 mm STL, and repair and smooth wrote it back at its original size, still marked calibrated. - The transform ops bake the node transforms into the vertices, so the stored geometry is what the scene shows. - The STL/OBJ/PLY exports and the repair and smooth ops read the mesh in world space, whatever GLB they get. - normalize_scale, and so prepare_for_printing and prepare_for_game, declare the real size too: the slicer no longer resizes a print prepared at 100 mm to the 50 mm default. - read_real_scale reads only the GLB header and JSON chunk. Each case has a test that runs the op end to end on a real file. --- api/routers/export.py | 13 ++-- api/routers/optimize.py | 12 +-- api/services/mesh_ops/builtin.py | 3 +- api/services/mesh_ops/geometry.py | 47 ++++++++++++ api/services/mesh_ops/operations.py | 12 +-- api/services/mesh_ops/presets.py | 5 +- api/services/mesh_ops/real_scale.py | 65 ++++++++-------- api/services/mesh_ops/transforms.py | 40 +++++++++- api/tests/test_mesh_ops_real_scale.py | 106 ++++++++++++++++++++++++++ 9 files changed, 238 insertions(+), 65 deletions(-) diff --git a/api/routers/export.py b/api/routers/export.py index c7d34a213..a2c4fc23b 100644 --- a/api/routers/export.py +++ b/api/routers/export.py @@ -9,6 +9,7 @@ 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. @@ -199,13 +200,11 @@ 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) diff --git a/api/routers/optimize.py b/api/routers/optimize.py index 7b6fadb55..faa44d4d3 100644 --- a/api/routers/optimize.py +++ b/api/routers/optimize.py @@ -26,6 +26,7 @@ mesh_ops_registry, ) from services.mesh_ops.glb_patch import GlbPatchError, VertexPatch, patch_glb_file +from services.mesh_ops.geometry import single_mesh from services.mesh_ops.real_scale import export_scale from services.mesh_ops.transforms import apply_transforms, transform_file from schemas.runs import RunRecord @@ -532,12 +533,11 @@ 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) diff --git a/api/services/mesh_ops/builtin.py b/api/services/mesh_ops/builtin.py index 4775254bb..ab064003a 100644 --- a/api/services/mesh_ops/builtin.py +++ b/api/services/mesh_ops/builtin.py @@ -4,9 +4,8 @@ from .inspection import inspect_mesh, suggest_fixes from .operations import decimate_mesh, repair_mesh, smooth_mesh from .presets import prepare_for_game, prepare_for_printing, prepare_for_web -from .real_scale import set_real_scale 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 diff --git a/api/services/mesh_ops/geometry.py b/api/services/mesh_ops/geometry.py index 3fa3c60f3..95a53b002 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 7fae38459..96651c58a 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 13d3cfbf3..f08c28819 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 index 3d950c300..b8211f53a 100644 --- a/api/services/mesh_ops/real_scale.py +++ b/api/services/mesh_ops/real_scale.py @@ -1,8 +1,9 @@ -"""Real-world scale: declare how big an asset really is, and remember it. +"""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. -That declaration is stored in the file itself, in the default scene's extras: +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"} @@ -11,14 +12,12 @@ 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 -from .operations import _output_path -from .presets import DIMENSION_NAMES -from .transforms import apply_transforms -from .types import MeshOpContext, MeshOpResult EXTRAS_KEY = "modly" @@ -27,6 +26,9 @@ # 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") @@ -39,16 +41,32 @@ def _default_scene(gltf: dict) -> Optional[dict]: 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, _ = _parse_glb(path.read_bytes()) - except (GlbPatchError, OSError): + gltf = _read_gltf_json(path) + except (OSError, UnicodeDecodeError, ValueError): return None - scene = _default_scene(gltf) + 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 @@ -86,6 +104,8 @@ def carry_real_scale(input_path: Path, output_path: Optional[Path]) -> None: """ 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 @@ -102,28 +122,3 @@ def export_scale(path: Path, fmt: str) -> float: the file's own units (metres for a calibrated GLB, arbitrary otherwise). """ return MM_PER_M if fmt.lower() == "stl" and is_calibrated(path) else 1.0 - - -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.""" - output_path = _output_path(context, "set-real-scale") - # normalize_scale already turns "size in a unit along a dimension" into - # metres; the declaration is what makes the result trustworthy downstream. - result = apply_transforms(input_path, [("normalize_scale", params)], output_path, context) - write_real_scale(output_path) - 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=output_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/services/mesh_ops/transforms.py b/api/services/mesh_ops/transforms.py index 12266f115..3f5ecd36e 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, @@ -16,6 +17,7 @@ # glTF's unit is the metre. UNIT_SCALE = {"m": 1.0, "cm": 0.01, "mm": 0.001} 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 +118,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 +156,13 @@ 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 unit is a statement of the real size. + if "normalize_scale" in requested 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 +201,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 index dd95af9b0..bbb5df53f 100644 --- a/api/tests/test_mesh_ops_real_scale.py +++ b/api/tests/test_mesh_ops_real_scale.py @@ -3,6 +3,7 @@ import tempfile import unittest from pathlib import Path +from unittest import mock import numpy as np import trimesh @@ -10,6 +11,7 @@ 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.mesh_ops import real_scale from services.mesh_ops.real_scale import ( is_calibrated, read_real_scale, @@ -113,6 +115,26 @@ def test_files_that_are_not_glb_are_never_calibrated(self) -> None: 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"] @@ -160,5 +182,89 @@ def test_the_slicer_gets_the_real_size_instead_of_the_default(self) -> None: 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_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) + + if __name__ == "__main__": unittest.main() From 16f9cb00c8de500f87f2a0a51392d3700a2ac93d Mon Sep 17 00:00:00 2001 From: Lightning Pixel Date: Tue, 17 Nov 2026 18:14:08 +0100 Subject: [PATCH 4/8] fix(print): drop stale load errors and ignore outdated model choices - A load error belongs to the model it was about: it is no longer shown once another model is on the plate. - A Library asset that finishes opening after a newer choice is ignored, instead of replacing it. - MM_PER_M comes from printBed, and the Print page shares mesh disposal and ground placement with the Generate viewer (shared/three/meshScene). --- src/areas/generate/components/Viewer3D.tsx | 19 ++----- src/areas/print/PrintPage.tsx | 58 ++++++++++------------ src/areas/print/printBed.test.ts | 23 +++++++++ src/areas/print/printBed.ts | 23 ++++++++- src/shared/three/meshScene.ts | 32 ++++++++++++ 5 files changed, 107 insertions(+), 48 deletions(-) create mode 100644 src/shared/three/meshScene.ts diff --git a/src/areas/generate/components/Viewer3D.tsx b/src/areas/generate/components/Viewer3D.tsx index 71d4a3219..8ee6c36a4 100644 --- a/src/areas/generate/components/Viewer3D.tsx +++ b/src/areas/generate/components/Viewer3D.tsx @@ -15,6 +15,7 @@ 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 { ViewerToolbar, type ViewMode } from './ViewerToolbar' import type { LightSettings, PointLight } from '@shared/stores/appStore' import { DEFAULT_LIGHT_SETTINGS } from '@shared/stores/appStore' @@ -256,13 +257,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 +284,9 @@ 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) + scene.position.copy(groundPlacement(scene).offset) // Compute stats let vertices = 0 diff --git a/src/areas/print/PrintPage.tsx b/src/areas/print/PrintPage.tsx index ecbd4c865..abda8081f 100644 --- a/src/areas/print/PrintPage.tsx +++ b/src/areas/print/PrintPage.tsx @@ -17,9 +17,12 @@ import { CUSTOM_PRINTER_ID, MAX_VOLUME_MM, MIN_VOLUME_MM, + MM_PER_M, PRINTER_PRESETS, checkFit, + createLatestGuard, describeOverflow, + errorForModel, isGlbUrl, formatVolume, isCalibrated, @@ -27,12 +30,13 @@ import { parsePrinterChoice, resolveBuildVolume, type BuildVolume, + type ModelError, type PrinterChoice, } from './printBed' import { usePrintStore } from './printStore' +import { disposeObject, groundPlacement } from '@shared/three/meshScene' const PRINTER_STORAGE_KEY = 'modly.print.printer' -const MM_PER_M = 1000 function readStoredPrinter(): PrinterChoice { try { return parsePrinterChoice(localStorage.getItem(PRINTER_STORAGE_KEY)) } catch { return parsePrinterChoice(null) } @@ -104,36 +108,21 @@ function BuildPlate({ volume, fits }: { volume: BuildVolume, fits: boolean }): J function PlacedModel({ url, onMeasured }: { url: string, onMeasured: (measured: Measured) => void }): JSX.Element { const { scene } = useGLTF(url) - const offset = useMemo(() => { - 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()) - const size = box.getSize(new THREE.Vector3()) - return { center, minY: box.min.y, size } - }, [scene]) + const placement = useMemo(() => groundPlacement(scene), [scene]) useEffect(() => { - onMeasured({ size: modelSizeMm(offset.size), calibrated: isCalibrated(scene.userData) }) - }, [offset, scene, onMeasured]) + onMeasured({ size: modelSizeMm(placement.size), calibrated: isCalibrated(scene.userData) }) + }, [placement, scene, onMeasured]) // Free the loader cache and GPU memory when the model changes, as Generate does. useEffect(() => () => { 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) }, [url, scene]) return ( - + @@ -238,7 +227,9 @@ export default function PrintPage(): JSX.Element { const [printer, setPrinter] = useState(readStoredPrinter) const [assets, setAssets] = useState([]) const [measured, setMeasured] = useState(null) - const [loadError, setLoadError] = useState(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 @@ -257,23 +248,26 @@ export default function PrintPage(): JSX.Element { 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) { - setLoadError(null) + 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 (!result.success) { setLoadError(result.message); return } + 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 ?? '' }) } - // 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 - useEffect(() => { setMeasured(null) }, [modelUrl]) const volume = resolveBuildVolume(printer) @@ -359,7 +353,7 @@ export default function PrintPage(): JSX.Element { modelUrl={modelUrl} fits={fit?.fits ?? true} onMeasured={setMeasured} - onError={() => setLoadError('The model could not be loaded.')} + onError={() => setError({ modelUrl, message: 'The model could not be loaded.' })} /> {!modelUrl && (
diff --git a/src/areas/print/printBed.test.ts b/src/areas/print/printBed.test.ts index ec9900a8e..93cf47eff 100644 --- a/src/areas/print/printBed.test.ts +++ b/src/areas/print/printBed.test.ts @@ -6,7 +6,9 @@ import { MAX_VOLUME_MM, MIN_VOLUME_MM, checkFit, + createLatestGuard, describeOverflow, + errorForModel, formatVolume, isCalibrated, isGlbUrl, @@ -71,6 +73,27 @@ test('presets and formatting', () => { 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')) diff --git a/src/areas/print/printBed.ts b/src/areas/print/printBed.ts index be4602fd9..54363a663 100644 --- a/src/areas/print/printBed.ts +++ b/src/areas/print/printBed.ts @@ -93,7 +93,28 @@ export function isGlbUrl(url: string): boolean { return /\.(glb|gltf)$/i.test(path) } -const MM_PER_M = 1000 +/** 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 size in millimetres, from its glTF bounding box (metres, Y up). */ export function modelSizeMm(size: { x: number, y: number, z: number }): BuildVolume { diff --git a/src/shared/three/meshScene.ts b/src/shared/three/meshScene.ts new file mode 100644 index 000000000..0a719460f --- /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()), + } +} From d163c8464c717edf99af6276acef26dade67194a Mon Sep 17 00:00:00 2001 From: Lightning Pixel Date: Tue, 17 Nov 2026 18:32:53 +0100 Subject: [PATCH 5/8] fix(api): agree on real sizes across imports, exports and normalize_scale - GET /export/print-scale/{token} says how the slicer hand-off sizes a mesh (millimetres per unit, calibrated or not). print_mm_per_unit is the one rule behind both: the slicer route uses it, and the Print page will read it instead of guessing. - STL, OBJ and PLY imports are millimetres and Z-up by convention: they are now converted to a glTF in metres and Y-up, marked at real size, and registered as glTF so the slicer route rotates them back. - A calibrated asset's OBJ and PLY exports are in millimetres too, like STL and the slicer route. Uncalibrated assets do not change. - normalize_scale marks the mesh as real-sized only for a real unit. Its unit defaults to the new "units" (model units, no real size), so the Edit panel's default and an agent call without a unit no longer mark it; prepare_for_printing (mm) and prepare_for_game (m) still do. --- api/routers/agent.py | 5 +- api/routers/export.py | 46 +++++++---- api/routers/optimize.py | 37 ++++++--- api/services/imported_sources.py | 8 +- api/services/mesh_ops/builtin.py | 8 +- api/services/mesh_ops/real_scale.py | 11 ++- api/services/mesh_ops/transforms.py | 9 ++- api/tests/test_mesh_ops_real_scale.py | 110 ++++++++++++++++++++++++++ 8 files changed, 196 insertions(+), 38 deletions(-) diff --git a/api/routers/agent.py b/api/routers/agent.py index e8b1fd8b0..711eeb5fd 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 a2c4fc23b..def92cfb4 100644 --- a/api/routers/export.py +++ b/api/routers/export.py @@ -79,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]: @@ -135,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 @@ -166,12 +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])) - # A calibrated asset is in metres and slicers read millimetres; only an - # uncalibrated one needs the unit-size guess. - if is_calibrated(full_path): - mesh.apply_scale(MM_PER_M) - else: - _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): diff --git a/api/routers/optimize.py b/api/routers/optimize.py index faa44d4d3..573bc8719 100644 --- a/api/routers/optimize.py +++ b/api/routers/optimize.py @@ -26,8 +26,8 @@ mesh_ops_registry, ) from services.mesh_ops.glb_patch import GlbPatchError, VertexPatch, patch_glb_file -from services.mesh_ops.geometry import single_mesh -from services.mesh_ops.real_scale import export_scale +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 @@ -467,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 = { diff --git a/api/services/imported_sources.py b/api/services/imported_sources.py index a21dded52..3cddef295 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 ab064003a..325339342 100644 --- a/api/services/mesh_ops/builtin.py +++ b/api/services/mesh_ops/builtin.py @@ -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", diff --git a/api/services/mesh_ops/real_scale.py b/api/services/mesh_ops/real_scale.py index b8211f53a..c0989f298 100644 --- a/api/services/mesh_ops/real_scale.py +++ b/api/services/mesh_ops/real_scale.py @@ -115,10 +115,15 @@ def carry_real_scale(input_path: Path, output_path: Optional[Path]) -> None: 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's STL is written in millimetres; everything else keeps - the file's own units (metres for a calibrated GLB, arbitrary otherwise). + 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() == "stl" and is_calibrated(path) else 1.0 + return MM_PER_M if fmt.lower() in MM_FORMATS and is_calibrated(path) else 1.0 diff --git a/api/services/mesh_ops/transforms.py b/api/services/mesh_ops/transforms.py index 3f5ecd36e..a138344d0 100644 --- a/api/services/mesh_ops/transforms.py +++ b/api/services/mesh_ops/transforms.py @@ -15,7 +15,9 @@ # 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: @@ -160,8 +162,9 @@ def apply_transforms( # vertices so every reader sees the transformed geometry. loaded = bake_transforms(loaded, trimesh) loaded.export(str(output_path)) - # Scaling to a size in a unit is a statement of the real size. - if "normalize_scale" in requested and output_path.suffix.lower() == ".glb": + # 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) diff --git a/api/tests/test_mesh_ops_real_scale.py b/api/tests/test_mesh_ops_real_scale.py index bbb5df53f..a3a1b92b3 100644 --- a/api/tests/test_mesh_ops_real_scale.py +++ b/api/tests/test_mesh_ops_real_scale.py @@ -1,9 +1,11 @@ +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 @@ -11,6 +13,7 @@ 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, @@ -259,6 +262,20 @@ def test_normalize_scale_declares_a_real_size(self) -> None: 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) @@ -266,5 +283,98 @@ def test_prepare_for_printing_reaches_the_slicer_at_the_requested_size(self) -> 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() From 51b2412261debbdef99f32223603fe3b1aa3dfae Mon Sep 17 00:00:00 2001 From: Lightning Pixel Date: Tue, 17 Nov 2026 18:32:53 +0100 Subject: [PATCH 6/8] feat(print): show uncalibrated models at the slicer's provisional size The Print page asks the backend how the slicer hand-off would size the model (GET /export/print-scale) and shows it at exactly that size, so the plate and OrcaSlicer never disagree: about 50 mm for unit-sized AI output, its real size once calibrated. The fit check uses the same size, and a "Provisional size" banner says how to set a real one. --- src/areas/generate/orcaSlicerLink.test.ts | 9 +++ src/areas/generate/orcaSlicerLink.ts | 14 +++- src/areas/print/PrintPage.tsx | 83 ++++++++++++++++------- src/areas/print/printBed.test.ts | 24 +++++-- src/areas/print/printBed.ts | 36 ++++++---- 5 files changed, 121 insertions(+), 45 deletions(-) diff --git a/src/areas/generate/orcaSlicerLink.test.ts b/src/areas/generate/orcaSlicerLink.test.ts index 514618eb2..813b7401d 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 d5a76c9b1..0764ff95f 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 index abda8081f..e9039140c 100644 --- a/src/areas/print/PrintPage.tsx +++ b/src/areas/print/PrintPage.tsx @@ -1,4 +1,4 @@ -import { Component, Suspense, useEffect, useMemo, useState } from 'react' +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' @@ -17,7 +17,6 @@ import { CUSTOM_PRINTER_ID, MAX_VOLUME_MM, MIN_VOLUME_MM, - MM_PER_M, PRINTER_PRESETS, checkFit, createLatestGuard, @@ -25,14 +24,16 @@ import { errorForModel, isGlbUrl, formatVolume, - isCalibrated, 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' @@ -42,11 +43,8 @@ function readStoredPrinter(): PrinterChoice { try { return parsePrinterChoice(localStorage.getItem(PRINTER_STORAGE_KEY)) } catch { return parsePrinterChoice(null) } } -interface Measured { - /** Size in millimetres, glTF units read as metres. */ - size: BuildVolume - calibrated: boolean -} +/** A model's bounding-box size, in its own (glTF) units. */ +type Size3 = { x: number, y: number, z: number } // ─── 3D scene (millimetres) ────────────────────────────────────────────────── @@ -104,15 +102,20 @@ function BuildPlate({ volume, fits }: { volume: BuildVolume, fits: boolean }): J ) } -/** The model at its real size, centred on the plate and resting on it. */ -function PlacedModel({ url, onMeasured }: { url: string, onMeasured: (measured: Measured) => void }): JSX.Element { +/** 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(() => { - onMeasured({ size: modelSizeMm(placement.size), calibrated: isCalibrated(scene.userData) }) - }, [placement, scene, onMeasured]) + 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(() => () => { @@ -121,7 +124,7 @@ function PlacedModel({ url, onMeasured }: { url: string, onMeasured: (measured: }, [url, scene]) return ( - + @@ -142,11 +145,13 @@ class ModelBoundary extends Component<{ children: ReactNode, resetKey: string, o render(): ReactNode { return this.state.failed ? null : this.props.children } } -function PlateScene({ volume, modelUrl, fits, onMeasured, onError }: { +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 - onMeasured: (measured: Measured) => void + onSize: (size: Size3) => void onError: () => void }): JSX.Element { const span = Math.max(volume.width, volume.depth, volume.height) @@ -166,10 +171,10 @@ function PlateScene({ volume, modelUrl, fits, onMeasured, onError }: { - {modelUrl && ( + {modelUrl && mmPerUnit !== null && ( - + )} @@ -226,7 +231,9 @@ export default function PrintPage(): JSX.Element { const [printer, setPrinter] = useState(readStoredPrinter) const [assets, setAssets] = useState([]) - const [measured, setMeasured] = useState(null) + // 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) @@ -268,7 +275,36 @@ export default function PrintPage(): JSX.Element { setSource({ kind: 'library', id: entry.id, name: entry.displayName, url: result.selection.job.outputUrl ?? '' }) } - useEffect(() => { setMeasured(null) }, [modelUrl]) + // 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 @@ -278,7 +314,7 @@ export default function PrintPage(): JSX.Element {