#783: Object Matte tool in the Composition viewer

Problem: Object Matte could only be applied through the objectMatte.apply
command; After Effects 26.2 picks the object directly in the viewer (click it,
or drag a box around it).

Fix: an Object Matte tool (with Roto Brush & Refine Edge in the Alt+W slot,
icon drawn in code). In the Composition viewer a click sets a point prompt and
a drag a box prompt (its centre the point) on the footage, solid or comp layer
under the pointer, converted to layer pixels through the layer's transform.
The layer's Object Matte (the selected one, else its last) is re-prompted;
Shift adds another. Everything goes through objectMatte.apply, which gained
`effect` to re-prompt an instance (point, box or no box, reference frame; the
reach kept unless given), so each prompt is one undo step; the effect is then
selected, so its point controls show in the viewer. Hover draws a crosshair
and the outline of the layer that would get the matte (a live matte preview
would need a segmentation per pointer move). Automation ids:
viewer.objectMatte.crosshair / .target / .box, tools.ObjectMatte.

Tests: ui-egui ui_object_matte (hover, click, drag box re-prompts the same
effect, Shift-click adds one, undo steps); engine
tests_roto::object_matte_reprompt_updates_the_instance. Checked with the
headless snapshot example (hover crosshair, box drag isolating a disk).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
vangel76
2026-10-11 13:50:01 +02:00
co-authored by Claude Opus 5.5
parent 4fc7b52351
commit 0c695cff77
9 changed files with 409 additions and 20 deletions
+58 -14
View File
@@ -59,7 +59,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Object Matte",
[],
None,
"{layer?, point: [x, y] (layer pixels: the object), box?: [x0, y0, x1, y1] (layer pixels, around the object), frame? (reference layer frame; default current), track?: follow the object from that frame (default true), before?, after? (frames to follow it; default 30, up to 600)}",
"{layer?, effect? (uid: re-prompt this Object Matte instead of adding one), point: [x, y] (layer pixels: the object), box?: [x0, y0, x1, y1] (layer pixels, around the object), frame? (reference layer frame; default current), track?: follow the object from that frame (default true), before?, after? (frames to follow it; default 30, up to 600)}",
has_comp,
object_matte_apply
),
@@ -455,6 +455,18 @@ fn object_matte_apply(s: &mut Session, p: &Value) -> Result<Value> {
if !layer.has_video() || layer.switches.adjustment || layer.effects().is_none() {
return Err(bad(cmd, "Object Matte works on footage, solid and composition layers"));
}
// Re-prompt an existing instance (the Object Matte tool on a layer that has one).
let existing = match p.get("effect").filter(|v| !v.is_null()) {
Some(v) => {
let uid = v.as_u64().ok_or_else(|| bad(cmd, "`effect` is an Object Matte effect's uid"))?;
layer
.effects()
.and_then(|f| f.groups().find(|g| g.uid == uid && rt::is_object_matte(g)))
.ok_or_else(|| bad(cmd, format!("no Object Matte effect {uid} on this layer")))?;
Some(uid)
}
None => None,
};
let size = rt::layer_size(&s.project, comp, layer);
let point = xy_p(p, "point", 2, cmd)?.ok_or_else(|| bad(cmd, "missing `point` [x, y] (layer pixels)"))?;
let inside = |x: f64, y: f64| (0.0..=size[0]).contains(&x) && (0.0..=size[1]).contains(&y);
@@ -473,42 +485,74 @@ fn object_matte_apply(s: &mut Session, p: &Value) -> Result<Value> {
if frame < limits[0] || frame > limits[1] {
return Err(bad(cmd, format!("frame {frame} is outside the layer ({}–{})", limits[0], limits[1])));
}
let track = b_p(p, "track").unwrap_or(true);
let reach = |k: &str| f_p(p, k).filter(|v| v.is_finite()).unwrap_or(om::DEFAULT_REACH).round().clamp(0.0, om::MAX_REACH);
// A new instance gets the defaults; a re-prompted one keeps what is not given.
let track = b_p(p, "track").or(existing.is_none().then_some(true));
let reach = |k: &str| {
let v = f_p(p, k).filter(|v| v.is_finite());
let v = if existing.is_none() { Some(v.unwrap_or(om::DEFAULT_REACH)) } else { v };
v.map(|v| v.round().clamp(0.0, om::MAX_REACH))
};
let (before, after) = (reach("before"), reach("after"));
let spec = effectcraft_effects::find(om::ID).ok_or_else(|| EngineError::Other("Object Matte effect missing".into()))?;
let uid = s.edit("Object Matte", None, |proj, _| {
let label = if existing.is_some() { "Object Matte Prompt" } else { "Object Matte" };
let uid = s.edit(label, None, |proj, _| {
let mut next = proj.next_id;
let l = layer_mut(proj, cid, lid)?;
let fxg = l.props.sub_mut("effects").ok_or_else(|| bad(cmd, "this layer has no effects"))?;
let n = fxg.groups().filter(|g| rt::is_object_matte(g)).count();
let name = if n == 0 { spec.name.to_string() } else { format!("{} {}", spec.name, n + 1) };
let mut g = effectcraft_effects::instantiate(spec, &mut Ids(&mut next), &name, size);
let mut fresh = None;
let g = match existing {
Some(u) => fxg.find_group_mut(u).ok_or_else(|| bad(cmd, "the Object Matte effect is gone"))?,
None => {
let n = fxg.groups().filter(|g| rt::is_object_matte(g)).count();
let name = if n == 0 { spec.name.to_string() } else { format!("{} {}", spec.name, n + 1) };
fresh.insert(effectcraft_effects::instantiate(spec, &mut Ids(&mut next), &name, size))
}
};
let mut set = |path: &str, v: KV| {
let (grp, leaf) = path.rsplit_once('/').map_or((None, path), |(a, b)| (Some(a), b));
let target = match grp {
Some(sub) => g.sub_mut(sub),
None => Some(&mut g),
None => Some(&mut *g),
};
// The prompt is static: a re-prompt replaces any keyframes.
if let Some(pr) = target.and_then(|t| t.get_mut(leaf)) {
pr.value = v;
pr.keys.clear();
}
};
set("objectPoint", KV::Vec2([point[0], point[1]]));
set("useBox", KV::Bool(bx.is_some()));
if let Some(b) = &bx {
set("useBox", KV::Bool(true));
set("boxTopLeft", KV::Vec2([b[0].min(b[2]), b[1].min(b[3])]));
set("boxBottomRight", KV::Vec2([b[0].max(b[2]), b[1].max(b[3])]));
}
set("objectTracking/trackObject", KV::Bool(track));
if let Some(t) = track {
set("objectTracking/trackObject", KV::Bool(t));
}
set("objectTracking/referenceFrame", KV::Scalar(frame as f64));
set("objectTracking/framesBefore", KV::Scalar(before));
set("objectTracking/framesAfter", KV::Scalar(after));
if let Some(v) = before {
set("objectTracking/framesBefore", KV::Scalar(v));
}
if let Some(v) = after {
set("objectTracking/framesAfter", KV::Scalar(v));
}
let u = g.uid;
fxg.children.push(g.into());
if let Some(g) = fresh {
fxg.children.push(g.into());
}
proj.next_id = proj.next_id.max(next);
Ok(u)
})?;
s.state.selected_layers = vec![lid];
Ok(json!({"layer": lid.0, "effect": uid, "frame": frame, "track": track, "span": [frame - before as i64, frame + after as i64]}))
let (track, before, after) = object_matte_reach(s, cid, lid, uid);
Ok(json!({"layer": lid.0, "effect": uid, "frame": frame, "track": track, "updated": existing.is_some(), "span": [frame - before, frame + after]}))
}
/// Track Object and Frames Before / After of an Object Matte instance (static values).
fn object_matte_reach(s: &Session, cid: ItemId, lid: LayerId, uid: Uid) -> (bool, i64, i64) {
use effectcraft_effects::object_matte as om;
let Ok((_, g)) = group(s, cid, lid, uid) else { return (true, 0, 0) };
let ps = params_static(g);
let [before, after] = om::reach(&ps);
(om::tracks(&ps), before, after)
}
+39
View File
@@ -426,3 +426,42 @@ fn object_matte_from_a_click() {
let g = s.active_comp().unwrap().layer(clip).unwrap().props.find_group(r["effect"].as_u64().unwrap()).unwrap().clone();
assert_eq!(g.name, "Object Matte 2");
}
/// `objectMatte.apply` with `effect` re-prompts that instance in one undo step: the new point and
/// box, the box dropped when none is given, the reach kept unless given.
#[test]
fn object_matte_reprompt_updates_the_instance() {
let (mut s, clip, solid) = setup(|f| draw(center(f)));
let cid = s.active_comp_id().unwrap();
s.edit("remove solid", None, |p, _| {
p.comp_mut(cid).unwrap().layers.retain(|l| l.id != solid);
Ok(())
})
.unwrap();
let c = center(0);
let r = s
.execute_checked(
"objectMatte.apply",
json!({"layer": clip.0, "point": c, "box": [c[0] - 50.0, c[1] - 50.0, c[0] + 50.0, c[1] + 50.0], "after": 6, "frame": 0}),
)
.unwrap();
let uid = r["effect"].as_u64().unwrap();
let count = |s: &Session| s.active_comp().unwrap().layer(clip).unwrap().effects().unwrap().groups().count();
let ps = |s: &Session| crate::roto::params_static(s.active_comp().unwrap().layer(clip).unwrap().props.find_group(uid).unwrap());
let undo0 = s.history.undo.len();
let r = s.execute_checked("objectMatte.apply", json!({"layer": clip.0, "effect": uid, "point": [c[0] + 4.0, c[1]], "frame": 2})).unwrap();
assert_eq!((r["effect"].as_u64(), r["updated"].as_bool()), (Some(uid), Some(true)));
assert_eq!(r["span"], json!([-28, 8]));
assert_eq!(count(&s), 1);
assert_eq!(s.history.undo.len(), undo0 + 1);
let p = ps(&s);
assert_eq!(p.get("objectPoint"), Some(&effectcraft_keyframe::Value::Vec2([c[0] + 4.0, c[1]])));
assert_eq!(p.get("useBox"), Some(&effectcraft_keyframe::Value::Bool(false)));
assert_eq!(effectcraft_effects::object_matte::reference_frame(&p), 2);
assert_eq!(effectcraft_effects::object_matte::reach(&p), [30, 6]);
// Undo restores the first prompt.
s.execute("edit.undo", json!({})).unwrap();
assert_eq!(ps(&s).get("useBox"), Some(&effectcraft_keyframe::Value::Bool(true)));
// An unknown or non-Object Matte effect is refused.
assert!(s.execute_checked("objectMatte.apply", json!({"layer": clip.0, "effect": 999_999, "point": c})).is_err());
}