"""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 json import os from pathlib import Path 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(color: int = 0xF800) -> bytes: hdr = bytearray(128) hdr[0:4] = b"DDS " struct.pack_into(" 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_unnamed_primitive_keeps_its_own_texture(self) -> None: with tempfile.TemporaryDirectory() as temp_dir: root = Path(temp_dir) in_dir, out_dir, tex_dir = root / "obj", root / "glb", root / "textures" in_dir.mkdir() tex_dir.mkdir() (in_dir / "panel.detail.obj").write_text( "v 0 0 0\nv 1 0 0\nv 0 1 0\n" "vt 0 0\nvt 1 0\nvt 0 1\n" "f 1/1 2/2 3/3\nusemtl panel\nf 1/1 2/2 3/3\n", encoding="utf-8", ) red_png = dds_png.dds_to_png_bytes(_create_minimal_bc1_dds()) blue_png = dds_png.dds_to_png_bytes(_create_minimal_bc1_dds(0x001F)) (tex_dir / "panel.detail.png").write_bytes(red_png) (tex_dir / "panel.png").write_bytes(blue_png) stats = batch_glb.batch_convert(str(in_dir), str(out_dir), texture_dir=str(tex_dir), jobs=1) self.assertEqual(stats, {"total": 1, "succeeded": 1, "failed": 0}) glb = (out_dir / "panel.detail.glb").read_bytes() json_size, = struct.unpack_from(" 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()