Skip to content

Commit e5f40a3

Browse files
TrueDoctorKeavon
andauthored
Add 'Instance Map' and 'Instance Vector' nodes (#3229)
* Add map instance and extract vector nodes * Cleanup * Inject index in map node * Code review --------- Co-authored-by: Keavon Chambers <[email protected]>
1 parent f102801 commit e5f40a3

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

node-graph/gcore/src/vector/vector_nodes.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::vector::misc::{MergeByDistanceAlgorithm, PointSpacingType, is_linear}
1717
use crate::vector::misc::{handles_to_segment, segment_to_handles};
1818
use crate::vector::style::{PaintOrder, StrokeAlign, StrokeCap, StrokeJoin};
1919
use crate::vector::{FillId, RegionId};
20-
use crate::{CloneVarArgs, Color, Context, Ctx, ExtractAll, Graphic, OwnedContextImpl};
20+
use crate::{CloneVarArgs, Color, Context, Ctx, ExtractAll, ExtractVarArgs, Graphic, OwnedContextImpl};
2121
use core::f64::consts::PI;
2222
use core::hash::{Hash, Hasher};
2323
use glam::{DAffine2, DVec2};
@@ -970,6 +970,31 @@ async fn separate_subpaths(_: impl Ctx, content: Table<Vector>) -> Table<Vector>
970970
.collect()
971971
}
972972

973+
#[node_macro::node(category("Vector: Modifier"), path(graphene_core::vector))]
974+
fn instance_vector(ctx: impl Ctx + ExtractVarArgs) -> Table<Vector> {
975+
let Ok(var_arg) = ctx.vararg(0) else { return Default::default() };
976+
let var_arg = var_arg as &dyn std::any::Any;
977+
978+
var_arg.downcast_ref().cloned().unwrap_or_default()
979+
}
980+
981+
#[node_macro::node(category("Vector: Modifier"), path(graphene_core::vector))]
982+
async fn instance_map(ctx: impl Ctx + CloneVarArgs + ExtractAll, content: Table<Vector>, mapped: impl Node<Context<'static>, Output = Table<Vector>>) -> Table<Vector> {
983+
let mut rows = Vec::new();
984+
985+
for (i, row) in content.into_iter().enumerate() {
986+
let owned_ctx = OwnedContextImpl::from(ctx.clone());
987+
let owned_ctx = owned_ctx.with_vararg(Box::new(Table::new_from_row(row))).with_index(i);
988+
let table = mapped.eval(owned_ctx.into_context()).await;
989+
990+
for inner_row in table {
991+
rows.push(inner_row);
992+
}
993+
}
994+
995+
rows.into_iter().collect()
996+
}
997+
973998
#[node_macro::node(category("Vector"), path(graphene_core::vector))]
974999
async fn flatten_path<I: 'n + Send>(_: impl Ctx, #[implementations(Table<Graphic>, Table<Vector>)] content: Table<I>) -> Table<Vector>
9751000
where

0 commit comments

Comments
 (0)