|
| 1 | +From 54e9ac37576f6a9af4576cbb52b70d328bcaebc1 Mon Sep 17 00:00:00 2001 |
| 2 | +From: =?UTF-8?q?=C3=98yvind=20Harboe?= <oyvind@ascenium.com> |
| 3 | +Date: Sun, 17 May 2026 15:02:44 +0200 |
| 4 | +Subject: [PATCH] render_gds: log VmRSS/VmPeak per phase + capture op |
| 5 | + tracebacks |
| 6 | +MIME-Version: 1.0 |
| 7 | +Content-Type: text/plain; charset=UTF-8 |
| 8 | +Content-Transfer-Encoding: 8bit |
| 9 | + |
| 10 | +Microwatt's 562 MB merged GDS makes the html-export step OOM-or-CANCELLED |
| 11 | +under Bazel with no useful diagnostic — the action just dies somewhere |
| 12 | +between addon-register and html-write. Add a small _log_phase() helper |
| 13 | +that scrapes VmRSS/VmPeak from /proc/self/status and prints a one-liner |
| 14 | +at each phase boundary (wheels-staged, addon-registered, gdsii-imported, |
| 15 | +gltf-addon-enabled, gltf-exported, html-written), plus the imported mesh |
| 16 | +polygon/vertex totals and the .glb/.html byte sizes. |
| 17 | + |
| 18 | +Also wraps bpy.ops.import_scene.gdsii and bpy.ops.export_scene.gltf in |
| 19 | +try/except so a Python exception inside the addon prints a full |
| 20 | +traceback before sys.exit, instead of being swallowed by Blender's |
| 21 | +operator dispatcher and turning into a bare CANCELLED return. |
| 22 | + |
| 23 | +Pure diagnostics — no behavioural change on the happy path. |
| 24 | +--- |
| 25 | + tools/blendergds/render_gds.py | 90 ++++++++++++++++++++++++++++------ |
| 26 | + 1 file changed, 74 insertions(+), 16 deletions(-) |
| 27 | + |
| 28 | +diff --git a/tools/blendergds/render_gds.py b/tools/blendergds/render_gds.py |
| 29 | +index 6cf4d75..8b10d79 100644 |
| 30 | +--- a/tools/blendergds/render_gds.py |
| 31 | ++++ b/tools/blendergds/render_gds.py |
| 32 | +@@ -33,10 +33,35 @@ import importlib.util |
| 33 | + import shutil |
| 34 | + import sys |
| 35 | + import tempfile |
| 36 | ++import traceback |
| 37 | + import zipfile |
| 38 | + from pathlib import Path |
| 39 | + |
| 40 | + |
| 41 | ++def _log_phase(phase: str, extra: str = "") -> None: |
| 42 | ++ """Print a single-line phase marker with VmRSS / VmPeak. |
| 43 | ++ |
| 44 | ++ Blender's --background mode produces a lot of output; these prefixed |
| 45 | ++ lines make it easy to grep the action log for the failing phase |
| 46 | ++ (e.g. addon-register, klayout-merge inside gdsii operator, gltf-export, |
| 47 | ++ html-write) and to see exactly where RSS spikes before an OOM. |
| 48 | ++ """ |
| 49 | ++ rss = peak = "?" |
| 50 | ++ try: |
| 51 | ++ with open("/proc/self/status", "r", encoding="utf-8") as f: |
| 52 | ++ for line in f: |
| 53 | ++ if line.startswith("VmRSS:"): |
| 54 | ++ rss = line.split(":", 1)[1].strip() |
| 55 | ++ elif line.startswith("VmPeak:"): |
| 56 | ++ peak = line.split(":", 1)[1].strip() |
| 57 | ++ except OSError: |
| 58 | ++ pass |
| 59 | ++ msg = f"render_gds.py [{phase}] VmRSS={rss} VmPeak={peak}" |
| 60 | ++ if extra: |
| 61 | ++ msg += f" {extra}" |
| 62 | ++ print(msg, flush=True) |
| 63 | ++ |
| 64 | ++ |
| 65 | + def _split_argv(): |
| 66 | + """Return Blender's pass-through args (everything after `--`).""" |
| 67 | + if "--" not in sys.argv: |
| 68 | +@@ -166,6 +191,8 @@ def main(): |
| 69 | + if not wheels_dir.is_dir(): |
| 70 | + sys.exit(f"render_gds.py: wheels/ dir not found under {addon_root}") |
| 71 | + |
| 72 | ++ _log_phase("startup", extra=f"gds={gds_path} ({gds_path.stat().st_size} bytes)") |
| 73 | ++ |
| 74 | + # Stage wheels into a temp site-packages and put it on sys.path BEFORE |
| 75 | + # importing the addon (which imports numpy/gdstk/klayout/yaml at module |
| 76 | + # scope). |
| 77 | +@@ -175,6 +202,7 @@ def main(): |
| 78 | + try: |
| 79 | + _stage_wheels(wheels_dir, site) |
| 80 | + sys.path.insert(0, str(site)) |
| 81 | ++ _log_phase("wheels-staged") |
| 82 | + |
| 83 | + # bpy is provided by Blender; importing it before this point would |
| 84 | + # leak partial init in --background mode. |
| 85 | +@@ -182,6 +210,7 @@ def main(): |
| 86 | + |
| 87 | + addon = _load_addon_module(addon_root) |
| 88 | + addon.register() |
| 89 | ++ _log_phase("addon-registered") |
| 90 | + |
| 91 | + # The addon's register_properties() declares four scene props via |
| 92 | + # the legacy `bpy.types.Scene.X = StringProperty(...)` form. In |
| 93 | +@@ -192,38 +221,63 @@ def main(): |
| 94 | + # which is what we want anyway. |
| 95 | + bpy.context.scene.gdsii_pdk_selection = args.pdk |
| 96 | + |
| 97 | +- result = bpy.ops.import_scene.gdsii( |
| 98 | +- filepath=str(gds_path), |
| 99 | +- setup_scene=True, |
| 100 | +- create_collection=True, |
| 101 | +- merge_layers=True, |
| 102 | +- color_scheme="realistic", |
| 103 | +- ) |
| 104 | ++ try: |
| 105 | ++ result = bpy.ops.import_scene.gdsii( |
| 106 | ++ filepath=str(gds_path), |
| 107 | ++ setup_scene=True, |
| 108 | ++ create_collection=True, |
| 109 | ++ merge_layers=True, |
| 110 | ++ color_scheme="realistic", |
| 111 | ++ ) |
| 112 | ++ except Exception: |
| 113 | ++ _log_phase("gdsii-import-exception") |
| 114 | ++ traceback.print_exc() |
| 115 | ++ sys.exit("render_gds.py: import_scene.gdsii raised — see traceback above") |
| 116 | + if "FINISHED" not in result: |
| 117 | ++ _log_phase("gdsii-import-nonfinished", extra=f"result={result!r}") |
| 118 | + sys.exit(f"render_gds.py: import_scene.gdsii returned {result!r}") |
| 119 | + |
| 120 | ++ mesh_objs = [o for o in bpy.data.objects if o.type == "MESH"] |
| 121 | ++ total_polys = sum(len(o.data.polygons) for o in mesh_objs) |
| 122 | ++ total_verts = sum(len(o.data.vertices) for o in mesh_objs) |
| 123 | ++ _log_phase( |
| 124 | ++ "gdsii-imported", |
| 125 | ++ extra=f"meshes={len(mesh_objs)} polygons={total_polys} verts={total_verts}", |
| 126 | ++ ) |
| 127 | ++ |
| 128 | + if args.out: |
| 129 | + out_path = Path(args.out).resolve() |
| 130 | + out_path.parent.mkdir(parents=True, exist_ok=True) |
| 131 | + bpy.ops.wm.save_as_mainfile(filepath=str(out_path), copy=True) |
| 132 | ++ _log_phase("blend-saved", extra=f"{out_path.stat().st_size} bytes") |
| 133 | + |
| 134 | + if args.out_html: |
| 135 | + # io_scene_gltf2 ships with Blender as a core addon but is not |
| 136 | + # active under --factory-startup. Enable it before exporting. |
| 137 | + bpy.ops.preferences.addon_enable(module="io_scene_gltf2") |
| 138 | ++ _log_phase("gltf-addon-enabled") |
| 139 | + |
| 140 | + glb_tmp = tmp / "scene.glb" |
| 141 | +- export_result = bpy.ops.export_scene.gltf( |
| 142 | +- filepath=str(glb_tmp), |
| 143 | +- export_format="GLB", |
| 144 | +- export_apply=True, |
| 145 | +- export_yup=True, |
| 146 | +- export_lights=False, |
| 147 | +- export_cameras=False, |
| 148 | +- use_active_scene=True, |
| 149 | +- ) |
| 150 | ++ try: |
| 151 | ++ export_result = bpy.ops.export_scene.gltf( |
| 152 | ++ filepath=str(glb_tmp), |
| 153 | ++ export_format="GLB", |
| 154 | ++ export_apply=True, |
| 155 | ++ export_yup=True, |
| 156 | ++ export_lights=False, |
| 157 | ++ export_cameras=False, |
| 158 | ++ use_active_scene=True, |
| 159 | ++ ) |
| 160 | ++ except Exception: |
| 161 | ++ _log_phase("gltf-export-exception") |
| 162 | ++ traceback.print_exc() |
| 163 | ++ sys.exit( |
| 164 | ++ "render_gds.py: export_scene.gltf raised — see traceback above" |
| 165 | ++ ) |
| 166 | + if "FINISHED" not in export_result: |
| 167 | ++ _log_phase("gltf-export-nonfinished", extra=f"result={export_result!r}") |
| 168 | + sys.exit(f"render_gds.py: export_scene.gltf returned {export_result!r}") |
| 169 | ++ _log_phase("gltf-exported", extra=f"glb={glb_tmp.stat().st_size} bytes") |
| 170 | + |
| 171 | + _write_html( |
| 172 | + template_path=Path(args.html_template).resolve(), |
| 173 | +@@ -232,6 +286,10 @@ def main(): |
| 174 | + title=args.title, |
| 175 | + out_path=Path(args.out_html).resolve(), |
| 176 | + ) |
| 177 | ++ _log_phase( |
| 178 | ++ "html-written", |
| 179 | ++ extra=f"out={Path(args.out_html).stat().st_size} bytes", |
| 180 | ++ ) |
| 181 | + finally: |
| 182 | + shutil.rmtree(tmp, ignore_errors=True) |
| 183 | + |
| 184 | +-- |
| 185 | +2.51.0 |
| 186 | + |
0 commit comments