M13.1: branching history, puppet pin recording, WebAssembly plug-in API, ScriptUI model and hosts, File ▸ Scripts, deterministic render test
- engine: History keeps undone lines as branches; edit.history.list/goto; History panel tree; MCP history tool - puppet.recordPin / puppet.recordOptions; viewer Cmd-drag records; Record Options form - effects::plugin (EffectPlugin trait, register_plugin, API v1); effectcraft-plugin (wasmi, sandboxed, fuel-limited); effect.plugins.load/list - ScriptUI: engine scriptui model + layout + scriptui.* commands; script crate scriptui.js and script-host threads (dialogs block show()) - File ▸ Scripts: install script / ScriptUI panel, sample scripts, window.scriptPanel - host test: bit-identical renders across runs and thread counts Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
ebcb8c95ce
commit
89c9b1b6ee
@@ -7,7 +7,7 @@
|
||||
|
||||
use effectcraft_effects::puppet::{self, MeshOpts, PinKind};
|
||||
use effectcraft_effects::{Buf, Params};
|
||||
use effectcraft_keyframe::Value as KV;
|
||||
use effectcraft_keyframe::{Keyframe, Value as KV};
|
||||
use effectcraft_project::build::Ids;
|
||||
use effectcraft_project::{ItemId, LayerId, Node, PropGroup, Uid};
|
||||
use effectcraft_render::{EvalCtx, RenderOpts, Renderer};
|
||||
@@ -309,6 +309,122 @@ fn mesh_opts(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
Ok(Value::Null)
|
||||
}
|
||||
|
||||
/// Parse recorded samples `[[t, x, y]…]` (t = seconds since the drag began, layer space).
|
||||
fn samples(p: &Value, cmd: &str) -> Result<Vec<(f64, [f64; 2])>> {
|
||||
let arr = p
|
||||
.get("samples")
|
||||
.and_then(Value::as_array)
|
||||
.ok_or_else(|| bad(cmd, "missing `samples`: [[t, x, y]…] (t = seconds since the drag began, layer space)"))?;
|
||||
let mut rec = Vec::with_capacity(arr.len());
|
||||
for (i, v) in arr.iter().enumerate() {
|
||||
let a: Vec<f64> = v.as_array().map(|a| a.iter().filter_map(Value::as_f64).collect()).unwrap_or_default();
|
||||
if a.len() < 3 || a.iter().any(|x| !x.is_finite()) {
|
||||
return Err(bad(cmd, format!("sample {i}: expected [t, x, y]")));
|
||||
}
|
||||
rec.push((a[0], [a[1], a[2]]));
|
||||
}
|
||||
if rec.is_empty() {
|
||||
return Err(bad(cmd, "need at least one sample"));
|
||||
}
|
||||
rec.sort_by(|a, b| a.0.total_cmp(&b.0));
|
||||
Ok(rec)
|
||||
}
|
||||
|
||||
/// Puppet tool Record mode (⌘/Ctrl-drag a pin): the pin's drag, sampled in real time, becomes
|
||||
/// Position keyframes at the comp frame rate from the current time on, replacing the keys in
|
||||
/// the recorded span. Record Options: `speed` (%: 100 plays back at the speed of the drag,
|
||||
/// 200 twice as slow) and `smoothing` (removes extraneous keys like the Smoother; 0 = a key
|
||||
/// on every frame). The current time stays where recording began.
|
||||
fn record_pin(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let cmd = "puppet.recordPin";
|
||||
let (cid, lid) = layer_p(s, p, cmd)?;
|
||||
let key = p.get("pin").cloned().ok_or_else(|| bad(cmd, "missing `pin` (uid or name)"))?;
|
||||
let uid = pin_uid(s, cid, lid, &key, cmd)?;
|
||||
let rec = samples(p, cmd)?;
|
||||
let opts = &s.state.puppet;
|
||||
let speed = f_p(p, "speed").unwrap_or(opts.record_speed).clamp(1.0, 10_000.0) / 100.0;
|
||||
// Smoothing 10 (the default) keeps the path within 1 layer pixel of the drag.
|
||||
let smoothing = f_p(p, "smoothing").unwrap_or(opts.record_smoothing).max(0.0) / 10.0;
|
||||
let comp = s.project.comp(cid).ok_or(EngineError::NoComp)?;
|
||||
let fr = comp.frame_rate;
|
||||
let fd = fr.frame_duration().seconds();
|
||||
let start = match p.get("start").and_then(Value::as_f64) {
|
||||
Some(t) => fr.snap_nearest(Tick::from_seconds_f64(t)),
|
||||
None => s.time_of(cid),
|
||||
};
|
||||
let f0 = fr.frame_at(start);
|
||||
let end_frame = fr.frame_at(comp.duration - comp.frame_duration());
|
||||
let t_rec0 = rec[0].0;
|
||||
let span = (rec[rec.len() - 1].0 - t_rec0) * speed;
|
||||
let nframes = (span / fd).round().max(0.0) as i64;
|
||||
let at = |secs: f64| {
|
||||
let i = rec.partition_point(|r| r.0 <= secs).clamp(1, rec.len().max(2) - 1);
|
||||
if rec.len() == 1 {
|
||||
return rec[0].1;
|
||||
}
|
||||
let (a, b) = (rec[i - 1], rec[i]);
|
||||
let f = if b.0 > a.0 { ((secs - a.0) / (b.0 - a.0)).clamp(0.0, 1.0) } else { 1.0 };
|
||||
[a.1[0] + (b.1[0] - a.1[0]) * f, a.1[1] + (b.1[1] - a.1[1]) * f]
|
||||
};
|
||||
let n = s.edit("Record Puppet Pin", None, |proj, st| {
|
||||
let l = layer_mut(proj, cid, lid)?;
|
||||
let lc = l.clone();
|
||||
let g = l.props.find_group_mut(uid).ok_or_else(|| bad(cmd, "pin gone"))?;
|
||||
let kind = PinKind::from_index(g.get("kind").map(|k| k.value.as_enum()).unwrap_or(0));
|
||||
if !kind.moves() {
|
||||
return Err(bad(cmd, "only Position and Advanced pins record motion"));
|
||||
}
|
||||
let pos = g.get_mut("position").ok_or_else(|| bad(cmd, "the pin has no Position"))?;
|
||||
let mut new: Vec<Keyframe> = vec![];
|
||||
for k in 0..=nframes {
|
||||
let f = f0 + k;
|
||||
if f > end_frame {
|
||||
break;
|
||||
}
|
||||
let q = at(t_rec0 + k as f64 * fd / speed);
|
||||
new.push(Keyframe::new(lc.layer_time(fr.tick_of(f)), KV::Vec2(q)));
|
||||
}
|
||||
let (Some(t0), Some(t1)) = (new.first().map(|k| k.time), new.last().map(|k| k.time)) else { return Ok(0) };
|
||||
let mut keys: Vec<Keyframe> = pos.keys.iter().filter(|k| k.time < t0 || k.time > t1).cloned().collect();
|
||||
keys.extend(new);
|
||||
keys.sort_by_key(|k| k.time);
|
||||
let (a, b) = (keys.iter().position(|k| k.time == t0).unwrap_or(0), keys.iter().position(|k| k.time == t1).unwrap_or(0));
|
||||
if smoothing > 0.0 && b > a + 1 {
|
||||
let samples: Vec<(Tick, Vec<f64>)> = keys[a..=b].iter().map(|k| (k.time, k.value.components())).collect();
|
||||
keys = super::anim_tools::smooth_keys(&keys, a, b, &samples, smoothing, true);
|
||||
}
|
||||
pos.keys = keys;
|
||||
pos.value = KV::Vec2(rec[0].1);
|
||||
let puid = pos.uid;
|
||||
st.selected_props = vec![(lid, uid)];
|
||||
st.selected_keys = pos.keys.iter().filter(|k| k.time >= t0 && k.time <= t1).map(|k| crate::KeyRef { layer: lid, prop: puid, time: k.time }).collect();
|
||||
Ok(st.selected_keys.len())
|
||||
})?;
|
||||
// The current time returns to where recording began.
|
||||
s.state.times.insert(cid, start);
|
||||
let end = fr.tick_of((f0 + nframes).min(end_frame));
|
||||
Ok(json!({"keys": n, "start": start.seconds(), "end": end.seconds(), "frames": nframes + 1}))
|
||||
}
|
||||
|
||||
/// Record Options (the Puppet tool's Record Options… dialog): `speed`, `smoothing`,
|
||||
/// `useDraftDeformation`, `showMesh`. Returns the options.
|
||||
fn record_options(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let o = &mut s.state.puppet;
|
||||
if let Some(v) = f_p(p, "speed") {
|
||||
o.record_speed = v.clamp(1.0, 10_000.0);
|
||||
}
|
||||
if let Some(v) = f_p(p, "smoothing") {
|
||||
o.record_smoothing = v.clamp(0.0, 100.0);
|
||||
}
|
||||
if let Some(v) = p.get("useDraftDeformation").and_then(Value::as_bool) {
|
||||
o.record_draft = v;
|
||||
}
|
||||
if let Some(v) = p.get("showMesh").and_then(Value::as_bool) {
|
||||
o.record_show_mesh = v;
|
||||
}
|
||||
Ok(json!({"speed": o.record_speed, "smoothing": o.record_smoothing, "useDraftDeformation": o.record_draft, "showMesh": o.record_show_mesh}))
|
||||
}
|
||||
|
||||
/// Meshes, pins and the deformed mesh at the current time (layer space).
|
||||
fn info(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let (cid, lid) = layer_p(s, p, "puppet.info")?;
|
||||
@@ -364,6 +480,24 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
mesh_opts
|
||||
),
|
||||
crate::query!("puppet.info", "Puppet Mesh Info", "{layer?, time? | frame?}", info),
|
||||
cmd!(
|
||||
"puppet.recordPin",
|
||||
"Record Puppet Pin",
|
||||
[],
|
||||
None,
|
||||
"{layer?, pin: uid | name, samples: [[t, x, y]…] (t = seconds since the drag began, layer space), start? (s, default current time), speed? (%, Record Options), smoothing? (Record Options)}",
|
||||
has_comp,
|
||||
record_pin
|
||||
),
|
||||
cmd!(
|
||||
"puppet.recordOptions",
|
||||
"Record Options...",
|
||||
[],
|
||||
None,
|
||||
"{speed? (%, 100), smoothing? (0-100), useDraftDeformation?, showMesh?}",
|
||||
super::always,
|
||||
record_options
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
@@ -376,11 +510,30 @@ pub struct PuppetOptions {
|
||||
pub triangles: f64,
|
||||
/// Show the mesh in the viewer.
|
||||
pub show_mesh: bool,
|
||||
/// Record Options ▸ Speed (%): 100 plays a recording back at the speed of the drag.
|
||||
pub record_speed: f64,
|
||||
/// Record Options ▸ Smoothing: removes extraneous keys from recorded motion (0 = a key per
|
||||
/// frame).
|
||||
pub record_smoothing: f64,
|
||||
/// Record Options ▸ Use Draft Deformation: the viewer shows the draft (unexpanded) mesh
|
||||
/// while recording.
|
||||
pub record_draft: bool,
|
||||
/// Record Options ▸ Show Mesh while recording.
|
||||
pub record_show_mesh: bool,
|
||||
}
|
||||
|
||||
impl Default for PuppetOptions {
|
||||
fn default() -> Self {
|
||||
let d = MeshOpts::default();
|
||||
PuppetOptions { density: d.density, expansion: d.expansion, triangles: d.triangles, show_mesh: false }
|
||||
PuppetOptions {
|
||||
density: d.density,
|
||||
expansion: d.expansion,
|
||||
triangles: d.triangles,
|
||||
show_mesh: false,
|
||||
record_speed: 100.0,
|
||||
record_smoothing: 10.0,
|
||||
record_draft: false,
|
||||
record_show_mesh: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user