[Bf-blender-cvs] [00928f7826e] geometry-nodes: Geometry Nodes: simplify attributes api and support deletion

Jacques Lucke noreply at git.blender.org
Thu Nov 19 13:18:39 CET 2020


Commit: 00928f7826e9f38416f6f1fb7112a2badc752656
Author: Jacques Lucke
Date:   Thu Nov 19 13:17:11 2020 +0100
Branches: geometry-nodes
https://developer.blender.org/rB00928f7826e9f38416f6f1fb7112a2badc752656

Geometry Nodes: simplify attributes api and support deletion

The main difference is that the functions to access attributes have
been moved to MeshComponent and PointCloudComponent.

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

M	source/blender/blenkernel/BKE_attribute_access.hh
M	source/blender/blenkernel/BKE_geometry_set.hh
M	source/blender/blenkernel/intern/attribute_access.cc
M	source/blender/blenkernel/intern/geometry_set.cc
M	source/blender/depsgraph/CMakeLists.txt
M	source/blender/nodes/geometry/nodes/node_geo_point_distribute.cc
M	source/blender/nodes/geometry/nodes/node_geo_point_instance.cc
M	source/blender/nodes/geometry/nodes/node_geo_random_attribute.cc

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

diff --git a/source/blender/blenkernel/BKE_attribute_access.hh b/source/blender/blenkernel/BKE_attribute_access.hh
index 41f5a1a3605..39fe3e6ba06 100644
--- a/source/blender/blenkernel/BKE_attribute_access.hh
+++ b/source/blender/blenkernel/BKE_attribute_access.hh
@@ -19,7 +19,8 @@
 #include "FN_cpp_type.hh"
 
 #include "BKE_attribute.h"
-#include "BKE_geometry_set.hh"
+
+#include "BLI_float3.hh"
 
 struct Mesh;
 
@@ -196,65 +197,6 @@ using Float3ReadAttribute = TypedReadAttribute<float3>;
 using FloatWriteAttribute = TypedWriteAttribute<float>;
 using Float3WriteAttribute = TypedWriteAttribute<float3>;
 
-/* --------------------------------------------------------------------
- * Main attribute getters.
- */
-
-ReadAttributePtr mesh_attribute_get_for_read(const MeshComponent &mesh_component,
-                                             const StringRef attribute_name);
-WriteAttributePtr mesh_attribute_get_for_write(MeshComponent &mesh_component,
-                                               const StringRef attribute_name);
-
-ReadAttributePtr pointcloud_attribute_get_for_read(const PointCloudComponent &pointcloud_component,
-                                                   const StringRef attribute_name);
-WriteAttributePtr pointcloud_attribute_get_for_write(
-    const PointCloudComponent &pointcloud_component, const StringRef attribute_name);
-
-/* --------------------------------------------------------------------
- * Utilities for getting more specific attributes.
- */
-
-ReadAttributePtr mesh_attribute_adapt_domain(const MeshComponent &mesh_component,
-                                             ReadAttributePtr attribute,
-                                             const AttributeDomain to_domain);
-
-ReadAttributePtr mesh_attribute_get_for_read(const MeshComponent &mesh_component,
-                                             const StringRef attribute_name,
-                                             const CPPType &cpp_type,
-                                             const AttributeDomain domain,
-                                             const void *default_value = nullptr);
-
-template<typename T>
-TypedReadAttribute<T> mesh_attribute_get_for_read(const MeshComponent &mesh_component,
-                                                  const StringRef attribute_name,
-                                                  const AttributeDomain domain,
-                                                  const T &default_value)
-{
-  ReadAttributePtr attribute = mesh_attribute_get_for_read(
-      mesh_component,
-      attribute_name,
-      CPPType::get<T>(),
-      domain,
-      static_cast<const void *>(&default_value));
-  BLI_assert(attribute);
-  return attribute;
-}
-
-ReadAttributePtr pointcloud_attribute_get_for_read(const PointCloudComponent &pointcloud_component,
-                                                   const StringRef attribute_name,
-                                                   const CPPType &cpp_type,
-                                                   const void *default_value = nullptr);
-
-template<typename T>
-TypedReadAttribute<T> pointcloud_attribute_get_for_read(
-    const PointCloudComponent &pointcloud_component,
-    const StringRef attribute_name,
-    const T &default_value)
-{
-  ReadAttributePtr attribute = pointcloud_attribute_get_for_read(
-      pointcloud_component, attribute_name, CPPType::get<T>(), &default_value);
-  BLI_assert(attribute);
-  return attribute;
-}
+const CPPType *custom_data_type_to_cpp_type(const int type);
 
 }  // namespace blender::bke
