Skip to content

EffectCraft: An open-source motion graphics and compositing tool written by scratch in pure Rust

S2.75: propagate saved tracking with the newly selected model

Kristian Myram6d10a6e93241
diff --git a/crates/effects/src/roto.rs b/crates/effects/src/roto.rs
index e8cae42c3..5bcb7386d 100644
--- a/crates/effects/src/roto.rs
+++ b/crates/effects/src/roto.rs
@@ -598,6 +598,14 @@ impl Chain {
self.model.as_ref().is_some_and(|m| m.tracker().is_some())
}
+ /// Explicit propagation replaces tracking made by another model. Rendering and Freeze keep
+ /// using saved mattes even when that model is unavailable, so this is only called for Propagate.
+ pub fn prepare_propagation(&mut self) {
+ if self.saved_model().is_some_and(|saved| self.model.as_ref().is_none_or(|model| model.info().id != saved)) {
+ self.saved = None;
+ }
+ }
+
/// Follow the object through the span with the model's tracker, from the strokes on their
/// frames (only the frames without a tracked result yet: after a correction stroke, the frames it
/// changes, tracked on from the nearest prompted frame), and cache every frame's segmentation (under the tracker's keys, with its
@@ -1374,6 +1382,56 @@ mod tests {
assert_eq!(completed, 0, "partial objects are not complete tracked frames");
}
+ #[test]
+ fn propagation_replaces_saved_tracking_from_a_different_model() {
+ let mut d = RotoData::default();
+ d.add(Stroke { kind: StrokeKind::Fg, frame: 0, radius: 3.0, points: vec![[35.0, 40.0]], object: 0 }, 2, [0, 24]);
+ let mut params = params_with(&d);
+ params.values.insert(INPUT_KEY.into(), Value::Str("replace-saved-tracking-model".into()));
+ let size = [100.0, 80.0];
+ let old_model = effectcraft_segment::SAM2_SMALL.id;
+ let old_keys = d.chain_keys(seed_with(&params, size, 25.0, old_model));
+ let empty = vec![0; 100 * 80];
+ let old_frame = TrackFrame { m: rle::encode(&empty), r: rle::encode(&empty), c: 0.2, ..Default::default() };
+ let saved = TrackCache {
+ key: tracks_key(&old_keys),
+ model: old_model.into(),
+ size: [100, 80],
+ frames: old_keys.keys().map(|f| (*f, old_frame.clone())).collect(),
+ ..Default::default()
+ };
+ params.values.insert(TRACKED.into(), Value::Str(saved.to_json()));
+ let mut chain = Chain::new(&params, size, 25.0, 1.0);
+ let fake = Arc::new(TwoObjects(Default::default()));
+ chain.model = Some(fake.clone());
+ chain.keys = d.chain_keys(seed_with(&params, size, 25.0, fake.info().id));
+ let mut get = |f: i64| Some(Arc::new(frame(f as f64 / 25.0)));
+ assert_eq!(chain.saved_model(), Some(old_model));
+ assert_eq!(chain.ensure(0, &mut get, &|| false).unwrap().area(), 0, "rendering keeps the saved matte until propagation");
+ chain.prepare_propagation();
+ assert_eq!(chain.saved_model(), None);
+ assert_eq!(chain.track(&mut get, &|| false, &mut |_| {}).unwrap().unwrap().frames, 3);
+ assert_eq!(fake.0.lock().unwrap().len(), 1, "the newly selected tracker must actually run");
+ assert!(chain.ensure(0, &mut get, &|| false).unwrap().area() > 0);
+ let replacement = tracks_of(&chain).expect("new tracking is saved");
+ assert_eq!(replacement.model, effectcraft_segment::SAM2_TINY.id);
+ assert_eq!(replacement.frames.len(), 3);
+ params.values.insert(TRACKED.into(), Value::Str(replacement.to_json()));
+ let mut reopened = Chain::new(&params, size, 25.0, 1.0);
+ assert_eq!(reopened.saved_model(), Some(effectcraft_segment::SAM2_TINY.id));
+ assert!(reopened.ensure(0, &mut get, &|| false).unwrap().area() > 0, "reopening uses the replacement without loading its model");
+ reopened.model = Some(fake.clone());
+ reopened.keys = chain.keys.clone();
+ reopened.prepare_propagation();
+ assert_eq!(reopened.saved_model(), Some(effectcraft_segment::SAM2_TINY.id));
+ assert_eq!(reopened.track(&mut get, &|| false, &mut |_| {}).unwrap().unwrap().frames, 0, "matching saved tracking is reused");
+ assert_eq!(fake.0.lock().unwrap().len(), 1);
+ let mut classic = Chain::new(&params, size, 25.0, 1.0);
+ classic.model = None;
+ classic.prepare_propagation();
+ assert_eq!(classic.saved_model(), None, "Classic propagation also replaces saved model tracking");
+ }
+
#[test]
fn objects_are_followed_in_one_pass_and_their_mattes_united() {
let mut d = RotoData::default();
diff --git a/crates/engine/src/analysis/roto.rs b/crates/engine/src/analysis/roto.rs
index 399da1869..7a240f9df 100644
--- a/crates/engine/src/analysis/roto.rs
+++ b/crates/engine/src/analysis/roto.rs
@@ -446,7 +446,10 @@ async fn run_work(w: Work, shared: &RotoShared) {
s.object_matte = w.object_matte;
}
let cancel = || shared.cancel.load(Ordering::Relaxed);
- let chain = fx::Chain::new(&w.params, w.size, w.fps, 1.0);
+ let mut chain = fx::Chain::new(&w.params, w.size, w.fps, 1.0);
+ if w.task == RotoTask::Propagate && !w.object_matte {
+ chain.prepare_propagation();
+ }
let model = fx::installed_model();
let key = if w.object_matte { om::frozen_key(&w.params, w.size, w.fps, model.as_deref()) } else { fx::frozen_key(&w.params, w.size, w.fps) };
let mut frozen = FrozenCache { key, ..Default::default() };
diff --git a/crates/engine/src/tests/roto.rs b/crates/engine/src/tests/roto.rs
index e4db67647..b28490a57 100644
--- a/crates/engine/src/tests/roto.rs
+++ b/crates/engine/src/tests/roto.rs
@@ -827,6 +827,9 @@ fn saved_tracking_is_kept_in_the_project_and_applies_when_it_reopens() {
assert_eq!(st["source"], "tracker", "{st}");
assert!(st["notice"].as_str().unwrap().contains("Propagate again"), "{st}");
// A classic Propagate replaces the saved tracking with nothing.
+ // The synthetic source is not a file on disk; give the reopened session its provider before
+ // asking it to recompute frames. The checks above intentionally rendered without that provider.
+ s2.footage = s.footage.clone();
s2.execute_checked("roto.propagate", json!({"layer": clip.0, "direction": "both", "wait": true})).unwrap();
let st = s2.execute("roto.status", json!({"layer": clip.0, "frame": 12})).unwrap();
assert_eq!(st["source"], "classical", "{st}");
@@ -834,6 +837,32 @@ fn saved_tracking_is_kept_in_the_project_and_applies_when_it_reopens() {
let _ = std::fs::remove_dir_all(std::path::Path::new(&path).parent().unwrap());
}
+/// Propagate replaces a fully saved span when a different tracking model is active.
+#[test]
+fn propagating_saved_tracking_uses_the_new_model() {
+ struct OtherModel;
+ impl effectcraft_segment::MaskModel for OtherModel {
+ fn info(&self) -> &'static effectcraft_segment::ModelInfo {
+ &effectcraft_segment::SAM2_SMALL
+ }
+ fn segment(&self, _: &[[f32; 3]], _: usize, _: usize, _: &effectcraft_segment::Prompt) -> effectcraft_segment::Result<Vec<f32>> {
+ Err("no weights".into())
+ }
+ fn tracker(&self) -> Option<&dyn effectcraft_segment::ObjectTracker> {
+ Some(&Exact)
+ }
+ }
+ let (mut s, clip, _uid, path) = saved_project("model-switch");
+ effectcraft_effects::roto::set_model(Some(std::sync::Arc::new(OtherModel)));
+ let result = s.execute_checked("roto.propagate", json!({"layer": clip.0, "direction": "both", "wait": true}));
+ effectcraft_effects::roto::set_model(None);
+ result.unwrap();
+ let status = s.execute("roto.status", json!({"layer": clip.0, "frame": 12})).unwrap();
+ assert_eq!(status["tracker"]["saved"], "sam2.1-hiera-small", "{status}");
+ assert_eq!(status["tracker"]["frames"].as_object().unwrap().len(), 21, "{status}");
+ let _ = std::fs::remove_dir_all(std::path::Path::new(&path).parent().unwrap());
+}
+
/// A tracked clip saved to a project file: the session that tracked it, its layer and instance, and the
/// file.
fn saved_project(name: &str) -> (Session, LayerId, u64, String) {