Skip to content

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

Report parameter and accepted value from effects.setParam (#3281)

bzeb5618273562
diff --git a/crates/engine/src/commands.rs b/crates/engine/src/commands.rs
index ccd452420..8f54af3e9 100644
--- a/crates/engine/src/commands.rs
+++ b/crates/engine/src/commands.rs
@@ -1177,6 +1177,7 @@ fn marker_list_mut(s: &mut Session) -> Result<(ItemId, Tick)> {
Ok((seq, s.playhead()))
}
+/// Builds the command registry shared by the UI, CLI and control interfaces.
fn build() -> Vec<CommandSpec> {
let mut v = vec![
// ================= File =================
@@ -3146,7 +3147,7 @@ fn build() -> Vec<CommandSpec> {
if bool_p(p, "begin").unwrap_or(false) {
s.history.merge_key = None;
}
- let keyframes = s.edit_sequence_as("Change Effect Parameter", merge.as_deref(), |q, _, _| {
+ let (keyframes, value) = 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(at.unwrap_or_else(|| ph.clamp(it.start, (it.end() - Tick(1)).max(it.start))));
let e = match &eff {
@@ -3200,6 +3201,8 @@ fn build() -> Vec<CommandSpec> {
};
(keyframes, v)
};
+ let accepted_value = param_value_to_json(&v);
+
if e.effect == "ultra_key"
&& pid == "setting"
&& let ParamValue::Choice(setting) = v
@@ -3214,10 +3217,10 @@ fn build() -> Vec<CommandSpec> {
}
}
}
- Ok(keyframes)
+ Ok((keyframes, accepted_value))
})?;
// 0 keyframes: the value is static (`time` was not used)
- Ok(json!({"keyframes": keyframes}))
+ Ok(json!({"keyframes": keyframes, "param": pid, "value": value}))
}
),
cmd!("effects.toggleAnimation", "Toggle Animation", [], None, r#"{"clip":id,"effect":str|index,"param":str,"mask":n?}"#, has_seq, |s, p| {
@@ -3743,6 +3746,7 @@ fn set_work_area_point(s: &mut Session, p: &Value, is_in: bool) -> Result<Value>
Ok(json!({"start": start.0, "end": end.0}))
}
+/// Decodes caller JSON using the stored parameter as a type template.
pub(crate) fn json_to_param(template: &ParamValue, v: &Value) -> Option<ParamValue> {
Some(match template {
ParamValue::Float(_) => ParamValue::Float(v.as_f64()?),
@@ -3761,7 +3765,7 @@ pub(crate) fn json_to_param(template: &ParamValue, v: &Value) -> Option<ParamVal
_ => return None,
},
ParamValue::Bool(_) => ParamValue::Bool(v.as_bool()?),
- ParamValue::Choice(_) => ParamValue::Choice(v.as_u64()? as u32),
+ ParamValue::Choice(_) => ParamValue::Choice(u32::try_from(v.as_u64()?).ok()?),
ParamValue::Text(_) => ParamValue::Text(v.as_str()?.to_string()),
ParamValue::Path(_) => ParamValue::Path(crate::masks::path_from_json(v)?),
ParamValue::Curve(_) => {
@@ -3770,6 +3774,20 @@ pub(crate) fn json_to_param(template: &ParamValue, v: &Value) -> Option<ParamVal
})
}
+/// Encode the accepted parameter value in the JSON shape returned to command callers.
+fn param_value_to_json(value: &ParamValue) -> Value {
+ match value {
+ ParamValue::Float(value) => json!(value),
+ ParamValue::Vec2(value) => json!([value.x, value.y]),
+ ParamValue::Color(value) => json!(value),
+ ParamValue::Bool(value) => json!(value),
+ ParamValue::Choice(value) => json!(value),
+ ParamValue::Text(value) => json!(value),
+ ParamValue::Curve(value) => json!(value),
+ ParamValue::Path(value) => crate::masks::path_to_json(value),
+ }
+}
+
fn copy_selection(s: &mut Session) {
let Some(q) = s.active_sequence() else { return };
let sel = with_links(s, &s.state.selection);
diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs
index fe3e35133..1161a2c3b 100644
--- a/crates/engine/src/tests.rs
+++ b/crates/engine/src/tests.rs
@@ -349,11 +349,11 @@ fn set_param_writes_a_keyframe_when_asked_or_animated() {
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!(r, json!({"keyframes": 1, "param": "scale", "value": 60.0}));
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}");
+ assert_eq!(r, json!({"keyframes": 2, "param": "scale", "value": 120.0}));
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
@@ -381,6 +381,38 @@ fn set_param_writes_a_keyframe_when_asked_or_animated() {
assert_eq!(s.execute("effects.setParam", odd).unwrap()["keyframes"], 2);
}
+/// The response reflects the stored point when extra input coordinates are ignored.
+#[test]
+fn set_param_reports_the_stored_vector_value() {
+ let mut s = demo();
+ let c = s.active_sequence().unwrap().video_tracks[0].items[0].id.0;
+ let result = s.execute("effects.setParam", json!({"clip": c, "effect": "motion", "param": "position", "value": [11.0, 22.0, 33.0]})).unwrap();
+ assert_eq!(result, json!({"keyframes": 0, "param": "position", "value": [11.0, 22.0]}));
+
+ let sequence = s.execute("sequence.inspect", json!({})).unwrap();
+ let effects = sequence["video"][0]["items"][0]["effects"].as_array().unwrap();
+ let motion = effects.iter().find(|e| e["effect"] == "motion").unwrap();
+ assert_eq!(motion["params"]["position"]["value"], "Vec2(Vec2 { x: 11.0, y: 22.0 })");
+}
+
+/// A static edit succeeds without creating keyframes; its response identifies the accepted value.
+#[test]
+fn set_param_reports_static_value_and_parameter() {
+ let mut s = demo();
+ let c = s.active_sequence().unwrap().video_tracks[0].items[0].id.0;
+ for (effect, param, value, stored, inspected) in
+ [("opacity", "blend", json!("Screen"), json!(8), "Choice(8)"), ("motion", "scale", json!(80.0), json!(80.0), "Float(80.0)")]
+ {
+ let result = s.execute("effects.setParam", json!({"clip": c, "effect": effect, "param": param, "value": value})).unwrap();
+ assert_eq!(result, json!({"keyframes": 0, "param": param, "value": stored}));
+ let sequence = s.execute("sequence.inspect", json!({})).unwrap();
+ let effects = sequence["video"][0]["items"][0]["effects"].as_array().unwrap();
+ let effect = effects.iter().find(|e| e["effect"] == effect).unwrap();
+ assert_eq!(effect["params"][param]["value"], inspected);
+ assert_eq!(effect["params"][param]["keyframes"], 0);
+ }
+}
+
/// A keyframe set with a `time` outside the clip stays at that time (#1055), before the start as
/// after the end: a ramp that spans a cut shows its true slice, and a clip-end keyframe is not
/// overwritten by a later one. Only the playhead fallback is pinned to the clip.
@@ -457,7 +489,7 @@ fn set_param_takes_a_choice_by_name() {
// 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)] {
+ for bad in [json!(""), json!(null), json!([8]), json!({"name": "Screen"}), json!(-1), json!(1.5), json!(u64::from(u32::MAX) + 1)] {
assert!(s.execute("effects.setParam", set(bad.clone())).is_err(), "{bad}");
}
assert_eq!(blend(&s), ParamValue::Choice(12));
diff --git a/docs/agents.md b/docs/agents.md
index b680ce99a..34c49e1ac 100644
--- a/docs/agents.md
+++ b/docs/agents.md
@@ -38,6 +38,16 @@ claude mcp add filmcraft-headless -- /abs/path/filmcraft/target/release/filmcraf
{ "mcpServers": { "filmcraft": { "command": "/abs/path/filmcraft-cli", "args": ["mcp", "--bridge", "127.0.0.1:9876"] } } }
```
+### Effect parameter replies
+
+`effects.setParam` returns `{"keyframes": n, "param": id, "value": value}` on success.
+The keyframe count is zero for a static parameter: this is a successful static edit,
+not a failure. `param` identifies the edited parameter and `value` reports the accepted
+value; a choice supplied by name is returned as its numeric option index. An animated
+edit reports the value written at the requested time, rather than the static fallback.
+For example, setting opacity `blend` to `"Screen"` returns
+`{"keyframes": 0, "param": "blend", "value": 8}`.
+
### From an installed release
The release packages ship `filmcraft-cli` alongside the desktop app, so no build is needed: