Skip to content

3D engine that AI agents drive

Show the Classroom sun gap is the per-light bounce limit (#1153)

Nguyen Xuan Anf704cb6d0dd7
diff --git a/bench/cycles-parity/classroom_bridge.py b/bench/cycles-parity/classroom_bridge.py
index 7df2998e..85869e30 100644
--- a/bench/cycles-parity/classroom_bridge.py
+++ b/bench/cycles-parity/classroom_bridge.py
@@ -32,6 +32,8 @@ SCRIPT_DIRECTORY = os.path.dirname(os.path.abspath(__file__))
REPOSITORY_ROOT = os.path.abspath(os.path.join(SCRIPT_DIRECTORY, "..", ".."))
# The scene reading (camera, lights, world description, meshes) is shared with the Blender render add-on.
sys.path.insert(0, os.path.join(REPOSITORY_ROOT, "packages", "blender-addon"))
+sys.path.insert(0, SCRIPT_DIRECTORY)
+from light_bounce_limits import clear_light_bounce_limits # noqa: E402
from cook_blender import blender_conversions as conversions # noqa: E402
from cook_blender.scene_export import add_light_entry, build_camera_entries, column_major_yup_matrix, describe_world, encode_array, geometry_key, mesh_to_arrays # noqa: E402
HDR_RADIANCE_LIMIT = 60000.0
@@ -548,12 +550,15 @@ def apply_reference_variant(scene, variant):
scene.view_settings.gamma = 1.0
elif variant == "no-world":
scene.world = None
- elif variant in ("sun-only", "points-only", "areas-only", "spots-only"):
- keep = {"sun-only": "SUN", "points-only": "POINT", "areas-only": "AREA", "spots-only": "SPOT"}[variant]
+ elif variant in ("sun-only", "points-only", "areas-only", "spots-only", "sun-only-unlimited-bounces"):
+ keep = {"sun-only": "SUN", "points-only": "POINT", "areas-only": "AREA", "spots-only": "SPOT", "sun-only-unlimited-bounces": "SUN"}[variant]
for light_object in bpy.data.objects:
if light_object.type == "LIGHT" and light_object.data.type != keep:
hide_object_everywhere(light_object)
scene.world = None
+ if variant == "sun-only-unlimited-bounces":
+ # The JSON carries no per-light limit, so clear it on the original to match what the export can say.
+ log("cleared per-light bounce limits " + json.dumps(clear_light_bounce_limits(bpy.data.objects)))
def run_reference(options):
diff --git a/bench/cycles-parity/light_bounce_limits.py b/bench/cycles-parity/light_bounce_limits.py
new file mode 100644
index 00000000..cc8b8b1d
--- /dev/null
+++ b/bench/cycles-parity/light_bounce_limits.py
@@ -0,0 +1,30 @@
+"""Clears the per-light bounce limit of Cycles lights on a scene, so the original file can be rendered the way the exported JSON is.
+
+The JSON scene format carries one bounce limit for the whole render and no per-light limit, so a light that stops at
+two bounces in the original runs to the full limit in the export. Rendering the original with the limits cleared
+shows how much of the brightness gap between the two comes from that limit alone.
+
+This module does not import bpy: it reads and writes `light_object.data.cycles.max_bounces` only, so a test can pass
+plain stand-in objects.
+"""
+
+# Blender's own default for a light's `cycles.max_bounces`; the Cycles kernel treats it as "no limit of its own".
+UNLIMITED_LIGHT_BOUNCES = 1024
+
+
+def clear_light_bounce_limits(light_objects, unlimited=UNLIMITED_LIGHT_BOUNCES):
+ """Sets every light's own bounce limit to `unlimited` and returns {light name: limit it had} for the lights that changed.
+
+ Objects that are not lights, and lights whose data has no Cycles settings, are left alone."""
+ previous_limits = {}
+ for light_object in light_objects:
+ if light_object.type != "LIGHT":
+ continue
+ cycles_settings = getattr(light_object.data, "cycles", None)
+ if cycles_settings is None or not hasattr(cycles_settings, "max_bounces"):
+ continue
+ if cycles_settings.max_bounces == unlimited:
+ continue
+ previous_limits[light_object.name] = cycles_settings.max_bounces
+ cycles_settings.max_bounces = unlimited
+ return previous_limits
diff --git a/bench/cycles-parity/run-classroom-parity.py b/bench/cycles-parity/run-classroom-parity.py
index 108215f0..300104d4 100644
--- a/bench/cycles-parity/run-classroom-parity.py
+++ b/bench/cycles-parity/run-classroom-parity.py
@@ -419,7 +419,7 @@ def stage_point_probe(blender, scene_json, options, summary):
DIAGNOSTIC_SAMPLES = 256
-BLEND_VARIANTS = ("sun-only", "points-only", "areas-only", "spots-only")
+BLEND_VARIANTS = ("sun-only", "points-only", "areas-only", "spots-only", "sun-only-unlimited-bounces")
LIGHT_SUBSETS = {
"sun-only": {"pointLights": [], "areaLights": [], "spotLights": []},
"points-only": {"keyColor": [0, 0, 0], "areaLights": [], "spotLights": []},
diff --git a/bench/cycles-parity/test_light_bounce_limits.py b/bench/cycles-parity/test_light_bounce_limits.py
new file mode 100644
index 00000000..f549a23e
--- /dev/null
+++ b/bench/cycles-parity/test_light_bounce_limits.py
@@ -0,0 +1,40 @@
+"""Checks for light_bounce_limits.py. Run from the repository root:
+
+ uv run --no-project python -m unittest bench/cycles-parity/test_light_bounce_limits.py
+"""
+import importlib.util
+import os
+import types
+import unittest
+
+HERE = os.path.dirname(os.path.abspath(__file__))
+specification = importlib.util.spec_from_file_location("light_bounce_limits", os.path.join(HERE, "light_bounce_limits.py"))
+limits = importlib.util.module_from_spec(specification)
+specification.loader.exec_module(limits)
+
+
+def make_object(name, object_type="LIGHT", max_bounces=1024, with_cycles=True):
+ cycles = types.SimpleNamespace(max_bounces=max_bounces) if with_cycles else None
+ data = types.SimpleNamespace(cycles=cycles) if with_cycles else types.SimpleNamespace()
+ return types.SimpleNamespace(name=name, type=object_type, data=data)
+
+
+class ClearLightBounceLimitsTests(unittest.TestCase):
+ def test_a_limited_light_is_cleared_and_its_old_limit_reported(self):
+ sun = make_object("sun", max_bounces=2)
+ self.assertEqual(limits.clear_light_bounce_limits([sun]), {"sun": 2})
+ self.assertEqual(sun.data.cycles.max_bounces, limits.UNLIMITED_LIGHT_BOUNCES)
+
+ def test_an_unlimited_light_is_not_reported(self):
+ lamp = make_object("lamp")
+ self.assertEqual(limits.clear_light_bounce_limits([lamp]), {})
+
+ def test_meshes_and_lights_without_cycles_settings_are_left_alone(self):
+ mesh = make_object("wall", object_type="MESH", max_bounces=3)
+ bare = make_object("bare", with_cycles=False)
+ self.assertEqual(limits.clear_light_bounce_limits([mesh, bare]), {})
+ self.assertEqual(mesh.data.cycles.max_bounces, 3)
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/docs/cycles-parity.md b/docs/cycles-parity.md
index 39adec39..bc99fe47 100644
--- a/docs/cycles-parity.md
+++ b/docs/cycles-parity.md
@@ -1089,8 +1089,9 @@ The bridge is not exact. Cycles on the exported JSON has a mean radiance of 0.17
| the same JSON, 3 bounces | 0.4065 / 0.3795 / 0.3391 |
| the same JSON, 2 bounces | 0.3416 / 0.3190 / 0.2850 (0.91 / 0.96 / 0.92 of the original) |
| the same JSON, 1 bounce | 0.2555 / 0.2386 / 0.2132 |
+| original `.blend`, every light's own bounce limit cleared (`--variant sun-only-unlimited-bounces`) | 0.6371 / 0.5605 / 0.5272 (the export at 8 bounces is 0.91 / 0.97 / 0.92 of it) |
-The gap is now the other way round: the export is 1.6 times brighter, not 10 times dimmer, so the sun fix is in and the sun-only gate is not met. The sun in the file has a per-light limit of 2 bounces (`cyclesSettings.max_bounces`), which the JSON format cannot carry, so the original's sun light stops at two bounces and the export's runs to eight. Capping the whole export at 2 bounces lands within 4 to 9 percent of the original `[inferred: it caps camera paths and every other light as well, which are off here, so only the sun matters; a run that clears the sun's limit in the original would settle it]`; the remainder is not explained. Cook was not measured: its native addon is not built on this machine. This does not affect the Cook against Cycles comparison above, which reads one file on both sides; it means the scene Cook was tested on is a faithful reading of the Classroom only in its camera, the 886 instances it exports, the sun and the world.
+The gap is now the other way round: the export is 1.6 times brighter, not 10 times dimmer, so the sun fix is in and the sun-only gate against the original as it stands is not met. The cause is shown, not only inferred: the original has per-light bounce limits (`cyclesSettings.max_bounces`: sun 2, corridor ceiling light 2, blackboard light 1, one point light 1) that the JSON format cannot carry, so the original's sun stops at two bounces and the export's runs to eight. Clearing those limits in the original (`classroom_bridge.py reference --variant sun-only-unlimited-bounces`, same 960x540, 256 spp, seed 1, 8 bounces) raises its sun-only mean from 0.3762 / 0.3310 / 0.3113 to 0.6371 / 0.5605 / 0.5272, and the export is then 0.91 / 0.97 / 0.92 of it, 3 to 9 percent dimmer. So the 1.6 times is the bounce limit, and a bridge that cannot carry the limit differs from the file by that limit; the 3 to 9 percent left, a little more in red and blue than in green, is not explained. Capping the whole export at 2 bounces lands within 4 to 9 percent of the original only because it applies the limit to every path. Cook was not measured: its native addon is not built on this machine. This does not affect the Cook against Cycles comparison above, which reads one file on both sides; it means the scene Cook was tested on is a faithful reading of the Classroom only in its camera, the 886 instances it exports, the sun and the world.
### Commands