[Bf-blender-cvs] [200cbb6adfc] geometry-nodes-point-separate-node: Geometry Nodes: Add utilities for getting an attribute's data type

Hans Goudey noreply at git.blender.org
Wed Dec 9 23:42:31 CET 2020


Commit: 200cbb6adfca8f5fe55ff73650882cc0026da27b
Author: Hans Goudey
Date:   Wed Dec 9 16:29:49 2020 -0600
Branches: geometry-nodes-point-separate-node
https://developer.blender.org/rB200cbb6adfca8f5fe55ff73650882cc0026da27b

Geometry Nodes: Add utilities for getting an attribute's data type

With the previous helper function in this file you had to know the data
type you wanted for the input attribute before requesting it. This adds
another utility function for getting the data type without building the
attribute. The attribute's can be requested later after doing some
logic to figure out the right data type to read.

No functional changes in this commit.

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

M	source/blender/nodes/NOD_geometry_exec.hh
M	source/blender/nodes/intern/node_geometry_exec.cc

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

diff --git a/source/blender/nodes/NOD_geometry_exec.hh b/source/blender/nodes/NOD_geometry_exec.hh
index a9c208bbc93..805f843c162 100644
--- a/source/blender/nodes/NOD_geometry_exec.hh
+++ b/source/blender/nodes/NOD_geometry_exec.hh
@@ -170,10 +170,21 @@ class GeoNodeExecParams {
     return this->get_input_attribute(name, component, domain, type, &default_value);
   }
 
+  /**
+   * Get the type of an input property or the associated constant socket types.
+   * Fall back to the default value if no attribute exists with the name.
+   */
+  CustomDataType get_input_attribute_data_type(const StringRef name,
+                                               const GeometryComponent &component,
+                                               const CustomDataType default_type) const;
+
  private:
   /* Utilities for detecting common errors at when using this class. */
   void check_extract_input(StringRef identifier, const CPPType *requested_type = nullptr) const;
   void check_set_output(StringRef identifier, const CPPType &value_type) const;
+
+  /* Finds a socket with the name (not identifier) that is currently active. */
+  const bNodeSocket *find_available_socket(const StringRef name) const;
 };
 
 }  // namespace blender::nodes
diff --git a/source/blender/nodes/intern/node_geometry_exec.cc b/source/blender/nodes/intern/node_geometry_exec.cc
index a6d9115f01f..eef2c6c9125 100644
--- a/source/blender/nodes/intern/node_geometry_exec.cc
+++ b/source/blender/nodes/intern/node_geometry_exec.cc
@@ -19,23 +19,31 @@
 
 namespace blender::nodes {
 
-ReadAttributePtr GeoNodeExecParams::get_input_attribute(const StringRef name,
-                                                        const GeometryComponent &component,
-                                                        const AttributeDomain domain,
-                                                        const CustomDataType type,
-                                                        const void *default_value) const
+const bNodeSocket *GeoNodeExecParams::find_available_socket(const StringRef name) const
 {
-  const bNodeSocket *found_socket = nullptr;
   LISTBASE_FOREACH (const bNodeSocket *, socket, &node_.inputs) {
     if ((socket->flag & SOCK_UNAVAIL) != 0) {
       continue;
     }
     if (name == socket->name) {
-      found_socket = socket;
-      break;
+      return socket;
     }
   }
-  BLI_assert(found_socket != nullptr);
+
+  return nullptr;
+}
+
+ReadAttributePtr GeoNodeExecParams::get_input_attribute(const StringRef name,
+                                                        const GeometryComponent &component,
+                                                        const AttributeDomain domain,
+                                                        const CustomDataType type,
+                                                        const void *default_value) const
+{
+  const bNodeSocket *found_socket = this->find_available_socket(name);
+  BLI_assert(found_socket != nullptr); /* There should always be available socket for the name. */
+  if (found_socket == nullptr) {
+    return component.attribute_get_constant_for_read(domain, type, default_value);
+  }
 
   if (found_socket->type == SOCK_STRING) {
     const std::string name = this->get_input<std::string>(found_socket->identifier);
@@ -60,6 +68,42 @@ ReadAttributePtr GeoNodeExecParams::get_input_attribute(const StringRef name,
   return component.attribute_get_constant_for_read(domain, type, default_value);
 }
 
+CustomDataType GeoNodeExecParams::get_input_attribute_data_type(
+    const StringRef name,
+    const GeometryComponent &component,
+    const CustomDataType default_type) const
+{
+  const bNodeSocket *found_socket = this->find_available_socket(name);
+  BLI_assert(found_socket != nullptr); /* There should always be available socket for the name. */
+  if (found_socket == nullptr) {
+    return default_type;
+  }
+
+  if (found_socket->type == SOCK_STRING) {
+    const std::string name = this->get_input<std::string>(found_socket->identifier);
+    ReadAttributePtr attribute = component.attribute_try_get_for_read(name);
+    if (!attribute) {
+      return default_type;
+    }
+    return attribute->custom_data_type();
+  }
+  if (found_socket->type == SOCK_FLOAT) {
+    return CD_PROP_FLOAT;
+  }
+  if (found_socket->type == SOCK_VECTOR) {
+    return CD_PROP_FLOAT3;
+  }
+  if (found_socket->type == SOCK_RGBA) {
+    return CD_PROP_COLOR;
+  }
+  if (found_socket->type == SOCK_BOOLEAN) {
+    return CD_PROP_BOOL;
+  }
+
+  BLI_assert(false);
+  return default_type;
+}
+
 void GeoNodeExecParams::check_extract_input(StringRef identifier,
                                             const CPPType *requested_type) const
 {



More information about the Bf-blender-cvs mailing list