diff --git a/crates/effects/src/lib.rs b/crates/effects/src/lib.rs index e3915797f2..aa95c8cfdd 100644 --- a/crates/effects/src/lib.rs +++ b/crates/effects/src/lib.rs @@ -103,8 +103,8 @@ pub use solids::{ with_graphic, }; pub use solids::{ - GRAPHIC_MAX_OFFSET, GRAPHIC_SCALE_RANGE, GRAPHICS_NOT_TAKEN, appearance_graphic_symbols, graphic_frame_at, graphics_reshaped_after, - rename_appearance_graphic_symbol, + GRAPHIC_MAX_OFFSET, GRAPHIC_SCALE_RANGE, GRAPHICS_NOT_TAKEN, appearance_graphic_symbols, exports_three_d, graphic_frame_at, + graphics_reshaped_after, rename_appearance_graphic_symbol, }; pub use stylize::{ScribbleSetting, scribble_setting_id, scribble_settings}; pub use svg_filter::SVG_FILTER; diff --git a/crates/effects/src/solids.rs b/crates/effects/src/solids.rs index e10771a8d0..06f6951791 100644 --- a/crates/effects/src/solids.rs +++ b/crates/effects/src/solids.rs @@ -102,6 +102,21 @@ pub fn has_solid(n: &Node) -> bool { solid(&n.appearance.effects).is_some() || n.appearance.items.iter().any(|i| i.visible() && solid(i.effects()).is_some()) } +/// Does `n` carry a 3D effect that [`three_d_meshes`] builds solids from: a visible Revolve, or +/// else a visible Extrude & Bevel, Inflate or Rotate effect, which on a group or layer must be on +/// the object itself (one on a group's fill or stroke draws nothing to export)? It does not build +/// the solids, so [`three_d_meshes`] can still refuse an object it passes. +pub fn exports_three_d(n: &Node) -> bool { + let container = is_container(n); + if crate::has_revolve(n) { + !container || n.appearance.effects.iter().any(|e| e.visible && crate::is_revolve(&e.id)) + } else if container { + solid(&n.appearance.effects).is_some() + } else { + has_solid(n) + } +} + /// The Position presets: (id, [rotation X, Y, Z]). Front, Back, Left, Right, Top and Bottom face /// the art that way; Off-Axis tilts each 20° down and 25° aside (showing the top and right); /// Isometric lays the art on the left, right, top or bottom plane of an isometric view (the diff --git a/crates/engine/src/cmd/assets.rs b/crates/engine/src/cmd/assets.rs index cb990e4857..fcb251347d 100644 --- a/crates/engine/src/cmd/assets.rs +++ b/crates/engine/src/cmd/assets.rs @@ -8,7 +8,7 @@ use std::collections::HashSet; use serde_json::{Map, Value, json}; use vectorcraft_doc::{ExportAsset, NodeId}; -use super::edit::{roots_of, selected_roots}; +use super::edit::{refuse_symbol_part, roots_of, selected_roots}; use super::fileio::{ DEFAULT_FORMAT_3D, FORMATS_3D, SCREEN_SETTINGS_KEYS, check_screen_settings, create_dir, export_crop, export_screens, mesh_file_names, mesh_files, safe_file_name, store_screen_settings, with_screen_export_preset, write_file, @@ -62,7 +62,7 @@ pub fn specs() -> Vec { "Export 3D Object", [], None, - "{ids?: [id…] (default: the selected objects), format?: usda|usdz|gltf|obj (default usda)} collect each object with an Extrude & Bevel, Revolve, Inflate or Rotate effect as a 3D asset of Window › Asset Export, named after the object (else Asset 1, Asset 2…): assets.export3D writes it as a mesh file. An object already collected as a 3D asset keeps it; an object without such an effect is refused → {assets: [asset id…] (in order), added: n}", + "{ids?: [id…] (default: the selected objects), format?: usda|usdz|gltf|obj (default usda)} collect each object with an Extrude & Bevel, Revolve, Inflate or Rotate effect as a 3D asset of Window › Asset Export, named after the object (else Asset 1, Asset 2…): assets.export3D writes it as a mesh file. An object already collected as a 3D asset keeps it; an object in ids without such an effect is refused. Without ids, the selected objects without one (on a group or layer, one on its fill or stroke draws nothing and does not count), and a selected object inside another selected object that has one, are left out; refused when none is left → {assets: [asset id…] (in order), added: n}", has_doc, add_3d ), @@ -203,10 +203,36 @@ fn three_d_asset_ids(d: &vectorcraft_doc::Document, p: &Value, key: &str, cmd: & Ok(ids) } +/// The selected objects `assets.add3D` collects without `ids`: those with a 3D effect it exports +/// ([`vectorcraft_render::effects::exports_three_d`]; a path of a compound path counts as the +/// compound path), layers included, in tree order, dropping +/// each one inside another of them (that one's asset holds it). Refused while an object of a +/// dynamic symbol instance's art is selected, as [`selected_roots`] is. +fn selected_3d_roots(s: &Session) -> Result> { + refuse_symbol_part(s)?; + let st = s.doc()?; + // A path of a compound path stands for the compound path, which holds the paint and effects. + let found = st + .selection + .objects + .iter() + .map(|id| super::path::compound_or_self(&st.doc, *id)) + .filter(|id| st.doc.node(*id).is_some_and(vectorcraft_render::effects::exports_three_d)); + let mut paths: Vec<(Vec, NodeId)> = st.doc.index_paths(found).into_iter().map(|(id, path)| (path, id)).collect(); + paths.sort_by(|a, b| a.0.cmp(&b.0)); + let mut kept: Vec<(Vec, NodeId)> = Vec::with_capacity(paths.len()); + for (path, id) in paths { + if !kept.last().is_some_and(|(outer, _)| path.starts_with(outer)) { + kept.push((path, id)); + } + } + Ok(kept.into_iter().map(|(_, id)| id).collect()) +} + fn add_3d(s: &mut Session, p: &Value) -> Result { const C: &str = "assets.add3D"; let format = format_3d_param(p, C, Some(DEFAULT_FORMAT_3D))?; - let ids = if p.get("ids").is_some_and(|v| !v.is_null()) { checked_ids_param(s, p, "ids", C)? } else { selected_roots(s)? }; + let ids = if p.get("ids").is_some_and(|v| !v.is_null()) { checked_ids_param(s, p, "ids", C)? } else { selected_3d_roots(s)? }; if ids.is_empty() { return Err(bad(C, "select an object with a 3D effect (or give its ids)")); } diff --git a/crates/engine/src/tests/assets_3d.rs b/crates/engine/src/tests/assets_3d.rs index cdef5b1dfc..2d1fbd7761 100644 --- a/crates/engine/src/tests/assets_3d.rs +++ b/crates/engine/src/tests/assets_3d.rs @@ -89,6 +89,45 @@ fn export_3d_object_refuses_objects_without_a_3d_effect_and_bad_formats() { assert_eq!(r["added"], 1); } +/// Without `ids`, the selected objects without a 3D effect, a group whose 3D effect is on its fill +/// (it draws nothing to export) and a 3D object inside another selected one are left out; a +/// selection with none left is refused. +#[test] +fn export_3d_object_leaves_flat_objects_in_the_selection_out() { + let mut s = session(); + let plain = run(&mut s, "shape.rectangle", json!({"x": 10.0, "y": 100.0, "width": 40.0, "height": 30.0}))["id"].as_u64().unwrap(); + let filled = run(&mut s, "shape.rectangle", json!({"x": 10.0, "y": 200.0, "width": 40.0, "height": 30.0}))["id"].as_u64().unwrap(); + run(&mut s, "select.set", json!({"ids": [filled]})); + let group = run(&mut s, "object.group", json!({}))["id"].as_u64().unwrap(); + run(&mut s, "select.set", json!({"ids": [group]})); + run(&mut s, "appearance.addFill", json!({})); + run(&mut s, "effect.apply", json!({"effect": "threeD.extrude"})); + // `effect.apply` puts a group's 3D effect on the group itself; a file can hold one on its fill. + s.edit("fill", |d, _| { + let n = d.node_mut(vectorcraft_doc::NodeId(group)).unwrap(); + let fx = n.appearance.effects.remove(0); + n.appearance.items[0].effects_mut().push(fx); + Ok(()) + }) + .unwrap(); + run(&mut s, "select.set", json!({"ids": [plain, group]})); + let before = undo_len(&s); + assert!(s.execute("assets.add3D", &json!({})).is_err(), "no object with a 3D effect to export"); + assert_eq!(undo_len(&s), before); + let inner = extruded(&mut s, 100.0, "Inner"); + let other = run(&mut s, "shape.rectangle", json!({"x": 200.0, "y": 10.0, "width": 40.0, "height": 30.0}))["id"].as_u64().unwrap(); + run(&mut s, "select.set", json!({"ids": [inner, other]})); + let outer = run(&mut s, "object.group", json!({}))["id"].as_u64().unwrap(); + run(&mut s, "select.set", json!({"ids": [outer]})); + run(&mut s, "effect.apply", json!({"effect": "threeD.extrude"})); + run(&mut s, "select.set", json!({"ids": [plain, group, inner, outer]})); + let r = run(&mut s, "assets.add3D", json!({})); + assert_eq!(r["added"], 1, "{r}"); + let nodes: Vec = run(&mut s, "assets.list", json!({}))["assets"].as_array().unwrap().iter().map(|a| a["nodes"].clone()).collect(); + assert_eq!(nodes, [json!([outer])]); + assert!(s.execute("assets.add3D", &json!({"ids": [plain, outer]})).is_err(), "ids are still refused"); +} + #[test] fn the_format_is_set_per_asset_and_only_a_change_is_an_undo_step() { let mut s = session(); diff --git a/crates/ui-egui/src/panels/tests_three_d.rs b/crates/ui-egui/src/panels/tests_three_d.rs index 8948e9acd5..5ff986af13 100644 --- a/crates/ui-egui/src/panels/tests_three_d.rs +++ b/crates/ui-egui/src/panels/tests_three_d.rs @@ -1165,8 +1165,9 @@ fn export_3d_object_adds_the_selected_3d_objects_to_asset_export() { let mut app = app(); assert!(painted_text(&mut app, show).contains("Export 3D Object")); let ctx = egui::Context::default(); - // Without a 3D object: an error, nothing recorded, no asset. + // Without a 3D object: the button is dimmed, and an error, nothing recorded, no asset. let history = undo_len(&app); + assert!(!super::can_export_3d(&app)); assert!(super::export_3d_object(&mut app, &ctx).is_err()); assert_eq!((undo_len(&app), assets(&mut app).len()), (history, 0)); let a = app.session.active().unwrap().selection.objects[0].0; @@ -1178,9 +1179,10 @@ fn export_3d_object_adds_the_selected_3d_objects_to_asset_export() { let hidden = app.run("shape.rectangle", json!({"x": 100, "y": 250, "width": 100, "height": 100})).unwrap()["id"].as_u64().unwrap(); app.run("effect.apply", json!({"effect": "threeD.extrude", "item": null})).unwrap(); app.run("effect.setParams", json!({"index": 0, "item": null, "visible": false})).unwrap(); + assert!(!super::can_export_3d(&app)); assert!(super::export_3d_object(&mut app, &ctx).is_err()); app.run("select.set", json!({"ids": [plain, a, hidden]})).unwrap(); - assert_eq!(super::selected_3d_objects(&app), [a]); + assert!(super::can_export_3d(&app)); assert!(!crate::panel_docking::visible(&app.ui, crate::panels::asset_export::ID)); let history = undo_len(&app); super::export_3d_object(&mut app, &ctx).unwrap(); @@ -1217,7 +1219,44 @@ fn export_3d_object_leaves_out_a_3d_object_inside_another() { app.run("select.set", json!({"ids": [inner, group]})).unwrap(); let selected: Vec = app.session.active().unwrap().selection.objects.iter().map(|id| id.0).collect(); assert!(selected.contains(&inner) && selected.contains(&group), "{selected:?}"); - assert_eq!(super::selected_3d_objects(&app), [group]); + super::export_3d_object(&mut app, &egui::Context::default()).unwrap(); + let list = assets(&mut app); + assert_eq!(list.iter().map(|a| a["nodes"].clone()).collect::>(), [json!([group])]); +} + +/// A 3D effect on a group's fill draws nothing to export: the button is dimmed for that group +/// alone, and beside a 3D object the group is left out instead of refusing them both. +#[test] +fn export_3d_object_leaves_out_a_group_with_a_3d_fill() { + let mut app = app(); + let a = app.session.active().unwrap().selection.objects[0].0; + let other = app.run("shape.rectangle", json!({"x": 250, "y": 100, "width": 100, "height": 100})).unwrap()["id"].as_u64().unwrap(); + app.run("select.set", json!({"ids": [other]})).unwrap(); + let group = app.run("object.group", json!({})).unwrap()["id"].as_u64().unwrap(); + app.run("select.set", json!({"ids": [group]})).unwrap(); + app.run("appearance.addFill", json!({})).unwrap(); + app.run("effect.apply", json!({"effect": "threeD.extrude"})).unwrap(); + // `effect.apply` puts a group's 3D effect on the group itself; a file can hold one on its fill. + app.session + .edit("fill", |d, _| { + let n = d.node_mut(vectorcraft_doc::NodeId(group)).unwrap(); + let fx = n.appearance.effects.remove(0); + n.appearance.items[0].effects_mut().push(fx); + Ok(()) + }) + .unwrap(); + assert!( + vectorcraft_effects::has_solid(app.session.active().unwrap().doc.node(vectorcraft_doc::NodeId(group)).unwrap()), + "the fill holds the effect" + ); + assert!(!super::can_export_3d(&app)); + app.run("select.set", json!({"ids": [a]})).unwrap(); + set_type(&mut app, "extrude").unwrap(); + app.run("select.set", json!({"ids": [a, group]})).unwrap(); + assert!(super::can_export_3d(&app)); + super::export_3d_object(&mut app, &egui::Context::default()).unwrap(); + let list = assets(&mut app); + assert_eq!(list.iter().map(|a| a["nodes"].clone()).collect::>(), [json!([a])]); } #[test] diff --git a/crates/ui-egui/src/panels/three_d.rs b/crates/ui-egui/src/panels/three_d.rs index 2611824cda..be7731daf5 100644 --- a/crates/ui-egui/src/panels/three_d.rs +++ b/crates/ui-egui/src/panels/three_d.rs @@ -10,7 +10,7 @@ use egui::Ui; use serde_json::{Map, Value, json}; -use vectorcraft_doc::{Effect, Node, NodeId}; +use vectorcraft_doc::{Effect, Node, NodeId, NodeKind}; use vectorcraft_effects::{CLASSIC_EXTRUDE, CLASSIC_REVOLVE, EXTRUDE, INFLATE, THREE_D_TYPES, is_three_d}; use super::{live_run, pstate, set_pstate}; @@ -51,13 +51,6 @@ fn find(n: &Node) -> Option { in_list(&n.appearance.effects, None).or_else(|| n.appearance.items.iter().enumerate().find_map(|(k, item)| in_list(item.effects(), Some(k)))) } -/// Does object `n` have a visible 3D effect (where [`find`] looks, in a visible fill or stroke), -/// the kind Export 3D Object exports, without copying its settings? -fn has_three_d(n: &Node) -> bool { - let in_list = |fx: &[Effect]| fx.iter().any(|e| e.visible && is_three_d(&e.id)); - in_list(&n.appearance.effects) || n.appearance.items.iter().any(|item| item.visible() && in_list(item.effects())) -} - /// The panel's object type of effect `id` (`plane`, `extrude`, `revolve` or `inflate`). pub(crate) fn type_of(id: &str) -> Option<&'static str> { THREE_D_TYPES.iter().find(|(_, effect)| *effect == id).map(|(kind, _)| *kind) @@ -284,10 +277,10 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) { export_button(app, ui, &ctx); } -/// The Export 3D Object button, available while a selected object has a 3D effect. An error goes -/// to the status bar. +/// The Export 3D Object button, available while a selected object has a 3D effect that +/// `assets.add3D` exports ([`can_export_3d`]). An error goes to the status bar. fn export_button(app: &mut VectorcraftApp, ui: &mut Ui, ctx: &egui::Context) { - let any = app.session.active().is_some_and(|st| st.selection.objects.iter().any(|id| st.doc.node(*id).is_some_and(has_three_d))); + let any = can_export_3d(app); ui.add_space(10.0); if ui.add_enabled(any, egui::Button::new(tl!("Export 3D Object"))).clicked() { finish(app, ctx); @@ -297,30 +290,22 @@ fn export_button(app: &mut VectorcraftApp, ui: &mut Ui, ctx: &egui::Context) { } } -/// The selected objects that have a 3D effect, in tree order, leaving out each one inside another -/// listed one (that one's asset holds it). -pub(crate) fn selected_3d_objects(app: &VectorcraftApp) -> Vec { - let Some(st) = app.session.active() else { return vec![] }; - let found = st.selection.objects.iter().copied().filter(|id| st.doc.node(*id).is_some_and(has_three_d)); - let mut paths: Vec<(Vec, NodeId)> = st.doc.index_paths(found).into_iter().map(|(id, path)| (path, id)).collect(); - paths.sort_by(|a, b| a.0.cmp(&b.0)); - let mut kept: Vec<(Vec, NodeId)> = Vec::with_capacity(paths.len()); - for (path, id) in paths { - if !kept.last().is_some_and(|(outer, _)| path.starts_with(outer)) { - kept.push((path, id)); - } - } - kept.into_iter().map(|(_, id)| id.0).collect() +/// Does a selected object have a 3D effect that `assets.add3D` exports +/// ([`vectorcraft_effects::exports_three_d`])? A path of a compound path counts as the compound +/// path, which holds the effects. +pub(crate) fn can_export_3d(app: &VectorcraftApp) -> bool { + let Some(st) = app.session.active() else { return false }; + let compound = |id: NodeId| st.doc.parent_of(id).filter(|p| st.doc.node(*p).is_some_and(|n| matches!(n.kind, NodeKind::Compound { .. }))); + st.selection.objects.iter().any(|id| st.doc.node(compound(*id).unwrap_or(*id)).is_some_and(vectorcraft_effects::exports_three_d)) } /// Export 3D Object: adds the selected objects that have a 3D effect to Asset Export as 3D assets -/// (`assets.add3D`, one undo step, none when each is an asset already), selects their assets there -/// and shows the Asset Export panel. Objects without a 3D effect in the selection are left out. +/// (`assets.add3D` on the selection, one undo step, none when each is an asset already), selects +/// their assets there and shows the Asset Export panel. The command leaves out the selected +/// objects without a 3D effect and those inside another selected 3D object. pub(crate) fn export_3d_object(app: &mut VectorcraftApp, ctx: &egui::Context) -> Result<(), String> { use super::asset_export; - let ids = selected_3d_objects(app); - // Without one, `assets.add3D` on the selection says why nothing was added. - asset_export::add_3d(app, ctx, (!ids.is_empty()).then_some(ids.as_slice()))?; + asset_export::add_3d(app, ctx, None)?; if !crate::panel_docking::visible(&app.ui, asset_export::ID) { app.run("window.panel", json!({"panel": asset_export::ID}))?; } diff --git a/docs/mcp.md b/docs/mcp.md index 0beec463bb..948bee5d94 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -8728,10 +8728,13 @@ becomes an asset (or one asset with `multiple: false`). ### 3D assets Asset Export's menu item Export 3D Object and the 3D and Materials panel's Export 3D Object button -(`assets.add3D {ids?, format?}`; the button passes the ids of the selected objects that have a 3D effect and then shows -the Asset Export panel, [three-d.md](three-d.md#3d-and-materials-panel)) collect each selected object (or `ids`) +(`assets.add3D {ids?, format?}`; the button then shows the Asset Export panel, +[three-d.md](three-d.md#3d-and-materials-panel)) collect each selected object (or `ids`) that carries Extrude & Bevel, Revolve, Inflate or Rotate as a 3D asset, named after the object; an object already -collected as a 3D asset keeps it, and an object without such an effect is refused. A 3D asset's Format is `usda` (the +collected as a 3D asset keeps it. An object in `ids` without such an effect is refused. Without `ids`, the selected +objects without one are left out, and so are a group or layer whose 3D effect is on a fill or stroke (it draws nothing +to export) and a selected object inside another selected 3D object; the command is refused when no selected object is +left. A 3D asset's Format is `usda` (the default), `usdz`, `gltf` or `obj`; the panel shows a Format row for the 3D assets it would export, and `assets.set3DFormat {assets, format}` sets it (an undo step only when the format changes). `assets.list` gives each asset's `format3D` (null for a picture asset). diff --git a/docs/three-d.md b/docs/three-d.md index f253f47f20..f7cab519f1 100644 --- a/docs/three-d.md +++ b/docs/three-d.md @@ -83,11 +83,12 @@ pages describe it. [Export 3D vector artwork](https://helpx.adobe.com/illustrator/desktop/special-effects-styles/create-3d-graphics/export-3d-vector-artwork.html) page describes; VectorCraft places it at the bottom of the panel. It adds each selected object that has a visible 3D effect to Asset Export as a 3D asset, the same as - Asset Export's own Export 3D Object (`assets.add3D` with those objects' ids), selects their assets in the Asset + Asset Export's own Export 3D Object (`assets.add3D` on the selection), selects their assets in the Asset Export panel and shows that panel, where the Format row and Export write the mesh files - ([3D assets in mcp.md](mcp.md#3d-assets)). Selected objects without a visible 3D effect are left out, and so is - a 3D object inside another selected 3D object, whose asset holds it. The button is unavailable while no selected - object has a visible 3D effect. Adding the assets is one undo step; when every one of the objects is an asset already, nothing is + ([3D assets in mcp.md](mcp.md#3d-assets)). Selected objects without a visible 3D effect are left out, and so are + a group whose 3D effect is on a fill or stroke, which draws nothing to export, and a 3D object inside another + selected 3D object, whose asset holds it. The button is unavailable while no selected object has a 3D effect that + can be exported. Adding the assets is one undo step; when every one of the objects is an asset already, nothing is recorded and their assets are selected again. - The Lighting tab also holds the Shadows switch and its settings ([Shadows](#shadows)). The Materials tab holds the Graphics ([Graphics](#graphics)); base materials and ray-traced rendering are not built (see below). The