diff --git a/crates/engine/src/file_cmds.rs b/crates/engine/src/file_cmds.rs index c572e62e..78916328 100644 --- a/crates/engine/src/file_cmds.rs +++ b/crates/engine/src/file_cmds.rs @@ -102,12 +102,12 @@ impl Session { pub fn mesh_obj_with_mtl(&mut self, stem: &str) -> Result<(Vec, texturecraft_formats::NamedFiles)> { // shortcut: UV-tiled sets get no maps (the OBJ keeps the mesh's UDIM UVs and an MTL map is // one image); their tiles export through Export textures with `$udim` names. - let files = self.material_files(texturecraft_formats::usd_export_options(), false)?; let mut mesh = self.source_mesh(); // A mesh without materials has one implicit material (and texture set). if mesh.materials.is_empty() { mesh.materials.push("default".into()); } + let files = self.material_files(texturecraft_formats::usd_export_options(), false, mesh.materials.len())?; Ok(texturecraft_formats::obj_with_mtl(&mesh, &self.doc.project.name, stem, &files)) } @@ -115,7 +115,7 @@ impl Session { /// composite, through the texture export pipeline) embedded. glTF has no UV tiles: a /// UV-tiled set embeds its tile atlas, with the mesh's UVs in atlas space. pub fn mesh_glb_bytes(&mut self) -> Result> { - let files = self.material_files(texturecraft_formats::glb_export_options(), true)?; + let files = self.material_files(texturecraft_formats::glb_export_options(), true, self.mesh.materials.len())?; let textures: Vec<_> = files.into_iter().map(texturecraft_formats::GlbTextures::from_files).collect(); texturecraft_formats::glb_bytes(&self.mesh, &self.doc.project.name, &textures).map_err(fe) } @@ -123,15 +123,15 @@ impl Session { /// The working mesh as USD with a UsdPreviewSurface per material (textures like /// [`Session::mesh_glb_bytes`]): usdz packages them, usda / usdc list them to write alongside. pub fn mesh_usd(&mut self, format: texturecraft_formats::UsdFormat) -> Result { - let files = self.material_files(texturecraft_formats::usd_export_options(), true)?; + let files = self.material_files(texturecraft_formats::usd_export_options(), true, self.mesh.materials.len())?; texturecraft_formats::usd_bytes(&self.mesh, &self.doc.project.name, &files, format).map_err(fe) } - /// Each material's texture set rendered with `opts` (as a tile atlas for UV-tiled sets, or - /// nothing for them without `tiled`). - fn material_files(&mut self, opts: ExportOptions, tiled: bool) -> Result>> { + /// The texture sets of the first `materials` materials rendered with `opts` (as a tile atlas + /// for UV-tiled sets, or nothing for them without `tiled`). + fn material_files(&mut self, opts: ExportOptions, tiled: bool, materials: usize) -> Result>> { let opts = ExportOptions { atlas: true, ..opts }; - (0..self.mesh.materials.len().max(1)) + (0..materials) .map(|m| match self.doc.project.texture_sets.iter().position(|t| t.material == m) { Some(ts) if tiled || self.doc.project.texture_sets.get(ts).is_some_and(|t| t.tile_grid().is_none()) => self.export_files(&[ts], &opts), _ => Ok(Vec::new()),