Motion-Rig / test_preflight.py
STATICAI's picture
Restore test_preflight.py from Make-It-Animatable
770a91a verified
Raw History Blame Contribute Delete
2.87 kB
import json
import struct
import tempfile
import unittest
from pathlib import Path
from preflight import inspect_glb
class PreflightTests(unittest.TestCase):
def write_fixture(self, root, faces, copies=1):
# Valid unindexed triangles. Repeated triangle vertices exercise the
# parser with real binary payload, without a 2M-face Python object graph.
count = faces * 3
doc = {"asset":{"version":"2.0"}, "buffers":[{"byteLength":count*12}],
"bufferViews":[{"buffer":0,"byteLength":count*12}],
"accessors":[{"bufferView":0,"componentType":5126,"count":count,
"type":"VEC3","min":[0,0,0],"max":[1,1,0]}],
"meshes":[{"primitives":[{"attributes":{"POSITION":0}}]}],
"nodes":[{"mesh":0} for _ in range(copies)],
"scenes":[{"nodes":list(range(copies))}],"scene":0}
payload = json.dumps(doc).encode()
payload += b" " * (-len(payload) % 4)
p = Path(root)/"fixture.glb"
with p.open("wb") as stream:
stream.write(struct.pack("<4sIIII", b"glTF",2,28+len(payload)+count*12,len(payload),0x4E4F534A))
stream.write(payload)
stream.write(struct.pack("<II",count*12,0x004E4942))
triangle = struct.pack("<9f",0,0,0,1,0,0,0,1,0)
block = triangle * 1000
for _ in range(faces//1000):
stream.write(block)
stream.write(triangle * (faces%1000))
return p
def test_normal(self):
with tempfile.TemporaryDirectory() as root:
result = inspect_glb(self.write_fixture(root,12))
self.assertTrue(result["inference_allowed"])
self.assertEqual(result["triangles"],12)
def test_two_million_faces_preserved_and_blocked(self):
with tempfile.TemporaryDirectory() as root:
path = self.write_fixture(root,2_000_000)
before = path.stat()
result = inspect_glb(path)
self.assertEqual(result["triangles"],2_000_000)
self.assertFalse(result["inference_allowed"])
self.assertIn("decimation required",result["warning"])
self.assertEqual(path.stat().st_size,before.st_size)
self.assertEqual(path.stat().st_mtime_ns,before.st_mtime_ns)
def test_instances_count_towards_limit(self):
with tempfile.TemporaryDirectory() as root:
result = inspect_glb(self.write_fixture(root,100_000,6))
self.assertFalse(result["inference_allowed"])
self.assertEqual(result["instantiated_triangles"],600_000)
def test_truncated_rejected(self):
with tempfile.TemporaryDirectory() as root:
path=Path(root)/"bad.glb"
path.write_bytes(b"glTF")
with self.assertRaises(ValueError):
inspect_glb(path)