diff --git a/crates/engine/src/cmd/paths.rs b/crates/engine/src/cmd/paths.rs index 2f1e1051b..00c38c502 100644 --- a/crates/engine/src/cmd/paths.rs +++ b/crates/engine/src/cmd/paths.rs @@ -9,7 +9,7 @@ use effectcraft_render::EvalCtx; use serde_json::{Value, json}; use super::layer_menu::target_masks; -use super::{CommandSpec, b_p, bad, f_p, has_comp, has_layers, layer_mut, layer_p, merge_p, str_p}; +use super::{CommandSpec, b_p, bad, comp_id, f_p, has_comp, has_layers, layer_mut, layer_p, layers_p, merge_p, str_p, unlocked}; use crate::{EngineError, Result, Session, VertexRef, cmd}; mod closed; @@ -153,26 +153,223 @@ fn parametric_path(ctx: &EvalCtx, layer: &effectcraft_project::Layer, g: &PropGr effectcraft_path::from_kurbo(&path).into_iter().next() } +/// How deep [`parametric_in`] looks into nested shape groups; deeper ones are left alone. +const MAX_SHAPE_DEPTH: usize = 64; + +/// The parametric paths in `g` (itself, if it is one), in drawing order. +fn parametric_in(g: &PropGroup, lid: LayerId, depth: usize, out: &mut Vec<(LayerId, Uid)>) { + if PARAMETRIC.contains(&g.match_id.as_str()) { + out.push((lid, g.uid)); + return; + } + if depth >= MAX_SHAPE_DEPTH { + return; + } + for c in g.groups() { + parametric_in(c, lid, depth + 1, out); + } +} + +/// Converts masks and the selected parametric shape paths (Rectangle, Ellipse, Polystar) to editable Bezier paths of the same geometry at the current time. +/// Items on locked layers are skipped, and it errors when nothing is left to convert. fn convert_to_bezier(s: &mut Session, p: &Value) -> Result { // On a shape layer this replaces a Rectangle, Ellipse or Polystar path with an editable // Path 1 of the same geometry, as After Effects does (#932). The parametric properties are // read at the current time and the path that comes out is static: a size or roundness // animation cannot survive the conversion, in After Effects either. - let (cid, items) = selected_shape_items(s, p)?; + const LABEL: &str = "Convert To Bezier Path"; + // An explicit `mask` is what converts: selected shape items (or `items`) don't take its place, + // and a `mask` that matches nothing is reported below rather than converting a shape. + // The composition is the one `comp` names (else the active one), as the mask and layer + // resolvers use; a selection made in another composition doesn't apply to it. + let cid = comp_id(s, p)?; + let mut items = if p.get("mask").is_some() { + Vec::new() + } else { + let (items_cid, items) = selected_shape_items(s, p, "path.convertToBezier")?; + if items_cid == cid { items } else { Vec::new() } + }; + // `items` count as given only as a list, as `selected_shape_items` reads them; a null or other + // value leaves the selection in charge. + let explicit_items = matches!(p.get("items"), Some(Value::Array(_))); + // An explicit `layer` / `layers` limits the selected shape items to those layers. + let explicit_layers = p.get("layer").is_some_and(|v| !v.is_null()) || p.get("layers").is_some(); + if explicit_layers && !explicit_items { + let (_, layers) = layers_p(s, p)?; + items.retain(|(l, _)| layers.contains(l)); + } + // Items on a locked layer are left alone (an error when they all are), whether they come from + // the selection or from `items`. + if !items.is_empty() { + let mut item_layers: Vec = items.iter().map(|(l, _)| *l).collect(); + item_layers.sort(); + item_layers.dedup(); + let open = unlocked(s, cid, item_layers, "path.convertToBezier")?; + items.retain(|(l, _)| open.contains(l)); + } let comp = s.project.comp(cid).ok_or(EngineError::NoComp)?; - let shapes: Vec<(LayerId, Uid)> = items - .into_iter() - .filter(|(l, u)| { - comp.layer(*l).and_then(|l| l.props.sub("contents")).and_then(|c| c.find_group(*u)).is_some_and(|g| PARAMETRIC.contains(&g.match_id.as_str())) - }) - .collect(); - if shapes.is_empty() { - return edit_mask_groups(s, p, "Convert To Bezier Path", |g| { - if let GroupKind::Mask { roto_bezier, .. } = &mut g.kind { - *roto_bezier = false; + // A selected parametric path converts itself, animated or not (After Effects keeps the shape + // at the current time). A selected shape group converts the parametric paths inside it + // (#3057: selecting Rectangle 1 rather than its Rectangle Path 1 reported "no masks"), but + // leaves the animated ones alone, as converting a whole layer does. + let mut shapes: Vec<(LayerId, Uid)> = Vec::new(); + let mut skipped: Vec<(LayerId, Uid)> = Vec::new(); + for (lid, uid) in &items { + let Some(g) = comp.layer(*lid).and_then(|l| l.props.sub("contents")).and_then(|c| c.find_group(*uid)) else { continue }; + if PARAMETRIC.contains(&g.match_id.as_str()) { + shapes.push((*lid, *uid)); + continue; + } + let mut found = Vec::new(); + parametric_in(g, *lid, 0, &mut found); + for (fl, fu) in found { + if g.find_group(fu).is_some_and(PropGroup::any_animated) { + skipped.push((fl, fu)); + } else { + shapes.push((fl, fu)); } - Ok(()) - }); + } + } + shapes.sort(); + shapes.dedup(); + // A path both selected itself and inside a selected group converts; it is not skipped. + skipped.sort(); + skipped.dedup(); + skipped.retain(|x| !shapes.contains(x)); + let mut skipped_animated = skipped.len(); + // Masks the selection targets too (a selected mask or one of its properties, mask vertices) + // convert with the shape paths in the same step, as the command acts on every selected path: + // selecting a mask and a Rectangle path must not convert only the rectangle. + // Explicit `items` name exactly what converts: the selection adds no masks to them. + let mut sel_masks: Vec<(LayerId, Uid)> = Vec::new(); + if !shapes.is_empty() && !explicit_items { + let masks_of = |l: LayerId| comp.layer(l).filter(|ly| !ly.switches.locked).and_then(|ly| ly.masks()); + for (l, u) in &s.state.selected_props { + let Some(m) = masks_of(*l) else { continue }; + sel_masks.extend(m.groups().filter(|g| g.uid == *u || g.find(*u).is_some()).map(|g| (*l, g.uid))); + } + for v in &s.state.selected_vertices { + if masks_of(v.layer).is_some_and(|m| m.groups().any(|g| g.uid == v.mask)) { + sel_masks.push((v.layer, v.mask)); + } + } + if explicit_layers { + let (_, layers) = layers_p(s, p)?; + sel_masks.retain(|(l, _)| layers.contains(l)); + } + sel_masks.sort(); + sel_masks.dedup(); + } + if shapes.is_empty() { + // Explicit `items` with nothing parametric in them (a Fill, a path that is already Bezier) + // convert nothing: they don't hand the conversion to a selected mask or the layer's masks. + if explicit_items && p.get("mask").is_none() { + let why = if skipped_animated > 0 { + "the parametric paths are animated: name the path itself in `items` to convert it at the current time (its animation is lost)" + } else { + "nothing to convert: the given items have no Rectangle, Ellipse or Polystar path" + }; + return Err(bad(LABEL, why)); + } + let (_, mut masks) = masks_for(s, p)?; + let mask_of = |l: LayerId, u: Uid| comp.layer(l).and_then(|l| l.masks()).and_then(|m| m.groups().find(|g| g.uid == u)); + masks.retain(|(l, u)| mask_of(*l, *u).is_some()); + // An explicit `layer` / `layers` is the scope: a mask selected on another layer is not + // converted in its place. With none of the selected masks in scope, the scoped layers' + // own masks are the targets, as for a selected layer. + let scope = if explicit_layers { Some(layers_p(s, p)?.1) } else { None }; + let in_scope = |l: &LayerId| scope.as_ref().is_none_or(|v| v.contains(l)); + if let Some(layers) = &scope { + masks.retain(|(l, _)| layers.contains(l)); + if masks.is_empty() && p.get("mask").is_none() { + for l in layers { + if let Some(m) = comp.layer(*l).and_then(|ly| ly.masks()) { + masks.extend(m.groups().map(|g| (*l, g.uid))); + } + } + } + } + masks.sort(); + masks.dedup(); + // Masks on a locked layer are left alone, as shape items are. This comes after the scope + // and its fallback, so a locked selected mask doesn't hand the conversion to other masks. + let mut mask_layers: Vec = masks.iter().map(|(l, _)| *l).collect(); + mask_layers.dedup(); + masks.retain(|(l, _)| comp.layer(*l).is_some_and(|ly| !ly.switches.locked)); + // Masks take priority when they are targeted (a `mask`, a selected mask or mask vertices) + // or when one of them has RotoBezier tangents to turn off. A selected layer's plain masks + // are already Bezier paths: converting them would do nothing and hide the layer's + // parametric shapes (#3057). + let mask_targeted = p.get("mask").is_some() + || (p.get("layer").is_none() && s.state.selected_vertices.iter().any(|v| in_scope(&v.layer))) + || s.state + .selected_props + .iter() + .any(|(l, u)| in_scope(l) && comp.layer(*l).and_then(|l| l.masks()).is_some_and(|m| m.uid != *u && m.find_group(*u).is_some())); + let any_roto = masks.iter().any(|(l, u)| mask_of(*l, *u).is_some_and(is_roto)); + // Every targeted mask is on a locked layer: say so rather than convert anything else. + if mask_targeted && masks.is_empty() && !mask_layers.is_empty() { + unlocked(s, cid, mask_layers, "path.convertToBezier")?; + return Err(bad(LABEL, "the masks are on locked layers")); + } + // A selected group whose paths were all skipped as animated reports that, rather than + // turning to the layer's masks. + if !masks.is_empty() && (mask_targeted || (any_roto && skipped_animated == 0)) { + // The scoped targets are edited as resolved here, not looked up again. + let n = masks.len(); + s.edit(LABEL, None, |proj, _| { + for (lid, uid) in &masks { + let l = layer_mut(proj, cid, *lid)?; + if let Some(g) = l.props.sub_mut("masks").and_then(|m| m.find_group_mut(*uid)) + && let GroupKind::Mask { roto_bezier, .. } = &mut g.kind + { + *roto_bezier = false; + } + } + Ok(()) + })?; + return Ok(json!({"masks": n})); + } + if p.get("mask").is_some() { + return Err(bad(LABEL, "no mask matches `mask`")); + } + // Only layers selected (#3057): a shape layer drawn with the Rectangle, Ellipse or Polygon + // tool converts its parametric paths, as the masks of a selected layer do. A selected + // shape item with nothing parametric in it (a Fill, a Bezier path) is not widened to the + // whole layer, and an animated parametric path is left alone here: converting keeps only + // the shape at the current time, so that takes selecting the path itself. + if items.is_empty() { + let (lcid, mut layers) = layers_p(s, p)?; + // A layer named twice (`layers: [1, "Shape 1"]`) converts once: its paths would + // otherwise be replaced twice, and the second replacement finds no such item. + layers.sort(); + layers.dedup(); + if lcid == cid { + for lid in unlocked(s, cid, layers, "path.convertToBezier")? { + let Some(l) = comp.layer(lid).filter(|l| matches!(l.source, LayerSource::Shape)) else { continue }; + let Some(contents) = l.props.sub("contents") else { continue }; + let mut found = Vec::new(); + parametric_in(contents, lid, 0, &mut found); + for (fl, fu) in found { + if contents.find_group(fu).is_some_and(PropGroup::any_animated) { + skipped_animated += 1; + } else { + shapes.push((fl, fu)); + } + } + } + } + } + if shapes.is_empty() { + let why = if skipped_animated > 0 { + "the parametric paths are animated: select one in the Timeline to convert it at the current time (its animation is lost)" + } else if !masks.is_empty() { + "nothing to convert: the masks are already Bezier paths" + } else { + "nothing to convert: select a Rectangle, Ellipse or Polystar path, a shape layer with one, or a layer with masks" + }; + return Err(bad(LABEL, why)); + } } let t = s.time(); let n = shapes.len(); @@ -210,13 +407,22 @@ fn convert_to_bezier(s: &mut Session, p: &Value) -> Result { made.push((lid, new_uid)); } proj.next_id = next; - // The new paths are what is selected, so the vertices can be edited straight away. + for (lid, uid) in &sel_masks { + let l = layer_mut(proj, cid, *lid)?; + if let Some(g) = l.props.sub_mut("masks").and_then(|m| m.find_group_mut(*uid)) + && let GroupKind::Mask { roto_bezier, .. } = &mut g.kind + { + *roto_bezier = false; + } + } + // The new paths are what is selected, so the vertices can be edited straight away; the + // converted masks stay selected with them. if !made.is_empty() { - st.selected_props = made.clone(); + st.selected_props = made.iter().chain(&sel_masks).copied().collect(); } Ok(made) })?; - Ok(json!({"paths": made.len(), "of": n})) + Ok(json!({"paths": made.len(), "of": n, "masks": sel_masks.len(), "skippedAnimated": skipped_animated})) } // ---------------------------------------------------------------- mask options @@ -389,24 +595,25 @@ const SHAPE_ITEMS: &[&str] = &[ "wiggle", "wiggler", "merge", "front", "bevel", "side", "back", ]; -/// Selected shape items (contents entries) of shape layers: (layer, uid). -fn selected_shape_items(s: &Session, p: &Value) -> Result<(ItemId, Vec<(LayerId, Uid)>)> { +/// Selected shape items (contents entries) of shape layers: (layer, uid), with their composition. +/// Explicit `items` are on the layer `layer_p` resolves, in the composition it resolves (`comp`, +/// else the active one); selected items are the active composition's. +fn selected_shape_items(s: &Session, p: &Value, cmd: &str) -> Result<(ItemId, Vec<(LayerId, Uid)>)> { + if let Some(Value::Array(a)) = p.get("items") { + let (cid, lid) = layer_p(s, p, cmd)?; + return Ok((cid, a.iter().filter_map(Value::as_u64).map(|u| (lid, u)).collect())); + } let cid = s.active_comp_id().ok_or(EngineError::NoComp)?; let comp = s.project.comp(cid).ok_or(EngineError::NoComp)?; let mut out = vec![]; - if let Some(Value::Array(a)) = p.get("items") { - let (_, lid) = layer_p(s, p, "path.groupShapes")?; - out.extend(a.iter().filter_map(Value::as_u64).map(|u| (lid, u))); - } else { - for (lid, uid) in &s.state.selected_props { - let Some(l) = comp.layer(*lid).filter(|l| matches!(l.source, LayerSource::Shape)) else { continue }; - if l.props - .sub("contents") - .and_then(|c| c.find_group(*uid)) - .is_some_and(|g| g.uid != l.props.sub("contents").map(|c| c.uid).unwrap_or(0) && SHAPE_ITEMS.contains(&g.match_id.as_str())) - { - out.push((*lid, *uid)); - } + for (lid, uid) in &s.state.selected_props { + let Some(l) = comp.layer(*lid).filter(|l| matches!(l.source, LayerSource::Shape)) else { continue }; + if l.props + .sub("contents") + .and_then(|c| c.find_group(*uid)) + .is_some_and(|g| g.uid != l.props.sub("contents").map(|c| c.uid).unwrap_or(0) && SHAPE_ITEMS.contains(&g.match_id.as_str())) + { + out.push((*lid, *uid)); } } Ok((cid, out)) @@ -421,8 +628,10 @@ fn has_shape_items(s: &Session) -> std::result::Result<(), String> { if ok { Ok(()) } else { Err("select shape items (Contents ▸ …) in the timeline".into()) } } +/// Wraps the selected shape items of one layer in a new group placed where the first of them was. +/// Errors when no shape items are selected or they do not share one parent group. fn group_shapes(s: &mut Session, p: &Value) -> Result { - let (cid, items) = selected_shape_items(s, p)?; + let (cid, items) = selected_shape_items(s, p, "path.groupShapes")?; let Some(lid) = items.first().map(|x| x.0) else { return Err(bad("path.groupShapes", "select shape items to group")) }; let uids: Vec = items.iter().filter(|(l, _)| *l == lid).map(|(_, u)| *u).collect(); let g = s.edit("Group Shapes", None, |proj, st| { @@ -496,8 +705,10 @@ fn offset_items(g: &mut PropGroup, d: [f64; 2]) { } } +/// Replaces each selected shape group with its children, baking the group's position offset into them. +/// Errors when no group is selected or a group has a scale, rotation, skew, opacity or animated transform. fn ungroup_shapes(s: &mut Session, p: &Value) -> Result { - let (cid, items) = selected_shape_items(s, p)?; + let (cid, items) = selected_shape_items(s, p, "path.ungroupShapes")?; let comp = s.project.comp(cid).ok_or(EngineError::NoComp)?; let groups: Vec<(LayerId, Uid)> = items .into_iter() @@ -548,11 +759,20 @@ fn ungroup_shapes(s: &mut Session, p: &Value) -> Result { Ok(json!({"ungrouped": n})) } +/// The command specs for the path commands: RotoBezier, Convert To Bezier Path, Group Shapes, Ungroup Shapes and the rest of this module. pub fn specs() -> Vec { vec![ cmd!("path.toggleClosed", "Closed", ["Layer", "Mask and Shape Path"], None, "{}", closed::has_paths, closed::toggle_closed), cmd!("path.rotoBezier", "RotoBezier", ["Layer", "Mask and Shape Path"], None, "{layer?, mask?, value?}", has_layers, roto_bezier), - cmd!("path.convertToBezier", "Convert To Bezier Path", ["Layer", "Mask and Shape Path"], None, "{layer?, mask?}", has_layers, convert_to_bezier), + cmd!( + "path.convertToBezier", + "Convert To Bezier Path", + ["Layer", "Mask and Shape Path"], + None, + "{layer?, layers?, mask?, items?: [uid]}", + has_layers, + convert_to_bezier + ), cmd!("path.groupShapes", "Group Shapes", ["Layer"], Some("Cmd+G"), "{layer?, items?: [uid]}", has_shape_items, group_shapes), cmd!("path.ungroupShapes", "Ungroup Shapes", ["Layer"], Some("Cmd+Shift+G"), "{layer?, items?: [group uid]}", has_shape_items, ungroup_shapes), cmd!( diff --git a/crates/engine/src/tests/stubs.rs b/crates/engine/src/tests/stubs.rs index 6f89f4f62..58fb21777 100644 --- a/crates/engine/src/tests/stubs.rs +++ b/crates/engine/src/tests/stubs.rs @@ -649,6 +649,432 @@ fn convert_to_bezier_evaluates_corner_expressions() { assert_eq!(path, expected, "the expression-evaluated corner is converted"); } +/// The uid of the first shape item with `match_id` in a shape layer's Contents. +fn shape_item(s: &Session, l: u64, match_id: &str) -> u64 { + /// Depth-first search of a property group tree for the uid of the first group with the given match id. + fn walk(g: &effectcraft_project::PropGroup, id: &str) -> Option { + if g.match_id == id { + return Some(g.uid); + } + g.groups().find_map(|c| walk(c, id)) + } + let ly = layer(s, l); + let contents = ly.props.sub("contents").unwrap(); + contents.groups().find_map(|c| walk(c, match_id)).unwrap() +} + +/// #3057: right after drawing with the Rectangle, Ellipse or Polygon tool only the layer is +/// selected; Convert To Bezier Path then converted nothing and reported "no masks". The selected +/// shape layer's parametric paths convert, as the masks of a selected layer do, and so do those in +/// a selected shape group (Rectangle 1 rather than its Rectangle Path 1). +#[test] +fn convert_to_bezier_on_a_selected_shape_layer_or_group() { + for kind in ["rect", "ellipse", "polygon"] { + let mut s = comp(); + let l = s.execute("layer.newShape", json!({"kind": kind, "size": [120, 80]})).unwrap()["layer"].as_u64().unwrap(); + assert_eq!(s.state.selected_layers, vec![LayerId(l)], "the new layer is selected"); + assert!(s.state.selected_props.is_empty(), "and no shape item"); + let out = s.execute("path.convertToBezier", json!({})).unwrap(); + assert_eq!(out["paths"].as_u64(), Some(1), "{kind}: {out}"); + let (new_uid, now) = first_shape(&s, l); + assert_eq!(now, "path", "{kind}: the layer's parametric path is a bezier path now"); + assert_eq!(s.state.selected_props, vec![(LayerId(l), new_uid)], "{kind}: and selected, to edit its vertices"); + } + + // A selected shape group converts the parametric path inside it. + let mut s = comp(); + let l = s.execute("layer.newShape", json!({"kind": "rect", "size": [120, 80]})).unwrap()["layer"].as_u64().unwrap(); + let group = shape_item(&s, l, "group"); + s.state.selected_props = vec![(LayerId(l), group)]; + let out = s.execute("path.convertToBezier", json!({})).unwrap(); + assert_eq!(out["paths"].as_u64(), Some(1), "{out}"); + assert_eq!(first_shape(&s, l).1, "path"); + + // Nothing parametric left: the error says what to select instead of asking for masks. + s.execute("layer.select", json!({"layers": [l]})).unwrap(); + let err = s.execute("path.convertToBezier", json!({})).unwrap_err().to_string(); + assert!(err.contains("Rectangle, Ellipse or Polystar"), "{err}"); +} + +/// #3057: a selected shape item with nothing parametric in it (a Fill) does not widen the +/// conversion to the rest of the layer. A guard for the fix (it passes on the old code too). +#[test] +fn convert_to_bezier_does_not_widen_a_selected_fill() { + let mut s = comp(); + let l = s.execute("layer.newShape", json!({"kind": "rect", "size": [120, 80], "fill": "#ff0000"})).unwrap()["layer"].as_u64().unwrap(); + let fill = shape_item(&s, l, "fill"); + s.state.selected_props = vec![(LayerId(l), fill)]; + assert!(s.execute("path.convertToBezier", json!({})).is_err()); + assert_eq!(first_shape(&s, l).1, "rect", "the rectangle stays parametric"); +} + +/// #3057: a selected shape layer's plain masks are already Bezier paths, so they don't hide its +/// rectangle: the rectangle converts. A RotoBezier mask still converts first. +#[test] +fn convert_to_bezier_on_a_shape_layer_with_a_plain_mask() { + let mut s = comp(); + let l = s.execute("layer.newShape", json!({"kind": "rect", "size": [120, 80]})).unwrap()["layer"].as_u64().unwrap(); + s.execute("mask.new", json!({"layer": l, "vertices": [[0, 0], [50, 0], [50, 50], [0, 50]], "closed": true})).unwrap(); + s.execute("layer.select", json!({"layers": [l]})).unwrap(); + s.state.selected_vertices.clear(); + let out = s.execute("path.convertToBezier", json!({})).unwrap(); + assert_eq!(out["paths"].as_u64(), Some(1), "{out}"); + assert_eq!(first_shape(&s, l).1, "path"); + + // Nothing parametric left and the mask is plain: say so rather than succeed doing nothing. + s.execute("layer.select", json!({"layers": [l]})).unwrap(); + s.state.selected_vertices.clear(); + let err = s.execute("path.convertToBezier", json!({})).unwrap_err().to_string(); + assert!(err.contains("already Bezier"), "{err}"); +} + +/// #3057: an explicit `mask` that matches no mask converts nothing, rather than every parametric +/// path of the layer. +#[test] +fn convert_to_bezier_with_an_unmatched_mask_converts_nothing() { + let mut s = comp(); + let l = s.execute("layer.newShape", json!({"kind": "rect", "size": [120, 80]})).unwrap()["layer"].as_u64().unwrap(); + assert!(s.execute("path.convertToBezier", json!({"layer": l, "mask": "No Such Mask"})).is_err()); + assert_eq!(first_shape(&s, l).1, "rect", "the rectangle stays parametric"); +} + +/// #3057: an explicit `layer` is the layer converted, even with a path selected on another one. +#[test] +fn convert_to_bezier_follows_an_explicit_layer() { + let mut s = comp(); + let a = s.execute("layer.newShape", json!({"kind": "rect", "size": [120, 80]})).unwrap()["layer"].as_u64().unwrap(); + let b = s.execute("layer.newShape", json!({"kind": "ellipse", "size": [120, 80]})).unwrap()["layer"].as_u64().unwrap(); + let (a_rect, _) = first_shape(&s, a); + s.state.selected_props = vec![(LayerId(a), a_rect)]; + let out = s.execute("path.convertToBezier", json!({"layer": b})).unwrap(); + assert_eq!(out["paths"].as_u64(), Some(1), "{out}"); + assert_eq!(first_shape(&s, b).1, "path", "the given layer converts"); + assert_eq!(first_shape(&s, a).1, "rect", "the selection on another layer does not"); +} + +/// #3057: converting a selected layer leaves an animated parametric path alone (its animation +/// would be lost) and reports it; selecting that path converts it at the current time, as After +/// Effects does. +#[test] +fn convert_to_bezier_skips_animated_paths_of_a_selected_layer() { + let mut s = comp(); + let l = s.execute("layer.newShape", json!({"kind": "rect", "size": [120, 80]})).unwrap()["layer"].as_u64().unwrap(); + let (rect, _) = first_shape(&s, l); + let size = layer(&s, l).props.sub("contents").unwrap().find_group(rect).unwrap().get("size").unwrap().uid; + for (t, v) in [(0.0, [120.0, 80.0]), (1.0, [200.0, 100.0])] { + s.execute("prop.addKey", json!({"layer": l, "path": format!("@{size}"), "time": t, "value": v})).unwrap(); + } + s.execute("layer.select", json!({"layers": [l]})).unwrap(); + let err = s.execute("path.convertToBezier", json!({})).unwrap_err().to_string(); + assert!(err.contains("animated"), "{err}"); + assert_eq!(first_shape(&s, l).1, "rect", "the animated rectangle stays parametric"); + + s.state.selected_props = vec![(LayerId(l), rect)]; + let out = s.execute("path.convertToBezier", json!({})).unwrap(); + assert_eq!(out["paths"].as_u64(), Some(1), "{out}"); + assert_eq!(first_shape(&s, l).1, "path", "the selected path converts"); +} + +/// #3057 review: a layer named twice in `layers` (by id and by name) converts once, and its +/// animated paths are counted once. +#[test] +fn convert_to_bezier_with_a_layer_named_twice() { + let mut s = comp(); + let l = s.execute("layer.newShape", json!({"kind": "rect", "size": [120, 80]})).unwrap()["layer"].as_u64().unwrap(); + let name = layer(&s, l).name; + let out = s.execute("path.convertToBezier", json!({"layers": [l, name, l]})).unwrap(); + assert_eq!(out["paths"].as_u64(), Some(1), "{out}"); + assert_eq!(out["of"].as_u64(), Some(1), "{out}"); + assert_eq!(first_shape(&s, l).1, "path"); + + let mut s = comp(); + let a = s.execute("layer.newShape", json!({"kind": "rect", "size": [120, 80]})).unwrap()["layer"].as_u64().unwrap(); + let b = s.execute("layer.newShape", json!({"kind": "ellipse", "size": [120, 80]})).unwrap()["layer"].as_u64().unwrap(); + let (rect, _) = first_shape(&s, a); + let size = layer(&s, a).props.sub("contents").unwrap().find_group(rect).unwrap().get("size").unwrap().uid; + for (t, v) in [(0.0, [120.0, 80.0]), (1.0, [200.0, 100.0])] { + s.execute("prop.addKey", json!({"layer": a, "path": format!("@{size}"), "time": t, "value": v})).unwrap(); + } + s.execute("layer.select", json!({"layers": [b]})).unwrap(); + let out = s.execute("path.convertToBezier", json!({"layers": [a, a, b]})).unwrap(); + assert_eq!(out["paths"].as_u64(), Some(1), "{out}"); + assert_eq!(out["skippedAnimated"].as_u64(), Some(1), "{out}"); +} + +/// #3057 review: shape items on a locked layer don't convert, whether given as `items` or +/// selected; unlocked, they do. +#[test] +fn convert_to_bezier_leaves_a_locked_layer_alone() { + for explicit in [true, false] { + let mut s = comp(); + let l = s.execute("layer.newShape", json!({"kind": "rect", "size": [120, 80]})).unwrap()["layer"].as_u64().unwrap(); + let (rect, _) = first_shape(&s, l); + let lock = |s: &mut Session, v: bool| { + s.execute("layer.setSwitch", json!({"layers": [l], "switch": "locked", "value": v})).unwrap(); + }; + let run = |s: &mut Session| { + if explicit { + s.execute("path.convertToBezier", json!({"layer": l, "items": [rect]})) + } else { + s.state.selected_props = vec![(LayerId(l), rect)]; + s.execute("path.convertToBezier", json!({})) + } + }; + lock(&mut s, true); + let err = run(&mut s).unwrap_err().to_string(); + assert!(err.contains("locked"), "explicit {explicit}: {err}"); + assert_eq!(first_shape(&s, l).1, "rect", "explicit {explicit}: the locked rectangle stays parametric"); + lock(&mut s, false); + let out = run(&mut s).unwrap(); + assert_eq!(out["paths"].as_u64(), Some(1), "explicit {explicit}: {out}"); + assert_eq!(first_shape(&s, l).1, "path"); + } +} + +/// #3057 review: a selected group leaves its animated paths alone and reports them, as +/// converting a whole layer does; the path itself, selected, still converts. +#[test] +fn convert_to_bezier_skips_animated_paths_of_a_selected_group() { + let mut s = comp(); + let l = s.execute("layer.newShape", json!({"kind": "rect", "size": [120, 80]})).unwrap()["layer"].as_u64().unwrap(); + let (rect, _) = first_shape(&s, l); + let group = shape_item(&s, l, "group"); + let size = layer(&s, l).props.sub("contents").unwrap().find_group(rect).unwrap().get("size").unwrap().uid; + s.execute("prop.setExpression", json!({"layer": l, "path": format!("@{size}"), "expression": "[100, 100]"})).unwrap(); + + s.state.selected_props = vec![(LayerId(l), group)]; + let err = s.execute("path.convertToBezier", json!({})).unwrap_err().to_string(); + assert!(err.contains("animated"), "{err}"); + assert_eq!(first_shape(&s, l).1, "rect", "the expression-driven rectangle stays parametric"); + + // The group and its path both selected: the path is converted, not skipped. + s.state.selected_props = vec![(LayerId(l), group), (LayerId(l), rect)]; + let out = s.execute("path.convertToBezier", json!({})).unwrap(); + assert_eq!(out["paths"].as_u64(), Some(1), "{out}"); + assert_eq!(out["skippedAnimated"].as_u64(), Some(0), "{out}"); + assert_eq!(first_shape(&s, l).1, "path"); +} + +/// #3057 review: an explicit `mask` is what converts, even with a parametric path selected or +/// `items` given; a `mask` that matches nothing is an error and converts no shape. +#[test] +fn convert_to_bezier_gives_an_explicit_mask_priority() { + // A shape layer with a rectangle and a RotoBezier mask; the rectangle's path is selected. + let setup = || { + let mut s = comp(); + let l = s.execute("layer.newShape", json!({"kind": "rect", "size": [120, 80]})).unwrap()["layer"].as_u64().unwrap(); + s.execute("mask.new", json!({"layer": l, "vertices": [[0, 0], [50, 0], [50, 50], [0, 50]], "closed": true})).unwrap(); + let m = layer(&s, l).masks().unwrap().groups().next().unwrap().uid; + s.execute("path.rotoBezier", json!({"layer": l, "mask": m, "value": true})).unwrap(); + let (rect, _) = first_shape(&s, l); + s.state.selected_props = vec![(LayerId(l), rect)]; + (s, l, m, rect) + }; + + // A matching mask converts, not the selected rectangle. + let (mut s, l, m, _) = setup(); + s.execute("path.convertToBezier", json!({"layer": l, "mask": m})).unwrap(); + assert!(matches!(mask_kind(&s, l), GroupKind::Mask { roto_bezier: false, .. }), "the mask converts"); + assert_eq!(first_shape(&s, l).1, "rect", "the selected rectangle stays parametric"); + + // An unmatched mask is an error, and the selected rectangle is not converted in its place. + let (mut s, l, _, _) = setup(); + let err = s.execute("path.convertToBezier", json!({"layer": l, "mask": "No Such Mask"})).unwrap_err().to_string(); + assert!(err.contains("mask"), "{err}"); + assert_eq!(first_shape(&s, l).1, "rect"); + assert!(matches!(mask_kind(&s, l), GroupKind::Mask { roto_bezier: true, .. })); + + // `mask` and `items` together: the mask converts, the items don't. + let (mut s, l, m, rect) = setup(); + s.state.selected_props.clear(); + s.execute("path.convertToBezier", json!({"layer": l, "mask": m, "items": [rect]})).unwrap(); + assert!(matches!(mask_kind(&s, l), GroupKind::Mask { roto_bezier: false, .. }), "the mask converts"); + assert_eq!(first_shape(&s, l).1, "rect", "the listed rectangle stays parametric"); +} + +/// #3057 review: a selected mask and a selected parametric path both convert, in one undo step; +/// neither is dropped for the other. +#[test] +fn convert_to_bezier_converts_a_selected_mask_and_path_together() { + let mut s = comp(); + let l = s.execute("layer.newShape", json!({"kind": "rect", "size": [120, 80]})).unwrap()["layer"].as_u64().unwrap(); + s.execute("mask.new", json!({"layer": l, "vertices": [[0, 0], [50, 0], [50, 50], [0, 50]], "closed": true})).unwrap(); + let m = layer(&s, l).masks().unwrap().groups().next().unwrap().uid; + s.execute("path.rotoBezier", json!({"layer": l, "mask": m, "value": true})).unwrap(); + let (rect, _) = first_shape(&s, l); + s.state.selected_vertices.clear(); + s.state.selected_props = vec![(LayerId(l), m), (LayerId(l), rect)]; + + let out = s.execute("path.convertToBezier", json!({})).unwrap(); + assert_eq!(out["paths"].as_u64(), Some(1), "{out}"); + assert_eq!(out["masks"].as_u64(), Some(1), "{out}"); + assert_eq!(first_shape(&s, l).1, "path", "the rectangle converts"); + assert!(matches!(mask_kind(&s, l), GroupKind::Mask { roto_bezier: false, .. }), "and so does the mask"); + + s.execute("edit.undo", json!({})).unwrap(); + assert_eq!(first_shape(&s, l).1, "rect", "one undo restores the rectangle"); + assert!(matches!(mask_kind(&s, l), GroupKind::Mask { roto_bezier: true, .. }), "and the mask"); +} + +/// #3057 review: explicit `items` convert only those shape paths; a RotoBezier mask that happens to +/// be selected on the same layer is not converted along with them. +#[test] +fn convert_to_bezier_with_explicit_items_leaves_a_selected_mask_alone() { + let mut s = comp(); + let l = s.execute("layer.newShape", json!({"kind": "rect", "size": [120, 80]})).unwrap()["layer"].as_u64().unwrap(); + s.execute("mask.new", json!({"layer": l, "vertices": [[0, 0], [50, 0], [50, 50], [0, 50]], "closed": true})).unwrap(); + let m = layer(&s, l).masks().unwrap().groups().next().unwrap().uid; + s.execute("path.rotoBezier", json!({"layer": l, "mask": m, "value": true})).unwrap(); + let (rect, _) = first_shape(&s, l); + s.state.selected_vertices.clear(); + s.state.selected_props = vec![(LayerId(l), m)]; + let out = s.execute("path.convertToBezier", json!({"layer": l, "items": [rect]})).unwrap(); + assert_eq!(out["paths"].as_u64(), Some(1), "{out}"); + assert!(out.get("masks").is_none_or(|v| v.as_u64().unwrap_or(0) == 0), "{out}"); + assert_eq!(first_shape(&s, l).1, "path", "the listed rectangle converts"); + assert!(matches!(mask_kind(&s, l), GroupKind::Mask { roto_bezier: true, .. }), "the selected mask is unchanged"); +} + +/// #3057 review: explicit `items` with nothing parametric in them (a Fill, a path that is already +/// Bezier) are an error and change nothing: neither a selected RotoBezier mask nor, with no mask +/// selected, the layer's own RotoBezier masks are converted in their place. +#[test] +fn convert_to_bezier_with_explicit_non_parametric_items_converts_no_mask() { + for item in ["fill", "path"] { + for mask_selected in [true, false] { + let mut s = comp(); + let l = s.execute("layer.newShape", json!({"kind": "rect", "size": [120, 80], "fill": "#ff0000"})).unwrap()["layer"].as_u64().unwrap(); + s.execute("mask.new", json!({"layer": l, "vertices": [[0, 0], [50, 0], [50, 50], [0, 50]], "closed": true})).unwrap(); + let m = layer(&s, l).masks().unwrap().groups().next().unwrap().uid; + s.execute("path.rotoBezier", json!({"layer": l, "mask": m, "value": true})).unwrap(); + let (rect, _) = first_shape(&s, l); + s.state.selected_vertices.clear(); + s.state.selected_props.clear(); + // An already-Bezier path: the rectangle converted first, the mask left as it is. + if item == "path" { + s.execute("path.convertToBezier", json!({"layer": l, "items": [rect]})).unwrap(); + assert!(matches!(mask_kind(&s, l), GroupKind::Mask { roto_bezier: true, .. })); + } + let uid = shape_item(&s, l, item); + s.state.selected_vertices.clear(); + s.state.selected_props = if mask_selected { vec![(LayerId(l), m)] } else { Vec::new() }; + let before = layer(&s, l).props; + + let err = s.execute("path.convertToBezier", json!({"layer": l, "items": [uid]})).unwrap_err().to_string(); + assert!(err.contains("Rectangle, Ellipse or Polystar"), "{item}, mask selected {mask_selected}: {err}"); + assert!(matches!(mask_kind(&s, l), GroupKind::Mask { roto_bezier: true, .. }), "{item}, mask selected {mask_selected}: the mask is unchanged"); + assert!(layer(&s, l).props == before, "{item}, mask selected {mask_selected}: no shape or mask changed"); + } + } +} + +/// #3057 review: an explicit `layer` scopes the mask conversion too: a RotoBezier mask selected +/// on another layer is left alone, and the given layer's masks convert. +#[test] +fn convert_to_bezier_keeps_masks_to_an_explicit_layer() { + let mut s = comp(); + let (a, _) = masked(&mut s); + let (b, mb) = masked(&mut s); + for l in [a, b] { + s.state.selected_props.clear(); + s.state.selected_vertices.clear(); + s.execute("path.rotoBezier", json!({"layer": l, "value": true})).unwrap(); + } + assert!(matches!(mask_kind(&s, a), GroupKind::Mask { roto_bezier: true, .. })); + assert!(matches!(mask_kind(&s, b), GroupKind::Mask { roto_bezier: true, .. })); + s.state.selected_vertices.clear(); + s.state.selected_props = vec![(LayerId(b), mb)]; + + let out = s.execute("path.convertToBezier", json!({"layer": a})).unwrap(); + assert_eq!(out["masks"].as_u64(), Some(1), "{out}"); + assert!(matches!(mask_kind(&s, a), GroupKind::Mask { roto_bezier: false, .. }), "the given layer's mask converts"); + assert!(matches!(mask_kind(&s, b), GroupKind::Mask { roto_bezier: true, .. }), "the mask selected on another layer does not"); +} + +/// #3057 review: a RotoBezier mask on a locked layer stays as it is, given as the layer or +/// selected, and removing it as a target does not hand the conversion to another layer's mask. +#[test] +fn convert_to_bezier_leaves_a_locked_layers_masks_alone() { + let mut s = comp(); + let (a, ma) = masked(&mut s); + let (b, _) = masked(&mut s); + for l in [a, b] { + s.state.selected_props.clear(); + s.state.selected_vertices.clear(); + s.execute("path.rotoBezier", json!({"layer": l, "value": true})).unwrap(); + } + s.execute("layer.setSwitch", json!({"layers": [a], "switch": "locked", "value": true})).unwrap(); + let roto = |s: &Session, l: u64| matches!(mask_kind(s, l), GroupKind::Mask { roto_bezier: true, .. }); + + // The locked layer given explicitly. + s.state.selected_props.clear(); + s.state.selected_vertices.clear(); + let err = s.execute("path.convertToBezier", json!({"layer": a})).unwrap_err().to_string(); + assert!(err.contains("locked"), "{err}"); + assert!(roto(&s, a), "the locked layer's mask stays RotoBezier"); + + // Its mask selected, with the unlocked layer in scope too: B's mask is not converted instead. + s.state.selected_props = vec![(LayerId(a), ma)]; + let err = s.execute("path.convertToBezier", json!({"layers": [a, b]})).unwrap_err().to_string(); + assert!(err.contains("locked"), "{err}"); + assert!(roto(&s, a) && roto(&s, b), "neither mask converts"); +} + +/// #3057 review: an explicit `comp` that is not the active composition is where `items` and +/// `mask` are looked up and converted. +#[test] +fn convert_to_bezier_in_a_composition_that_is_not_active() { + let mut s = comp(); + let main = s.active_comp_id().unwrap(); + let l = s.execute("layer.newShape", json!({"kind": "rect", "size": [120, 80]})).unwrap()["layer"].as_u64().unwrap(); + let (rect, _) = first_shape(&s, l); + let (solid, m) = masked(&mut s); + s.state.selected_props.clear(); + s.state.selected_vertices.clear(); + s.execute("path.rotoBezier", json!({"layer": solid, "value": true})).unwrap(); + s.execute("comp.new", json!({"name": "Other", "width": 400, "height": 300, "frameRate": 30, "duration": 4})).unwrap(); + assert_ne!(s.active_comp_id(), Some(main), "another composition is active"); + let in_main = |s: &Session| s.project.comp(main).unwrap().clone(); + let main_mask_roto = |s: &Session| { + let c = in_main(s); + matches!(c.layer(LayerId(solid)).unwrap().masks().unwrap().find_group(m).unwrap().kind, GroupKind::Mask { roto_bezier: true, .. }) + }; + + // Explicit shape items. + let out = s.execute("path.convertToBezier", json!({"comp": main.0, "layer": l, "items": [rect]})).unwrap(); + assert_eq!(out["paths"].as_u64(), Some(1), "{out}"); + let c = in_main(&s); + let contents = c.layer(LayerId(l)).unwrap().props.sub("contents").unwrap(); + assert!(contents.find_group(rect).is_none(), "the rectangle is replaced in that composition"); + + // An explicit mask. + assert!(main_mask_roto(&s)); + s.execute("path.convertToBezier", json!({"comp": main.0, "layer": solid, "mask": m})).unwrap(); + assert!(!main_mask_roto(&s), "the mask converts in that composition"); +} + +/// #3057: parametric paths in nested groups convert from the layer and from the outer group. +#[test] +fn convert_to_bezier_finds_paths_in_nested_groups() { + for select_outer in [false, true] { + let mut s = comp(); + let l = s.execute("layer.newShape", json!({"kind": "star", "size": [120, 120]})).unwrap()["layer"].as_u64().unwrap(); + let inner = shape_item(&s, l, "group"); + s.state.selected_props = vec![(LayerId(l), inner)]; + s.execute("path.groupShapes", json!({})).unwrap(); + let outer = shape_item(&s, l, "group"); + assert_ne!(outer, inner, "the shape is nested one group deeper"); + if select_outer { + s.state.selected_props = vec![(LayerId(l), outer)]; + } else { + s.execute("layer.select", json!({"layers": [l]})).unwrap(); + } + let out = s.execute("path.convertToBezier", json!({})).unwrap(); + assert_eq!(out["paths"].as_u64(), Some(1), "{out}"); + assert_eq!(first_shape(&s, l).1, "path"); + } +} + /// #2201: a viewer drag on the free-transform box sends one `path.freeTransform` per frame with a /// shared merge key; one undo must revert the whole drag, not the last frame. #[test]