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 <noreply@anthropic.com>
This commit is contained in:
Libby 2026-11-10 18:00:36 +00:00
commit d5a46a6be0
3 changed files with 26 additions and 8 deletions

View file

@ -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)

View file

@ -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.

View file

@ -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)