58 lines
2.6 KiB
Python
58 lines
2.6 KiB
Python
#!/usr/bin/env python3
|
|
"""Generate a native Blender material node fixture for one WBV2 reader batch."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import json
|
|
import subprocess
|
|
from pathlib import Path
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
NATIVE_BLENDER = ROOT / "build_blender_native/bin/blender"
|
|
|
|
|
|
def main() -> int:
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("--output", required=True)
|
|
parser.add_argument("--node", action="append", required=True)
|
|
args = parser.parse_args()
|
|
output = Path(args.output)
|
|
if output.is_absolute() or ".." in output.parts or output.suffix != ".blend" or not str(output).startswith("tests/files/web/"):
|
|
parser.error("--output must be a repository-relative .blend path under tests/files/web/")
|
|
nodes = list(dict.fromkeys(args.node))
|
|
if not nodes or any(not node.startswith("ShaderNode") or not node.replace("_", "").isalnum() for node in nodes):
|
|
parser.error("every --node must be a Blender ShaderNode identifier")
|
|
if not NATIVE_BLENDER.is_file():
|
|
raise SystemExit(f"native Blender fixture generator is unavailable: {NATIVE_BLENDER.relative_to(ROOT)}")
|
|
destination = ROOT / output
|
|
destination.parent.mkdir(parents=True, exist_ok=True)
|
|
script = (
|
|
"import bpy\n"
|
|
f"destination = {str(destination)!r}\n"
|
|
f"node_types = {nodes!r}\n"
|
|
"material = bpy.data.materials.new('WBV2 Shader Batch')\n"
|
|
"material.use_nodes = True\n"
|
|
"mesh = bpy.data.meshes.new('WBV2 Shader Mesh')\n"
|
|
"mesh.from_pydata([(-1,-1,0),(1,-1,0),(0,1,0)], [], [(0,1,2)])\n"
|
|
"obj = bpy.data.objects.new('WBV2 Shader Object', mesh)\n"
|
|
"bpy.context.collection.objects.link(obj)\n"
|
|
"obj.data.materials.append(material)\n"
|
|
"tree = material.node_tree\n"
|
|
"tree.nodes.clear()\n"
|
|
"for node_type in node_types:\n"
|
|
" node = tree.nodes.new(node_type)\n"
|
|
" node.name = 'WBV2 ' + node_type\n"
|
|
"bpy.ops.wm.save_as_mainfile(filepath=destination)\n"
|
|
)
|
|
result = subprocess.run([str(NATIVE_BLENDER), "--background", "--factory-startup", "--python-expr", script], cwd=ROOT, text=True, capture_output=True, check=False)
|
|
if result.returncode or not destination.is_file():
|
|
raise SystemExit(f"native shader fixture generation failed ({result.returncode}):\n{result.stdout}{result.stderr}")
|
|
print(json.dumps({"status": "PASS", "fixture": output.as_posix(), "nodes": nodes, "bytes": destination.stat().st_size}))
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|