From 6853d787dbe4dc203252bb55ee2be9f927d8ac4e Mon Sep 17 00:00:00 2001 From: Dalai Felinto Date: Fri, 13 Oct 2023 10:08:51 +0200 Subject: [PATCH] Geometry Nodes: Util function to propagate attributes This propagates from layer to instances, which is required by some of the Grease Pencil nodes. Part of #113602. Ref !113634. --- source/blender/blenkernel/BKE_geometry_set.hh | 5 ++++ .../blender/blenkernel/intern/geometry_set.cc | 27 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/source/blender/blenkernel/BKE_geometry_set.hh b/source/blender/blenkernel/BKE_geometry_set.hh index a6f0d03b1b4..5022728e834 100644 --- a/source/blender/blenkernel/BKE_geometry_set.hh +++ b/source/blender/blenkernel/BKE_geometry_set.hh @@ -229,6 +229,11 @@ struct GeometrySet { bool include_instances, AttributeForeachCallback callback) const; + static void propagate_attributes_from_layer_to_instances( + const AttributeAccessor src_attributes, + MutableAttributeAccessor dst_attributes, + const AnonymousAttributePropagationInfo &propagation_info); + void gather_attributes_for_propagation(Span component_types, GeometryComponent::Type dst_component_type, bool include_instances, diff --git a/source/blender/blenkernel/intern/geometry_set.cc b/source/blender/blenkernel/intern/geometry_set.cc index 66fef5bf69e..915df3f1078 100644 --- a/source/blender/blenkernel/intern/geometry_set.cc +++ b/source/blender/blenkernel/intern/geometry_set.cc @@ -605,6 +605,33 @@ void GeometrySet::attribute_foreach(const Span componen } } +void GeometrySet::propagate_attributes_from_layer_to_instances( + const AttributeAccessor src_attributes, + MutableAttributeAccessor dst_attributes, + const AnonymousAttributePropagationInfo &propagation_info) +{ + src_attributes.for_all([&](const AttributeIDRef &id, const AttributeMetaData meta_data) { + if (id.is_anonymous() && !propagation_info.propagate(id.anonymous_id())) { + return true; + } + const GAttributeReader src = src_attributes.lookup(id, ATTR_DOMAIN_LAYER); + if (src.sharing_info && src.varray.is_span()) { + const AttributeInitShared init(src.varray.get_internal_span().data(), *src.sharing_info); + if (dst_attributes.add(id, ATTR_DOMAIN_INSTANCE, meta_data.data_type, init)) { + return true; + } + } + GSpanAttributeWriter dst = dst_attributes.lookup_or_add_for_write_only_span( + id, ATTR_DOMAIN_INSTANCE, meta_data.data_type); + if (!dst) { + return true; + } + array_utils::copy(src.varray, dst.span); + dst.finish(); + return true; + }); +} + void GeometrySet::gather_attributes_for_propagation( const Span component_types, const GeometryComponent::Type dst_component_type,