From 2ee29d87b59f13de6a629df25b1151922303869a Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 19:18:54 +0000 Subject: [PATCH] Preserve repeated OBJ group declarations --- crates/mesh/src/obj.rs | 6 ++++++ crates/mesh/src/tests/obj.rs | 11 ++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/crates/mesh/src/obj.rs b/crates/mesh/src/obj.rs index 0a4cc3b68..b59f903d5 100644 --- a/crates/mesh/src/obj.rs +++ b/crates/mesh/src/obj.rs @@ -176,6 +176,8 @@ pub struct ReadOptions { pub preserve_runs: bool, /// Do not resolve or report material libraries; `usemtl` names remain available. pub ignore_material_libraries: bool, + /// Treat each group declaration as an object boundary, retaining repeated names. + pub groups_as_objects: bool, } struct Reader<'a> { @@ -407,6 +409,10 @@ impl Reader<'_> { }; } "o" => self.start_object(after.to_string())?, + "g" if self.options.groups_as_objects => { + self.group.clear(); + self.start_object(after.to_string())?; + } "g" => { self.slot = None; self.group = after.to_string(); diff --git a/crates/mesh/src/tests/obj.rs b/crates/mesh/src/tests/obj.rs index 583fcb4a0..ed8a89755 100644 --- a/crates/mesh/src/tests/obj.rs +++ b/crates/mesh/src/tests/obj.rs @@ -356,7 +356,7 @@ fn face_tokens_and_continued_lines_are_bounded_before_collection() { #[test] fn compatibility_options_keep_runs_and_skip_material_libraries() { let input = b"\xef\xbb\xbf v 0 0 0\nv 1 0 0\nv 0 1 0\nmtllib missing.mtl\nusemtl Red\nf 1 2 3\nusemtl Blue\nf 1 2 3\nusemtl Red\nf 1 2 3\n"; - let options = crate::obj::ReadOptions { strict: true, preserve_runs: true, ignore_material_libraries: true }; + let options = crate::obj::ReadOptions { strict: true, preserve_runs: true, ignore_material_libraries: true, ..Default::default() }; let scene = crate::obj::read_with_options(input, &Limits::default(), &mut no_external, &options).unwrap(); let parts = &scene.meshes[0].parts; assert_eq!(parts.len(), 3); @@ -364,3 +364,12 @@ fn compatibility_options_keep_runs_and_skip_material_libraries() { assert!(scene.warnings.is_empty()); assert!(crate::obj::read_with_options(b"v NaN 0 0\nv 1 0 0\nv 0 1 0\nf 1 2 3\n", &Limits::default(), &mut no_external, &options).is_err()); } + +#[test] +fn group_boundaries_keep_repeated_names_separate() { + let input = b"v 0 0 0\nv 1 0 0\nv 0 1 0\ng Same\nusemtl M\nf 1 2 3\ng Same\nf 1 3 2\n"; + let options = crate::obj::ReadOptions { groups_as_objects: true, ..Default::default() }; + let scene = crate::obj::read_with_options(input, &Limits::default(), &mut no_external, &options).unwrap(); + assert_eq!(scene.meshes.len(), 2); + assert!(scene.meshes.iter().all(|m| m.name == "Same")); +}