A 1.2 MB pure-Rust rewrite of headless Blender built for AI agents — 3D B-rep modeling engine, CLI, and MCP server
fix(import): convert .glb positions from metres to the scene unit
Faris Basha TM3dd15ac19f2c
diff --git a/crates/forma-core/src/scene.rs b/crates/forma-core/src/scene.rsindex 58b5ba2..9fd2d21 100644--- a/crates/forma-core/src/scene.rs+++ b/crates/forma-core/src/scene.rs@@ -253,19 +253,26 @@ impl Scene {/// Load a mesh from attached in-memory assets or (if `allow_fs_import`) from disk,/// with an optional explicit `yup` override for `.glb` imports.pub fn load_asset_mesh_with_opts(&self, path_or_key: &str, yup: Option<bool>) -> Result<PolyMesh, String> {- if let Some(bytes) = self.assets.get(path_or_key) {- let ext = Path::new(path_or_key)- .extension()- .and_then(|s| s.to_str())- .unwrap_or("glb");- return crate::import::import_mesh_bytes_with_opts(bytes, ext, yup);- }- if !self.allow_fs_import {+ let ext = Path::new(path_or_key).extension().and_then(|s| s.to_str());+ let mut mesh = if let Some(bytes) = self.assets.get(path_or_key) {+ crate::import::import_mesh_bytes_with_opts(bytes, ext.unwrap_or("glb"), yup)?+ } else if !self.allow_fs_import {return Err(format!("filesystem import disabled in sandboxed mode; upload '{path_or_key}' in request assets"));+ } else {+ crate::import::import_mesh_with_opts(Path::new(path_or_key), yup)?+ };+ // glTF positions are always metres (and `.glb` export scales to+ // metres), so convert them back to the scene unit. `.obj`/`.stl`+ // carry no unit and are read as model units, matching their export.+ let is_glb = ext.map_or(self.assets.contains_key(path_or_key), |e| e.eq_ignore_ascii_case("glb"));+ let k = self.unit.meters_per_unit();+ if is_glb && k != 1.0 {+ let s = 1.0 / k;+ mesh.scale(Vec3::new(s, s, s));}- crate::import::import_mesh_with_opts(Path::new(path_or_key), yup)+ Ok(mesh)}/// Load a mesh from attached in-memory assets or (if `allow_fs_import`) from disk.diff --git a/crates/forma-core/tests/units_export.rs b/crates/forma-core/tests/units_export.rsindex 8704701..ea76c04 100644--- a/crates/forma-core/tests/units_export.rs+++ b/crates/forma-core/tests/units_export.rs@@ -125,6 +125,32 @@ fn glb_is_scaled_to_metres_because_gltf_is_defined_in_metres() {assert!((glb_position_extent(&m)[0] - 100.0).abs() < 1e-4);}+#[test]+fn glb_import_converts_metres_back_to_the_scene_unit() {+ // A .glb written in a mm/inch scene must load back at its authored size.+ for unit in [Unit::Millimeter, Unit::Inch, Unit::Meter] {+ let mut s = Scene::new();+ s.unit = unit;+ let glb = s.export_mesh_bytes(&slab(), "glb").unwrap();+ s.insert_asset("slab.glb", glb);+ let size = s.load_asset_mesh("slab.glb").unwrap().aabb().size();+ assert!(+ (size.x - 100.0).abs() < 1e-4 && (size.y - 50.0).abs() < 1e-4 && (size.z - 20.0).abs() < 1e-4,+ "{unit:?}: {size:?}"+ );+ }++ // STL/OBJ carry no unit, so they load back unchanged in any scene unit.+ let mut s = Scene::new();+ s.unit = Unit::Millimeter;+ for ext in ["stl", "obj"] {+ let key = format!("slab.{ext}");+ s.insert_asset(key.clone(), export_mesh_bytes(&slab(), ext).unwrap());+ let size = s.load_asset_mesh(&key).unwrap().aabb().size();+ assert!((size.x - 100.0).abs() < 1e-4, "{ext}: {size:?}");+ }+}+#[test]fn stl_and_obj_have_no_unit_field_and_keep_model_units() {let mm_stl = export_mesh_bytes_with_unit(&slab(), "stl", Unit::Millimeter).unwrap();