From 6b7d6ae9e2db549313eab38e59c1990b323e2e6f Mon Sep 17 00:00:00 2001 From: Libby Date: Tue, 10 Nov 2026 14:31:58 +0000 Subject: [PATCH] blender: renderStill refuses GL engines headless when libEGL is not loadable BLENDER_WORKBENCH and BLENDER_EEVEE aborted the whole Blender process inside bpy.ops.render.render with no receipt. The probe is cached, Linux only, and a module-level function so tests replace it. Co-Authored-By: Claude Sonnet 5.5 --- .../blender/brokkr_bridge/blender_target.py | 26 ++++++++++ surfaces/blender/docs/install.md | 7 ++- surfaces/blender/tests/test_scene_actions.py | 48 +++++++++++++++++++ 3 files changed, 80 insertions(+), 1 deletion(-) diff --git a/surfaces/blender/brokkr_bridge/blender_target.py b/surfaces/blender/brokkr_bridge/blender_target.py index dcae328..641928f 100644 --- a/surfaces/blender/brokkr_bridge/blender_target.py +++ b/surfaces/blender/brokkr_bridge/blender_target.py @@ -1,5 +1,7 @@ from __future__ import annotations +import ctypes +import functools import json import math import os @@ -36,6 +38,11 @@ SYNC_RECEIPT_SCHEMA = "brokkr.sync.receipt.v0" CREATABLE_OBJECT_TYPES = ("MESH", "LIGHT", "CAMERA", "EMPTY") +# Engines that draw through the GL stack. A headless Linux Blender without libEGL aborts the whole +# process inside bpy.ops.render.render for these (no receipt); CYCLES renders on the CPU. +GL_RENDER_ENGINES = ("BLENDER_WORKBENCH", "BLENDER_EEVEE", "BLENDER_EEVEE_NEXT") +GL_RUNTIME_LIBRARY = "libEGL.so.1" + EVALUATED_POSITION_CAP = 256 EVALUATED_VERTEX_LIMIT = 1_000_000 EVALUATED_INSTANCE_SCAN_LIMIT = 100_000 # depsgraph items visited per capture; iterating one in Python costs ~5 us @@ -751,6 +758,12 @@ class BrokkrBlenderTarget: render.engine = engine except TypeError: raise sa.ActionRefused(f"engine {engine} is not available in this Blender.") from None + if render.engine in GL_RENDER_ENGINES and getattr(getattr(self.bpy, "app", None), "background", False): + missing = missing_gl_library() + if missing: + raise sa.ActionRefused( + f"engine {render.engine} needs {missing}, which this headless Blender cannot load " + "(rendering would abort the process); use engine CYCLES.") if width is not None: render.resolution_x = width if height is not None: @@ -1033,6 +1046,19 @@ def _load_cultmesh(cultlib_py_src: str) -> tuple[Any, Any]: return cultmesh_py, cultcache_py +@functools.lru_cache(maxsize=None) +def missing_gl_library() -> str | None: + """The GL runtime library this host cannot load, or None. Probed once, on Linux only: on + Windows and macOS a failed GPU render is not a headless-EGL abort.""" + if not sys.platform.startswith("linux"): + return None + try: + ctypes.CDLL(GL_RUNTIME_LIBRARY) + except OSError: + return GL_RUNTIME_LIBRARY + return None + + def _now() -> str: return datetime.now(timezone.utc).isoformat() diff --git a/surfaces/blender/docs/install.md b/surfaces/blender/docs/install.md index 04d5a1e..6f3ad73 100644 --- a/surfaces/blender/docs/install.md +++ b/surfaces/blender/docs/install.md @@ -137,7 +137,12 @@ gets a `failed` receipt whose message names the rule): `engine` (any engine of the running Blender, case-insensitive), `resolutionX`, `resolutionY` (whole numbers, 1 to 16384), `cameraName`. Engine, resolution, camera, file path and format are restored after the render. The render blocks - Blender's UI until it finishes. + Blender's UI until it finishes. In a background (`-b`) Blender on Linux, the GL + engines `BLENDER_WORKBENCH`, `BLENDER_EEVEE` and `BLENDER_EEVEE_NEXT` are refused + with a `failed` receipt when `libEGL.so.1` cannot be loaded (probed once), because + rendering would abort the whole process with no receipt; `CYCLES` renders on the + CPU. The check applies to the scene's engine when `engine` is omitted. No such + check runs on Windows or macOS. The scene actions were run against Blender 5.2.3 LTS headless with `surfaces/blender/tests/live/headless_smoke.py` (usage in its docstring). One real diff --git a/surfaces/blender/tests/test_scene_actions.py b/surfaces/blender/tests/test_scene_actions.py index aa4b498..88ff453 100644 --- a/surfaces/blender/tests/test_scene_actions.py +++ b/surfaces/blender/tests/test_scene_actions.py @@ -275,6 +275,54 @@ class ActionTests(unittest.TestCase): self.assertIn("CUBE", receipt["message"]) self.assertEqual(self.bpy.created, []) + # renderStill: GPU engines without a GL runtime + def test_render_still_refuses_gpu_engines_headless_when_the_gl_library_is_missing(self): + self.bpy.app.background = True + for engine in ("BLENDER_WORKBENCH", "BLENDER_EEVEE", "BLENDER_EEVEE_NEXT", "blender_workbench"): + self.bpy.scene.render.ENGINES = FakeRender.ENGINES + ("BLENDER_EEVEE_NEXT",) + with mock.patch.object(bt, "missing_gl_library", return_value="libEGL.so.1"): + receipt = self.run_action(action="renderStill", outputPath="a.png", engine=engine) + self.assertEqual(receipt["status"], "failed", engine) + self.assertIn("libEGL.so.1", receipt["message"]) + self.assertIn("CYCLES", receipt["message"]) + self.assertEqual(self.bpy.rendered, []) + self.assertEqual(self.bpy.scene.render.engine, "BLENDER_EEVEE") + + def test_render_still_refuses_the_scene_engine_too_and_leaves_no_folder(self): + self.bpy.app.background = True # scene engine is BLENDER_EEVEE + with mock.patch.object(bt, "missing_gl_library", return_value="libEGL.so.1"): + receipt = self.run_action(action="renderStill", outputPath="shots/a.png") + self.assertEqual(receipt["status"], "failed") + self.assertFalse((self.root / "out" / "shots").exists()) + + def test_render_still_gl_check_spares_cycles_foreground_and_a_present_library(self): + probe = mock.Mock(return_value="libEGL.so.1") + with mock.patch.object(bt, "missing_gl_library", probe): + self.bpy.app.background = True + self.assertEqual(self.run_action(action="renderStill", outputPath="a.png", engine="CYCLES")["status"], "accepted") + probe.assert_not_called() + self.bpy.app.background = False + self.assertEqual(self.run_action(action="renderStill", outputPath="b.png", engine="BLENDER_WORKBENCH")["status"], "accepted") + probe.assert_not_called() + self.bpy.app.background = True + probe.return_value = None + self.assertEqual(self.run_action(action="renderStill", outputPath="c.png", engine="BLENDER_WORKBENCH")["status"], "accepted") + + def test_missing_gl_library_probes_only_on_linux_and_only_once(self): + bt.missing_gl_library.cache_clear() + with mock.patch.object(bt.sys, "platform", "win32"), mock.patch.object(bt.ctypes, "CDLL", side_effect=OSError) as cdll: + self.assertIsNone(bt.missing_gl_library()) + cdll.assert_not_called() + bt.missing_gl_library.cache_clear() + with mock.patch.object(bt.sys, "platform", "linux"), mock.patch.object(bt.ctypes, "CDLL", side_effect=OSError) as cdll: + self.assertEqual(bt.missing_gl_library(), "libEGL.so.1") + self.assertEqual(bt.missing_gl_library(), "libEGL.so.1") + cdll.assert_called_once_with("libEGL.so.1") + bt.missing_gl_library.cache_clear() + with mock.patch.object(bt.sys, "platform", "linux"), mock.patch.object(bt.ctypes, "CDLL"): + self.assertIsNone(bt.missing_gl_library()) + bt.missing_gl_library.cache_clear() + # importGltf: names def test_import_refuses_drive_unc_extended_stream_and_device_names(self): for name in ("C:foo.glb", "C:\\x\\model.glb", "\\\\host\\share\\model.glb", "\\\\?\\C:\\model.glb",