148 lines
6.4 KiB
Python
148 lines
6.4 KiB
Python
# .blend fixtures (tools/fixtures.py): the same test scenes built by Blender 2.79, 2.93, 3.5, 4.1 (old nixpkgs
|
|
# releases) and the current nixpkgs Blender (5.x) through tools/fixtures/_blend_make.py, covering every mesh storage
|
|
# generation (MFace legacy save, MPoly / MLoop, attribute layers, AttributeStorage), gzip and zstd compression, the
|
|
# 5.0 header, layered actions. Reference values come from the current Blender opening each file
|
|
# (tools/fixtures/_blend_ref.py: bpy values plus its glTF exporter's output). An old Blender that does not run is
|
|
# skipped (its cases are missing; the test notes it). Plus a few Blender 1.6x / 2.0x files from download.blender.org's
|
|
# old demos (fetched, never committed; 32-bit pointers, big-endian SGI / PowerPC files among them), with references
|
|
# from Blender 4.1 (5.0 dropped big-endian files).
|
|
# tests/gen/blend_<ver>.luau { { name, data, ref } } (<ver>: 279, 293, 35, 41, cur, old)
|
|
# tests/gen/blend_files.luau sibling files the scenes refer to ({ ["outside.png"] = bytes, UDIM tiles })
|
|
import json, os, shutil, subprocess, tempfile
|
|
|
|
INPUTS = ["tools/fixtures/_blend_make.py", "tools/fixtures/_blend_ref.py"]
|
|
|
|
OLD = [
|
|
("279", ["--impure", "--expr", '(import (builtins.fetchTarball "https://github.com/NixOS/nixpkgs/archive/nixos-19.03.tar.gz") {}).blender']),
|
|
("293", ["github:NixOS/nixpkgs/nixos-21.11#blender"]),
|
|
("35", ["github:NixOS/nixpkgs/nixos-23.05#blender"]),
|
|
("41", ["github:NixOS/nixpkgs/nixos-24.05#blender"]),
|
|
]
|
|
|
|
|
|
OLD_DEMOS = "https://download.blender.org/demo/old_demos/"
|
|
OLD_FILES = ["escher.blend", "hairball.blend", "nmesh.blend", "relative.blend", "demo2.04.blend.gz"]
|
|
|
|
|
|
def old_samples(ctx, tmp):
|
|
import gzip, urllib.request
|
|
out = os.path.join(tmp, "olddemos")
|
|
os.makedirs(out)
|
|
files = []
|
|
for f in OLD_FILES:
|
|
try:
|
|
with urllib.request.urlopen(OLD_DEMOS + f, timeout=60) as r:
|
|
data = r.read()
|
|
except OSError as e:
|
|
print("blend: could not fetch %s: %s" % (f, e))
|
|
continue
|
|
if f.endswith(".gz"):
|
|
data = gzip.decompress(data)
|
|
f = f[:-3].replace("2.04", "204")
|
|
with open(os.path.join(out, f), "wb") as fh:
|
|
fh.write(data)
|
|
files.append(os.path.join(out, f))
|
|
if not files:
|
|
return []
|
|
refdir = os.path.join(tmp, "oldref")
|
|
os.makedirs(refdir)
|
|
ref = os.path.join(ctx.root, "tools", "fixtures", "_blend_ref.py")
|
|
ok, log = run(["nix", "shell"] + OLD[3][1] + ["-c", "blender", "-b", "--factory-startup", "--python", ref, "--", json.dumps(files), refdir])
|
|
cases = []
|
|
for p in files:
|
|
rp = os.path.join(refdir, os.path.basename(p) + ".json")
|
|
if not os.path.exists(rp):
|
|
continue
|
|
r = json.load(open(rp))
|
|
if r.get("error"):
|
|
continue
|
|
g = r.get("gltf")
|
|
if g:
|
|
for k in ("bufferViews", "asset", "scene", "scenes", "extensionsUsed", "samplers"):
|
|
g.pop(k, None)
|
|
cases.append({"name": "old/" + os.path.basename(p), "data": open(p, "rb").read(), "ref": clean(r)})
|
|
if not cases:
|
|
print("blend: Blender 4.1 gave no reference for the old demos: " + log[-400:])
|
|
return cases
|
|
|
|
|
|
def env():
|
|
e = dict(os.environ)
|
|
e.pop("LD_LIBRARY_PATH", None) # the host's alsa / pipewire libraries need a newer glibc than old Blenders have
|
|
return e
|
|
|
|
|
|
def current():
|
|
exe = shutil.which("blender")
|
|
return [exe] if exe else ["nix", "shell", "nixpkgs#blender", "-c", "blender"]
|
|
|
|
|
|
def run(cmd, timeout=1800):
|
|
try:
|
|
res = subprocess.run(cmd, capture_output=True, timeout=timeout, env=env())
|
|
return res.returncode == 0, res.stdout.decode("utf-8", "replace") + res.stderr.decode("utf-8", "replace")
|
|
except (subprocess.TimeoutExpired, OSError) as e:
|
|
return False, str(e)
|
|
|
|
|
|
def clean(v):
|
|
if isinstance(v, dict):
|
|
return {k: clean(x) for k, x in v.items() if x is not None}
|
|
if isinstance(v, list):
|
|
return [False if x is None else clean(x) for x in v]
|
|
return v
|
|
|
|
|
|
def generate(ctx):
|
|
tmp = tempfile.mkdtemp(prefix="blendfx")
|
|
make = os.path.join(ctx.root, "tools", "fixtures", "_blend_make.py")
|
|
ref = os.path.join(ctx.root, "tools", "fixtures", "_blend_ref.py")
|
|
made = {}
|
|
for tag, inst in OLD + [("cur", None)]:
|
|
out = os.path.join(tmp, tag)
|
|
os.makedirs(out)
|
|
if inst is None:
|
|
cmd = current()
|
|
else:
|
|
cmd = ["nix", "shell"] + inst + ["-c", "blender"]
|
|
ok, log = run(cmd + ["-b", "--factory-startup", "--python", make, "--", out])
|
|
files = sorted(f for f in os.listdir(out) if f.endswith(".blend"))
|
|
if not ok or not files:
|
|
print("blend: Blender %s did not run (skipped): %s" % (tag, log[-400:]))
|
|
continue
|
|
made[tag] = (out, files)
|
|
paths = [os.path.join(out, f) for out, files in made.values() for f in files]
|
|
refdir = os.path.join(tmp, "ref")
|
|
os.makedirs(refdir)
|
|
ok, log = run(current() + ["-b", "--factory-startup", "--python", ref, "--", json.dumps(paths), refdir], timeout=3600)
|
|
if not ok:
|
|
raise RuntimeError("blend: the reference Blender failed: " + log[-2000:])
|
|
sibling = None
|
|
tiles = {}
|
|
for tag, (out, files) in made.items():
|
|
cases = []
|
|
for f in files:
|
|
p = os.path.join(out, f)
|
|
r = json.load(open(os.path.join(refdir, f + ".json")))
|
|
g = r.get("gltf")
|
|
if g:
|
|
for k in ("bufferViews", "asset", "scene", "scenes", "extensionsUsed", "samplers"):
|
|
g.pop(k, None)
|
|
cases.append({"name": f, "data": open(p, "rb").read(), "ref": clean(r)})
|
|
name = "cur" if tag == "cur" else tag
|
|
ctx.emit("blend_" + name, cases)
|
|
if sibling is None and os.path.exists(os.path.join(out, "outside.png")):
|
|
sibling = open(os.path.join(out, "outside.png"), "rb").read()
|
|
for tile in ("udim.1001.png", "udim.1002.png"):
|
|
if tile not in tiles and os.path.exists(os.path.join(out, tile)):
|
|
tiles[tile] = open(os.path.join(out, tile), "rb").read()
|
|
for tag, _ in OLD + [("cur", None)]:
|
|
if tag not in made:
|
|
ctx.emit("blend_" + tag, [])
|
|
ctx.emit("blend_old", old_samples(ctx, tmp))
|
|
files = {"outside.png": sibling or b""}
|
|
files.update(tiles)
|
|
ctx.emit("blend_files", files)
|
|
ctx.emit("blend_versions", sorted(made.keys()))
|
|
shutil.rmtree(tmp, ignore_errors=True)
|