[Bf-blender-cvs] [e37f3388b15] master: Attributes: Add function to retrieve span without adding attribute

Hans Goudey noreply at git.blender.org
Mon Sep 12 18:39:50 CEST 2022


Commit: e37f3388b1563591153fc82259cf549f7942dcf0
Author: Hans Goudey
Date:   Mon Sep 12 10:36:56 2022 -0500
Branches: master
https://developer.blender.org/rBe37f3388b1563591153fc82259cf549f7942dcf0

Attributes: Add function to retrieve span without adding attribute

Previously, the only versions of attribute access that gave a span would
also add the attribute when it doesn't exist, which isn't always wanted.

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

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

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

diff --git a/source/blender/blenkernel/BKE_attribute.hh b/source/blender/blenkernel/BKE_attribute.hh
index 4aa6c133e9e..21d91af55d5 100644
--- a/source/blender/blenkernel/BKE_attribute.hh
+++ b/source/blender/blenkernel/BKE_attribute.hh
@@ -552,6 +552,11 @@ class MutableAttributeAccessor : public AttributeAccessor {
    */
   GAttributeWriter lookup_for_write(const AttributeIDRef &attribute_id);
 
+  /**
+   * Same as above, but returns a type that makes it easier to work with the attribute as a span.
+   */
+  GSpanAttributeWriter lookup_for_write_span(const AttributeIDRef &attribute_id);
+
   /**
    * Get a writable attribute or non if it does not exist.
    * Make sure to call #finish after changes are done.
@@ -568,6 +573,19 @@ class MutableAttributeAccessor : public AttributeAccessor {
     return attribute.typed<T>();
   }
 
+  /**
+   * Same as above, but returns a type that makes it easier to work with the attribute as a span.
+   */
+  template<typename T>
+  SpanAttributeWriter<T> lookup_for_write_span(const AttributeIDRef &attribute_id)
+  {
+    AttributeWriter<T> attribute = this->lookup_for_write<T>(attribute_id);
+    if (attribute) {
+      return SpanAttributeWriter<T>{std::move(attribute), true};
+    }
+    return {};
+  }
+
   /**
    * Create a new attribute.
    * \return True, when a new attribute has been created. False, when it's not possible to create
diff --git a/source/blender/blenkernel/intern/attribute_access.cc b/source/blender/blenkernel/intern/attribute_access.cc
index 6ca3a286a5e..69f3e5bb389 100644
--- a/source/blender/blenkernel/intern/attribute_access.cc
+++ b/source/blender/blenkernel/intern/attribute_access.cc
@@ -875,6 +875,16 @@ GAttributeWriter MutableAttributeAccessor::lookup_for_write(const AttributeIDRef
   return attribute;
 }
 
+GSpanAttributeWriter MutableAttributeAccessor::lookup_for_write_span(
+    const AttributeIDRef &attribute_id)
+{
+  GAttributeWriter attribute = this->lookup_for_write(attribute_id);
+  if (attribute) {
+    return GSpanAttributeWriter{std::move(attribute), true};
+  }
+  return {};
+}
+
 GAttributeWriter MutableAttributeAccessor::lookup_or_add_for_write(
     const AttributeIDRef &attribute_id,
     const eAttrDomain domain,



More information about the Bf-blender-cvs mailing list