#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:
co-authored by
Claude Opus 5.5
parent
4fc7b52351
commit
0c695cff77
@@ -34,6 +34,8 @@ pub enum Icon {
|
||||
Eraser,
|
||||
RotoBrush,
|
||||
RefineEdge,
|
||||
/// Object Matte: a selection box's corners around a solid object, with a click point.
|
||||
ObjectMatte,
|
||||
/// Puppet Position Pin: a pushpin. The other pin tools add a badge at the lower right.
|
||||
Puppet,
|
||||
PuppetAdvanced,
|
||||
@@ -314,6 +316,14 @@ pub fn paint(painter: &Painter, rect: Rect, icon: Icon, color: Color32) {
|
||||
pen.line(&[(8.0, 14.0), (9.5, 12.5)]);
|
||||
pen.line(&[(11.0, 14.0), (12.5, 12.5)]);
|
||||
}
|
||||
ObjectMatte => {
|
||||
// Corner brackets of a box around a filled object, and the click on it.
|
||||
for (x, y, dx, dy) in [(2.0, 2.0, 1.0, 1.0), (14.0, 2.0, -1.0, 1.0), (14.0, 14.0, -1.0, -1.0), (2.0, 14.0, 1.0, -1.0)] {
|
||||
pen.line(&[(x, y + 3.0 * dy), (x, y), (x + 3.0 * dx, y)]);
|
||||
}
|
||||
pen.dot(8.0, 8.0, 2.2);
|
||||
pen.circle(8.0, 8.0, 4.4);
|
||||
}
|
||||
Puppet => pen.pushpin(8.0),
|
||||
PuppetAdvanced => {
|
||||
// Rotation ring with its square scale handle.
|
||||
|
||||
@@ -28,6 +28,7 @@ pub mod learn;
|
||||
pub mod markers_ui;
|
||||
pub mod media_panels;
|
||||
pub mod misc;
|
||||
pub mod object_matte_tool;
|
||||
pub mod paint_panels;
|
||||
pub mod panel_kit;
|
||||
pub mod path_vr_panels;
|
||||
|
||||
@@ -0,0 +1,128 @@
|
||||
//! The Object Matte tool in the Composition viewer (After Effects 26.2's Object Matte, from its
|
||||
//! public description): click an object to isolate it, or drag a box around it. The layer under
|
||||
//! the pointer gets an Object Matte effect (`objectMatte.apply`), or its Object Matte is
|
||||
//! re-prompted (the selected one, else its last); Shift adds another. Every prompt is one undo
|
||||
//! step, and the effect is selected so its point controls show in the viewer.
|
||||
//!
|
||||
//! While hovering, a crosshair and the outline of the layer that would get the matte preview the
|
||||
//! click: segmenting on every pointer move would be far too slow for a hover preview (a model
|
||||
//! pass or a graph cut per move), so the matte itself appears once the prompt is set.
|
||||
|
||||
use effectcraft_engine::geom::vec2 as gv2;
|
||||
use effectcraft_engine::project::Layer;
|
||||
use effectcraft_engine::render::EvalCtx;
|
||||
use egui::{Color32, Pos2, Rect, Stroke, StrokeKind};
|
||||
use serde_json::json;
|
||||
|
||||
use super::viewer::{ViewerMap, fx2c, layers_at};
|
||||
use crate::EffectcraftApp;
|
||||
|
||||
/// Drags shorter than this (screen points) are clicks: a point prompt, no box.
|
||||
pub const MIN_BOX: f32 = 4.0;
|
||||
|
||||
/// Whether Object Matte can work on `layer` (footage, solid or composition layers with pixels).
|
||||
fn eligible(layer: &Layer) -> bool {
|
||||
layer.has_video() && !layer.switches.adjustment && layer.effects().is_some()
|
||||
}
|
||||
|
||||
/// The layer a prompt at comp point `cpt` goes to: a selected one under it, else the topmost.
|
||||
pub fn target<'a>(app: &EffectcraftApp, ectx: &EvalCtx<'a>, cpt: [f64; 2]) -> Option<&'a Layer> {
|
||||
let hits: Vec<&Layer> = layers_at(ectx, cpt).filter(|l| eligible(l)).collect();
|
||||
let selected = &app.session.state.selected_layers;
|
||||
hits.iter().find(|l| selected.contains(&l.id)).or(hits.first()).copied()
|
||||
}
|
||||
|
||||
/// The layer's effect-space rectangle (its pixels) on screen.
|
||||
fn outline(ectx: &EvalCtx, map: &ViewerMap, layer: &Layer) -> [Pos2; 4] {
|
||||
let [w, h] = ectx.effect_bounds(layer).0;
|
||||
let m = fx2c(ectx, layer);
|
||||
[[0.0, 0.0], [w, 0.0], [w, h], [0.0, h]].map(|p| {
|
||||
let c = m.apply(gv2(p[0], p[1]));
|
||||
map.to_screen([c.x, c.y])
|
||||
})
|
||||
}
|
||||
|
||||
/// Hover preview at `hp` (screen): the outline of the layer that would get the matte and a
|
||||
/// crosshair where the object point would go.
|
||||
pub fn hover(app: &mut EffectcraftApp, painter: &egui::Painter, map: &ViewerMap, ectx: &EvalCtx, hp: Pos2) {
|
||||
let accent = app.tokens.accent;
|
||||
let cpt = map.to_comp(hp);
|
||||
if let Some(layer) = target(app, ectx, cpt) {
|
||||
let q = outline(ectx, map, layer);
|
||||
let mut pts = q.to_vec();
|
||||
pts.push(q[0]);
|
||||
painter.add(egui::Shape::line(pts, Stroke::new(1.5, accent)));
|
||||
let r = Rect::from_points(&q);
|
||||
app.auto.add("viewer.objectMatte.target", r, &layer.name);
|
||||
}
|
||||
let (gap, arm) = (4.0, 11.0);
|
||||
for (dx, dy) in [(1.0, 0.0), (-1.0, 0.0), (0.0, 1.0), (0.0, -1.0)] {
|
||||
let d = egui::vec2(dx, dy);
|
||||
for (w, c) in [(3.0, Color32::from_black_alpha(160)), (1.5, Color32::WHITE)] {
|
||||
painter.line_segment([hp + d * gap, hp + d * arm], Stroke::new(w, c));
|
||||
}
|
||||
}
|
||||
painter.circle_stroke(hp, 2.0, Stroke::new(1.5, accent));
|
||||
app.auto.add("viewer.objectMatte.crosshair", Rect::from_center_size(hp, egui::vec2(2.0 * arm, 2.0 * arm)), "Object Matte point");
|
||||
}
|
||||
|
||||
/// The box being dragged (screen `a`–`b`).
|
||||
pub fn draw_box(app: &mut EffectcraftApp, painter: &egui::Painter, a: Pos2, b: Pos2) {
|
||||
let r = Rect::from_two_pos(a, b);
|
||||
let accent = app.tokens.accent;
|
||||
painter.rect_stroke(r, 0.0, Stroke::new(2.5, Color32::from_black_alpha(140)), StrokeKind::Middle);
|
||||
painter.rect_stroke(r, 0.0, Stroke::new(1.5, accent), StrokeKind::Middle);
|
||||
painter.circle_filled(r.center(), 2.5, accent);
|
||||
app.auto.add("viewer.objectMatte.box", r, "Object Matte box");
|
||||
}
|
||||
|
||||
/// Set the prompt from a click at `a` (screen), or a box dragged from `a` to `b`: on the
|
||||
/// target layer's Object Matte (re-prompted), or a new one with `add` or when it has none.
|
||||
pub fn apply(app: &mut EffectcraftApp, ectx: &EvalCtx, map: &ViewerMap, a: Pos2, b: Option<Pos2>, add: bool) {
|
||||
let boxed = b.filter(|b| (b.x - a.x).abs() >= MIN_BOX && (b.y - a.y).abs() >= MIN_BOX);
|
||||
let center = boxed.map_or(a, |b| Rect::from_two_pos(a, b).center());
|
||||
let Some(layer) = target(app, ectx, map.to_comp(center)) else {
|
||||
app.ui.status = "Object Matte: click an object on a footage, solid or composition layer".into();
|
||||
return;
|
||||
};
|
||||
let Some(inv) = fx2c(ectx, layer).inverse() else { return };
|
||||
let to_layer = |p: Pos2| {
|
||||
let c = map.to_comp(p);
|
||||
let l = inv.apply(gv2(c[0], c[1]));
|
||||
[l.x, l.y]
|
||||
};
|
||||
let point = to_layer(center);
|
||||
let mut q = json!({"layer": layer.id.0, "point": point});
|
||||
if let Some(b) = boxed {
|
||||
// The box in layer pixels: the bounds of its corners (the layer may be turned).
|
||||
let r = Rect::from_two_pos(a, b);
|
||||
let c = [r.left_top(), r.right_top(), r.right_bottom(), r.left_bottom()].map(to_layer);
|
||||
let (x0, x1) = c.iter().map(|p| p[0]).fold((f64::INFINITY, f64::NEG_INFINITY), |(lo, hi), v| (lo.min(v), hi.max(v)));
|
||||
let (y0, y1) = c.iter().map(|p| p[1]).fold((f64::INFINITY, f64::NEG_INFINITY), |(lo, hi), v| (lo.min(v), hi.max(v)));
|
||||
q["box"] = json!([x0, y0, x1, y1]);
|
||||
}
|
||||
if !add && let Some(uid) = existing(app, layer) {
|
||||
q["effect"] = json!(uid);
|
||||
}
|
||||
let lid = layer.id;
|
||||
match app.session.execute("objectMatte.apply", q) {
|
||||
Ok(r) => {
|
||||
if let Some(uid) = r["effect"].as_u64() {
|
||||
let _ = app.session.execute("prop.select", json!({"layer": lid.0, "prop": uid}));
|
||||
}
|
||||
}
|
||||
Err(e) => app.ui.status = e.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
/// The Object Matte a prompt on `layer` re-prompts: the selected one, else the layer's last.
|
||||
fn existing(app: &EffectcraftApp, layer: &Layer) -> Option<u64> {
|
||||
let fx = layer.effects()?;
|
||||
let om: Vec<u64> = fx.groups().filter(|g| is_object_matte(g)).map(|g| g.uid).collect();
|
||||
let sel = app.session.state.selected_props.iter().rev().find(|(l, u)| *l == layer.id && om.contains(u)).map(|(_, u)| *u);
|
||||
sel.or(om.last().copied())
|
||||
}
|
||||
|
||||
fn is_object_matte(g: &effectcraft_engine::project::PropGroup) -> bool {
|
||||
matches!(&g.kind, effectcraft_engine::project::GroupKind::Effect { effect } if effect == effectcraft_engine::effects::object_matte::ID)
|
||||
}
|
||||
@@ -164,6 +164,10 @@ enum Gesture {
|
||||
PinMarquee {
|
||||
start: Pos2,
|
||||
},
|
||||
/// Object Matte tool: a box around the object (screen point where the drag began).
|
||||
ObjectMatteBox {
|
||||
start: Pos2,
|
||||
},
|
||||
/// Puppet tool Record mode (⌘/Ctrl-drag a pin): the drag is sampled in real time (seconds
|
||||
/// since it began, effect space) and becomes keyframes on release (`puppet.recordPin`); the
|
||||
/// current time runs along while recording.
|
||||
@@ -1164,6 +1168,14 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) {
|
||||
}
|
||||
t if t.is_shape() => egui::CursorIcon::Crosshair,
|
||||
_ if app.ui.viewer.roi_draw => egui::CursorIcon::Crosshair,
|
||||
Tool::ObjectMatte => {
|
||||
// The tool draws its own crosshair (and the layer it would matte) while no box
|
||||
// is being dragged.
|
||||
if ui.data(|d| d.get_temp::<Gesture>(egui::Id::new("viewer-gesture"))).is_none() {
|
||||
super::object_matte_tool::hover(app, &painter, &map, &ectx, hp);
|
||||
}
|
||||
egui::CursorIcon::None
|
||||
}
|
||||
t if t.puppet_kind().is_some() => match super::puppet_tool::hit(&pin_hits, hp) {
|
||||
Some((_, super::puppet_tool::PinPart::Ring)) => egui::CursorIcon::Alias,
|
||||
Some((_, super::puppet_tool::PinPart::Scale)) => egui::CursorIcon::ResizeNwSe,
|
||||
@@ -1293,6 +1305,7 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) {
|
||||
// From where the button went down: by the time the pointer has moved far enough to
|
||||
// count as a drag it has already left the press point, and the shape is drawn there.
|
||||
t if t.is_shape() => Some(Gesture::Create { tool: t, start: map.to_comp(press) }),
|
||||
Tool::ObjectMatte => Some(Gesture::ObjectMatteBox { start: press }),
|
||||
t if t.puppet_kind().is_some() && super::puppet_tool::hit(&pin_hits, press).is_none() => {
|
||||
// Off the pins: Alt or a press outside the art drags a marquee that selects pins.
|
||||
(mods.alt || puppet_layer(app, cpt).is_none()).then_some(Gesture::PinMarquee { start: press })
|
||||
@@ -1609,6 +1622,7 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) {
|
||||
q["merge"] = json!(merge);
|
||||
let _ = app.session.execute("track.setPoint", q);
|
||||
}
|
||||
Gesture::ObjectMatteBox { start } => super::object_matte_tool::draw_box(app, &painter, start, pos),
|
||||
Gesture::Marquee { start } | Gesture::PinMarquee { start } => {
|
||||
let r = Rect::from_two_pos(start, pos);
|
||||
painter.rect_filled(r, 0.0, t.accent.gamma_multiply(0.12));
|
||||
@@ -1649,6 +1663,7 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) {
|
||||
}
|
||||
}
|
||||
Gesture::PuppetPin { layer, pin, .. } => super::puppet_tool::reveal_pin(app, layer, pin),
|
||||
Gesture::ObjectMatteBox { start } => super::object_matte_tool::apply(app, &ectx, &map, start, Some(pos), mods.shift),
|
||||
Gesture::Create { tool, start } => {
|
||||
let end = map.to_comp(pos);
|
||||
create_shape(app, tool, start, end, mods);
|
||||
@@ -1752,6 +1767,7 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) {
|
||||
// Clicking away from the free-transform box ends it.
|
||||
ov::end_free_transform(&ctx);
|
||||
}
|
||||
Tool::ObjectMatte => super::object_matte_tool::apply(app, &ectx, &map, pos, None, mods.shift),
|
||||
t if t.puppet_kind().is_some() => {
|
||||
if let Some((h, _)) = super::puppet_tool::hit(&pin_hits, pos) {
|
||||
// Click selects the pin; Shift-click adds or removes it.
|
||||
@@ -2075,7 +2091,7 @@ pub(crate) fn selectable_layers(comp: &Comp, time: Tick) -> impl Iterator<Item =
|
||||
}
|
||||
|
||||
/// Selectable layers under a comp point, front to back.
|
||||
fn layers_at<'a, 'b>(ectx: &'b EvalCtx<'a>, cpt: [f64; 2]) -> impl Iterator<Item = &'a Layer> + 'b {
|
||||
pub(crate) fn layers_at<'a, 'b>(ectx: &'b EvalCtx<'a>, cpt: [f64; 2]) -> impl Iterator<Item = &'a Layer> + 'b {
|
||||
selectable_layers(ectx.comp, ectx.time).filter(move |l| layer_quad(ectx, l).is_some_and(|(_, q, _)| point_in_quad(cpt, &q)))
|
||||
}
|
||||
|
||||
|
||||
@@ -35,6 +35,8 @@ pub enum Tool {
|
||||
Eraser,
|
||||
RotoBrush,
|
||||
RefineEdge,
|
||||
/// Object Matte: click an object (or drag a box around it) in the Composition viewer.
|
||||
ObjectMatte,
|
||||
Puppet,
|
||||
PuppetStarch,
|
||||
PuppetBend,
|
||||
@@ -59,10 +61,10 @@ impl Tool {
|
||||
&[Tool::Brush],
|
||||
&[Tool::Clone],
|
||||
&[Tool::Eraser],
|
||||
&[Tool::RotoBrush, Tool::RefineEdge],
|
||||
&[Tool::RotoBrush, Tool::RefineEdge, Tool::ObjectMatte],
|
||||
&[Tool::Puppet, Tool::PuppetStarch, Tool::PuppetBend, Tool::PuppetAdvanced, Tool::PuppetOverlap],
|
||||
];
|
||||
pub const ALL: [Tool; 30] = [
|
||||
pub const ALL: [Tool; 31] = [
|
||||
Tool::Selection,
|
||||
Tool::Hand,
|
||||
Tool::Zoom,
|
||||
@@ -88,6 +90,7 @@ impl Tool {
|
||||
Tool::Eraser,
|
||||
Tool::RotoBrush,
|
||||
Tool::RefineEdge,
|
||||
Tool::ObjectMatte,
|
||||
Tool::Puppet,
|
||||
Tool::PuppetStarch,
|
||||
Tool::PuppetBend,
|
||||
@@ -121,6 +124,7 @@ impl Tool {
|
||||
Tool::Eraser => "Eraser Tool",
|
||||
Tool::RotoBrush => "Roto Brush Tool",
|
||||
Tool::RefineEdge => "Refine Edge Tool",
|
||||
Tool::ObjectMatte => "Object Matte Tool",
|
||||
Tool::Puppet => "Puppet Position Pin Tool",
|
||||
Tool::PuppetStarch => "Puppet Starch Pin Tool",
|
||||
Tool::PuppetBend => "Puppet Bend Pin Tool",
|
||||
@@ -142,7 +146,7 @@ impl Tool {
|
||||
Tool::Pen | Tool::PenAdd | Tool::PenDelete | Tool::PenConvert | Tool::MaskFeather => Some("G"),
|
||||
Tool::Type | Tool::TypeVertical => Some("Cmd+T"),
|
||||
Tool::Brush | Tool::Clone | Tool::Eraser => Some("Cmd+B"),
|
||||
Tool::RotoBrush | Tool::RefineEdge => Some("Alt+W"),
|
||||
Tool::RotoBrush | Tool::RefineEdge | Tool::ObjectMatte => Some("Alt+W"),
|
||||
Tool::Puppet | Tool::PuppetStarch | Tool::PuppetBend | Tool::PuppetAdvanced | Tool::PuppetOverlap => Some("Cmd+P"),
|
||||
}
|
||||
}
|
||||
@@ -173,6 +177,7 @@ impl Tool {
|
||||
Tool::Eraser => Icon::Eraser,
|
||||
Tool::RotoBrush => Icon::RotoBrush,
|
||||
Tool::RefineEdge => Icon::RefineEdge,
|
||||
Tool::ObjectMatte => Icon::ObjectMatte,
|
||||
Tool::Puppet => Icon::Puppet,
|
||||
Tool::PuppetStarch => Icon::PuppetStarch,
|
||||
Tool::PuppetBend => Icon::PuppetBend,
|
||||
|
||||
Reference in New Issue
Block a user