[Bf-blender-cvs] [3da74c1c18d] master: Geometry Nodes: add method to get attribute by name and type

Jacques Lucke noreply at git.blender.org
Wed Apr 21 17:11:19 CEST 2021


Commit: 3da74c1c18dca19af9e2745ff9ea5ad587f3b47e
Author: Jacques Lucke
Date:   Wed Apr 21 17:07:00 2021 +0200
Branches: master
https://developer.blender.org/rB3da74c1c18dca19af9e2745ff9ea5ad587f3b47e

Geometry Nodes: add method to get attribute by name and type

This is needed by the upcoming Attribute Transfer node. It changes
its behavior based on what domain the attribute is on.

===================================================================

M	source/blender/blenkernel/BKE_geometry_set.hh
M	source/blender/blenkernel/intern/attribute_access.cc

===================================================================

diff --git a/source/blender/blenkernel/BKE_geometry_set.hh b/source/blender/blenkernel/BKE_geometry_set.hh
index bfb4afe085c..38f692fee0e 100644
--- a/source/blender/blenkernel/BKE_geometry_set.hh
+++ b/source/blender/blenkernel/BKE_geometry_set.hh
@@ -158,6 +158,12 @@ class GeometryComponent {
   std::unique_ptr<blender::fn::GVArray> attribute_try_get_for_read(
       const blender::StringRef attribute_name, const AttributeDomain domain) const;
 
+  /* Get a virtual array to read data of an attribute with the given data type. The domain is
+   * left unchanged. Returns null when the attribute does not exist or cannot be converted to the
+   * requested data type. */
+  blender::bke::ReadAttributeLookup attribute_try_get_for_read(
+      const blender::StringRef attribute_name, const CustomDataType data_type) const;
+
   /* Get a virtual array to read the data of an attribute. If that is not possible, the returned
    * virtual array will contain a default value. This never returns null. */
   std::unique_ptr<blender::fn::GVArray> attribute_get_for_read(
diff --git a/source/blender/blenkernel/intern/attribute_access.cc b/source/blender/blenkernel/intern/attribute_access.cc
index 70572e446b7..3b2ee126d91 100644
--- a/source/blender/blenkernel/intern/attribute_access.cc
+++ b/source/blender/blenkernel/intern/attribute_access.cc
@@ -786,6 +786,23 @@ std::unique_ptr<blender::bke::GVArray> GeometryComponent::attribute_try_get_for_
   return std::move(attribute.varray);
 }
 
+blender::bke::ReadAttributeLookup GeometryComponent::attribute_try_get_for_read(
+    const blender::StringRef attribute_name, const CustomDataType data_type) const
+{
+  blender::bke::ReadAttributeLookup attribute = this->attribute_try_get_for_read(attribute_name);
+  if (!attribute) {
+    return {};
+  }
+  const blender::fn::CPPType *type = blender::bke::custom_data_type_to_cpp_type(data_type);
+  BLI_assert(type != nullptr);
+  if (attribute.varray->type() == *type) {
+    return attribute;
+  }
+  const blender::nodes::DataTypeConversions &conversions =
+      blender::nodes::get_implicit_type_conversions();
+  return {conversions.try_convert(std::move(attribute.varray), *type), attribute.domain};
+}
+
 std::unique_ptr<blender::bke::GVArray> GeometryComponent::attribute_get_for_read(
     const StringRef attribute_name,
     const AttributeDomain domain,



More information about the Bf-blender-cvs mailing list