From d8d4140e11d286bd6dd6a060d5228892446defc1 Mon Sep 17 00:00:00 2001 From: SteamSmith <341182916+SteamSmith@users.noreply.github.com> Date: Sun, 8 Nov 2026 23:11:37 -0500 Subject: [PATCH 1/2] fix: Reimport mesh reads the mesh file again from disk Reimport mesh re-read the project's own stored copy of the mesh when it had one, so saving a change to the mesh file and reimporting did nothing. The project now remembers the path the mesh was imported from (mesh_path, the spec's "3D mesh path") and Reimport reads that file again. When the file is gone the menu asks for one, and a command without a path still falls back to the stored copy. Projects saved before this have no path and ask once. Co-Authored-By: Claude Sonnet 5.5 --- crates/core/src/project.rs | 6 ++++ crates/engine/src/file_cmds.rs | 58 ++++++++++++++++++++++++++++++++-- crates/ui-egui/src/windows.rs | 4 ++- 3 files changed, 64 insertions(+), 4 deletions(-) diff --git a/crates/core/src/project.rs b/crates/core/src/project.rs index 36b5f856..3fe26e04 100644 --- a/crates/core/src/project.rs +++ b/crates/core/src/project.rs @@ -395,6 +395,11 @@ pub struct Project { /// it, so parts the file leaves unnamed get the same name (from it) as on import. #[serde(default, skip_serializing_if = "String::is_empty")] pub mesh_file: String, + /// Where the mesh file was imported from (the project's "3D mesh path"). Reimport mesh reads + /// it again from there, so edits made to the file since are picked up. Empty when unknown + /// (built-in meshes, web imports, projects saved before it was kept). + #[serde(default, skip_serializing_if = "String::is_empty")] + pub mesh_path: String, pub normal_format: NormalFormat, pub texture_sets: Vec, pub layers: BTreeMap, @@ -412,6 +417,7 @@ impl Project { name: name.into(), mesh: mesh.into(), mesh_file: String::new(), + mesh_path: String::new(), normal_format: NormalFormat::OpenGl, texture_sets: Vec::new(), layers: BTreeMap::new(), diff --git a/crates/engine/src/file_cmds.rs b/crates/engine/src/file_cmds.rs index f2b95e03..d87b7985 100644 --- a/crates/engine/src/file_cmds.rs +++ b/crates/engine/src/file_cmds.rs @@ -333,6 +333,11 @@ mod native { /// Mesh file `path` with the files it references next to it folded in (a `.gltf`'s buffers /// and images, a loose USD layer's textures), ready to import and store in the project. + /// `path` made absolute (as given when that fails), for [`Project::mesh_path`]. + pub fn absolute(path: &str) -> String { + std::path::absolute(Path::new(path)).map_or_else(|_| path.to_string(), |p| p.display().to_string()) + } + pub fn read_mesh(cmd: &str, path: &str) -> Result { texturecraft_formats::read_mesh_file(Path::new(path)).map_err(|e| bad(cmd, format!("cannot read {path}: {e}"))) } @@ -349,6 +354,7 @@ mod native { } else { let f = read_mesh("file.open", path)?; let mut s = Session::from_mesh_bytes(&f.name, &f.bytes, 2048, &stem_of(path))?; + s.doc.project.mesh_path = absolute(path); s.log.extend(f.warnings); Ok(s) } @@ -367,7 +373,7 @@ mod native { } #[cfg(not(target_arch = "wasm32"))] -pub use native::{open_path, read_mesh}; +pub use native::{absolute, open_path, read_mesh}; #[cfg(not(target_arch = "wasm32"))] fn new_from_mesh(s: &mut Session, p: &Value) -> Result { @@ -377,6 +383,7 @@ fn new_from_mesh(s: &mut Session, p: &Value) -> Result { let res = u64p(p, "resolution").or(template.as_ref().map(|t| u64::from(t.resolution))).unwrap_or(2048); let name = strp(p, "name").map(str::to_string).unwrap_or_else(|| stem_of(path)); let mut next = Session::from_mesh_bytes_tiled(&f.name, &f.bytes, u32::try_from(res).unwrap_or(0), &name, crate::commands::boolp(p, "uvTiles"))?; + next.doc.project.mesh_path = native::absolute(path); next.log.extend(f.warnings); if let Some(t) = &template { next.apply_template(t); @@ -416,8 +423,16 @@ fn save_copy(s: &mut Session, p: &Value) -> Result { #[cfg(not(target_arch = "wasm32"))] fn reimport(s: &mut Session, p: &Value) -> Result { - let file = strp(p, "path").map(|path| native::read_mesh("file.reimportMesh", path)).transpose()?; - s.reimport_mesh(file, crate::commands::boolp(p, "preserveStrokes").unwrap_or(false)) + // No path given: read the file the project's mesh came from, as it is on disk now. Only when + // that file is gone does it fall back to the project's own copy of the mesh. + let remembered = Some(s.doc.project.mesh_path.as_str()).filter(|m| !m.is_empty() && std::path::Path::new(m).is_file()); + let path = strp(p, "path").or(remembered).map(str::to_string); + let file = path.as_deref().map(|path| native::read_mesh("file.reimportMesh", path)).transpose()?; + let reply = s.reimport_mesh(file, crate::commands::boolp(p, "preserveStrokes").unwrap_or(false))?; + if let Some(path) = path { + s.doc.project.mesh_path = native::absolute(&path); + } + Ok(reply) } #[cfg(not(target_arch = "wasm32"))] @@ -611,6 +626,43 @@ fn close(s: &mut Session, _: &Value) -> Result { mod tests { use super::*; + /// Reimport mesh reads the mesh file again from where it was imported, so a change saved to + /// the file since shows up (it used to re-read the project's own copy and change nothing). + #[test] + fn reimport_picks_up_changes_saved_to_the_mesh_file() { + let dir = std::env::temp_dir().join(format!("tc_reimport_disk_{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let file = dir.join("tri.obj"); + let obj = |z: f32| { + format!( + "v -1 -1 {z} +v 1 -1 0 +v 1 1 0 +vt 0 0 +vt 1 0 +vt 1 1 +f 1/1 2/2 3/3 +" + ) + }; + std::fs::write(&file, obj(0.0)).unwrap(); + let path = file.display().to_string(); + let mut s = Session::new_project("t", primitives::material_ball(), "builtin:material_ball", None, 128).unwrap(); + crate::execute(&mut s, "file.new", &json!({"mesh": path, "resolution": 128})).unwrap(); + let low = |s: &Session| s.mesh.positions.iter().map(|p| p.z.abs()).fold(0.0_f32, f32::max); + assert_eq!(low(&s), 0.0); + // Edit the file, save the project, reopen it, then reimport with no path. + std::fs::write(&file, obj(0.5)).unwrap(); + let mut reopened = Session::from_project_bytes(&s.to_project_bytes().unwrap()).unwrap(); + assert_eq!(low(&reopened), 0.0, "the project still holds the old copy"); + crate::execute(&mut reopened, "file.reimportMesh", &Value::Null).unwrap(); + assert!(low(&reopened) > 0.0, "the edited file must be read again"); + // The file gone: the project's own copy is used instead of failing. + std::fs::remove_file(&file).unwrap(); + crate::execute(&mut reopened, "file.reimportMesh", &Value::Null).unwrap(); + let _ = std::fs::remove_dir_all(&dir); + } + #[test] fn reimporting_a_saved_project_marks_it_modified() { let obj = b"v -1 -1 0\nv 1 -1 0\nv 1 1 0\nvt 0 0\nvt 1 0\nvt 1 1\nf 1/1 2/2 3/3\n"; diff --git a/crates/ui-egui/src/windows.rs b/crates/ui-egui/src/windows.rs index 4bd8197f..eef332ff 100644 --- a/crates/ui-egui/src/windows.rs +++ b/crates/ui-egui/src/windows.rs @@ -833,7 +833,9 @@ pub fn file_action(app: &mut TextureCraftApp, _ctx: &egui::Context, id: &str) -> } "file.reimportMesh" => { // Painted strokes follow the surface when the new mesh's UVs differ. - let path = match app.session.mesh_source { + // The file the mesh came from is read again from disk; ask only when it's gone. + let remembered = Some(app.session.doc.project.mesh_path.as_str()).filter(|m| !m.is_empty() && std::path::Path::new(m).is_file()); + let path = match remembered { Some(_) => None, None => match rfd::FileDialog::new().add_filter(tr("3D meshes"), &texturecraft_engine::formats::mesh_import::MESH_EXTENSIONS).pick_file() { Some(p) => Some(p.display().to_string()), From e5f76eb3107f04d679fabeace31d1392c4fe66d9 Mon Sep 17 00:00:00 2001 From: SteamSmith <341182916+SteamSmith@users.noreply.github.com> Date: Sun, 8 Nov 2026 23:24:24 -0500 Subject: [PATCH 2/2] fix: never read the remembered mesh path implicitly A project file could set mesh_path to any host file, and automation allows a file.reimportMesh without a path, so that command would have read it without workspace authorization. A command without a path now always uses the project's own copy again. The desktop UI passes the remembered path itself (Session::remembered_mesh_path, one rule for "remembered and still there"), and asks for a file when there is none. Also moves the misplaced doc comment on read_mesh and documents the touched functions. Co-Authored-By: Claude Sonnet 5.5 --- crates/engine/src/file_cmds.rs | 32 ++++++++++++++++++++++---------- crates/ui-egui/src/windows.rs | 5 ++--- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/crates/engine/src/file_cmds.rs b/crates/engine/src/file_cmds.rs index d87b7985..b0cc9604 100644 --- a/crates/engine/src/file_cmds.rs +++ b/crates/engine/src/file_cmds.rs @@ -331,13 +331,13 @@ mod native { strp(p, key).filter(|s| !s.trim().is_empty()).ok_or_else(|| bad(cmd, format!("`{key}` is required"))) } - /// Mesh file `path` with the files it references next to it folded in (a `.gltf`'s buffers - /// and images, a loose USD layer's textures), ready to import and store in the project. /// `path` made absolute (as given when that fails), for [`Project::mesh_path`]. pub fn absolute(path: &str) -> String { std::path::absolute(Path::new(path)).map_or_else(|_| path.to_string(), |p| p.display().to_string()) } + /// Mesh file `path` with the files it references next to it folded in (a `.gltf`'s buffers + /// and images, a loose USD layer's textures), ready to import and store in the project. pub fn read_mesh(cmd: &str, path: &str) -> Result { texturecraft_formats::read_mesh_file(Path::new(path)).map_err(|e| bad(cmd, format!("cannot read {path}: {e}"))) } @@ -375,6 +375,8 @@ mod native { #[cfg(not(target_arch = "wasm32"))] pub use native::{absolute, open_path, read_mesh}; +/// `file.new {mesh, name?, resolution?, uvTiles?, template?}`: a new project from the mesh file `mesh`, +/// remembering its path for Reimport mesh. #[cfg(not(target_arch = "wasm32"))] fn new_from_mesh(s: &mut Session, p: &Value) -> Result { let path = native::path_param(p, "mesh", "file.new")?; @@ -421,22 +423,28 @@ fn save_copy(s: &mut Session, p: &Value) -> Result { native::save_to(s, &path, false) } +/// `file.reimportMesh {path?, preserveStrokes?}`: reimport from `path`, or from the project's own +/// copy of the mesh when there is none. A path given is remembered as the project's mesh path. +/// The remembered path is never read implicitly (a project file could point it anywhere on the +/// host); callers that may read host files pass [`Session::remembered_mesh_path`] as `path`. #[cfg(not(target_arch = "wasm32"))] fn reimport(s: &mut Session, p: &Value) -> Result { - // No path given: read the file the project's mesh came from, as it is on disk now. Only when - // that file is gone does it fall back to the project's own copy of the mesh. - let remembered = Some(s.doc.project.mesh_path.as_str()).filter(|m| !m.is_empty() && std::path::Path::new(m).is_file()); - let path = strp(p, "path").or(remembered).map(str::to_string); - let file = path.as_deref().map(|path| native::read_mesh("file.reimportMesh", path)).transpose()?; + let path = strp(p, "path"); + let file = path.map(|path| native::read_mesh("file.reimportMesh", path)).transpose()?; let reply = s.reimport_mesh(file, crate::commands::boolp(p, "preserveStrokes").unwrap_or(false))?; if let Some(path) = path { - s.doc.project.mesh_path = native::absolute(&path); + s.doc.project.mesh_path = native::absolute(path); } Ok(reply) } #[cfg(not(target_arch = "wasm32"))] impl Session { + /// The file the mesh was imported from, when it is still there: what Reimport mesh reads again. + pub fn remembered_mesh_path(&self) -> Option<&str> { + Some(self.doc.project.mesh_path.as_str()).filter(|m| !m.is_empty() && std::path::Path::new(m).is_file()) + } + /// Replace the mesh with `file` (or re-read the project's own copy) and keep the project: /// texture sets map by material index. Nothing changes unless the new mesh fits the project. /// With `preserve_strokes`, painted pixels follow the surface where the UVs changed. Replies @@ -655,11 +663,15 @@ f 1/1 2/2 3/3 std::fs::write(&file, obj(0.5)).unwrap(); let mut reopened = Session::from_project_bytes(&s.to_project_bytes().unwrap()).unwrap(); assert_eq!(low(&reopened), 0.0, "the project still holds the old copy"); + // A command without a path never reads the remembered host path (automation allows it). crate::execute(&mut reopened, "file.reimportMesh", &Value::Null).unwrap(); + assert_eq!(low(&reopened), 0.0, "no path: the project's own copy"); + let remembered = reopened.remembered_mesh_path().map(str::to_string).expect("the imported file is remembered"); + crate::execute(&mut reopened, "file.reimportMesh", &json!({ "path": remembered })).unwrap(); assert!(low(&reopened) > 0.0, "the edited file must be read again"); - // The file gone: the project's own copy is used instead of failing. + // The file gone: nothing is remembered, so the UI asks for one. std::fs::remove_file(&file).unwrap(); - crate::execute(&mut reopened, "file.reimportMesh", &Value::Null).unwrap(); + assert!(reopened.remembered_mesh_path().is_none()); let _ = std::fs::remove_dir_all(&dir); } diff --git a/crates/ui-egui/src/windows.rs b/crates/ui-egui/src/windows.rs index eef332ff..ddb7a463 100644 --- a/crates/ui-egui/src/windows.rs +++ b/crates/ui-egui/src/windows.rs @@ -834,9 +834,8 @@ pub fn file_action(app: &mut TextureCraftApp, _ctx: &egui::Context, id: &str) -> "file.reimportMesh" => { // Painted strokes follow the surface when the new mesh's UVs differ. // The file the mesh came from is read again from disk; ask only when it's gone. - let remembered = Some(app.session.doc.project.mesh_path.as_str()).filter(|m| !m.is_empty() && std::path::Path::new(m).is_file()); - let path = match remembered { - Some(_) => None, + let path = match app.session.remembered_mesh_path().map(str::to_string) { + Some(path) => Some(path), None => match rfd::FileDialog::new().add_filter(tr("3D meshes"), &texturecraft_engine::formats::mesh_import::MESH_EXTENSIONS).pick_file() { Some(p) => Some(p.display().to_string()), None => return true,