#!/usr/bin/env python3 """Import one desktop Blender studiolight world EXR as web environment data. Desktop Material Preview lights from `blender-5.2.0/release/datafiles/ studiolights/world/forest.exr` (STUDIOLIGHT_WORLD_DEFAULT in `blenkernel/intern/studiolight.cc`). The browser has no EXR decoder, so this script decodes the EXR with the same oiiotool the datafiles were produced with, and writes the exact float32 pixels plus a manifest. Output: .bin "WBENV1\n" + u32 width + u32 height + RGB float32 LE, row 0 = top .json manifest: source EXR sha256, pixel-block sha256, samples """ import argparse import hashlib import json import pathlib import struct import subprocess import tempfile import numpy as np MAGIC = b"WBENV1\n" def read_tiff_float(path: pathlib.Path): """Minimal baseline-TIFF reader for the oiiotool float RGB output.""" data = path.read_bytes() endian = "<" if data[:2] == b"II" else ">" if struct.unpack(endian + "H", data[2:4])[0] != 42: raise SystemExit("not a baseline TIFF") ifd = struct.unpack(endian + "I", data[4:8])[0] entries = {} count = struct.unpack(endian + "H", data[ifd:ifd + 2])[0] for index in range(count): offset = ifd + 2 + index * 12 tag, field_type, length = struct.unpack(endian + "HHI", data[offset:offset + 8]) raw = data[offset + 8:offset + 12] sizes = {1: 1, 2: 1, 3: 2, 4: 4, 5: 8, 11: 4, 12: 8} size = sizes.get(field_type, 1) * length payload = raw if size <= 4 else data[struct.unpack(endian + "I", raw)[0]:][:size] if field_type == 3: entries[tag] = list(struct.unpack(endian + str(length) + "H", payload[:2 * length])) elif field_type == 4: entries[tag] = list(struct.unpack(endian + str(length) + "I", payload[:4 * length])) width, height = entries[256][0], entries[257][0] bits = entries.get(258, [32]) sample_format = entries.get(339, [1]) planar = entries.get(284, [1])[0] if bits[0] != 32 or sample_format[0] != 3 or planar != 1: raise SystemExit("expected one uncompressed chunky float32 TIFF") strips = entries[273] rows = entries.get(278, [height])[0] rows = min(rows, height) block = b"".join( data[offset:offset + rows * width * 3 * 4] for offset in strips ) pixels = np.frombuffer(block, dtype=np.dtype(endian + "f4")) expected = width * height * 3 if pixels.size < expected: raise SystemExit(f"short pixel block: {pixels.size} < {expected}") return width, height, pixels[:expected] def main() -> None: parser = argparse.ArgumentParser() parser.add_argument("--source", required=True, help="desktop world EXR path") parser.add_argument("--output-dir", required=True) parser.add_argument("--name", required=True, help="asset base name, e.g. forest") args = parser.parse_args() source = pathlib.Path(args.source) output = pathlib.Path(args.output_dir) output.mkdir(parents=True, exist_ok=True) with tempfile.TemporaryDirectory() as temporary: tiff = pathlib.Path(temporary) / "world.tiff" subprocess.run( ["oiiotool", str(source), "--ch", "R,G,B", "-d", "float", "--compression", "none", "-o", str(tiff)], check=True, ) width, height, pixels = read_tiff_float(tiff) pixel_bytes = pixels.astype(" --ch R,G,B -d float --compression none -o world.tiff", "pixelsSha256": hashlib.sha256(pixel_bytes).hexdigest(), "samples": [sample(0, 0), sample(width // 4, height // 8), sample(width // 2, height // 2), sample(width - 1, height - 1)], }, indent=2) + "\n") print(f"wrote {args.name}: {width}x{height}, " f"{len(pixel_bytes)} bytes, sha256 {hashlib.sha256(pixel_bytes).hexdigest()}") if __name__ == "__main__": main()