From d5a46a6be01d4cb756222351f7d9f3c7875a8b3a Mon Sep 17 00:00:00 2001 From: Libby Date: Tue, 10 Nov 2026 18:00:36 +0000 Subject: [PATCH] Blender colour components: one rule, range first, limits pinned by tests A huge int no longer escapes the named refusal (math.isfinite raised OverflowError); color() and color_rgba() share unit_component(). Tests accept 0 and 1 exactly and refuse 1.0000001, -1e-9 and 10**400; "color": null is documented as absent. Co-Authored-By: Claude Sonnet 5.5 --- surfaces/blender/brokkr_bridge/scene_actions.py | 17 +++++++++++------ surfaces/blender/docs/install.md | 2 +- surfaces/blender/tests/test_material_actions.py | 15 ++++++++++++++- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/surfaces/blender/brokkr_bridge/scene_actions.py b/surfaces/blender/brokkr_bridge/scene_actions.py index 39c6f48..415185c 100644 --- a/surfaces/blender/brokkr_bridge/scene_actions.py +++ b/surfaces/blender/brokkr_bridge/scene_actions.py @@ -187,6 +187,15 @@ def integer(command: dict[str, Any], key: str, low: int, high: int) -> int | Non return int(value) +def unit_component(key: str, item: Any) -> float: + """One colour component: a number in [0, 1]. The range test comes first: it also refuses nan and inf, + and a huge int (10**400) never reaches math.isfinite, which would raise OverflowError instead of + naming the field.""" + if isinstance(item, bool) or not isinstance(item, (int, float)) or not 0.0 <= item <= 1.0: + raise ActionRefused(f"{key} components must be numbers between 0 and 1.") + return float(item) + + def color(command: dict[str, Any], key: str = "color") -> tuple[float, float, float] | None: """Linear RGB, three finite numbers in [0, 1], or None when absent.""" if key not in command or command[key] is None: @@ -196,9 +205,7 @@ def color(command: dict[str, Any], key: str = "color") -> tuple[float, float, fl raise ActionRefused(f"{key} must be three numbers [r, g, b].") parts = [] for item in value: - if isinstance(item, bool) or not isinstance(item, (int, float)) or not math.isfinite(item) or not 0.0 <= item <= 1.0: - raise ActionRefused(f"{key} components must be numbers between 0 and 1.") - parts.append(float(item)) + parts.append(unit_component(key, item)) return (parts[0], parts[1], parts[2]) @@ -211,9 +218,7 @@ def color_rgba(command: dict[str, Any], key: str = "color") -> tuple[float, floa raise ActionRefused(f"{key} must be [r, g, b] or [r, g, b, a].") parts = [] for item in value: - if isinstance(item, bool) or not isinstance(item, (int, float)) or not math.isfinite(item) or not 0.0 <= item <= 1.0: - raise ActionRefused(f"{key} components must be numbers between 0 and 1.") - parts.append(float(item)) + parts.append(unit_component(key, item)) return (parts[0], parts[1], parts[2], parts[3] if len(parts) == 4 else 1.0) diff --git a/surfaces/blender/docs/install.md b/surfaces/blender/docs/install.md index ebcb863..3d01134 100644 --- a/surfaces/blender/docs/install.md +++ b/surfaces/blender/docs/install.md @@ -133,7 +133,7 @@ gets a `failed` receipt whose message names the rule): is reused) and `color` (linear `[r, g, b]` or `[r, g, b, a]`, each a finite number 0 to 1; alpha defaults to 1). `color` sets the Principled BSDF Base Color (the node tree is created if the material has none) and `diffuse_color`; a malformed `color` is - refused naming the field and nothing is created or changed. Without `color` an + refused naming the field and nothing is created or changed (`"color": null` counts as absent). Without `color` an existing material's colour is left as it is. Idempotent per (object, material): if the object already has a slot with that material no second slot is added. Other keys are ignored, as for every action. diff --git a/surfaces/blender/tests/test_material_actions.py b/surfaces/blender/tests/test_material_actions.py index 53b69c0..9ecc2ec 100644 --- a/surfaces/blender/tests/test_material_actions.py +++ b/surfaces/blender/tests/test_material_actions.py @@ -83,13 +83,26 @@ class MaterialTests(unittest.TestCase): self.assertEqual(self.base_color("glass"), (0.1, 0.2, 0.3, 0.5)) self.assertEqual(tuple(self.bpy.data.materials["glass"].diffuse_color), (0.1, 0.2, 0.3, 0.5)) + def test_the_colour_limits_themselves_are_accepted(self): + self.assign(materialName="black", color=[0, 0, 0]) + self.assertEqual(self.base_color("black"), (0.0, 0.0, 0.0, 1.0)) + self.assign(materialName="white", color=[1, 1, 1, 1]) + self.assertEqual(self.base_color("white"), (1.0, 1.0, 1.0, 1.0)) + self.assign(materialName="clear", color=[1.0, 0.0, 0.5, 0]) + self.assertEqual(self.base_color("clear"), (1.0, 0.0, 0.5, 0.0)) + + def test_an_explicit_null_colour_is_no_colour(self): + self.assertEqual(self.assign(materialName="plain", color=None)["status"], "accepted") + self.assertEqual(tuple(self.bpy.data.materials["plain"].diffuse_color), (0.8, 0.8, 0.8, 1.0)) + def test_no_colour_leaves_a_new_material_at_blender_defaults(self): self.assertEqual(self.assign(materialName="plain")["status"], "accepted") self.assertEqual(tuple(self.bpy.data.materials["plain"].diffuse_color), (0.8, 0.8, 0.8, 1.0)) def test_malformed_colour_is_refused_naming_color_and_changes_nothing(self): for bad in ([1], [1, 0], [0, 0, 0, 1, 1], "red", 0.5, {"r": 1}, ["a", 0, 0], [True, 0, 0], [None, 0, 0], - [float("nan"), 0, 0], [float("inf"), 0, 0], [-0.1, 0, 0], [1.1, 0, 0], [0, 0, 0, 2]): + [float("nan"), 0, 0], [float("inf"), 0, 0], [-0.1, 0, 0], [1.1, 0, 0], [0, 0, 0, 2], + [1.0000001, 0, 0], [-1e-9, 0, 0], [0, 0, 0, 1.0000001], [10**400, 0, 0], [0, -10**400, 0]): receipt = self.assign(materialName="oxide red", color=bad) self.assertEqual(receipt["status"], "failed", bad) self.assertIn("color", receipt["message"], bad)