Skip to content

EffectCraft: An open-source motion graphics and compositing tool written by scratch in pure Rust

1063: use canonical text animator match names and resolve main

Kristian Myram114d8709a10e
diff --cc crates/script/src/matchnames.rs
index 60a40e695,60a40e695..f0bf5f6aa
--- a/crates/script/src/matchnames.rs
+++ b/crates/script/src/matchnames.rs
@@@ -90,6 -90,6 +90,34 @@@ pub fn stroke_dash(name: &str) -> Optio
const TEXT_ANIMATOR: &[(&str, &str)] = &[("selectors", "ADBE Text Selectors"), ("properties", "ADBE Text Animator Properties")];
++/// Animator property ids and their stable After Effects scripting names. Shared by lookup,
++/// `.matchName`, and `addProperty` so the same property has one name in every direction.
++pub const TEXT_ANIMATOR_PROPERTIES: &[(&str, &str)] = &[
++ ("anchor", "ADBE Text Anchor Point 3D"),
++ ("position", "ADBE Text Position 3D"),
++ ("scale", "ADBE Text Scale 3D"),
++ ("skew", "ADBE Text Skew"),
++ ("rotation", "ADBE Text Rotation"),
++ ("opacity", "ADBE Text Opacity"),
++ ("fillColor", "ADBE Text Fill Color"),
++ ("fillHue", "ADBE Text Fill Hue"),
++ ("fillSaturation", "ADBE Text Fill Saturation"),
++ ("fillBrightness", "ADBE Text Fill Brightness"),
++ ("fillOpacity", "ADBE Text Fill Opacity"),
++ ("strokeColor", "ADBE Text Stroke Color"),
++ ("strokeHue", "ADBE Text Stroke Hue"),
++ ("strokeSaturation", "ADBE Text Stroke Saturation"),
++ ("strokeBrightness", "ADBE Text Stroke Brightness"),
++ ("strokeOpacity", "ADBE Text Stroke Opacity"),
++ ("strokeWidth", "ADBE Text Stroke Width"),
++ ("tracking", "ADBE Text Tracking Amount"),
++ ("lineAnchor", "ADBE Text Line Anchor"),
++ ("lineSpacing", "ADBE Text Line Spacing"),
++ ("characterOffset", "ADBE Text Character Offset"),
++ ("characterValue", "ADBE Text Character Replace"),
++ ("blur", "ADBE Text Blur"),
++];
++
const TEXT_SELECTORS: &[(&str, &str)] =
&[("rangeSelector", "ADBE Text Selector"), ("wigglySelector", "ADBE Text Wiggly Selector"), ("expressionSelector", "ADBE Text Expressible Selector")];
@@@ -447,6 -447,6 +475,9 @@@ pub fn node(chain: &[&Node]) -> String
"text" if pid == "animators" => "ADBE Text Animator".into(),
"text" if pid == "selectors" => find(TEXT_SELECTORS, id).map(str::to_string).unwrap_or_else(own),
"text" if pid == "rangeSelector" => find(TEXT_RANGE_SELECTOR, id).map(str::to_string).unwrap_or_else(own),
++ "text" if pid == "properties" && chain.len() == 5 && chain.get(1).is_some_and(|n| n.match_id() == "animators") => {
++ find(TEXT_ANIMATOR_PROPERTIES, id).map(str::to_string).unwrap_or_else(own)
++ }
"text" if grand.map(|g| g.match_id()) == Some("animators") => find(TEXT_ANIMATOR, id).map(str::to_string).unwrap_or_else(own),
"cameraOptions" => find(CAMERA, id).map(str::to_string).unwrap_or_else(own),
"lightOptions" => find(LIGHT, id).map(str::to_string).unwrap_or_else(own),
@@@ -497,6 -497,6 +528,17 @@@ mod tests
assert_eq!(shape_kind("nope"), None);
}
++ #[test]
++ fn text_animator_property_names_are_canonical_and_scoped() {
++ let n = |m: &str| Node::from(effectcraft_project::PropGroup::new(1, m, m));
++ let (text, animators, animator, properties) = (n("text"), n("animators"), n("animator"), n("properties"));
++ for (id, name) in TEXT_ANIMATOR_PROPERTIES {
++ let prop = n(id);
++ assert_eq!(node(&[&text, &animators, &animator, &properties, &prop]), *name);
++ assert_eq!(node(&[&properties, &prop]), *id, "an unrelated Properties group keeps its own ids");
++ }
++ }
++
#[test]
fn text_range_selector_names() {
let n = |m: &str| Node::from(effectcraft_project::PropGroup::new(1, m, m));
diff --cc crates/script/src/model.rs
index 8f2e4f1ba,90b03ae59..8ae2819fc
--- a/crates/script/src/model.rs
+++ b/crates/script/src/model.rs
@@@ -745,47 -742,36 +742,12 @@@ fn resolve_add(s: &Session, a: &J) ->
/// A text animator property (`layer.addTextAnimatorProperty` key) from an After Effects match
/// name or a display name.
fn text_animator_property(name: &str) -> Option<&'static str> {
- let table = animator_property_table();
- if let Some((_, k)) = table.iter().find(|(m, _)| m.eq_ignore_ascii_case(name)) {
- const AE: &[(&str, &str)] = &[
- ("ADBE Text Anchor Point 3D", "anchor"),
- ("ADBE Text Position 3D", "position"),
- ("ADBE Text Scale 3D", "scale"),
- ("ADBE Text Skew", "skew"),
- ("ADBE Text Rotation", "rotation"),
- ("ADBE Text Opacity", "opacity"),
- ("ADBE Text Fill Color", "fillColor"),
- ("ADBE Text Fill Hue", "fillHue"),
- ("ADBE Text Fill Saturation", "fillSaturation"),
- ("ADBE Text Fill Brightness", "fillBrightness"),
- ("ADBE Text Fill Opacity", "fillOpacity"),
- ("ADBE Text Stroke Color", "strokeColor"),
- ("ADBE Text Stroke Hue", "strokeHue"),
- ("ADBE Text Stroke Saturation", "strokeSaturation"),
- ("ADBE Text Stroke Brightness", "strokeBrightness"),
- ("ADBE Text Stroke Opacity", "strokeOpacity"),
- ("ADBE Text Stroke Width", "strokeWidth"),
- ("ADBE Text Tracking Amount", "tracking"),
- ("ADBE Text Line Anchor", "lineAnchor"),
- ("ADBE Text Line Spacing", "lineSpacing"),
- ("ADBE Text Character Offset", "characterOffset"),
- ("ADBE Text Character Replace", "characterValue"),
- ("ADBE Text Blur", "blur"),
- ];
- if let Some((_, k)) = AE.iter().find(|(m, _)| m.eq_ignore_ascii_case(name)) {
++ let table = matchnames::TEXT_ANIMATOR_PROPERTIES;
++ if let Some((k, _)) = table.iter().find(|(_, m)| m.eq_ignore_ascii_case(name)) {
return Some(k);
}
let n = loose(name);
- table.iter().map(|(_, k)| *k).find(|k| loose(k) == n || (n == "anchorpoint" && *k == "anchor") || (n == "trackingamount" && *k == "tracking"))
- }
-
- /// The After Effects match name of a text animator property key (`tracking` is
- /// `ADBE Text Tracking Amount`), for scripts that look a property up by that name (#1063).
- fn animator_property_match_name(key: &str) -> Option<&'static str> {
- animator_property_table().iter().find(|(_, k)| *k == key).map(|(m, _)| *m)
- AE.iter().map(|(_, k)| *k).find(|k| loose(k) == n || (n == "anchorpoint" && *k == "anchor") || (n == "trackingamount" && *k == "tracking"))
++ table.iter().map(|(k, _)| *k).find(|k| loose(k) == n || (n == "anchorpoint" && *k == "anchor") || (n == "trackingamount" && *k == "tracking"))
}
/// `app.findMenuCommandId(name)`: 1-based index of the command whose label matches (menu items
diff --cc crates/script/src/tests.rs
index d05f82f1f,109b09747..e384ae1d6
--- a/crates/script/src/tests.rs
+++ b/crates/script/src/tests.rs
@@@ -1376,22 -1444,41 +1444,62 @@@ fn scripted_levels_alpha_controls_resol
assert_eq!(o.result, json!([0.5, 0.5]));
}
+ /// After Effects resolves the Transform properties and Source Text from the layer itself, without
+ /// naming their group: `layer.property("Position")` (#1348).
+ #[test]
+ fn layer_property_resolves_transform_and_source_text_shortcuts() {
+ let mut s = session();
+ let o = ok(
+ &mut s,
+ r#"
+ var c = app.project.items.addComp("x", 100, 100, 1, 5, 30);
+ var t = c.layers.addText("hi");
+ var s = c.layers.addSolid([1, 0, 0], "s", 100, 100, 1);
+ var listed = [t.property("Source Text"), s.property("Position"), s.property("Opacity"), s.property("Scale")].join(", ");
+ s.property("Position").setValue([30, 40]);
+ s.property("Opacity").setValue(25);
+ t.property("Source Text").setValue("bye");
+ s.threeDLayer = true;
+ [listed, s.property("Position").value[0], s.property("Opacity").value, t.property("Source Text").value.text,
+ s.property("ADBE Position").matchName, s("Anchor Point").matchName, s.property("Rotation").matchName,
+ s.property("X Rotation").matchName, s.property("Orientation").matchName]
+ "#,
+ );
+ assert_eq!(
+ o.result,
+ json!([
+ "[object Property], [object Property], [object Property], [object Property]",
+ 30,
+ 25,
+ "bye",
+ "ADBE Position",
+ "ADBE Anchor Point",
+ "ADBE Rotate Z",
+ "ADBE Rotate X",
+ "ADBE Orientation"
+ ])
+ );
+ let (_, c) = comp_named(&s, "x");
+ assert_eq!(layer(c, "s").props.prop("transform/opacity").unwrap().value, KV::Scalar(25.0));
+ }
++
+/// A text animator property added by its After Effects name is the one asked for, not its Tracking
+/// Type companion, and it can be found again by that name (#1063).
+#[test]
+fn scripted_text_animator_property_is_the_one_asked_for() {
+ let mut s = session();
+ let o = ok(
+ &mut s,
+ r#"
+ var c = app.project.items.addComp("Track", 960, 300, 1, 3, 30);
+ var t = c.layers.addText("MOVARA");
+ var a = t.property("ADBE Text Properties").property("ADBE Text Animators").addProperty("ADBE Text Animator");
+ var props = a.property("ADBE Text Animator Properties");
+ var added = props.addProperty("ADBE Text Tracking Amount");
+ added.setValue(20);
- [added.name, props.property("ADBE Text Tracking Amount").value, props.property("ADBE Text Tracking Amount").name]
++ [added.name, props.property("ADBE Text Tracking Amount").value, props.property("ADBE Text Tracking Amount").name,
++ added.matchName, props.property(added.matchName).propertyIndex]
+ "#,
+ );
- assert_eq!(o.result, json!(["Tracking Amount", 20, "Tracking Amount"]));
++ assert_eq!(o.result, json!(["Tracking Amount", 20, "Tracking Amount", "ADBE Text Tracking Amount", 2]));
+}
diff --cc crates/script/src/tests_services.rs
index 000000000,c0ecfab40..f5ba0bf3d
mode 000000,100644..100644
--- a/crates/script/src/tests_services.rs
+++ b/crates/script/src/tests_services.rs
@@@ -1,0 -1,47 +1,52 @@@
+ //! Scripts see the host's files: existence checks go through `Services`, like reads, so a host
+ //! that keeps files in memory (the web editor's browser storage) answers for its own files.
+
+ use std::collections::HashMap;
-use std::sync::{Arc, Mutex};
++use std::sync::{Arc, Mutex, PoisonError};
+
+ use effectcraft_engine::{Services, Session};
+
+ use crate::run_code;
+
+ #[derive(Default)]
+ struct Mem(Mutex<HashMap<String, Vec<u8>>>);
+
+ impl Services for Mem {
+ fn read_file(&self, path: &str) -> std::io::Result<Vec<u8>> {
- self.0.lock().unwrap().get(path).cloned().ok_or_else(|| std::io::Error::new(std::io::ErrorKind::NotFound, path.to_string()))
++ self.0
++ .lock()
++ .unwrap_or_else(PoisonError::into_inner)
++ .get(path)
++ .cloned()
++ .ok_or_else(|| std::io::Error::new(std::io::ErrorKind::NotFound, path.to_string()))
+ }
+ fn write_file(&self, path: &str, data: &[u8]) -> std::io::Result<()> {
- self.0.lock().unwrap().insert(path.into(), data.into());
++ self.0.lock().unwrap_or_else(PoisonError::into_inner).insert(path.into(), data.into());
+ Ok(())
+ }
+ fn exists(&self, path: &str) -> bool {
- self.0.lock().unwrap().contains_key(path)
++ self.0.lock().unwrap_or_else(PoisonError::into_inner).contains_key(path)
+ }
+ fn is_dir(&self, path: &str) -> bool {
+ path == "/browser/footage"
+ }
+ }
+
+ #[test]
+ fn a_script_opens_a_file_only_the_host_holds() {
+ let mem = Arc::new(Mem::default());
+ mem.write_file("/browser/data.txt", b"hello").unwrap();
+ let mut s = Session { services: mem, ..Default::default() };
+ crate::install(&mut s);
+ // A project in a folder that isn't on disk: scripts may read files next to it.
+ s.path = Some("/browser/project.ecproj".into());
+ let out = run_code(
+ &mut s,
+ r#"var f = new File("/browser/data.txt"); var ok = f.open("r");
+ writeLn(ok + " " + (ok ? f.read() : f.error));
+ writeLn(new File("/browser/missing.txt").exists + " " + new Folder("/browser/footage").exists);"#,
+ "t.jsx",
+ );
+ assert!(out.error.is_none(), "{:?}", out.error);
+ assert_eq!(out.output, vec!["true hello".to_string(), "false true".to_string()]);
+ }