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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 92 additions & 9 deletions api/routers/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,29 +201,112 @@ 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:
raise HTTPException(422, str(exc)) from exc
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 ``<stem>.obj``, ``<stem>.mtl`` and
``<stem>_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": <base64>}, ...]}``, 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":
Expand Down
80 changes: 80 additions & 0 deletions api/tests/test_export_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
14 changes: 13 additions & 1 deletion electron/main/ipc-handlers.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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' }
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 ."
Expand Down
28 changes: 19 additions & 9 deletions src/areas/generate/GeneratePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -71,14 +71,17 @@ function ExportDropdown({
}) {
return (
<div className="absolute top-full left-0 mt-1 z-50 bg-zinc-900 border border-zinc-700/60 rounded-xl p-1 flex flex-col gap-0.5 min-w-[150px] shadow-xl">
{EXPORT_FORMATS.map(({ fmt, desc }) => (
{EXPORT_FORMATS.map(({ fmt, desc, hint }) => (
<button
key={fmt}
onClick={() => { onExport(fmt); onClose() }}
className="px-3 py-2 text-left hover:bg-zinc-800 rounded-lg transition-colors flex items-center gap-2.5"
className="px-3 py-2 text-left hover:bg-zinc-800 rounded-lg transition-colors flex flex-col gap-0.5"
>
<span className="text-xs font-mono font-semibold text-zinc-200">.{fmt}</span>
<span className="text-[10px] text-zinc-500">{desc}</span>
<span className="flex items-center gap-2.5">
<span className="text-xs font-mono font-semibold text-zinc-200">.{fmt}</span>
<span className="text-[10px] text-zinc-500">{desc}</span>
</span>
{hint && <span className="text-[10px] leading-snug text-zinc-600 max-w-[180px]">{hint}</span>}
</button>
))}
{canOpenInSlicer && (
Expand Down Expand Up @@ -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') {
Expand Down
31 changes: 28 additions & 3 deletions src/areas/workflows/meshExporter.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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: () => ({
Expand Down Expand Up @@ -135,4 +142,22 @@ for (const scenario of [
rmSync(root, { recursive: true, force: true })
}
})
}
}
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 })
}
})
14 changes: 9 additions & 5 deletions src/areas/workflows/nodes/ExtensionNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,16 @@ function ParamControl({ param, value, onChange, resolvedParams }: {
return <FileSelectControl param={param} value={String(value ?? '')} dirValue={dirValue} onChange={onChange} />
}
if (param.type === 'select') {
const hint = param.options?.find((o) => o.value === value)?.hint
return (
<select value={value} onChange={(e) => onChange(e.target.value)} className={inputCls}>
{param.options?.map((o) => (
<option key={String(o.value)} value={o.value}>{o.label ?? String(o.value)}</option>
))}
</select>
<>
<select value={value} onChange={(e) => onChange(e.target.value)} className={inputCls}>
{param.options?.map((o) => (
<option key={String(o.value)} value={o.value}>{o.label ?? String(o.value)}</option>
))}
</select>
{hint && <p className="mt-1 text-[10px] leading-snug text-zinc-500">{hint}</p>}
</>
)
}
if (param.type === 'string') {
Expand Down
Loading
Loading