Files
mandy/tests/test_batch_glb.py

141 lines
5.0 KiB
Python

"""Unit tests for tools/batch_glb.py batch OBJ -> glTF 2.0 (.glb) converter.
Usage:
python -I tests/test_batch_glb.py
"""
from __future__ import annotations
import os
import struct
import sys
import tempfile
import unittest
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TOOLS = os.path.join(ROOT, "tools")
if TOOLS not in sys.path:
sys.path.insert(0, TOOLS)
import batch_glb
import dds_png
def _create_minimal_bc1_dds() -> bytes:
hdr = bytearray(128)
hdr[0:4] = b"DDS "
struct.pack_into("<I", hdr, 4, 124)
struct.pack_into("<I", hdr, 8, 0x1007)
struct.pack_into("<I", hdr, 12, 2)
struct.pack_into("<I", hdr, 16, 2)
struct.pack_into("<I", hdr, 20, 8)
struct.pack_into("<I", hdr, 76, 32)
struct.pack_into("<I", hdr, 80, 0x4)
hdr[84:88] = b"DXT1"
struct.pack_into("<I", hdr, 108, 0x1000)
bc1_block = struct.pack("<HHBBBB", 0xF800, 0x07E0, 0, 0, 0, 0)
return bytes(hdr) + bc1_block
class TestBatchGlb(unittest.TestCase):
def test_batch_convert_synthetic(self) -> None:
with tempfile.TemporaryDirectory() as in_dir, tempfile.TemporaryDirectory() as out_dir:
for i in range(3):
obj_path = os.path.join(in_dir, f"test_model_{i}.obj")
with open(obj_path, "w", encoding="utf-8", newline="\n") as f:
f.write(
"o test\n"
"v 0.0 0.0 0.0\n"
"v 1.0 0.0 0.0\n"
"v 0.0 1.0 0.0\n"
"f 1 2 3\n"
)
stats = batch_glb.batch_convert(in_dir, out_dir, up_axis="z", jobs=2)
self.assertEqual(stats["total"], 3)
self.assertEqual(stats["succeeded"], 3)
self.assertEqual(stats["failed"], 0)
for i in range(3):
glb_file = os.path.join(out_dir, f"test_model_{i}.glb")
self.assertTrue(os.path.isfile(glb_file))
self.assertGreater(os.path.getsize(glb_file), 100)
def test_batch_convert_with_png_texture(self) -> None:
with tempfile.TemporaryDirectory() as in_dir, \
tempfile.TemporaryDirectory() as out_dir, \
tempfile.TemporaryDirectory() as tex_dir:
# Create a 2x2 PNG texture
dds_data = _create_minimal_bc1_dds()
png_bytes = dds_png.dds_to_png_bytes(dds_data)
tex_file = os.path.join(tex_dir, "mat_custom.png")
with open(tex_file, "wb") as f:
f.write(png_bytes)
# Create an OBJ with UVs and material reference
obj_path = os.path.join(in_dir, "textured_cube.obj")
with open(obj_path, "w", encoding="utf-8", newline="\n") as f:
f.write(
"o textured_cube\n"
"usemtl mat_custom\n"
"v 0.0 0.0 0.0\n"
"v 1.0 0.0 0.0\n"
"v 0.0 1.0 0.0\n"
"vt 0.0 0.0\n"
"vt 1.0 0.0\n"
"vt 0.0 1.0\n"
"f 1/1 2/2 3/3\n"
)
stats = batch_glb.batch_convert(
in_dir, out_dir, up_axis="y", texture_dir=tex_dir
)
self.assertEqual(stats["total"], 1)
self.assertEqual(stats["succeeded"], 1)
self.assertEqual(stats["failed"], 0)
glb_file = os.path.join(out_dir, "textured_cube.glb")
self.assertTrue(os.path.isfile(glb_file))
with open(glb_file, "rb") as f:
content = f.read()
self.assertIn(b"image/png", content)
self.assertIn(b"mat_custom", content)
def test_batch_convert_with_dds_texture(self) -> None:
with tempfile.TemporaryDirectory() as in_dir, \
tempfile.TemporaryDirectory() as out_dir, \
tempfile.TemporaryDirectory() as tex_dir:
# Create a DDS texture directly
dds_data = _create_minimal_bc1_dds()
tex_file = os.path.join(tex_dir, "car_body.dds")
with open(tex_file, "wb") as f:
f.write(dds_data)
obj_path = os.path.join(in_dir, "car_body.obj")
with open(obj_path, "w", encoding="utf-8", newline="\n") as f:
f.write(
"o car_body\n"
"v 0.0 0.0 0.0\n"
"v 1.0 0.0 0.0\n"
"v 0.0 1.0 0.0\n"
"vt 0.0 0.0\n"
"vt 1.0 0.0\n"
"vt 0.0 1.0\n"
"f 1/1 2/2 3/3\n"
)
stats = batch_glb.batch_convert(
in_dir, out_dir, up_axis="z", texture_dir=tex_dir
)
self.assertEqual(stats["total"], 1)
self.assertEqual(stats["succeeded"], 1)
glb_file = os.path.join(out_dir, "car_body.glb")
self.assertTrue(os.path.isfile(glb_file))
with open(glb_file, "rb") as f:
content = f.read()
self.assertIn(b"image/png", content)
if __name__ == "__main__":
unittest.main()