Files
workinf_Blender_Wasm/tools/web/import-studiolight-world.py
mes123456 63db6caf1f
Some checks failed
M6 deployable RC / quick (push) Has been cancelled
M6 deployable RC / chromium (push) Has been cancelled
M6 deployable RC / release (push) Has been cancelled
feat(web): studio worlds, camera-view/offscreen environment parity, material and undo coverage
Environment lighting:
- ship all eight desktop studiolights/world datasets (WBENV1 float32) and a
  shared loader used by both the main viewport and the offscreen worker
- add the N-panel "studio 环境" selector
- rotate the world background together with the environment
  (backgroundRotation/backgroundIntensity), so camera view and offscreen
  renders match the desktop Mapping-node behaviour

Verification (Chromium, OPFS, NOT_CLAIMED):
- environment lighting + 8 world pixel identity
- camera-view rotation correspondence (camera matrices, 207 samples)
- offscreen vs on-screen background parity after hiding geometry (1305 samples)
- metals case extended with material-field OPFS roundtrip
- material Principled edits and rename undo/redo
- eight studio worlds: pairwise frame differences + luminance rank correlation
- undo history depth capped at 64 steps and undo across OPFS save/reopen

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-23 09:27:47 +08:00

116 lines
4.5 KiB
Python

#!/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:
<name>.bin "WBENV1\n" + u32 width + u32 height + RGB float32 LE, row 0 = top
<name>.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("<f4", copy=False).tobytes()
(output / f"{args.name}.bin").write_bytes(
MAGIC + struct.pack("<II", width, height) + pixel_bytes
)
def sample(x: int, y: int):
index = (y * width + x) * 3
return [x, y] + [round(float(value), 6) for value in pixels[index:index + 3]]
(output / f"{args.name}.json").write_text(json.dumps({
"name": args.name,
"source": str(source),
"sourceSha256": hashlib.sha256(source.read_bytes()).hexdigest(),
"width": width,
"height": height,
"format": "rgb-float32",
"generator": "tools/web/import-studiolight-world.py",
"decode": "oiiotool <source> --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()