Reversed keyframes keep the shape of manually eased curves (#4283)

* Reversed keyframes keep the shape of manually eased value curves

Time-Reverse Keyframes and Paste Reversed Keyframes swapped the ease sides
but kept the signs of numeric component speeds, which are signed value
slopes. Mirror them now, while spatial and Orientation speeds along the
path or arc and progress eases of paths stay positive.

Fixes #4273

* Paste Reversed Keyframes flips ease speeds by the property pasted into

The sign of the speeds was decided by the copied property, so Scale keys
pasted reversed into Position got negated speeds, which run a motion path
backwards. Paste Reversed now swaps the sides and keys.paste flips the
speeds for each property the keys land in: value curves flip, motion paths
and Orientation stay positive.
This commit is contained in:
Valentin Klein
2026-12-01 11:25:14 +01:00
committed by GitHub
parent 9f3e8ef810
commit c228ce284b
7 changed files with 187 additions and 16 deletions
+12 -2
View File
@@ -42,7 +42,7 @@ fn copy(s: &mut Session, _: &Value) -> Result<Value> {
for k in &mut keys {
k.time = l.comp_time(k.time) - earliest;
}
clip.push(KeyClip { layer: lid, path, keys });
clip.push(KeyClip { layer: lid, path, keys, mirrored: false });
}
let n: usize = clip.iter().map(|c| c.keys.len()).sum();
s.state.keyframe_text = crate::keyframe_text::text(&s.project, comp, &s.state.selected_keys, &[]);
@@ -145,6 +145,10 @@ fn paste(s: &mut Session, p: &Value) -> Result<Value> {
if let Some(v) = pasted_value(&pr.value, &k.value) {
k.value = v;
}
// Paste Reversed Keyframes: the sign of the speeds follows the property pasted into.
if clip[*ci].mirrored {
effectcraft_keyframe::flip_ease_speeds(&mut k, eases_along_arc(pr));
}
if pr.hold_only {
k = k.hold();
}
@@ -518,6 +522,12 @@ fn info(s: &mut Session, p: &Value) -> Result<Value> {
Ok(json!(out))
}
/// Whether the property's 2D/3D eases are speeds along a motion path or a rotation arc: spatial
/// properties, and 3D Orientation, which `Property::value_at` routes to the shortest-arc slerp.
pub(super) fn eases_along_arc(pr: &Property) -> bool {
pr.spatial || (pr.match_id == "orientation" && matches!(pr.value, KValue::Vec3(_)))
}
/// Keyframe Assistant ▸ Time-Reverse Keyframes: mirror the selected keys of each property.
fn time_reverse(s: &mut Session, _: &Value) -> Result<Value> {
let cid = s.active_comp_id().ok_or(EngineError::NoComp)?;
@@ -538,7 +548,7 @@ fn time_reverse(s: &mut Session, _: &Value) -> Result<Value> {
}
let Some(pr) = layer.props.find_mut(uid) else { continue };
let (mut picked, rest): (Vec<Keyframe>, Vec<Keyframe>) = pr.keys.drain(..).partition(|k| times.contains(&k.time));
effectcraft_keyframe::time_reverse(&mut picked);
effectcraft_keyframe::time_reverse(&mut picked, eases_along_arc(pr));
new_sel.extend(picked.iter().map(|k| KeyRef { layer: lid, prop: uid, time: k.time }));
pr.keys = rest;
for k in picked {
+6 -5
View File
@@ -731,8 +731,10 @@ fn label(s: &mut Session, p: &Value) -> Result<Value> {
}
/// Edit ▸ Paste Reversed Keyframes: the copied keys mirrored in time over their whole span (the
/// last key first, incoming and outgoing eases swapped), pasted like Edit ▸ Paste
/// (`keys.paste`: at the current time, into the selected or the same properties).
/// last key first, incoming and outgoing eases swapped), pasted like Edit ▸ Paste (`keys.paste`:
/// at the current time, into the selected or the same properties). The ease speeds change sign in
/// `keys.paste`, by the kind of each property the keys land in (a source that no longer exists
/// does not matter).
fn paste_reversed(s: &mut Session, p: &Value) -> Result<Value> {
let original = s.state.key_clipboard.clone();
let span = original.iter().flat_map(|c| c.keys.iter().map(|k| k.time)).max().unwrap_or(Tick::ZERO);
@@ -746,12 +748,11 @@ fn paste_reversed(s: &mut Session, p: &Value) -> Result<Value> {
.map(|k| {
let mut k = k.clone();
k.time = span - k.time;
std::mem::swap(&mut k.in_interp, &mut k.out_interp);
std::mem::swap(&mut k.in_ease, &mut k.out_ease);
std::mem::swap(&mut k.spatial_in, &mut k.spatial_out);
effectcraft_keyframe::swap_key_sides(&mut k);
k
})
.collect(),
mirrored: true,
..c.clone()
})
.collect();
+1 -1
View File
@@ -286,7 +286,7 @@ fn extract_face(s: &mut Session, p: &Value) -> Result<Value> {
k
})
.collect();
clip.push(crate::KeyClip { layer: lid, path, keys });
clip.push(crate::KeyClip { layer: lid, path, keys, mirrored: false });
}
s.state.key_clipboard = clip;
s.state.keyframe_text = None;
+5
View File
@@ -27,6 +27,11 @@ pub struct KeyClip {
pub path: String,
/// Keys with times relative to the earliest copied key (layer time).
pub keys: Vec<effectcraft_keyframe::Keyframe>,
/// Set by Paste Reversed Keyframes on the mirrored copy it pastes: the keys' sides are already
/// swapped, and `keys.paste` still has to flip the signs of the ease speeds for the property
/// each key lands in, which may be of another kind than the source.
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub mirrored: bool,
}
/// A selected mask vertex: layer, mask group uid, vertex index.
+73
View File
@@ -405,6 +405,79 @@ fn time_reverse_keyframes() {
undo_redo_roundtrip(&mut s, after, |s| assert_eq!(prop(s, l, "transform/opacity").keys[0].value, KV::Scalar(0.0)));
}
/// #4273: Time-Reverse Keyframes and Paste Reversed Keyframes play manually eased Opacity,
/// Position and Orientation backwards without changing the curve: each sample of the reversed keys
/// equals the original at the mirrored time.
#[test]
fn reversed_keys_keep_manual_eases() {
let (mut s, l) = setup();
s.execute("layer.setSwitch", json!({"layers": [l], "switch": "threeD", "value": true})).unwrap();
let paths = ["transform/opacity", "transform/position", "transform/orientation"];
let ends = [(json!(0), json!(100)), (json!([0, 0, 0]), json!([200, 50, 0])), (json!([0, 0, 0]), json!([80, 0, 0]))];
for (path, (a, b)) in paths.iter().zip(ends) {
animate(&mut s, l, path, &[(0.0, a), (1.0, b)]);
s.execute("keys.setEase", json!({"layer": l, "path": path, "time": 0.0, "side": "out", "speed": 160, "influence": 33.3})).unwrap();
s.execute("keys.setEase", json!({"layer": l, "path": path, "time": 1.0, "side": "in", "speed": 40, "influence": 33.3})).unwrap();
}
let before: Vec<Property> = paths.iter().map(|p| prop(&s, l, p)).collect();
let mirrored = |s: &Session, layer: u64| {
for (path, orig) in paths.iter().zip(&before) {
let rev = prop(s, layer, path);
for i in 1..20 {
let t = effectcraft_time::Tick::from_seconds_f64(f64::from(i) / 20.0);
let (a, b) = (orig.value_at(t).components(), rev.value_at(effectcraft_time::Tick::from_seconds_f64(1.0) - t).components());
assert!(a.iter().zip(&b).all(|(x, y)| (x - y).abs() < 1e-2), "{path} at {i}/20: {a:?} vs reversed {b:?}");
}
}
};
s.execute("keys.selectAll", json!({"layers": [l]})).unwrap();
s.execute("keys.timeReverse", json!({})).unwrap();
mirrored(&s, l);
// Paste Reversed onto a second 3D layer mirrors the copied keys the same way.
s.execute("edit.undo", json!({})).unwrap();
s.execute("keys.selectAll", json!({"layers": [l]})).unwrap();
s.execute("keys.copy", json!({})).unwrap();
let m = s.execute("layer.newSolid", json!({"color": "#ffffff", "width": 100, "height": 100})).unwrap()["layer"].as_u64().unwrap();
s.execute("layer.setSwitch", json!({"layers": [m], "switch": "threeD", "value": true})).unwrap();
s.execute("time.set", json!({"time": 0.0})).unwrap();
s.execute("layer.select", json!({"layers": [m]})).unwrap();
s.execute("edit.pasteReversedKeyframes", json!({})).unwrap();
mirrored(&s, m);
}
/// #4273: Paste Reversed Keyframes decides the sign of the ease speeds by the property the keys land
/// in, not only by the one they were copied from. Scale keys pasted into Position become a motion
/// path whose speeds stay positive (negated ones would run the arc backwards); keys pasted into
/// Scale, from Position or from Scale, have their speeds flipped like any value curve.
#[test]
fn paste_reversed_follows_the_destination_property() {
let (mut s, a) = setup();
let new_solid = |s: &mut Session| s.execute("layer.newSolid", json!({"color": "#ffffff", "width": 100, "height": 100})).unwrap()["layer"].as_u64().unwrap();
let b = new_solid(&mut s);
for (layer, path, from, to) in [(a, "transform/scale", json!([100, 100]), json!([150, 150])), (b, "transform/position", json!([10, 20]), json!([200, 80]))]
{
animate(&mut s, layer, path, &[(0.0, from), (1.0, to)]);
s.execute("keys.setEase", json!({"layer": layer, "path": path, "time": 0.0, "side": "out", "speed": 160, "influence": 33.3})).unwrap();
s.execute("keys.setEase", json!({"layer": layer, "path": path, "time": 1.0, "side": "in", "speed": 40, "influence": 33.3})).unwrap();
}
// The pasted keys' (first key out, last key in) speeds: the sides swap, so (40, 160) are the
// originals (160, 40) swapped.
let mut paste_reversed = |source: u64, into: &str| {
let target = new_solid(&mut s);
s.execute("keys.selectAll", json!({"layers": [source]})).unwrap();
s.execute("keys.copy", json!({})).unwrap();
s.execute("time.set", json!({"time": 0.0})).unwrap();
s.execute("layer.select", json!({"layers": [target]})).unwrap();
s.execute("edit.pasteReversedKeyframes", json!({"path": into})).unwrap();
let keys = prop(&s, target, into).keys;
assert_eq!(keys.len(), 2, "{into}");
(keys[0].out_ease[0].speed, keys[1].in_ease[0].speed)
};
assert_eq!(paste_reversed(a, "transform/position"), (40.0, 160.0), "Scale into Position");
assert_eq!(paste_reversed(b, "transform/scale"), (-40.0, -160.0), "Position into Scale");
assert_eq!(paste_reversed(a, "transform/scale"), (-40.0, -160.0), "Scale into Scale");
}
#[test]
fn pen_tool_draws_edits_and_closes_a_mask() {
let (mut s, l) = setup();
+33 -5
View File
@@ -526,20 +526,48 @@ pub fn retime_roving(keys: &mut [Keyframe], spatial: bool) -> bool {
}
/// Reverse a run of keys in time within `[first, last]` (Time-Reverse Keyframes): key `i` moves
/// to `first + last - t` and in/out sides swap.
pub fn time_reverse(keys: &mut [Keyframe]) {
/// to `first + last - t` and in/out sides swap (see [`mirror_key_sides`] for `arc_speed`).
pub fn time_reverse(keys: &mut [Keyframe], arc_speed: bool) {
let (Some(first), Some(last)) = (keys.first().map(|k| k.time), keys.last().map(|k| k.time)) else { return };
for k in keys.iter_mut() {
// Imported Tick values may span i64's full range; keep the intermediate exact.
let reversed = i128::from(first.0) + i128::from(last.0) - i128::from(k.time.0);
k.time = Tick(reversed.clamp(i128::from(i64::MIN), i128::from(i64::MAX)) as i64);
std::mem::swap(&mut k.in_interp, &mut k.out_interp);
std::mem::swap(&mut k.in_ease, &mut k.out_ease);
std::mem::swap(&mut k.spatial_in, &mut k.spatial_out);
mirror_key_sides(k, arc_speed);
}
keys.reverse();
}
/// Swap a key's incoming and outgoing sides for playing it backwards in time. Numeric component
/// eases are signed value derivatives, so their speeds change sign and the mirrored curve keeps its
/// shape. `arc_speed` marks a property whose 2D/3D eases are speeds along a motion path or a
/// rotation arc (spatial properties, 3D Orientation); those, like the progress eases of paths and
/// gradients, stay positive.
pub fn mirror_key_sides(k: &mut Keyframe, arc_speed: bool) {
swap_key_sides(k);
flip_ease_speeds(k, arc_speed);
}
/// Swap a key's incoming and outgoing interpolation, eases and spatial tangents.
pub fn swap_key_sides(k: &mut Keyframe) {
std::mem::swap(&mut k.in_interp, &mut k.out_interp);
std::mem::swap(&mut k.in_ease, &mut k.out_ease);
std::mem::swap(&mut k.spatial_in, &mut k.spatial_out);
}
/// The sign change of [`mirror_key_sides`] on its own: negate the speeds of a key's numeric
/// component eases unless `arc_speed` applies to its 2D/3D value (or the value has no numeric
/// components).
pub fn flip_ease_speeds(k: &mut Keyframe, arc_speed: bool) {
let along_arc = arc_speed && matches!(k.value, Value::Vec2(_) | Value::Vec3(_));
if !along_arc && k.value.dims() > 0 {
// A zero speed stays 0.0 rather than becoming -0.0 in saved projects.
for e in k.in_ease.iter_mut().chain(&mut k.out_ease).filter(|e| e.speed != 0.0) {
e.speed = -e.speed;
}
}
}
/// Evaluate a keyframed property at `t`. `spatial` selects motion-path semantics for 2D/3D values.
/// Muted keys are skipped, so the active neighbours interpolate as if they were not there; `None`
/// means every key is muted (the property's static value applies).
+57 -3
View File
@@ -20,12 +20,12 @@ fn time_reverse_handles_deserialized_tick_extremes() {
// Tick's public serde representation can contain values beyond from_seconds_f64's cap.
for (first, last) in [(i64::MIN, i64::MIN + 10), (i64::MAX - 10, i64::MAX), (i64::MIN, i64::MAX)] {
let mut keys = vec![Keyframe::new(Tick(first), Value::Scalar(1.0)), Keyframe::new(Tick(last), Value::Scalar(2.0))];
time_reverse(&mut keys);
time_reverse(&mut keys, false);
assert_eq!(keys[0].time, Tick(first));
assert_eq!(keys[0].value, Value::Scalar(2.0));
assert_eq!(keys[1].time, Tick(last));
assert_eq!(keys[1].value, Value::Scalar(1.0));
time_reverse(&mut keys);
time_reverse(&mut keys, false);
assert_eq!(keys[0].value, Value::Scalar(1.0));
}
}
@@ -567,12 +567,66 @@ fn time_reverse_mirrors_keys() {
let mut keys =
vec![Keyframe::new(s(1.0), Value::Scalar(0.0)).eased(), Keyframe::new(s(2.0), Value::Scalar(5.0)), Keyframe::new(s(4.0), Value::Scalar(10.0))];
keys[0].out_interp = Interp::Hold;
time_reverse(&mut keys);
time_reverse(&mut keys, false);
assert_eq!(keys.iter().map(|k| k.time.seconds().round() as i64).collect::<Vec<_>>(), vec![1, 3, 4]);
assert_eq!(keys[0].value, Value::Scalar(10.0));
assert_eq!(keys[2].in_interp, Interp::Hold);
}
/// #4273: a reversed animation plays the original backwards, so every sample of the reversed keys
/// equals the original at the mirrored time. Value eases are signed slopes (their sign flips);
/// spatial and Orientation eases are speeds along the path or arc (they stay positive).
#[test]
fn time_reverse_keeps_the_eased_curve_shape() {
let manual = |t: f64, v: Value, out: f64, inn: f64| {
let mut k = Keyframe::new(s(t), v).eased();
let n = k.out_ease.len();
k.out_ease = vec![Ease { speed: out, influence: 1.0 / 3.0 }; n];
k.in_ease = vec![Ease { speed: inn, influence: 1.0 / 3.0 }; n];
k
};
let assert_mirrored = |keys: &[Keyframe], arc: bool, eval: &dyn Fn(&[Keyframe], Tick) -> Value, tol: f64| {
let mut rev = keys.to_vec();
time_reverse(&mut rev, arc);
let (first, last) = (keys[0].time.0, keys[keys.len() - 1].time.0);
for i in 1..40 {
let t = first + (last - first) / 40 * i;
let (a, b) = (eval(keys, Tick(t)).components(), eval(&rev, Tick(first + last - t)).components());
assert!(a.iter().zip(&b).all(|(x, y)| (x - y).abs() < tol), "at {i}/40: {a:?} vs reversed {b:?}");
}
};
let value = |keys: &[Keyframe], t: Tick| evaluate(keys, t, false).unwrap_or(Value::Scalar(f64::NAN));
// The issue's case: 36.25 at 0.25 s must come back at 0.75 s.
let issue = [manual(0.0, Value::Scalar(0.0), 160.0, 0.0), manual(1.0, Value::Scalar(100.0), 0.0, 40.0)];
assert!((evaluate(&issue, s(0.25), false).unwrap().as_f64() - 36.25).abs() < 1e-6);
assert_mirrored(&issue, false, &value, 1e-6);
// Three keys whose middle key has different in and out speeds.
let three = [manual(0.0, Value::Scalar(0.0), 20.0, 0.0), manual(1.0, Value::Scalar(50.0), 90.0, -30.0), manual(3.0, Value::Scalar(10.0), 0.0, 5.0)];
assert_mirrored(&three, false, &value, 1e-6);
// A non-spatial vector (Scale) whose components move in opposite directions.
let scale = [manual(0.0, Value::Vec2([0.0, 100.0]), 160.0, 0.0), manual(1.0, Value::Vec2([100.0, 0.0]), 0.0, -40.0)];
assert_mirrored(&scale, false, &value, 1e-6);
// A curved motion path with manual tangents and unequal speeds along it.
let mut path = [manual(0.0, Value::Vec2([0.0, 0.0]), 300.0, 0.0), manual(1.0, Value::Vec2([100.0, 0.0]), 0.0, 60.0)];
for k in &mut path {
k.out_ease.truncate(1);
k.in_ease.truncate(1);
k.spatial_auto = false;
}
path[0].spatial_out = [30.0, 60.0, 0.0];
path[1].spatial_in = [-20.0, 40.0, 0.0];
let spatial = |keys: &[Keyframe], t: Tick| evaluate(keys, t, true).unwrap_or(Value::Scalar(f64::NAN));
assert_mirrored(&path, true, &spatial, 1e-2);
// Orientation turns at a speed along the shortest arc.
let mut turn = [manual(0.0, Value::Vec3([0.0; 3]), 200.0, 0.0), manual(1.0, Value::Vec3([80.0, 0.0, 0.0]), 0.0, 30.0)];
for k in &mut turn {
k.out_ease.truncate(1);
k.in_ease.truncate(1);
}
let orientation = |keys: &[Keyframe], t: Tick| evaluate_orientation(keys, t).unwrap_or(Value::Scalar(f64::NAN));
assert_mirrored(&turn, true, &orientation, 1e-6);
}
proptest::proptest! {
#[test]
fn eased_values_stay_in_range(t in 0.0f64..1.0, i0 in 0.01f64..1.0, i1 in 0.01f64..1.0) {