M13.11: essential graphics mirrored and linked properties

Adding a property already in the Essential Graphics panel adds a mirror
(EgKind::Mirror: own name and place, one value in the comp and in every
instance; editing, reverting or pushing either acts on both). A property
control can also drive further properties of the comp (links follow the
main property both ways and take instance values). essential.addMirror,
essential.linkProperty, essential.unlinkProperty; panel badges and menu;
ExtendScript addToMotionGraphicsTemplate(As), canAddToMotionGraphicsTemplate,
motionGraphicsTemplateName, controller count/name and
exportAsMotionGraphicsTemplate.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Brandon Thomas
2026-10-03 09:38:19 -04:00
co-authored by Claude Opus 5.5
parent bb24b931a6
commit d774d11231
6 changed files with 628 additions and 55 deletions
+178 -30
View File
@@ -88,6 +88,8 @@ fn add_property(s: &mut Session, p: &Value) -> Result<Value> {
let group = p.get("group").and_then(Value::as_u64);
let index = p.get("index").and_then(Value::as_u64).map(|i| i as usize);
let rename = str_p(p, "name").map(str::to_string);
// A property already in the panel is added again as a mirror (`mirror: false`: skipped).
let mirror = b_p(p, "mirror").unwrap_or(true);
let mut added = vec![];
for (cid, lid, uid) in &targets {
let l = s.project.comp(*cid).and_then(|c| c.layer(*lid)).ok_or(EngineError::NoComp)?;
@@ -95,25 +97,113 @@ fn add_property(s: &mut Session, p: &Value) -> Result<Value> {
if control_type(pr).is_none() {
return Err(bad(C, format!("`{}` can't be added to Essential Graphics (unsupported property type)", pr.name)));
}
if s.project.comp(*cid).and_then(|c| c.essential.as_ref()).is_some_and(|e| e.control_for(*lid, *uid).is_some()) {
continue;
}
added.push((*cid, *lid, *uid, rename.clone().unwrap_or_else(|| pr.name.clone())));
let existing = s.project.comp(*cid).and_then(|c| c.essential.as_ref()).and_then(|e| e.driver_of(*lid, *uid)).map(|c| (c.id, c.name.clone()));
let kind = match existing {
Some(_) if !mirror => continue,
Some((of, _)) => EgKind::Mirror { of },
None => EgKind::Property { layer: *lid, prop: *uid, links: vec![] },
};
let name = rename.clone().or(existing.map(|e| e.1)).unwrap_or_else(|| pr.name.clone());
added.push((*cid, kind, name));
}
let ids = s.edit("Add Property to Essential Graphics", None, |proj, st| {
if let Some((cid, ..)) = added.first() {
st.essential_primary.get_or_insert(*cid);
}
let mut ids = vec![];
for (k, (cid, lid, uid, name)) in added.iter().enumerate() {
for (k, (cid, kind, name)) in added.iter().enumerate() {
let id = proj.alloc();
let eg = eg_of(proj, *cid)?;
insert(eg, EgControl { id, name: name.clone(), kind: EgKind::Property { layer: *lid, prop: *uid } }, group, index.map(|i| i + k), C)?;
insert(eg, EgControl { id, name: name.clone(), kind: kind.clone() }, group, index.map(|i| i + k), C)?;
ids.push(id);
}
Ok(ids)
})?;
Ok(json!({"controls": ids}))
let mirrors: Vec<u64> = ids.iter().zip(&added).filter(|(_, a)| matches!(a.1, EgKind::Mirror { .. })).map(|(i, _)| *i).collect();
Ok(json!({"controls": ids, "mirrors": mirrors}))
}
/// Add a mirror of a property control (the same property shown again, e.g. in another group).
fn add_mirror(s: &mut Session, p: &Value) -> Result<Value> {
const C: &str = "essential.addMirror";
let cid = eg_comp(s, p)?;
let of = control_p(p, C)?;
let eg = s.project.comp(cid).and_then(|c| c.essential.clone()).unwrap_or_default();
let master =
eg.resolve(of).filter(|c| matches!(c.kind, EgKind::Property { .. })).ok_or_else(|| bad(C, format!("control {of} is not a property control")))?;
let (of, master_name) = (master.id, master.name.clone());
let name = str_p(p, "name").map(str::to_string).unwrap_or(master_name);
let group = p.get("group").and_then(Value::as_u64);
let index = p.get("index").and_then(Value::as_u64).map(|i| i as usize);
let id = s.edit("Add Mirror to Essential Graphics", None, |proj, _| {
let id = proj.alloc();
insert(eg_of(proj, cid)?, EgControl { id, name, kind: EgKind::Mirror { of } }, group, index, C)?;
Ok(id)
})?;
Ok(json!({"control": id, "of": of}))
}
/// The property a link command names: `{layer, path|prop}` or the first selected property.
fn link_target(s: &Session, p: &Value, cmd: &str) -> Result<(ItemId, LayerId, Uid)> {
if p.get("path").is_some() || p.get("prop").is_some() {
return super::prop::prop_ref(s, p, cmd);
}
let cid = comp_id(s, p)?;
selected_leaf_props(s)
.into_iter()
.next()
.map(|(l, u)| (cid, l, u))
.ok_or_else(|| bad(cmd, "select a property in the timeline (or give `layer` and `path`)"))
}
/// Make a property control drive another property too (same kind of value).
fn link_property(s: &mut Session, p: &Value) -> Result<Value> {
const C: &str = "essential.linkProperty";
let cid = eg_comp(s, p)?;
let control = control_p(p, C)?;
let (pc, lid, uid) = link_target(s, p, C)?;
if pc != cid {
return Err(bad(C, "the property must be in the Essential Graphics composition"));
}
let eg = s.project.comp(cid).and_then(|c| c.essential.clone()).unwrap_or_default();
let ctl = eg.resolve(control).ok_or_else(|| bad(C, format!("no control {control}")))?.clone();
let (main, _) = essential::source_prop(&s.project, cid, &ctl).ok_or_else(|| bad(C, format!("control {control} is not a property control")))?;
let main = main.props.find(match ctl.kind {
EgKind::Property { prop, .. } => prop,
_ => 0,
});
let target = s.project.comp(cid).and_then(|c| c.layer(lid)).and_then(|l| l.props.find(uid)).ok_or_else(|| bad(C, "no such property"))?;
if !main.is_some_and(|m| essential::linkable(m, target)) {
return Err(bad(C, format!("`{}` is not the same kind of property as the control", target.name)));
}
if let Some(d) = eg.driver_of(lid, uid) {
return Err(bad(C, format!("`{}` is already driven by control {}", target.name, d.id)));
}
let id = ctl.id;
s.edit("Link Property to Essential Graphics Control", None, |proj, _| {
if let Some(EgControl { kind: EgKind::Property { links, .. }, .. }) = eg_of(proj, cid)?.find_mut(id) {
links.push(essential::EgLink { layer: lid, prop: uid });
}
Ok(())
})?;
Ok(json!({"control": id, "layer": lid.0, "prop": uid}))
}
fn unlink_property(s: &mut Session, p: &Value) -> Result<Value> {
const C: &str = "essential.unlinkProperty";
let cid = eg_comp(s, p)?;
let control = control_p(p, C)?;
let (_, lid, uid) = link_target(s, p, C)?;
let eg = s.project.comp(cid).and_then(|c| c.essential.clone()).unwrap_or_default();
let id = eg.resolve(control).map(|c| c.id).ok_or_else(|| bad(C, format!("no control {control}")))?;
s.edit("Unlink Property from Essential Graphics Control", None, |proj, _| match eg_of(proj, cid)?.find_mut(id) {
Some(EgControl { kind: EgKind::Property { links, .. }, .. }) if links.iter().any(|k| k.layer == lid && k.prop == uid) => {
links.retain(|k| !(k.layer == lid && k.prop == uid));
Ok(())
}
_ => Err(bad(C, "the property is not linked to this control")),
})?;
Ok(json!({"control": id, "layer": lid.0, "prop": uid}))
}
fn control_type(pr: &effectcraft_project::Property) -> Option<ControlType> {
@@ -190,11 +280,17 @@ fn remove(s: &mut Session, p: &Value) -> Result<Value> {
const C: &str = "essential.remove";
let cid = eg_comp(s, p)?;
let id = control_p(p, C)?;
// A property control's mirrors go with it.
let mirrors = s.project.comp(cid).and_then(|c| c.essential.as_ref()).map(|e| e.mirrors_of(id)).unwrap_or_default();
s.edit("Remove Essential Graphics Control", None, |proj, _| {
eg_of(proj, cid)?.remove(id).ok_or_else(|| bad(C, format!("no control {id}")))?;
let eg = eg_of(proj, cid)?;
eg.remove(id).ok_or_else(|| bad(C, format!("no control {id}")))?;
for m in &mirrors {
eg.remove(*m);
}
Ok(())
})?;
Ok(json!({"control": id}))
Ok(json!({"control": id, "mirrors": mirrors}))
}
fn move_control(s: &mut Session, p: &Value) -> Result<Value> {
@@ -293,8 +389,12 @@ pub fn supported_properties(project: &Project, cid: ItemId) -> Vec<Value> {
fn control_json(project: &Project, cid: ItemId, c: &EgControl) -> Value {
match &c.kind {
EgKind::Property { layer, prop } => {
EgKind::Property { layer, prop, .. } => {
let src = essential::source_prop(project, cid, c);
let links: Vec<Value> = essential::linked_props(project, cid, c)
.into_iter()
.map(|(l, pr)| json!({"layer": l.id.0, "layerName": l.name, "prop": pr.uid, "path": super::match_path_of(&l.props, pr.uid)}))
.collect();
json!({
"id": c.id, "name": c.name, "kind": "property", "layer": layer.0, "prop": prop,
"layerName": src.map(|(l, _)| l.name.clone()),
@@ -302,6 +402,21 @@ fn control_json(project: &Project, cid: ItemId, c: &EgControl) -> Value {
"type": src.and_then(|(_, pr)| essential::control_type(pr)),
"value": src.map(|(_, pr)| pr.value_at(Tick::ZERO).to_json()),
"missing": src.is_none(),
"links": links,
"mirrors": project.comp(cid).and_then(|c| c.essential.as_ref()).map(|e| e.mirrors_of(c.id)).unwrap_or_default(),
})
}
EgKind::Mirror { of } => {
let src = essential::source_prop(project, cid, c);
json!({
"id": c.id, "name": c.name, "kind": "mirror", "of": of,
"layer": src.map(|(l, _)| l.id.0),
"prop": src.map(|(_, pr)| pr.uid),
"layerName": src.map(|(l, _)| l.name.clone()),
"path": src.and_then(|(l, pr)| super::match_path_of(&l.props, pr.uid)),
"type": src.and_then(|(_, pr)| essential::control_type(pr)),
"value": src.map(|(_, pr)| pr.value_at(Tick::ZERO).to_json()),
"missing": src.is_none(),
})
}
EgKind::Media { layer } => json!({
@@ -376,6 +491,7 @@ fn instance_json(s: &mut Session, p: &Value) -> Result<Value> {
}
let kind = match c.kind {
EgKind::Property { .. } => "property",
EgKind::Mirror { .. } => "mirror",
EgKind::Media { .. } => "media",
EgKind::Comment { .. } => "comment",
EgKind::Group { .. } => "group",
@@ -426,11 +542,21 @@ fn targets(s: &Session, cid: ItemId, lid: LayerId, p: &Value) -> Vec<(u64, Uid)>
let Some(l) = s.project.comp(cid).and_then(|c| c.layer(lid)) else { return vec![] };
let over = essential::overridden(l);
let Some(g) = essential::group(l) else { return vec![] };
let want = p.get("control").and_then(Value::as_u64);
// A control, its master and the master's mirrors share one value: act on all of them.
let want: Option<Vec<u64>> = p.get("control").and_then(Value::as_u64).map(|w| {
let src = match l.source {
LayerSource::Comp { item } => s.project.comp(item).and_then(|c| c.essential.as_ref()),
_ => None,
};
match src.and_then(|e| e.resolve(w).map(|m| (e, m.id))) {
Some((e, m)) => std::iter::once(m).chain(e.mirrors_of(m)).chain(std::iter::once(w)).collect(),
None => vec![w],
}
});
let mut out = vec![];
g.walk("", &mut |_, pr| {
if let Some(c) = essential::control_of(&pr.match_id)
&& want.is_none_or(|w| w == c)
&& want.as_ref().is_none_or(|w| w.contains(&c))
&& (want.is_some() || over.contains(&pr.uid))
{
out.push((c, pr.uid));
@@ -476,29 +602,32 @@ fn push_to_comp(s: &mut Session, p: &Value) -> Result<Value> {
let l = s.project.comp(cid).and_then(|c| c.layer(lid)).cloned().ok_or(EngineError::NoComp)?;
let mut writes = vec![];
for (control, uid) in &ts {
let (Some(ctl), Some(pr)) = (eg.find(*control), l.props.find(*uid)) else { continue };
// A mirror pushes into its master's properties.
let (Some(ctl), Some(pr)) = (eg.resolve(*control), l.props.find(*uid)) else { continue };
writes.push((ctl.kind.clone(), pr.clone()));
}
s.edit("Push Override Values to Source", None, |proj, _| {
let sc = proj.comp_mut(src).ok_or(EngineError::NoComp)?;
for (kind, pr) in &writes {
match kind {
EgKind::Property { layer, prop } => {
let Some(inner) = sc.layer_mut(*layer) else { continue };
// Instance keys are in the precomp layer's time (= the source comp's time);
// the source property's keys are in the inner layer's time.
let keys: Vec<_> = pr
.keys
.iter()
.map(|k| {
let mut k = k.clone();
k.time = inner.layer_time(k.time);
k
})
.collect();
if let Some(target) = inner.props.find_mut(*prop) {
target.value = pr.value.clone();
target.keys = keys;
EgKind::Property { .. } => {
for (layer, prop) in kind.targets() {
let Some(inner) = sc.layer_mut(layer) else { continue };
// Instance keys are in the precomp layer's time (= the source comp's
// time); the source property's keys are in the inner layer's time.
let keys: Vec<_> = pr
.keys
.iter()
.map(|k| {
let mut k = k.clone();
k.time = inner.layer_time(k.time);
k
})
.collect();
if let Some(target) = inner.props.find_mut(prop) {
target.value = pr.value.clone();
target.keys = keys;
}
}
}
EgKind::Media { layer } => {
@@ -740,10 +869,29 @@ pub fn specs() -> Vec<CommandSpec> {
"Add Property to Essential Graphics",
["Animation"],
None,
"{comp?, layer?, path?|prop?: uid (default: the selected properties), name?, group?: control id, index?}",
"{comp?, layer?, path?|prop?: uid (default: the selected properties), name?, group?: control id, index?, mirror?: bool (a property already present is added as a mirror; default true)}",
has_selected_props,
add_property
),
cmd!("essential.addMirror", "Add Mirror", [], None, "{comp?, control, name?, group?, index?}", has_eg, add_mirror),
cmd!(
"essential.linkProperty",
"Link Property to Control",
[],
None,
"{comp?, control, layer?, path?|prop? (default: the selected property)}",
has_eg,
link_property
),
cmd!(
"essential.unlinkProperty",
"Unlink Property",
[],
None,
"{comp?, control, layer?, path?|prop? (default: the selected property)}",
has_eg,
unlink_property
),
cmd!("essential.addMedia", "Add Media Replacement", [], None, "{layer, name?, group?}", has_comp, add_media),
cmd!("essential.addGroup", "Add Group", [], None, "{comp?, name?, index?}", has_comp, add_group),
cmd!("essential.addComment", "Add Comment", [], None, "{comp?, text?, group?}", has_comp, add_comment),
+79 -2
View File
@@ -67,11 +67,15 @@ fn controls_are_listed_typed_and_editable() {
assert_eq!(l["controls"][1]["name"], "Opacity");
s.execute("essential.remove", json!({"control": opacity})).unwrap();
assert_eq!(s.execute("essential.list", json!({})).unwrap()["controls"].as_array().unwrap().len(), 3);
// Unsupported properties are refused; adding the same property twice adds nothing.
// Unsupported properties are refused; adding the same property twice adds a mirror (or
// nothing with `mirror: false`).
s.execute("layer.addMask", json!({"layer": solid})).unwrap();
assert!(s.execute("essential.addProperty", json!({"layer": solid, "path": "masks/#1/path"})).is_err());
let again = s.execute("essential.addProperty", json!({"layer": solid, "path": "effects/#1/color"})).unwrap();
let again = s.execute("essential.addProperty", json!({"layer": solid, "path": "effects/#1/color", "mirror": false})).unwrap();
assert_eq!(again["controls"], json!([]));
let again = s.execute("essential.addProperty", json!({"layer": solid, "path": "effects/#1/color"})).unwrap();
assert_eq!(again["mirrors"], again["controls"]);
s.execute("edit.undo", json!({})).unwrap();
// Dropdown Menu Control: its items are editable and it becomes a dropdown control.
s.execute("effect.apply", json!({"layer": solid, "effect": "Dropdown Menu Control"})).unwrap();
s.execute("effect.editDropdown", json!({"layer": solid, "items": ["Small", "Medium", "Large", "Huge"]})).unwrap();
@@ -141,6 +145,79 @@ fn master_properties_override_per_instance_push_and_revert() {
assert!(essential::group(&l).unwrap().children.iter().all(|n| n.name() != "Card Color"));
}
#[test]
fn mirrored_and_linked_properties() {
let (mut s, card, solid, ids) = card();
let opacity = ids["opacity"].as_u64().unwrap();
// A second solid whose opacity the Opacity control drives too.
let bg2 = s.execute("layer.newSolid", json!({"name": "BG2", "color": "#00ff00"})).unwrap()["layer"].as_u64().unwrap();
s.execute("essential.linkProperty", json!({"control": opacity, "layer": bg2, "path": "transform/opacity"})).unwrap();
// Not the same kind of property; already driven.
assert!(s.execute("essential.linkProperty", json!({"control": opacity, "layer": bg2, "path": "transform/position"})).is_err());
assert!(s.execute("essential.linkProperty", json!({"control": ids["color"], "layer": bg2, "path": "transform/opacity"})).is_err());
let opa = |s: &Session, l: u64| s.project.comp(card).unwrap().layer(LayerId(l)).unwrap().props.prop("transform/opacity").unwrap().value.as_f64();
// The link follows the main property, and the main property follows the link.
s.execute("prop.set", json!({"comp": card.0, "layer": solid, "path": "transform/opacity", "value": 40})).unwrap();
assert_eq!(opa(&s, bg2), 40.0);
s.execute("prop.set", json!({"comp": card.0, "layer": bg2, "path": "transform/opacity", "value": 70})).unwrap();
assert_eq!(opa(&s, solid), 70.0);
let l = s.execute("essential.list", json!({})).unwrap();
let c = l["controls"].as_array().unwrap().iter().find(|c| c["id"] == opacity).unwrap().clone();
assert_eq!(c["links"][0]["layer"], bg2);
assert_eq!(c["links"][0]["path"], "transform/opacity");
// Adding the Opacity again (into the Words group) mirrors it.
let m = s.execute("essential.addProperty", json!({"layer": solid, "path": "transform/opacity", "group": ids["group"], "name": "Fade"})).unwrap();
let mirror = m["mirrors"][0].as_u64().unwrap();
let l = s.execute("essential.list", json!({})).unwrap();
let mc = &l["controls"][2]["children"][1];
assert_eq!((mc["id"].as_u64(), mc["kind"].as_str(), mc["of"].as_u64(), mc["name"].as_str()), (Some(mirror), Some("mirror"), Some(opacity), Some("Fade")));
assert_eq!(mc["value"], json!(70.0));
// A mirror of the mirror shows the same master; the linked solid also counts as present.
let m2 = s.execute("essential.addMirror", json!({"control": mirror})).unwrap();
assert_eq!(m2["of"], opacity);
let again = s.execute("essential.addProperty", json!({"layer": bg2, "path": "transform/opacity"})).unwrap();
assert_eq!(again["mirrors"].as_array().unwrap().len(), 1);
s.execute("edit.undo", json!({})).unwrap();
s.execute("edit.undo", json!({})).unwrap();
// Instances: setting the mirror sets the master (and both links) for that instance only.
s.execute("comp.new", json!({"name": "Main", "width": 64, "height": 64, "frameRate": 30, "duration": 2})).unwrap();
let main = s.active_comp_id().unwrap();
let a = s.execute("layer.addItem", json!({"item": card.0})).unwrap()["layer"].as_u64().unwrap();
let inst = s.execute("essential.instance", json!({"layer": a})).unwrap();
let find = |inst: &Value, id: u64| inst["controls"].as_array().unwrap().iter().find(|c| c["control"] == id).unwrap().clone();
assert_eq!(find(&inst, mirror)["kind"], "mirror");
s.execute("essential.set", json!({"layer": a, "control": mirror, "value": 20})).unwrap();
let inst = s.execute("essential.instance", json!({"layer": a})).unwrap();
assert_eq!((find(&inst, opacity)["value"].clone(), find(&inst, opacity)["overridden"].clone()), (json!(20.0), json!(true)));
assert_eq!((find(&inst, mirror)["value"].clone(), find(&inst, mirror)["overridden"].clone()), (json!(20.0), json!(true)));
// Rendered: both solids at 20 % (green BG2 on top of the red-filled solid).
let px = center(&s, main);
assert!((px[3] - (1.0 - 0.8 * 0.8)).abs() < 0.02, "{px:?}");
assert_eq!(opa(&s, solid), 70.0, "the source comp keeps its value");
// Setting the master updates the mirror.
s.execute("essential.set", json!({"layer": a, "control": opacity, "value": 55})).unwrap();
assert_eq!(find(&s.execute("essential.instance", json!({"layer": a})).unwrap(), mirror)["value"], json!(55.0));
// Reverting the mirror reverts the master too.
s.execute("essential.revert", json!({"layer": a, "control": mirror})).unwrap();
let inst = s.execute("essential.instance", json!({"layer": a})).unwrap();
assert_eq!((find(&inst, opacity)["overridden"].clone(), find(&inst, mirror)["value"].clone()), (json!(false), json!(70.0)));
// Push to Comp from a mirror writes the master and its links.
s.execute("essential.set", json!({"layer": a, "control": mirror, "value": 35})).unwrap();
s.execute("essential.pushToComp", json!({"layer": a, "control": mirror})).unwrap();
assert_eq!((opa(&s, solid), opa(&s, bg2)), (35.0, 35.0));
// Saved and loaded with the project.
let back = effectcraft_project::Project::from_json(&s.project.to_json()).unwrap();
assert_eq!(back.comp(card).unwrap().essential, s.project.comp(card).unwrap().essential);
// Unlink; removing the master removes its mirrors.
s.execute("essential.unlinkProperty", json!({"comp": card.0, "control": opacity, "layer": bg2, "path": "transform/opacity"})).unwrap();
s.execute("prop.set", json!({"comp": card.0, "layer": solid, "path": "transform/opacity", "value": 10})).unwrap();
assert_eq!(opa(&s, bg2), 35.0);
let r = s.execute("essential.remove", json!({"comp": card.0, "control": opacity})).unwrap();
assert_eq!(r["mirrors"], json!([mirror]));
let eg = s.project.comp(card).unwrap().essential.clone().unwrap();
assert!(eg.find(mirror).is_none() && essential::group(s.project.comp(main).unwrap().layer(LayerId(a)).unwrap()).is_some());
}
#[test]
fn template_export_import_round_trip_with_overrides() {
let dir = out_dir("ectemplate");