#!/usr/bin/env python3 """Build Blender's pinned Alembic for the existing Web CacheFile reader.""" import hashlib from pathlib import Path import re import subprocess import tarfile import urllib.request ROOT = Path(__file__).resolve().parents[2] BASE = ROOT / "build_web_alembic" OCIO = ROOT / "build_web_ocio/install" def main(): versions = (ROOT / "blender-5.2.0/build_files/build_environment/cmake/versions.cmake").read_text() version = re.search(r"set\(ALEMBIC_VERSION ([^)]+)\)", versions)[1] expected = re.search(r"set\(ALEMBIC_HASH ([^)]+)\)", versions)[1] algorithm = re.search(r"set\(ALEMBIC_HASH_TYPE ([^)]+)\)", versions)[1].lower() if not (OCIO / "lib/cmake/Imath/ImathConfig.cmake").is_file(): raise SystemExit("Reuse/build the Web OCIO Imath installation first") BASE.mkdir(exist_ok=True) source = BASE / ("alembic-" + version) if not (source / "CMakeLists.txt").is_file(): archive = BASE / ("alembic-" + version + ".tar.gz") if not archive.is_file(): with urllib.request.urlopen( "https://codeload.github.com/alembic/alembic/tar.gz/" + version, timeout=60, ) as response: data = response.read() if hashlib.new(algorithm, data).hexdigest() != expected: raise SystemExit("Alembic download differs from Blender's pinned source hash") archive.write_bytes(data) if hashlib.new(algorithm, archive.read_bytes()).hexdigest() != expected: raise SystemExit("Alembic archive differs from Blender's pinned source hash") with tarfile.open(archive) as package: package.extractall(BASE, filter="data") toolchain = ROOT / ".emsdk/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake" subprocess.run([ "cmake", "-S", str(source), "-B", str(BASE / "build"), "-G", "Ninja", "-DCMAKE_TOOLCHAIN_FILE=" + str(toolchain), "-DCMAKE_BUILD_TYPE=Release", "-DCMAKE_INSTALL_LIBDIR=lib", "-DCMAKE_INSTALL_PREFIX=" + str(BASE / "install"), "-DCMAKE_PREFIX_PATH=" + str(OCIO), "-DCMAKE_FIND_ROOT_PATH=" + str(OCIO), "-DImath_DIR=" + str(OCIO / "lib/cmake/Imath"), "-DCMAKE_CXX_FLAGS=-fexceptions", "-DALEMBIC_SHARED_LIBS=OFF", "-DUSE_HDF5=OFF", "-DUSE_TESTS=OFF", "-DUSE_BINARIES=OFF", "-DUSE_EXAMPLES=OFF", "-DUSE_PYALEMBIC=OFF", "-DUSE_MAYA=OFF", "-DUSE_ARNOLD=OFF", "-DUSE_PRMAN=OFF", ], cwd=ROOT, check=True) subprocess.run(["cmake", "--build", str(BASE / "build"), "--target", "install", "--parallel", "8"], cwd=ROOT, check=True) if __name__ == "__main__": main()