Use shared aligned stored ZIP entries for USDZ export

Co-authored-by: bflatastic <[email protected]>
This commit is contained in:
Dylan M. Taylor
2026-11-16 23:26:09 -05:00
co-authored by bflatastic
parent 1e9194d9a6
commit 9d7077601b
4 changed files with 56 additions and 7 deletions
Generated
+11
View File
@@ -957,6 +957,16 @@ dependencies = [
"thiserror 2.0.21",
]
[[package]]
name = "craft-zip"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=6128eabbfb98e5fe486fa7ee4413724e2a4ca908#6128eabbfb98e5fe486fa7ee4413724e2a4ca908"
dependencies = [
"flate2",
"thiserror 2.0.21",
"zip",
]
[[package]]
name = "craft-zip"
version = "0.1.0"
@@ -4473,6 +4483,7 @@ dependencies = [
"craft-mesh",
"craft-tcraft",
"craft-usd",
"craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=6128eabbfb98e5fe486fa7ee4413724e2a4ca908)",
"craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=c376774f15ae7cd3b06a7ac664f01a8645af4b83)",
"exr",
"flate2",
+2
View File
@@ -70,6 +70,8 @@ craft-i18n = { git = "https://github.com/storytold/craft-libs", rev = "390fe98b0
craft-ui = { git = "https://github.com/storytold/craft-libs", rev = "390fe98b0dd3fb84db7c3af7cbd958db4e8cc585", features = ["serde"] }
craft-materialx = { git = "https://github.com/storytold/craft-libs", rev = "390fe98b0dd3fb84db7c3af7cbd958db4e8cc585" }
craft-tcraft = { git = "https://github.com/storytold/craft-libs", rev = "c376774f15ae7cd3b06a7ac664f01a8645af4b83" }
# Isolated aligned writer; existing archive readers retain their pin and behavior.
craft-zip-usdz = { package = "craft-zip", git = "https://github.com/storytold/craft-libs", rev = "6128eabbfb98e5fe486fa7ee4413724e2a4ca908" }
craft-zip = { git = "https://github.com/storytold/craft-libs", rev = "c376774f15ae7cd3b06a7ac664f01a8645af4b83" }
craft-mesh = { git = "https://github.com/storytold/craft-libs", rev = "b952ca0231867c8ad6749ac44cab5fc903c3ad52", default-features = false, features = ["stl", "ply", "fbx", "obj"] }
craft-mcmat = { git = "https://github.com/storytold/craft-libs", rev = "c376774f15ae7cd3b06a7ac664f01a8645af4b83" }
+1
View File
@@ -17,6 +17,7 @@ craft-mesh = { workspace = true }
craft-mcmat = { workspace = true }
craft-tcraft = { workspace = true }
craft-zip = { workspace = true }
craft-zip-usdz = { workspace = true }
texturecraft-core = { workspace = true }
texturecraft-texel = { workspace = true }
texturecraft-geom = { workspace = true }
+42 -7
View File
@@ -6,7 +6,6 @@
//! UsdUVTexture shaders. Meters, Y up.
use std::fmt::Write as _;
use std::io::Write as _;
use texturecraft_core::{Channel, NormalFormat};
use texturecraft_geom::Mesh;
@@ -344,14 +343,12 @@ pub(crate) fn material_maps(mesh: &Mesh, stem: &str, textures: &[Vec<ExportedFil
/// A usdz package: the layer first, every entry stored uncompressed at a 64-byte aligned offset.
pub(crate) fn usdz(layer_name: &str, layer: &[u8], files: &[(String, Vec<u8>)]) -> Result<Vec<u8>> {
let mut zip = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
let opts = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored).with_alignment(64);
let err = |e: zip::result::ZipError| FormatError::Invalid(format!("usdz package: {e}"));
let mut zip = craft_zip_usdz::Writer::new();
let err = |e| FormatError::Invalid(format!("usdz package: {e}"));
for (name, bytes) in std::iter::once((layer_name, layer)).chain(files.iter().map(|(n, b)| (n.as_str(), b.as_slice()))) {
zip.start_file(name, opts).map_err(err)?;
zip.write_all(bytes)?;
zip.add_stored_aligned(name, bytes, 64).map_err(err)?;
}
Ok(zip.finish().map_err(err)?.into_inner())
zip.finish().map_err(err)
}
/// Text of a value as usda writes it.
@@ -439,3 +436,41 @@ pub(crate) fn usda(layer: &UsdLayer) -> String {
usda_prim(&mut s, &layer.root, 0);
s
}
#[cfg(test)]
mod aligned_package_tests {
use super::*;
use std::io::{Cursor, Read, Write};
#[test]
fn shared_usdz_keeps_layer_first_and_exact_archive_bytes() {
let files = vec![("textures/color.png".to_owned(), vec![0, 1, 255]), ("empty".to_owned(), Vec::new())];
let bytes = usdz("scene.usdc", b"PXR-USDC", &files).unwrap();
let mut previous = zip::ZipWriter::new(Cursor::new(Vec::new()));
let opts = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored).with_alignment(64);
for (name, data) in std::iter::once(("scene.usdc", b"PXR-USDC".as_slice())).chain(files.iter().map(|(n, b)| (n.as_str(), b.as_slice()))) {
previous.start_file(name, opts).unwrap();
previous.write_all(data).unwrap();
}
assert_eq!(bytes, previous.finish().unwrap().into_inner());
let mut archive = zip::ZipArchive::new(Cursor::new(bytes)).unwrap();
assert_eq!(archive.len(), 3);
for (i, (name, data)) in
std::iter::once(("scene.usdc", b"PXR-USDC".as_slice())).chain(files.iter().map(|(n, b)| (n.as_str(), b.as_slice()))).enumerate()
{
let mut entry = archive.by_index(i).unwrap();
assert_eq!(entry.name().unwrap(), name);
assert_eq!(entry.compression(), zip::CompressionMethod::Stored);
assert_eq!(entry.data_start().unwrap() % 64, 0);
let mut actual = Vec::new();
entry.read_to_end(&mut actual).unwrap();
assert_eq!(actual, data);
}
}
#[test]
fn shared_usdz_rejects_duplicate_layer_name() {
let err = usdz("scene.usdc", b"PXR-USDC", &[("scene.usdc".to_owned(), Vec::new())]).unwrap_err();
assert!(matches!(err, FormatError::Invalid(ref why) if why.starts_with("usdz package:")));
}
}