diff options
| author | Kam471 <115741199+Kam471@users.noreply.github.com> | 2026-11-22 20:57:51 +0000 |
|---|---|---|
| committer | Kam471 <115741199+Kam471@users.noreply.github.com> | 2026-11-22 20:57:51 +0000 |
| commit | fbfd3ff562bfc77f4c5d4e2af6b968cd7efc77cb (patch) | |
| tree | a120d59bab74f951bd3cf32c3f62934432bd36ee | |
| parent | f847748cc76bd3777f9c070c4034eb2fc1c04e64 (diff) | |
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.
| -rw-r--r-- | tests/test_glb_writer.py | 66 | ||||
| -rw-r--r-- | tools/glb_writer.py | 20 |
2 files changed, 84 insertions, 2 deletions
diff --git a/tests/test_glb_writer.py b/tests/test_glb_writer.py index 78a63e383..4bbaf3e39 100644 --- a/tests/test_glb_writer.py +++ b/tests/test_glb_writer.py | |||
| @@ -911,6 +911,72 @@ f 1//1 2//2 3//3 | |||
| 911 | self.assertEqual(len(prims), 1) | 911 | self.assertEqual(len(prims), 1) |
| 912 | self.assertEqual(len(prims[0]["indices"]), 3) | 912 | self.assertEqual(len(prims[0]["indices"]), 3) |
| 913 | 913 | ||
| 914 | def test_parse_obj_single_line_face_remains_unchanged(self): | ||
| 915 | obj_text = "\n".join([ | ||
| 916 | "v 0 0 0", | ||
| 917 | "v 1 0 0", | ||
| 918 | "v 0 1 0", | ||
| 919 | "f 1 2 3", | ||
| 920 | ]) | ||
| 921 | prim, = glb_writer.parse_obj(obj_text, is_path=False, up_axis="y") | ||
| 922 | self.assertEqual(prim["indices"], [0, 1, 2]) | ||
| 923 | |||
| 924 | def test_parse_obj_joins_face_split_across_two_lines(self): | ||
| 925 | obj_text = "\n".join([ | ||
| 926 | "v 0 0 0", | ||
| 927 | "v 1 0 0", | ||
| 928 | "v 0 1 0", | ||
| 929 | "f 1 2 \\ ", | ||
| 930 | "3", | ||
| 931 | ]) | ||
| 932 | prim, = glb_writer.parse_obj(obj_text, is_path=False, up_axis="y") | ||
| 933 | self.assertEqual(prim["indices"], [0, 1, 2]) | ||
| 934 | |||
| 935 | def test_parse_obj_joins_face_split_across_three_lines(self): | ||
| 936 | obj_text = "\n".join([ | ||
| 937 | "v 0 0 0", | ||
| 938 | "v 1 0 0", | ||
| 939 | "v 0 1 0", | ||
| 940 | "f 1 \\", | ||
| 941 | "2 \\", | ||
| 942 | "3", | ||
| 943 | ]) | ||
| 944 | prim, = glb_writer.parse_obj(obj_text, is_path=False, up_axis="y") | ||
| 945 | self.assertEqual(prim["indices"], [0, 1, 2]) | ||
| 946 | |||
| 947 | def test_parse_obj_joins_face_split_immediately_after_keyword(self): | ||
| 948 | obj_text = "\n".join([ | ||
| 949 | "v 0 0 0", | ||
| 950 | "v 1 0 0", | ||
| 951 | "v 0 1 0", | ||
| 952 | "f \\", | ||
| 953 | "1 2 3", | ||
| 954 | ]) | ||
| 955 | prim, = glb_writer.parse_obj(obj_text, is_path=False, up_axis="y") | ||
| 956 | self.assertEqual(prim["indices"], [0, 1, 2]) | ||
| 957 | |||
| 958 | def test_parse_obj_continuation_skips_blank_and_comment_lines(self): | ||
| 959 | obj_text = "\r\n".join([ | ||
| 960 | "v 0 0 0", | ||
| 961 | "v 1 0 0", | ||
| 962 | "v 0 1 0", | ||
| 963 | "f 1 2 \\", | ||
| 964 | " # split after two corners", | ||
| 965 | "", | ||
| 966 | "3", | ||
| 967 | ]) + "\r\n" | ||
| 968 | prim, = glb_writer.parse_obj(obj_text, is_path=False, up_axis="y") | ||
| 969 | self.assertEqual(prim["indices"], [0, 1, 2]) | ||
| 970 | |||
| 971 | def test_parse_obj_backslash_inside_line_stays_an_invalid_token(self): | ||
| 972 | obj_text = "\n".join([ | ||
| 973 | "v 0 0 0", | ||
| 974 | "v 1 0 0", | ||
| 975 | "v 0 1 0", | ||
| 976 | "f 1 2\\ 3", | ||
| 977 | ]) | ||
| 978 | self.assertEqual(glb_writer.parse_obj(obj_text, is_path=False, up_axis="y"), []) | ||
| 979 | |||
| 914 | def test_compute_bounds_calculation(self): | 980 | def test_compute_bounds_calculation(self): |
| 915 | prims = [ | 981 | prims = [ |
| 916 | {"positions": [(-5.0, 1.0, 2.0), (10.0, -3.0, 8.0)]}, | 982 | {"positions": [(-5.0, 1.0, 2.0), (10.0, -3.0, 8.0)]}, |
diff --git a/tools/glb_writer.py b/tools/glb_writer.py index bd2039ba8..eead700a8 100644 --- a/tools/glb_writer.py +++ b/tools/glb_writer.py | |||
| @@ -22,7 +22,7 @@ import struct | |||
| 22 | import sys | 22 | import sys |
| 23 | import tempfile | 23 | import tempfile |
| 24 | import time | 24 | import time |
| 25 | from collections.abc import Iterable | 25 | from collections.abc import Iterable, Iterator |
| 26 | 26 | ||
| 27 | sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) | 27 | sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) |
| 28 | from file_mode import published_mode # noqa: E402 | 28 | from file_mode import published_mode # noqa: E402 |
| @@ -196,6 +196,22 @@ def _concave_triangles(points: list[tuple[float, float, float]]) -> list[tuple[i | |||
| 196 | return triangles | 196 | return triangles |
| 197 | 197 | ||
| 198 | 198 | ||
| 199 | def _logical_obj_lines(text: str) -> Iterator[str]: | ||
| 200 | """Yield OBJ logical lines, joining trailing-backslash continuations with a space.""" | ||
| 201 | pending = "" | ||
| 202 | for raw_line in text.splitlines(): | ||
| 203 | line = raw_line.rstrip() | ||
| 204 | if not line or line.lstrip().startswith("#"): | ||
| 205 | continue | ||
| 206 | if line.endswith("\\"): | ||
| 207 | pending += line[:-1].rstrip() + " " | ||
| 208 | continue | ||
| 209 | yield pending + line | ||
| 210 | pending = "" | ||
| 211 | if pending: | ||
| 212 | yield pending | ||
| 213 | |||
| 214 | |||
| 199 | def parse_obj( | 215 | def parse_obj( |
| 200 | text_or_path: str, | 216 | text_or_path: str, |
| 201 | *, | 217 | *, |
| @@ -246,7 +262,7 @@ def parse_obj( | |||
| 246 | # Preserve order of appearance for (group_index, material_name) primitives. | 262 | # Preserve order of appearance for (group_index, material_name) primitives. |
| 247 | builders: dict[tuple[int, str], dict] = {} | 263 | builders: dict[tuple[int, str], dict] = {} |
| 248 | 264 | ||
| 249 | for raw_line in text.removeprefix("\ufeff").splitlines(): | 265 | for raw_line in _logical_obj_lines(text.removeprefix("\ufeff")): |
| 250 | line = raw_line.strip() | 266 | line = raw_line.strip() |
| 251 | if not line or line[0] == "#": | 267 | if not line or line[0] == "#": |
| 252 | continue | 268 | continue |