diff --git a/api/routers/export.py b/api/routers/export.py index def92cfb..e0b79859 100644 --- a/api/routers/export.py +++ b/api/routers/export.py @@ -201,22 +201,17 @@ def export_for_slicer(fmt: str, token: str, filename: str): ) -@router.get("/{fmt}") -def export_mesh(fmt: str, path: str): - if fmt not in SUPPORTED: - raise HTTPException(400, f"Unsupported format: {fmt}. Supported: {', '.join(SUPPORTED)}") - +def _workspace_source(path: str) -> Path: full_path = (registry.WORKSPACE_DIR / path).resolve() if not registry.is_within_workspace(full_path): raise HTTPException(400, "Invalid path") if not full_path.exists(): raise HTTPException(404, f"File not found: {path}") + return full_path - # GLB — serve directly, no conversion needed - if fmt == "glb": - return FileResponse(str(full_path), media_type="model/gltf-binary") - # Flatten to a single mesh in world space (node transforms applied). +def _load_for_export(full_path: Path, fmt: str) -> "trimesh.Trimesh": + """One world-space mesh (node transforms applied), in the format's units.""" try: mesh = single_mesh(trimesh.load(str(full_path)), trimesh) except ValueError as exc: @@ -224,6 +219,94 @@ def export_mesh(fmt: str, path: str): scale = export_scale(full_path, fmt) if scale != 1.0: mesh.apply_scale(scale) + return mesh + + +def _base_color_image(mesh: "trimesh.Trimesh"): + """The mesh's base color texture (a PIL image), or None when it has none.""" + visual = getattr(mesh, "visual", None) + if getattr(visual, "kind", None) != "texture" or getattr(visual, "uv", None) is None: + return None + material = getattr(visual, "material", None) + image = getattr(material, "baseColorTexture", None) + if image is None: + image = getattr(material, "image", None) + return image + + +def obj_files(mesh: "trimesh.Trimesh", stem: str) -> dict[str, bytes]: + """The files of an OBJ export named ``stem``, by file name. + + Like Blender: a textured mesh gives ``.obj``, ``.mtl`` and + ``_texture.png`` side by side, the OBJ and MTL referring to the others + by relative name. An untextured mesh gives the OBJ alone, as before. + trimesh keeps UVs with OBJ's bottom-left origin (it flips V when reading + glTF), so the coordinates are written as they are. + """ + obj_name = f"{stem}.obj" + image = _base_color_image(mesh) + if image is None: + data = mesh.export(file_type="obj") + return {obj_name: data.encode("utf-8") if isinstance(data, str) else data} + + mtl_name, texture_name = f"{stem}.mtl", f"{stem}_texture.png" + textured = mesh.copy() + # A material of our own: a known `usemtl` name, defined in the MTL below. + textured.visual.material = trimesh.visual.material.SimpleMaterial(name="material", image=image) + obj_text, _ = trimesh.exchange.obj.export_obj( + textured, include_texture=True, return_texture=True, mtl_name=mtl_name, + ) + mtl_text = "\n".join([ + "# Exported by Modly", + "newmtl material", + "Ka 1.000000 1.000000 1.000000", + "Kd 1.000000 1.000000 1.000000", + "Ks 0.000000 0.000000 0.000000", + "d 1.000000", + "illum 1", + f"map_Kd {texture_name}", + "", + ]) + if image.mode not in ("L", "LA", "RGB", "RGBA", "P"): + image = image.convert("RGBA") + png = io.BytesIO() + image.save(png, format="PNG") + return { + obj_name: obj_text.encode("utf-8"), + mtl_name: mtl_text.encode("utf-8"), + texture_name: png.getvalue(), + } + + +@router.get("/obj/files") +def export_obj_files(path: str, name: str): + """An OBJ export as its separate files, for the caller to write side by side. + + ``name`` is the file stem the user chose (``model`` for ``model.obj``). + Answers ``{"files": [{"name": ..., "data": }, ...]}``, the OBJ first. + """ + if not name or name in (".", "..") or "/" in name or "\\" in name: + raise HTTPException(400, "Invalid file name") + full_path = _workspace_source(path) + files = obj_files(_load_for_export(full_path, "obj"), name) + return {"files": [ + {"name": file_name, "data": base64.b64encode(data).decode("ascii")} + for file_name, data in files.items() + ]} + + +@router.get("/{fmt}") +def export_mesh(fmt: str, path: str): + if fmt not in SUPPORTED: + raise HTTPException(400, f"Unsupported format: {fmt}. Supported: {', '.join(SUPPORTED)}") + + full_path = _workspace_source(path) + + # GLB — serve directly, no conversion needed + if fmt == "glb": + return FileResponse(str(full_path), media_type="model/gltf-binary") + + mesh = _load_for_export(full_path, fmt) buf = io.BytesIO() if fmt == "stl": diff --git a/api/tests/test_export_router.py b/api/tests/test_export_router.py index 2bed2a68..cbe0ccd6 100644 --- a/api/tests/test_export_router.py +++ b/api/tests/test_export_router.py @@ -204,5 +204,85 @@ def test_registered_but_deleted_file_is_404(self) -> None: self.assertEqual(ctx.exception.status_code, 404) +def _textured_quad() -> "trimesh.Trimesh": + """A quad whose texture is red on its top half and green on its bottom half. + + Vertex 2 sits at the top of the UV square (OBJ/trimesh convention: V up), + so a correctly oriented export samples red there. + """ + from PIL import Image + + image = Image.new("RGB", (8, 8), (0, 255, 0)) + image.paste((255, 0, 0), (0, 0, 8, 4)) # rows 0-3: the top of the image + mesh = trimesh.Trimesh( + vertices=[[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0]], + faces=[[0, 1, 2], [0, 2, 3]], + process=False, + ) + mesh.visual = trimesh.visual.TextureVisuals( + uv=[[0.1, 0.1], [0.9, 0.1], [0.9, 0.9], [0.1, 0.9]], + material=trimesh.visual.material.PBRMaterial(name="Base Material", baseColorTexture=image), + ) + return mesh + + +@unittest.skipUnless(HAVE_TRIMESH, "trimesh not installed") +class ExportObjFilesTests(unittest.TestCase): + def setUp(self) -> None: + self._tmp = tempfile.TemporaryDirectory() + self.workspace = Path(self._tmp.name).resolve() + self._orig_workspace = export_router.registry.WORKSPACE_DIR + export_router.registry.WORKSPACE_DIR = self.workspace + + def tearDown(self) -> None: + export_router.registry.WORKSPACE_DIR = self._orig_workspace + self._tmp.cleanup() + + def _export(self, mesh, name: str = "my model") -> dict: + # Through a GLB in the workspace, like a generated mesh. + mesh.export(str(self.workspace / "mesh.glb")) + resp = export_router.export_obj_files(path="mesh.glb", name=name) + return {f["name"]: base64.b64decode(f["data"]) for f in resp["files"]} + + def test_textured_mesh_writes_obj_mtl_and_texture_side_by_side(self) -> None: + files = self._export(_textured_quad()) + self.assertEqual(list(files), ["my model.obj", "my model.mtl", "my model_texture.png"]) + + obj = files["my model.obj"].decode("utf-8") + self.assertIn("mtllib my model.mtl", obj.splitlines()) + self.assertIn("usemtl material", obj.splitlines()) + mtl = files["my model.mtl"].decode("utf-8").splitlines() + self.assertIn("newmtl material", mtl) + self.assertIn("map_Kd my model_texture.png", mtl) + + def test_texture_lands_upright(self) -> None: + from PIL import Image + + files = self._export(_textured_quad()) + texture = Image.open(io.BytesIO(files["my model_texture.png"])).convert("RGB") + obj = files["my model.obj"].decode("utf-8").splitlines() + uvs = [tuple(map(float, line.split()[1:3])) for line in obj if line.startswith("vt ")] + self.assertEqual(len(uvs), 4) + # OBJ's V runs bottom-up while image rows run top-down. + w, h = texture.size + top = max(uvs, key=lambda uv: uv[1]) + bottom = min(uvs, key=lambda uv: uv[1]) + sample = lambda uv: texture.getpixel((int(uv[0] * (w - 1)), int((1 - uv[1]) * (h - 1)))) + self.assertEqual(sample(top), (255, 0, 0)) + self.assertEqual(sample(bottom), (0, 255, 0)) + + def test_untextured_mesh_writes_the_obj_alone(self) -> None: + files = self._export(trimesh.creation.box()) + self.assertEqual(list(files), ["my model.obj"]) + self.assertNotIn("mtllib", files["my model.obj"].decode("utf-8")) + + def test_rejects_a_name_with_a_path(self) -> None: + trimesh.creation.box().export(str(self.workspace / "mesh.glb")) + for name in ("../escape", "dir\\model", "", ".."): + with self.assertRaises(HTTPException) as ctx: + export_router.export_obj_files(path="mesh.glb", name=name) + self.assertEqual(ctx.exception.status_code, 400) + + if __name__ == "__main__": unittest.main() diff --git a/electron/main/ipc-handlers.ts b/electron/main/ipc-handlers.ts index c14132b1..a18ab114 100644 --- a/electron/main/ipc-handlers.ts +++ b/electron/main/ipc-handlers.ts @@ -1,7 +1,7 @@ import { ipcMain, BrowserWindow, Notification, dialog, app, shell } from 'electron' import { buildSync } from 'esbuild' import { autoUpdater } from 'electron-updater' -import { basename, join } from 'path' +import { basename, dirname, extname, join } from 'path' import { rm as rmAsync, readFile, writeFile, mkdir, readdir, rename, cp, symlink, lstat, copyFile } from 'fs/promises' import { existsSync, mkdirSync, readdirSync, statSync } from 'fs' import axios from 'axios' @@ -848,6 +848,18 @@ export function setupIpcHandlers(pythonBridge: PythonBridge, getWindow: WindowGe if (result.canceled || !result.filePath) return { success: false } try { + if (format === 'obj') { + // Like Blender: the material and texture go next to the chosen .obj. + const dir = dirname(result.filePath) + const stem = basename(result.filePath, extname(result.filePath)) + const { data } = await axios.get<{ files: { name: string; data: string }[] }>( + `${apiBaseUrl()}/export/obj/files?path=${encodeURIComponent(meshPath)}&name=${encodeURIComponent(stem)}`, + ) + for (const file of data.files) { + await writeFile(join(dir, basename(file.name)), Buffer.from(file.data, 'base64')) + } + return { success: true } + } const response = await axios.get( `${apiBaseUrl()}/export/${format}?path=${encodeURIComponent(meshPath)}`, { responseType: 'arraybuffer' } diff --git a/package.json b/package.json index b165d513..c641af68 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/api-guard.test.ts electron/main/agent-conversations.test.ts electron/main/gpu-memory.test.ts electron/preload/artifact-registry-preload.test.ts src/shared/stores/meshHistory.test.ts src/areas/generate/meshEditPanel.test.ts src/areas/generate/sculpt/SculptMesh.test.ts src/areas/generate/sculpt/SculptMesh.perf.test.ts src/areas/generate/sculpt/sculptLogic.test.ts src/shared/pages/pageRegistry.test.ts src/shared/pages/startPage.test.ts src/areas/home/recents.test.ts src/areas/workflows/workflowRunRecord.test.ts src/areas/queue/queueModel.test.ts electron/main/workspace-images.test.ts src/areas/image-studio/imageStudioStore.test.ts src/areas/image-studio/imageOps.test.ts src/shared/utils/imageInput.test.ts src/areas/print/printBed.test.ts src/shared/three/viewScale.test.ts && node --test electron/main/*.test.mjs src/**/*.test.mjs", + "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/api-guard.test.ts electron/main/agent-conversations.test.ts electron/main/gpu-memory.test.ts electron/preload/artifact-registry-preload.test.ts src/shared/stores/meshHistory.test.ts src/areas/generate/meshEditPanel.test.ts src/areas/generate/sculpt/SculptMesh.test.ts src/areas/generate/sculpt/SculptMesh.perf.test.ts src/areas/generate/sculpt/sculptLogic.test.ts src/shared/pages/pageRegistry.test.ts src/shared/pages/startPage.test.ts src/areas/home/recents.test.ts src/areas/workflows/workflowRunRecord.test.ts src/areas/queue/queueModel.test.ts electron/main/workspace-images.test.ts src/areas/image-studio/imageStudioStore.test.ts src/areas/image-studio/imageOps.test.ts src/shared/utils/imageInput.test.ts src/areas/print/printBed.test.ts src/shared/three/viewScale.test.ts src/areas/workflows/nodes/mesh-exporter/obj.test.ts && node --test electron/main/*.test.mjs src/**/*.test.mjs", "package": "cross-env CSC_IDENTITY_AUTO_DISCOVERY=false npm run build && npm run prepare-resources && electron-builder", "package:mac": "cross-env CSC_IDENTITY_AUTO_DISCOVERY=false npm run build && npm run prepare-resources && electron-builder --mac --arm64", "lint": "eslint ." diff --git a/src/areas/generate/GeneratePage.tsx b/src/areas/generate/GeneratePage.tsx index b546ef3a..819009f6 100644 --- a/src/areas/generate/GeneratePage.tsx +++ b/src/areas/generate/GeneratePage.tsx @@ -51,11 +51,11 @@ function createPointLight(): PointLight { // Export dropdown // --------------------------------------------------------------------------- -const EXPORT_FORMATS = [ - { fmt: 'glb' as const, desc: 'Binary glTF' }, - { fmt: 'obj' as const, desc: 'Wavefront' }, - { fmt: 'stl' as const, desc: '3D Print' }, - { fmt: 'ply' as const, desc: 'Polygon File' }, +const EXPORT_FORMATS: { fmt: 'glb' | 'obj' | 'stl' | 'ply'; desc: string; hint?: string }[] = [ + { fmt: 'glb', desc: 'Binary glTF' }, + { fmt: 'obj', desc: 'Wavefront', hint: 'Texture saved as separate files. GLB keeps everything in one.' }, + { fmt: 'stl', desc: '3D Print' }, + { fmt: 'ply', desc: 'Polygon File' }, ] function ExportDropdown({ @@ -71,14 +71,17 @@ function ExportDropdown({ }) { return (
- {EXPORT_FORMATS.map(({ fmt, desc }) => ( + {EXPORT_FORMATS.map(({ fmt, desc, hint }) => ( ))} {canOpenInSlicer && ( @@ -836,6 +839,13 @@ export default function GeneratePage(): JSX.Element { async function handleExport(format: 'glb' | 'obj' | 'stl' | 'ply') { const outputUrl = await committedMeshUrl() if (!outputUrl) return + if (format === 'obj') { + // OBJ is several files (mesh, material, texture): the main process asks + // where to save the .obj and writes the others next to it. + const result = await window.electron.model.export({ outputUrl, format }) + if (!result.success && result.error) showError(result.error) + return + } const stem = `modly-${Date.now()}` const link = document.createElement('a') if (format === 'glb') { diff --git a/src/areas/workflows/meshExporter.test.mjs b/src/areas/workflows/meshExporter.test.mjs index 09a154a9..557462fd 100644 --- a/src/areas/workflows/meshExporter.test.mjs +++ b/src/areas/workflows/meshExporter.test.mjs @@ -93,10 +93,17 @@ test('a filename cannot silently use a different export format', async () => { }) // One triangle, 0.1 m along X and 0.2 m along Y, with or without the real-size marker. -function triangleDoc(extras) { +// With `texture`, it also has UVs and a base color texture of that MIME type. +function triangleDoc(extras, texture) { + const attributes = { + POSITION: new Float32Array([0, 0, 0, 0.1, 0, 0, 0, 0.2, 0]), + TEXCOORD_0: texture ? new Float32Array([0, 0, 1, 0, 0, 1]) : undefined, + } + const baseColor = texture && { getImage: () => new Uint8Array([1, 2, 3]), getMimeType: () => texture } const prim = { - getAttribute: (name) => (name === 'POSITION' ? { getArray: () => new Float32Array([0, 0, 0, 0.1, 0, 0, 0, 0.2, 0]) } : null), + getAttribute: (name) => (attributes[name] ? { getArray: () => attributes[name] } : null), getIndices: () => null, + getMaterial: () => (baseColor ? { getBaseColorTexture: () => baseColor } : null), } return { getRoot: () => ({ @@ -135,4 +142,22 @@ for (const scenario of [ rmSync(root, { recursive: true, force: true }) } }) -} \ No newline at end of file +} +test('a textured OBJ gets its material and texture next to it', async () => { + const root = mkdtempSync(join(tmpdir(), 'modly-export-')) + try { + const context = { workspaceDir: join(root, 'workspace'), log() {}, progress() {} } + const result = await loadProcessor(triangleDoc({}, 'image/png'))( + { filePath: join(root, 'input.glb') }, { export_format: 'obj', output_path: 'models/part.obj' }, context, + ) + const dir = join(context.workspaceDir, 'models') + assert.equal(result.filePath, join(dir, 'part.obj')) + const obj = readFileSync(result.filePath, 'utf8').split('\n') + assert.ok(obj.includes('mtllib part.mtl')) + assert.ok(obj.includes('vt 1.000000 1.000000')) + assert.ok(readFileSync(join(dir, 'part.mtl'), 'utf8').split('\n').includes('map_Kd part_texture.png')) + assert.deepEqual([...readFileSync(join(dir, 'part_texture.png'))], [1, 2, 3]) + } finally { + rmSync(root, { recursive: true, force: true }) + } +}) diff --git a/src/areas/workflows/nodes/ExtensionNode.tsx b/src/areas/workflows/nodes/ExtensionNode.tsx index 17e5f3fa..c89601c0 100644 --- a/src/areas/workflows/nodes/ExtensionNode.tsx +++ b/src/areas/workflows/nodes/ExtensionNode.tsx @@ -77,12 +77,16 @@ function ParamControl({ param, value, onChange, resolvedParams }: { return } if (param.type === 'select') { + const hint = param.options?.find((o) => o.value === value)?.hint return ( - + <> + + {hint &&

{hint}

} + ) } if (param.type === 'string') { diff --git a/src/areas/workflows/nodes/mesh-exporter/manifest.json b/src/areas/workflows/nodes/mesh-exporter/manifest.json index e603f729..8901072c 100644 --- a/src/areas/workflows/nodes/mesh-exporter/manifest.json +++ b/src/areas/workflows/nodes/mesh-exporter/manifest.json @@ -21,7 +21,7 @@ "options": [ { "value": "glb", "label": "GLB (Binary glTF)" }, { "value": "stl", "label": "STL (3D printing)" }, - { "value": "obj", "label": "OBJ (Wavefront)" }, + { "value": "obj", "label": "OBJ (Wavefront)", "hint": "Texture saved as separate files. GLB keeps everything in one." }, { "value": "ply", "label": "PLY (Polygon)" } ], "tooltip": "Output file format." diff --git a/src/areas/workflows/nodes/mesh-exporter/obj.test.ts b/src/areas/workflows/nodes/mesh-exporter/obj.test.ts new file mode 100644 index 00000000..dc563f15 --- /dev/null +++ b/src/areas/workflows/nodes/mesh-exporter/obj.test.ts @@ -0,0 +1,71 @@ +import test from 'node:test' +import assert from 'node:assert/strict' +import { objFiles, type ObjPrimitive } from './obj' + +const PNG = new Uint8Array([0x89, 0x50, 0x4e, 0x47]) + +function triangle(texture: number | null, uvs: number[] | null = [0, 0.25, 1, 0, 0, 1]): ObjPrimitive { + return { + positions: new Float32Array([0, 0, 0, 1, 0, 0, 0, 1, 0]), + normals: null, + uvs: uvs ? new Float32Array(uvs) : null, + indices: [0, 1, 2], + texture, + } +} + +const lines = (data: string | Uint8Array) => String(data).split('\n') + +test('a textured mesh writes the obj, mtl and texture side by side', () => { + const files = objFiles([triangle(0)], [{ image: PNG, mimeType: 'image/png' }], 'my model') + assert.deepEqual(files.map((f) => f.name), ['my model.obj', 'my model.mtl', 'my model_texture.png']) + + const obj = lines(files[0].data) + assert.ok(obj.includes('mtllib my model.mtl')) + assert.ok(obj.includes('usemtl material')) + assert.ok(obj.includes('f 1/1 2/2 3/3')) + const mtl = lines(files[1].data) + assert.ok(mtl.includes('newmtl material')) + assert.ok(mtl.includes('map_Kd my model_texture.png')) + assert.equal(files[2].data, PNG) +}) + +test('flips V from the glTF top-left origin to the OBJ bottom-left one', () => { + const [obj] = objFiles([triangle(0)], [{ image: PNG, mimeType: 'image/png' }], 'm') + const vts = lines(obj.data).filter((l) => l.startsWith('vt ')) + assert.deepEqual(vts, ['vt 0.000000 0.750000', 'vt 1.000000 1.000000', 'vt 0.000000 0.000000']) +}) + +test('keeps a JPEG texture as JPEG', () => { + const files = objFiles([triangle(0)], [{ image: PNG, mimeType: 'image/jpeg' }], 'm') + assert.equal(files[2].name, 'm_texture.jpg') +}) + +test('an untextured mesh writes the obj alone', () => { + const files = objFiles([triangle(null)], [], 'm') + assert.deepEqual(files.map((f) => f.name), ['m.obj']) + assert.ok(!String(files[0].data).includes('mtllib')) + assert.ok(!String(files[0].data).includes('usemtl')) +}) + +test('a texture without UVs or in an unreadable format is left out', () => { + assert.deepEqual(objFiles([triangle(0, null)], [{ image: PNG, mimeType: 'image/png' }], 'm').map((f) => f.name), ['m.obj']) + assert.deepEqual(objFiles([triangle(0)], [{ image: PNG, mimeType: 'image/ktx2' }], 'm').map((f) => f.name), ['m.obj']) +}) + +test('several textures are numbered and untextured parts get their own material', () => { + const textures = [{ image: PNG, mimeType: 'image/png' }, { image: PNG, mimeType: 'image/png' }] + const files = objFiles([triangle(0), triangle(null), triangle(1)], textures, 'm') + assert.deepEqual(files.map((f) => f.name), ['m.obj', 'm.mtl', 'm_texture_1.png', 'm_texture_2.png']) + const usemtl = lines(files[0].data).filter((l) => l.startsWith('usemtl ')) + assert.deepEqual(usemtl, ['usemtl material_1', 'usemtl untextured', 'usemtl material_2']) + const mtl = lines(files[1].data) + assert.ok(mtl.includes('map_Kd m_texture_1.png')) + assert.ok(mtl.includes('map_Kd m_texture_2.png')) + assert.ok(mtl.includes('newmtl untextured')) +}) + +test('face indices continue across primitives', () => { + const [obj] = objFiles([triangle(0), triangle(0)], [{ image: PNG, mimeType: 'image/png' }], 'm') + assert.ok(lines(obj.data).includes('f 4/4 5/5 6/6')) +}) diff --git a/src/areas/workflows/nodes/mesh-exporter/obj.ts b/src/areas/workflows/nodes/mesh-exporter/obj.ts new file mode 100644 index 00000000..7d281c77 --- /dev/null +++ b/src/areas/workflows/nodes/mesh-exporter/obj.ts @@ -0,0 +1,122 @@ +// Wavefront OBJ export: the .obj, plus a .mtl and the base color textures when +// the mesh has any, written side by side like Blender does. + +export interface ObjPrimitive { + positions: Float32Array + normals: Float32Array | null + uvs: Float32Array | null + indices: number[] + /** Index into the textures list, or null when the primitive is untextured. */ + texture: number | null +} + +export interface ObjTexture { + image: Uint8Array + mimeType: string +} + +export interface ObjFile { + name: string + data: string | Uint8Array +} + +const TEXTURE_EXTENSIONS: Record = { + 'image/png': 'png', + 'image/jpeg': 'jpg', + 'image/webp': 'webp', +} + +/** The file extension for a texture image, or null for a format OBJ readers won't load. */ +export function textureExtension(mimeType: string): string | null { + return TEXTURE_EXTENSIONS[mimeType.toLowerCase()] ?? null +} + +/** + * The files of an OBJ export named `stem`: `.obj`, and when a primitive is + * textured, `.mtl` and `_texture.` (numbered when there are several). + * Only the base color texture is carried. + */ +export function objFiles(prims: ObjPrimitive[], textures: ObjTexture[], stem: string): ObjFile[] { + const isTextured = (p: ObjPrimitive) => p.texture !== null && p.uvs !== null + && textures[p.texture] !== undefined && textureExtension(textures[p.texture].mimeType) !== null + // The textures some primitive actually shows, in order. + const used = textures + .map((texture, index) => ({ texture, index })) + .filter(({ index }) => prims.some((p) => isTextured(p) && p.texture === index)) + + if (used.length === 0) return [{ name: `${stem}.obj`, data: buildObj(prims, null, () => null) }] + + const single = used.length === 1 + const materials = new Map(used.map(({ texture, index }, i) => [index, { + name: single ? 'material' : `material_${i + 1}`, + file: `${stem}_texture${single ? '' : `_${i + 1}`}.${textureExtension(texture.mimeType)}`, + image: texture.image, + }])) + + const mtlName = `${stem}.mtl` + const mtl: string[] = ['# Exported by Modly mesh-exporter'] + for (const { name, file } of materials.values()) { + mtl.push('', `newmtl ${name}`, 'Ka 1.000000 1.000000 1.000000', 'Kd 1.000000 1.000000 1.000000', + 'Ks 0.000000 0.000000 0.000000', 'd 1.000000', 'illum 1', `map_Kd ${file}`) + } + // An OBJ material carries over to the faces that follow: untextured parts get their own. + if (prims.some((p) => !isTextured(p))) { + mtl.push('', 'newmtl untextured', 'Kd 0.800000 0.800000 0.800000', 'Ks 0.000000 0.000000 0.000000', + 'd 1.000000', 'illum 1') + } + mtl.push('') + + const materialFor = (p: ObjPrimitive) => (isTextured(p) ? materials.get(p.texture!)!.name : 'untextured') + return [ + { name: `${stem}.obj`, data: buildObj(prims, mtlName, materialFor) }, + { name: mtlName, data: mtl.join('\n') }, + ...[...materials.values()].map(({ file, image }) => ({ name: file, data: image })), + ] +} + +function buildObj( + prims: ObjPrimitive[], + mtlName: string | null, + materialFor: (p: ObjPrimitive) => string | null, +): string { + const lines: string[] = ['# Exported by Modly mesh-exporter'] + if (mtlName) lines.push(`mtllib ${mtlName}`) + lines.push('') + let vOff = 1, vnOff = 1, vtOff = 1 + + for (let pi = 0; pi < prims.length; pi++) { + const prim = prims[pi] + const { positions: p, normals: n, uvs: uv, indices } = prim + const vc = p.length / 3 + + lines.push(`g mesh_${pi}`) + const material = materialFor(prim) + if (material) lines.push(`usemtl ${material}`) + + for (let i = 0; i < p.length; i += 3) + lines.push(`v ${p[i].toFixed(6)} ${p[i+1].toFixed(6)} ${p[i+2].toFixed(6)}`) + + if (n) for (let i = 0; i < n.length; i += 3) + lines.push(`vn ${n[i].toFixed(6)} ${n[i+1].toFixed(6)} ${n[i+2].toFixed(6)}`) + + // glTF puts the UV origin at the image's top-left, OBJ at its bottom-left. + if (uv) for (let i = 0; i < uv.length; i += 2) + lines.push(`vt ${uv[i].toFixed(6)} ${(1 - uv[i+1]).toFixed(6)}`) + + for (let i = 0; i + 2 < indices.length; i += 3) { + const [a, b, c] = [indices[i]+vOff, indices[i+1]+vOff, indices[i+2]+vOff] + const [ua, ub, uc] = [indices[i]+vtOff, indices[i+1]+vtOff, indices[i+2]+vtOff] + const [na, nb, nc] = [indices[i]+vnOff, indices[i+1]+vnOff, indices[i+2]+vnOff] + if (n && uv) lines.push(`f ${a}/${ua}/${na} ${b}/${ub}/${nb} ${c}/${uc}/${nc}`) + else if (n) lines.push(`f ${a}//${na} ${b}//${nb} ${c}//${nc}`) + else if (uv) lines.push(`f ${a}/${ua} ${b}/${ub} ${c}/${uc}`) + else lines.push(`f ${a} ${b} ${c}`) + } + + vOff += vc + if (n) vnOff += n.length / 3 + if (uv) vtOff += uv.length / 2 + } + + return lines.join('\n') +} diff --git a/src/areas/workflows/nodes/mesh-exporter/processor.ts b/src/areas/workflows/nodes/mesh-exporter/processor.ts index 5a91fc34..de56af8f 100644 --- a/src/areas/workflows/nodes/mesh-exporter/processor.ts +++ b/src/areas/workflows/nodes/mesh-exporter/processor.ts @@ -1,5 +1,6 @@ import path = require('path') import fs = require('fs') +import { objFiles, textureExtension, type ObjTexture } from './obj' interface ProcessInput { filePath?: string; text?: string } interface ProcessResult { filePath?: string; text?: string } @@ -17,10 +18,14 @@ interface PrimGeometry { normals: Float32Array | null uvs: Float32Array | null indices: number[] + /** Index into the base color textures, or null when untextured. */ + texture: number | null } -function extractPrimitives(doc: any): PrimGeometry[] { +function extractPrimitives(doc: any, textures: ObjTexture[]): PrimGeometry[] { const result: PrimGeometry[] = [] + // One entry per distinct texture, shared by the primitives that use it. + const textureIndex = new Map() for (const mesh of doc.getRoot().listMeshes()) { for (const prim of mesh.listPrimitives()) { const posArr = prim.getAttribute('POSITION')?.getArray() as Float32Array | null @@ -32,7 +37,17 @@ function extractPrimitives(doc: any): PrimGeometry[] { const indices = idxRaw ? Array.from(idxRaw as Uint16Array | Uint32Array) : Array.from({ length: vertCount }, (_, i) => i) - result.push({ positions: posArr, normals: normArr, uvs: uvArr, indices }) + let texture: number | null = null + const baseColor = prim.getMaterial()?.getBaseColorTexture() ?? null + const image = baseColor?.getImage() as Uint8Array | null | undefined + if (baseColor && image && textureExtension(baseColor.getMimeType() ?? '')) { + if (!textureIndex.has(baseColor)) { + textureIndex.set(baseColor, textures.length) + textures.push({ image, mimeType: baseColor.getMimeType() }) + } + texture = textureIndex.get(baseColor)! + } + result.push({ positions: posArr, normals: normArr, uvs: uvArr, indices, texture }) } } return result @@ -109,45 +124,12 @@ function writeSTL(prims: PrimGeometry[], outPath: string): void { // ─── OBJ ────────────────────────────────────────────────────────────────────── -function writeOBJ(prims: PrimGeometry[], outPath: string): void { - const lines: string[] = ['# Exported by Modly mesh-exporter', ''] - let vOff = 1, vnOff = 1, vtOff = 1 - - for (let pi = 0; pi < prims.length; pi++) { - const { positions: p, normals: n, uvs: uv, indices } = prims[pi] - const vc = p.length / 3 - - lines.push(`g mesh_${pi}`) - - for (let i = 0; i < p.length; i += 3) - lines.push(`v ${p[i].toFixed(6)} ${p[i+1].toFixed(6)} ${p[i+2].toFixed(6)}`) - - if (n) for (let i = 0; i < n.length; i += 3) - lines.push(`vn ${n[i].toFixed(6)} ${n[i+1].toFixed(6)} ${n[i+2].toFixed(6)}`) - - if (uv) for (let i = 0; i < uv.length; i += 2) - lines.push(`vt ${uv[i].toFixed(6)} ${uv[i+1].toFixed(6)}`) - - for (let i = 0; i + 2 < indices.length; i += 3) { - const [a, b, c] = [indices[i]+vOff, indices[i+1]+vOff, indices[i+2]+vOff] - if (n && uv) { - const [ua, ub, uc] = [indices[i]+vtOff, indices[i+1]+vtOff, indices[i+2]+vtOff] - const [na, nb, nc] = [indices[i]+vnOff, indices[i+1]+vnOff, indices[i+2]+vnOff] - lines.push(`f ${a}/${ua}/${na} ${b}/${ub}/${nb} ${c}/${uc}/${nc}`) - } else if (n) { - const [na, nb, nc] = [indices[i]+vnOff, indices[i+1]+vnOff, indices[i+2]+vnOff] - lines.push(`f ${a}//${na} ${b}//${nb} ${c}//${nc}`) - } else { - lines.push(`f ${a} ${b} ${c}`) - } - } - - vOff += vc - if (n) vnOff += n.length / 3 - if (uv) vtOff += uv.length / 2 - } - - fs.writeFileSync(outPath, lines.join('\n'), 'utf-8') +/** `.obj`, plus `.mtl` and its textures next to it when the mesh is textured. */ +function writeOBJ(prims: PrimGeometry[], textures: ObjTexture[], outPath: string): string[] { + const dir = path.dirname(outPath) + const files = objFiles(prims, textures, path.basename(outPath, path.extname(outPath))) + for (const file of files) fs.writeFileSync(path.join(dir, file.name), file.data) + return files.map((file) => file.name) } // ─── PLY (ASCII) ────────────────────────────────────────────────────────────── @@ -247,11 +229,15 @@ const processor = async ( await io.write(outPath, doc) } else { const calibrated = isCalibrated(doc) - const prims = scalePrimitives(extractPrimitives(doc), calibrated ? MM_PER_M : 1) + const textures: ObjTexture[] = [] + const prims = scalePrimitives(extractPrimitives(doc, textures), calibrated ? MM_PER_M : 1) if (prims.length === 0) throw new Error('mesh-exporter: no mesh data found in input') if (calibrated) context.log('Real-size asset: written in millimetres') if (format === 'stl') writeSTL(prims, outPath) - else if (format === 'obj') writeOBJ(prims, outPath) + else if (format === 'obj') { + const written = writeOBJ(prims, textures, outPath) + if (written.length > 1) context.log(`Material and texture: ${written.slice(1).join(', ')}`) + } else if (format === 'ply') writePLY(prims, outPath) } diff --git a/src/shared/types/electron.d.ts b/src/shared/types/electron.d.ts index 0935069c..871f8945 100644 --- a/src/shared/types/electron.d.ts +++ b/src/shared/types/electron.d.ts @@ -71,7 +71,8 @@ export interface ParamSchema { label: string type: 'select' | 'int' | 'float' | 'string' | 'file-select' default: number | string - options?: { value: number | string; label: string }[] + /** select: `hint` is shown under the field while that option is selected. */ + options?: { value: number | string; label: string; hint?: string }[] min?: number max?: number step?: number diff --git a/tsconfig.builtins.json b/tsconfig.builtins.json index 15c7e29d..18761efb 100644 --- a/tsconfig.builtins.json +++ b/tsconfig.builtins.json @@ -13,5 +13,8 @@ }, "include": [ "src/areas/workflows/nodes/mesh-exporter/**/*.ts" + ], + "exclude": [ + "src/areas/workflows/nodes/**/*.test.ts" ] }