diff --git a/source/blender/blenkernel/BKE_geometry_set.hh b/source/blender/blenkernel/BKE_geometry_set.hh
index 4fe74aa2e1d..6e375f42e78 100644
--- a/source/blender/blenkernel/BKE_geometry_set.hh
+++ b/source/blender/blenkernel/BKE_geometry_set.hh
@@ -28,6 +28,7 @@
 #include "BLI_map.hh"
 #include "BLI_user_counter.hh"
 
+#include "BKE_attribute_access.hh"
 #include "BKE_geometry_set.h"
 
 struct Mesh;
@@ -52,6 +53,12 @@ enum class GeometryOwnershipType {
   ReadOnly = 2,
 };
 
+enum class AttributeDeleteStatus {
+  Deleted,
+  NotFound,
+  CannotBeDeleted,
+};
+
 /* Make it possible to use the component type as key in hash tables. */
 namespace blender {
 template<> struct DefaultHash<GeometryComponentType> {
@@ -159,11 +166,32 @@ class MeshComponent : public GeometryComponent {
   Mesh *release();
 
   void copy_vertex_group_names_from_object(const struct Object &object);
-  int vertex_group_index(blender::StringRef vertex_group_name) const;
 
   const Mesh *get_for_read() const;
   Mesh *get_for_write();
 
+  blender::bke::ReadAttributePtr attribute_get_for_read(
+      const blender::StringRef attribute_name) const;
+
+  blender::bke::WriteAttributePtr attribute_get_for_write(const blender::StringRef attribute_name);
+
+  blender::bke::ReadAttributePtr attribute_get_for_read(const blender::StringRef attribute_name,
+                                                        const blender::fn::CPPType &cpp_type,
+                                                        const AttributeDomain domain,
+                                                        const void *default_value = nullptr) const;
+
+  template<typename T>
+  blender::bke::TypedReadAttribute<T> attribute_get_for_read(
+      const blender::StringRef attribute_name,
+      const AttributeDomain domain,
+      const T &default_value) const
+  {
+    return this->attribute_get_for_read(
+        attribute_name, blender::fn::CPPType::get<T>(), domain, &default_value);
+  }
+
+  AttributeDeleteStatus attribute_delete(const blender::StringRef attribute_name);
+
   static constexpr inline GeometryComponentType type = GeometryComponentType::Mesh;
 };
 
@@ -186,6 +214,25 @@ class PointCloudComponent : public GeometryComponent {
   const PointCloud *get_for_read() const;
   PointCloud *get_for_write();
 
+  blender::bke::ReadAttributePtr attribute_get_for_read(
+      const blender::StringRef attribute_name) const;
+
+  blender::bke::WriteAttributePtr attribute_get_for_write(const blender::StringRef attribute_name);
+
+  blender::bke::ReadAttributePtr attribute_get_for_read(const blender::StringRef attribute_name,
+                                                        const blender::fn::CPPType &cpp_type,
+                                                        const void *default_value) const;
+
+  template<typename T>
+  blender::bke::TypedReadAttribute<T> attribute_get_for_read(
+      const blender::StringRef attribute_name, const T &default_value) const
+  {
+    return this->attribute_get_for_read(
+        attribute_name, blender::fn::CPPType::get<T>(), &default_value);
+  }
+
+  AttributeDeleteStatus attribute_delete(const blender::StringRef attribute_name);
+
   static constexpr inline GeometryComponentType type = GeometryComponentType::PointCloud;
 };
 
diff --git a/source/blender/blenkernel/intern/attribute_access.cc b/source/blender/blenkernel/intern/attribute_access.cc
index a0d9c5b7df1..10834e6e885 100644
--- a/source/blender/blenkernel/intern/attribute_access.cc
+++ b/source/blender/blenkernel/intern/attribute_access.cc
@@ -17,7 +17,9 @@
 #include <utility>
 
 #include "BKE_attribute_access.hh"
+#include "BKE_customdata.h"
 #include "BKE_deform.h"
+#include "BKE_geometry_set.hh"
 
 #include "DNA_mesh_types.h"
 #include "DNA_meshdata_types.h"
@@ -279,22 +281,77 @@ static WriteAttributePtr write_attribute_from_custom_data(CustomData custom_data
   return {};
 }
 
-/** \} */
+const CPPType *custom_data_type_to_cpp_type(const int type)
+{
+  switch (type) {
+    case CD_PROP_FLOAT:
+      return &CPPType::get<float>();
+    case CD_PROP_FLOAT2:
+      return &CPPType::get<float2>();
+    case CD_PROP_FLOAT3:
+      return &CPPType::get<float3>();
+    case CD_PROP_INT32:
+      return &CPPType::get<int>();
+    case CD_PROP_COLOR:
+      return &CPPType::get<Color4f>();
+  }
+  return nullptr;
+}
 
-/* -------------------------------------------------------------------- */
-/** \name Get Read/Write attributes for meshes.
- * \{ */
+/** \} */
 
-ReadAttributePtr mesh_attribute_get_for_read(const MeshComponent &mesh_component,
-                                             const StringRef attribute_name)
+static int get_domain_length(const Mesh *mesh, const AttributeDomain domain)
 {
-  const Mesh *mesh = mesh_component.get_for_read();
   if (mesh == nullptr) {
+    return 0;
+  }
+  switch (domain) {
+    case ATTR_DOMAIN_CORNER:
+      return mesh->totloop;
+    case ATTR_DOMAIN_VERTEX:
+      return mesh->totvert;
+    case ATTR_DOMAIN_EDGE:
+      return mesh->totedge;
+    case ATTR_DOMAIN_POLYGON:
+      return mesh->totpoly;
+    default:
+      break;
+  }
+  return 0;
+}
+
+/* Returns true when the layer was found and is deleted. */
+static bool delete_named_custom_data_layer(CustomData &custom_data,
+                                           const StringRef attribute_name,
+                                           const int size)
+{
+  for (const int index : IndexRange(custom_data.totlayer)) {
+    const CustomDataLayer &layer = custom_data.layers[index];
+    if (layer.name == attribute_name) {
+      CustomData_free_layer(
+          &custom_data, layer.type, size, index - custom_data.typemap[layer.type]);
+      return true;
+    }
+  }
+  return false;
+}
+
+/** \} */
+
+}  // namespace blender::bke
+
+blender::bke::ReadAttributePtr MeshComponent::attribute_get_for_read(
+    const blender::StringRef attribute_name) const
+{
+  using namespace blender;
+  using namespace blender::bke;
+
+  if (mesh_ == nullptr) {
     return {};
   }
 
   ReadAttributePtr corner_attribute = read_attribute_from_custom_data(
-      mesh->ldata, mesh->totloop, attribute_name, ATTR_DOMAIN_CORNER);
+      mesh_->ldata, mesh_->totloop, attribute_name, ATTR_DOMAIN_CORNER);
   if (corner_attribute) {
     return corner_attribute;
   }
@@ -303,29 +360,29 @@ ReadAttributePtr mesh_attribute_get_for_read(const MeshComponent &mesh_component
     auto get_vertex_position = [](const MVert &vert) { return float3(vert.co); };
     return std::make_unique<
         DerivedArrayReadAttribute<MVert, float3, decltype(get_vertex_position)>>(
-        ATTR_DOMAIN_VERTEX, Span(me

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list