From ccf290f0703b6c5c9ce6cf56c1b0d93ce968196e Mon Sep 17 00:00:00 2001 From: Spencer Killen Date: Tue, 2 Jan 2024 10:19:11 -0700 Subject: [PATCH] Adjust --- src/lib.rs | 15 +++++++++------ tools/src/main.rs | 21 ++++++++++----------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 5a61333..cc9626b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -51,6 +51,8 @@ pub struct AnimationInfo { pub total_points: usize, pub start_frame: usize, pub end_frame: usize, + // MDD file format supports varying FPS , but Blender's exporter does not + // FPS is only correct if it's not varying, see individual frame times for more granularity pub fps: f32, } @@ -206,13 +208,14 @@ impl PointCache for MDDSeekableFile { let mut fps: f32 = -1.0; let mut prev_frame = frames.next().ok_or(EmptyAnimationError)??; let start_frame = prev_frame.frame_idx; - if let Some(second_frame) = frames.next() { - let second_frame = second_frame?; - fps = 1.0 / (second_frame.time - prev_frame.time); - prev_frame = second_frame; - } + // TODO: sometimes the time difference between frames is 0, not sure why + // Need to rely on later frames to avoid divide by zero for next_frame in frames { - prev_frame = next_frame?; + let next_frame = next_frame?; + if fps < 0.0 && (next_frame.time - prev_frame.time) > 0.0 { + fps = 1.0 / (next_frame.time - prev_frame.time); + } + prev_frame = next_frame; } let end_frame = prev_frame.frame_idx; diff --git a/tools/src/main.rs b/tools/src/main.rs index e97e2f6..3e4bdb7 100644 --- a/tools/src/main.rs +++ b/tools/src/main.rs @@ -126,7 +126,6 @@ impl PointState { } } - fn main() { let basename = std::env::args() .nth(1) @@ -134,11 +133,11 @@ fn main() { let mdd_filename = basename.clone() + ".mdd"; - let mut mdd_file = File::open(mdd_filename).expect("Mdd filename is not valid"); + let mdd_file = File::open(mdd_filename).expect("Mdd filename is not valid"); - let (total_frames, total_points) = MDDSeekableFile::read_header(&mut mdd_file).unwrap(); + let info = MDDSeekableFile::read_animation_info(mdd_file.try_clone().unwrap()).unwrap(); - let mut mask_last: Vec = vec![Unseen; total_points as usize]; + let mut mask_last: Vec = vec![Unseen; info.total_points]; let pc = MDDSeekableFile::read_frames(mdd_file.try_clone().unwrap()).unwrap(); let mut total_varying = 0; for frame in pc { @@ -168,11 +167,11 @@ fn main() { write_mask_image( &(basename.clone() + "_mask.exr"), &mask, - total_points as usize, + info.total_points, ) .unwrap(); - let mut pixels: Vec = Vec::with_capacity(total_varying * (total_frames as usize)); + let mut pixels: Vec = Vec::with_capacity(total_varying * (info.total_frames)); let pc = MDDSeekableFile::read_frames(mdd_file.try_clone().unwrap()).unwrap(); let pc: Vec<_> = pc.collect(); @@ -183,7 +182,7 @@ fn main() { .cloned() .max() .unwrap(); - assert_eq!(max_point_idx + 1, total_points as usize); + assert_eq!(max_point_idx + 1, info.total_points); let pc = pc.into_iter(); @@ -198,7 +197,7 @@ fn main() { } })); - assert!(pixels.len() == total_varying * (total_frames as usize)); + assert!(pixels.len() == total_varying * (info.total_frames)); if total_varying == 0 { eprintln!("No varying frames, not outputting .exr image"); @@ -209,13 +208,13 @@ fn main() { &(basename + ".exr"), &pixels, total_varying, - total_frames as usize, + info.total_frames, ) .unwrap(); println!( "Total {} varying points of {} total points", - total_varying, total_points + total_varying, info.total_points ); - println!() + println!("{:?}", info); }