Skip to content

FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust

engine: effects.setParam takes a choice by name and can write a keyframe

Tommaso Maccagniecfa560ed39d
diff --git a/crates/engine/src/commands.rs b/crates/engine/src/commands.rs
index b5e4c847a..d57b96c60 100644
--- a/crates/engine/src/commands.rs
+++ b/crates/engine/src/commands.rs
@@ -2391,13 +2391,14 @@ fn build() -> Vec<CommandSpec> {
"Set Effect Parameter",
[],
None,
- r##"{"clip":id,"effect":"motion"|index,"param":str,"mask":n?,"value":num|[x,y]|"#rrggbb"|bool|path,"time":ticks?,"merge":bool?,"begin":bool?}"##,
+ r##"{"clip":id,"effect":"motion"|index,"param":str,"mask":n?,"value":num|[x,y]|"#rrggbb"|bool|path|"option name" (a choice takes its index or its name),"time":ticks? (where the keyframe goes when the parameter is animated; a parameter that is not animated gets a new static value whatever the time),"keyframe":bool? (true: write a keyframe at `time` and animate the parameter),"merge":bool?,"begin":bool?}"##,
has_seq,
|s, p| {
let c = clip_p(p, "clip").ok_or_else(|| bad("effects.setParam", "need `clip`"))?;
let pid = str_p(p, "param").ok_or_else(|| bad("effects.setParam", "need `param`"))?.to_string();
let val = p.get("value").cloned().ok_or_else(|| bad("effects.setParam", "need `value`"))?;
let eff = p.get("effect").cloned().unwrap_or(json!("motion"));
+ let keyframe = bool_p(p, "keyframe").unwrap_or(false);
let ph = s.playhead();
let tl = time_p(s, p, "").unwrap_or(ph);
let pq = p.clone();
@@ -2406,7 +2407,7 @@ fn build() -> Vec<CommandSpec> {
if bool_p(p, "begin").unwrap_or(false) {
s.history.merge_key = None;
}
- s.edit_sequence_as("Change Effect Parameter", merge.as_deref(), |q, _, _| {
+ let keyframes = s.edit_sequence_as("Change Effect Parameter", merge.as_deref(), |q, _, _| {
let (_, it) = q.find_item_mut(c).ok_or(filmcraft_edit::EditError::NoItem(c))?;
let mt = it.source_time_at(tl.clamp(it.start, (it.end() - Tick(1)).max(it.start)));
let e = match &eff {
@@ -2415,6 +2416,22 @@ fn build() -> Vec<CommandSpec> {
_ => None,
}
.ok_or_else(|| bad("effects.setParam", "no such effect on clip"))?;
+ // the parameter's definition (a mask's own parameters have none)
+ let def = if pq.get("mask").is_none_or(Value::is_null) { e.def().and_then(|d| d.param(&pid)) } else { None };
+ // a choice takes its option name as well as its index, as transition parameters do
+ let val = match (def.map(|d| &d.kind), &val) {
+ (Some(filmcraft_project::ParamKind::Choice(opts)), Value::String(name)) => {
+ let i = opts
+ .iter()
+ .position(|o| o.eq_ignore_ascii_case(name))
+ .ok_or_else(|| bad("effects.setParam", format!("`{pid}` must be one of {opts:?}")))?;
+ json!(i)
+ }
+ _ => val.clone(),
+ };
+ if keyframe && def.is_some_and(|d| !d.animatable) {
+ return Err(bad("effects.setParam", format!("`{pid}` cannot be animated")));
+ }
if eff == json!("opacity") || eff == json!("motion") {
e.enabled = true;
}
@@ -2426,10 +2443,16 @@ fn build() -> Vec<CommandSpec> {
}
let prm = crate::masks::target_param(e, &pq, &pid).ok_or_else(|| bad("effects.setParam", format!("no param `{pid}`")))?;
let v = json_to_param(&prm.value, &val).ok_or_else(|| bad("effects.setParam", "value has the wrong type"))?;
- prm.set_at(mt, v);
- Ok(())
+ if keyframe {
+ // the stopwatch and the value in one step
+ prm.put_keyframe(mt, v);
+ } else {
+ prm.set_at(mt, v);
+ }
+ Ok(prm.keyframes.len())
})?;
- Ok(Value::Null)
+ // 0 keyframes: the value is static (`time` was not used)
+ Ok(json!({"keyframes": keyframes}))
}
),
cmd!("effects.toggleAnimation", "Toggle Animation", [], None, r#"{"clip":id,"effect":str|index,"param":str,"mask":n?}"#, has_seq, |s, p| {
diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs
index 01d5b8133..5143fdf10 100644
--- a/crates/engine/src/tests.rs
+++ b/crates/engine/src/tests.rs
@@ -282,6 +282,92 @@ fn add_keyframe_toggles_the_keyframe_at_the_playhead() {
assert_eq!(scale(&s), (0, 100.0));
}
+/// `effects.setParam` writes a keyframe only on an animated parameter. An agent that sends a value
+/// with a `time` to a parameter that is not animated gets a static value (the result says so:
+/// 0 keyframes); `"keyframe": true` is the stopwatch and the value in one step.
+#[test]
+fn set_param_writes_a_keyframe_when_asked_or_animated() {
+ let mut s = demo();
+ let c = s.active_sequence().unwrap().video_tracks[0].items[0].id.0;
+ // (keyframes, static value, value at 0.5 s, value at 2.5 s)
+ let scale = |s: &Session| {
+ let it = s.active_sequence().unwrap().find_item(filmcraft_project::ClipId(c)).unwrap().1;
+ let p = &it.effect("motion").unwrap().params["scale"];
+ let at = |seconds: f64| p.f64_at(it.source_time_at(Tick::from_seconds_f64(seconds)));
+ (p.keyframes.len(), p.value.as_f64().unwrap(), at(0.5), at(2.5))
+ };
+ let set = |value: f64, seconds: f64| json!({"clip": c, "effect": "motion", "param": "scale", "value": value, "seconds": seconds});
+ // not animated: the time is not used and the value is static
+ let r = s.execute("effects.setParam", set(80.0, 0.5)).unwrap();
+ assert_eq!(r["keyframes"], 0, "{r}");
+ assert_eq!(scale(&s), (0, 80.0, 80.0, 80.0));
+ // `keyframe`: the parameter becomes animated at that time
+ let mut first = set(60.0, 0.5);
+ first["keyframe"] = json!(true);
+ let r = s.execute("effects.setParam", first).unwrap();
+ assert_eq!(r["keyframes"], 1, "{r}");
+ assert_eq!(scale(&s).0, 1);
+ // animated now, so a plain set at another time is a second keyframe
+ let r = s.execute("effects.setParam", set(120.0, 2.5)).unwrap();
+ assert_eq!(r["keyframes"], 2, "{r}");
+ let (n, _, a, b) = scale(&s);
+ assert_eq!((n, a, b), (2, 60.0, 120.0));
+ // a keyframe that is already there is replaced, not doubled
+ let mut again = set(70.0, 0.5);
+ again["keyframe"] = json!(true);
+ assert_eq!(s.execute("effects.setParam", again).unwrap()["keyframes"], 2);
+ assert_eq!(scale(&s).2, 70.0);
+ s.undo();
+ assert_eq!(scale(&s).2, 60.0);
+ s.undo();
+ assert_eq!(scale(&s).0, 1);
+ s.redo();
+ s.redo();
+ let (n, _, a, b) = scale(&s);
+ assert_eq!((n, a, b), (2, 70.0, 120.0));
+ // a parameter that cannot be animated refuses the keyframe and keeps its value
+ let blend = json!({"clip": c, "effect": "opacity", "param": "blend", "value": 8, "keyframe": true});
+ let e = s.execute("effects.setParam", blend).unwrap_err().to_string();
+ assert!(e.contains("cannot be animated"), "{e}");
+ let it = s.active_sequence().unwrap().find_item(filmcraft_project::ClipId(c)).unwrap().1;
+ assert_eq!(it.effect("opacity").unwrap().params["blend"].value, filmcraft_project::ParamValue::Choice(0));
+ // `keyframe` that is not a bool means no keyframe request
+ let mut odd = set(70.0, 0.5);
+ odd["keyframe"] = json!("yes");
+ assert_eq!(s.execute("effects.setParam", odd).unwrap()["keyframes"], 2);
+}
+
+/// A choice parameter takes its option name as well as its index, as transition parameters do
+/// (`effects.list {"detail": true}` lists the names).
+#[test]
+fn set_param_takes_a_choice_by_name() {
+ use filmcraft_project::ParamValue;
+ let mut s = demo();
+ let c = s.active_sequence().unwrap().video_tracks[0].items[0].id.0;
+ let blend =
+ |s: &Session| s.active_sequence().unwrap().find_item(filmcraft_project::ClipId(c)).unwrap().1.effect("opacity").unwrap().params["blend"].value.clone();
+ let set = |value: serde_json::Value| json!({"clip": c, "effect": "opacity", "param": "blend", "value": value});
+ s.execute("effects.setParam", set(json!("Screen"))).unwrap();
+ assert_eq!(blend(&s), ParamValue::Choice(8));
+ s.execute("effects.setParam", set(json!("linear dodge (add)"))).unwrap();
+ assert_eq!(blend(&s), ParamValue::Choice(10), "names are matched without case");
+ s.execute("effects.setParam", set(json!(12))).unwrap();
+ assert_eq!(blend(&s), ParamValue::Choice(12), "an index works as before");
+ s.undo();
+ assert_eq!(blend(&s), ParamValue::Choice(10));
+ s.redo();
+ assert_eq!(blend(&s), ParamValue::Choice(12));
+ // a name that is not an option is an error that lists the options and changes nothing
+ let e = s.execute("effects.setParam", set(json!("Glow"))).unwrap_err().to_string();
+ assert!(e.contains("Screen") && e.contains("Overlay"), "{e}");
+ for bad in [json!(""), json!(null), json!([8]), json!({"name": "Screen"}), json!(-1), json!(1.5)] {
+ assert!(s.execute("effects.setParam", set(bad.clone())).is_err(), "{bad}");
+ }
+ assert_eq!(blend(&s), ParamValue::Choice(12));
+ // a name is still the wrong type for a parameter that is not a choice
+ assert!(s.execute("effects.setParam", json!({"clip": c, "effect": "motion", "param": "scale", "value": "Screen"})).is_err());
+}
+
#[test]
fn transitions_and_markers() {
let mut s = demo();