mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 14:58:37 +00:00
glb_writer: join split OBJ lines before parsing
Join physical OBJ lines that end in a backslash into one logical line with a separating space, skipping blank and comment lines inside a continuation.
This commit is contained in:
@ -911,6 +911,72 @@ f 1//1 2//2 3//3
|
||||
self.assertEqual(len(prims), 1)
|
||||
self.assertEqual(len(prims[0]["indices"]), 3)
|
||||
|
||||
def test_parse_obj_single_line_face_remains_unchanged(self):
|
||||
obj_text = "\n".join([
|
||||
"v 0 0 0",
|
||||
"v 1 0 0",
|
||||
"v 0 1 0",
|
||||
"f 1 2 3",
|
||||
])
|
||||
prim, = glb_writer.parse_obj(obj_text, is_path=False, up_axis="y")
|
||||
self.assertEqual(prim["indices"], [0, 1, 2])
|
||||
|
||||
def test_parse_obj_joins_face_split_across_two_lines(self):
|
||||
obj_text = "\n".join([
|
||||
"v 0 0 0",
|
||||
"v 1 0 0",
|
||||
"v 0 1 0",
|
||||
"f 1 2 \\ ",
|
||||
"3",
|
||||
])
|
||||
prim, = glb_writer.parse_obj(obj_text, is_path=False, up_axis="y")
|
||||
self.assertEqual(prim["indices"], [0, 1, 2])
|
||||
|
||||
def test_parse_obj_joins_face_split_across_three_lines(self):
|
||||
obj_text = "\n".join([
|
||||
"v 0 0 0",
|
||||
"v 1 0 0",
|
||||
"v 0 1 0",
|
||||
"f 1 \\",
|
||||
"2 \\",
|
||||
"3",
|
||||
])
|
||||
prim, = glb_writer.parse_obj(obj_text, is_path=False, up_axis="y")
|
||||
self.assertEqual(prim["indices"], [0, 1, 2])
|
||||
|
||||
def test_parse_obj_joins_face_split_immediately_after_keyword(self):
|
||||
obj_text = "\n".join([
|
||||
"v 0 0 0",
|
||||
"v 1 0 0",
|
||||
"v 0 1 0",
|
||||
"f \\",
|
||||
"1 2 3",
|
||||
])
|
||||
prim, = glb_writer.parse_obj(obj_text, is_path=False, up_axis="y")
|
||||
self.assertEqual(prim["indices"], [0, 1, 2])
|
||||
|
||||
def test_parse_obj_continuation_skips_blank_and_comment_lines(self):
|
||||
obj_text = "\r\n".join([
|
||||
"v 0 0 0",
|
||||
"v 1 0 0",
|
||||
"v 0 1 0",
|
||||
"f 1 2 \\",
|
||||
" # split after two corners",
|
||||
"",
|
||||
"3",
|
||||
]) + "\r\n"
|
||||
prim, = glb_writer.parse_obj(obj_text, is_path=False, up_axis="y")
|
||||
self.assertEqual(prim["indices"], [0, 1, 2])
|
||||
|
||||
def test_parse_obj_backslash_inside_line_stays_an_invalid_token(self):
|
||||
obj_text = "\n".join([
|
||||
"v 0 0 0",
|
||||
"v 1 0 0",
|
||||
"v 0 1 0",
|
||||
"f 1 2\\ 3",
|
||||
])
|
||||
self.assertEqual(glb_writer.parse_obj(obj_text, is_path=False, up_axis="y"), [])
|
||||
|
||||
def test_compute_bounds_calculation(self):
|
||||
prims = [
|
||||
{"positions": [(-5.0, 1.0, 2.0), (10.0, -3.0, 8.0)]},
|
||||
|
||||
@ -22,7 +22,7 @@ import struct
|
||||
import sys
|
||||
import tempfile
|
||||
import time
|
||||
from collections.abc import Iterable
|
||||
from collections.abc import Iterable, Iterator
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
from file_mode import published_mode # noqa: E402
|
||||
@ -196,6 +196,22 @@ def _concave_triangles(points: list[tuple[float, float, float]]) -> list[tuple[i
|
||||
return triangles
|
||||
|
||||
|
||||
def _logical_obj_lines(text: str) -> Iterator[str]:
|
||||
"""Yield OBJ logical lines, joining trailing-backslash continuations with a space."""
|
||||
pending = ""
|
||||
for raw_line in text.splitlines():
|
||||
line = raw_line.rstrip()
|
||||
if not line or line.lstrip().startswith("#"):
|
||||
continue
|
||||
if line.endswith("\\"):
|
||||
pending += line[:-1].rstrip() + " "
|
||||
continue
|
||||
yield pending + line
|
||||
pending = ""
|
||||
if pending:
|
||||
yield pending
|
||||
|
||||
|
||||
def parse_obj(
|
||||
text_or_path: str,
|
||||
*,
|
||||
@ -246,7 +262,7 @@ def parse_obj(
|
||||
# Preserve order of appearance for (group_index, material_name) primitives.
|
||||
builders: dict[tuple[int, str], dict] = {}
|
||||
|
||||
for raw_line in text.removeprefix("\ufeff").splitlines():
|
||||
for raw_line in _logical_obj_lines(text.removeprefix("\ufeff")):
|
||||
line = raw_line.strip()
|
||||
if not line or line[0] == "#":
|
||||
continue
|
||||
|
||||
Reference in New Issue
Block a user