|
| 1 | +""" |
| 2 | +Mesh Remesher — built-in process extension. |
| 3 | +
|
| 4 | +Protocol: reads one JSON line from stdin, writes JSON lines to stdout. |
| 5 | + stdin : { input, params, workspaceDir, tempDir } |
| 6 | + stdout: { type: "progress"|"log"|"done"|"error", ... } |
| 7 | +""" |
| 8 | +import json |
| 9 | +import os |
| 10 | +import shutil |
| 11 | +import sys |
| 12 | +import tempfile |
| 13 | +from pathlib import Path |
| 14 | + |
| 15 | + |
| 16 | +def emit(obj: dict) -> None: |
| 17 | + print(json.dumps(obj), flush=True) |
| 18 | + |
| 19 | + |
| 20 | +def progress(pct: int, label: str) -> None: |
| 21 | + emit({"type": "progress", "percent": pct, "label": label}) |
| 22 | + |
| 23 | + |
| 24 | +def log(msg: str) -> None: |
| 25 | + emit({"type": "log", "message": msg}) |
| 26 | + |
| 27 | + |
| 28 | +def done(file_path: str) -> None: |
| 29 | + emit({"type": "done", "result": {"filePath": file_path}}) |
| 30 | + |
| 31 | + |
| 32 | +def error(msg: str) -> None: |
| 33 | + emit({"type": "error", "message": msg}) |
| 34 | + |
| 35 | + |
| 36 | +def main() -> None: |
| 37 | + raw = sys.stdin.readline() |
| 38 | + data = json.loads(raw) |
| 39 | + |
| 40 | + input_data = data.get("input", {}) |
| 41 | + params = data.get("params", {}) |
| 42 | + workspace_dir = data.get("workspaceDir", "") |
| 43 | + |
| 44 | + input_path = input_data.get("filePath") |
| 45 | + if not input_path or not Path(input_path).is_file(): |
| 46 | + error(f"mesh-remesher: input file not found: {input_path}") |
| 47 | + return |
| 48 | + |
| 49 | + mode = str(params.get("mode", "triangle")) |
| 50 | + target_edge_length = float(params.get("target_edge_length", 0.0)) |
| 51 | + |
| 52 | + out_dir = Path(workspace_dir) / "Workflows" |
| 53 | + out_dir.mkdir(parents=True, exist_ok=True) |
| 54 | + from time import time |
| 55 | + out_path = str(out_dir / f"mesh-remesher-{int(time() * 1000)}.glb") |
| 56 | + |
| 57 | + log(f"Mode: {mode}, edge length: {target_edge_length or 'auto'}") |
| 58 | + |
| 59 | + if mode == "none": |
| 60 | + progress(50, "Passing through…") |
| 61 | + shutil.copy2(input_path, out_path) |
| 62 | + progress(100, "Done") |
| 63 | + done(out_path) |
| 64 | + return |
| 65 | + |
| 66 | + try: |
| 67 | + import pymeshlab |
| 68 | + except ImportError: |
| 69 | + error("mesh-remesher: pymeshlab is not available on this system") |
| 70 | + return |
| 71 | + |
| 72 | + import trimesh |
| 73 | + |
| 74 | + progress(10, "Loading mesh…") |
| 75 | + loaded = trimesh.load(input_path) |
| 76 | + if isinstance(loaded, trimesh.Scene): |
| 77 | + geoms = list(loaded.geometry.values()) |
| 78 | + geom = trimesh.util.concatenate(geoms) if len(geoms) > 1 else geoms[0] |
| 79 | + else: |
| 80 | + geom = loaded |
| 81 | + |
| 82 | + tmp_dir = tempfile.mkdtemp() |
| 83 | + try: |
| 84 | + ply_in = os.path.join(tmp_dir, "input.ply") |
| 85 | + ply_out = os.path.join(tmp_dir, "output.ply") |
| 86 | + geom.export(ply_in) |
| 87 | + |
| 88 | + ms = pymeshlab.MeshSet() |
| 89 | + ms.load_new_mesh(ply_in) |
| 90 | + |
| 91 | + if target_edge_length <= 0: |
| 92 | + measures = ms.get_geometric_measures() |
| 93 | + target_edge_length = float(measures.get("avg_edge_length", 0.02)) |
| 94 | + log(f"Auto edge length: {target_edge_length:.6f}") |
| 95 | + |
| 96 | + progress(30, f"Remeshing ({mode})…") |
| 97 | + |
| 98 | + if mode == "triangle": |
| 99 | + ms.meshing_isotropic_explicit_remeshing( |
| 100 | + targetlen=pymeshlab.PureValue(target_edge_length), |
| 101 | + iterations=3, |
| 102 | + ) |
| 103 | + elif mode == "quad": |
| 104 | + ms.meshing_isotropic_explicit_remeshing( |
| 105 | + targetlen=pymeshlab.PureValue(target_edge_length), |
| 106 | + iterations=3, |
| 107 | + ) |
| 108 | + try: |
| 109 | + ms.generate_polygonal_mesh() |
| 110 | + ms.meshing_poly_to_tri() |
| 111 | + except Exception: |
| 112 | + pass |
| 113 | + |
| 114 | + progress(80, "Exporting…") |
| 115 | + ms.save_current_mesh(ply_out) |
| 116 | + result = trimesh.load(ply_out, force="mesh") |
| 117 | + finally: |
| 118 | + shutil.rmtree(tmp_dir, ignore_errors=True) |
| 119 | + |
| 120 | + result.export(out_path) |
| 121 | + log(f"Output: {out_path} ({len(result.faces)} faces)") |
| 122 | + progress(100, "Done") |
| 123 | + done(out_path) |
| 124 | + |
| 125 | + |
| 126 | +if __name__ == "__main__": |
| 127 | + try: |
| 128 | + main() |
| 129 | + except Exception as exc: |
| 130 | + import traceback |
| 131 | + error(f"{exc}\n{traceback.format_exc()}") |
0 commit comments