mirror of
https://github.com/storytold/texturecraft.git
synced 2026-11-20 06:20:50 +01:00
Merge pull request #366 from SteamSmith/fix/reimport-from-disk
Reimport mesh reads the mesh file again from disk
This commit is contained in:
3 files changed
+76
-5
No files matched your search
@@ -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<TextureSet>,
|
||||
pub layers: BTreeMap<LayerId, Layer>,
|
||||
@@ -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(),
|
||||
|
||||
@@ -331,6 +331,11 @@ mod native {
|
||||
strp(p, key).filter(|s| !s.trim().is_empty()).ok_or_else(|| bad(cmd, format!("`{key}` is required")))
|
||||
}
|
||||
|
||||
/// `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::MeshFile> {
|
||||
@@ -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,8 +373,10 @@ mod native {
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
pub use native::{open_path, read_mesh};
|
||||
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<Value> {
|
||||
let path = native::path_param(p, "mesh", "file.new")?;
|
||||
@@ -377,6 +385,7 @@ fn new_from_mesh(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
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);
|
||||
@@ -414,14 +423,28 @@ fn save_copy(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
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<Value> {
|
||||
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))
|
||||
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);
|
||||
}
|
||||
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
|
||||
@@ -611,6 +634,47 @@ fn close(s: &mut Session, _: &Value) -> Result<Value> {
|
||||
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");
|
||||
// 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: nothing is remembered, so the UI asks for one.
|
||||
std::fs::remove_file(&file).unwrap();
|
||||
assert!(reopened.remembered_mesh_path().is_none());
|
||||
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";
|
||||
|
||||
@@ -833,8 +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 {
|
||||
Some(_) => None,
|
||||
// The file the mesh came from is read again from disk; ask only when it's gone.
|
||||
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,
|
||||
|
||||
Reference in new issue
Block a user