[Bf-blender-cvs] [fa37d090402] temp-geometry-nodes-expandable-geometry-socket-prototype: reuse GeometryExpanderOutput struct

Jacques Lucke noreply at git.blender.org
Thu Aug 5 17:27:07 CEST 2021


Commit: fa37d09040214858cce516792afaec5b1555ad6a
Author: Jacques Lucke
Date:   Thu Aug 5 15:53:48 2021 +0200
Branches: temp-geometry-nodes-expandable-geometry-socket-prototype
https://developer.blender.org/rBfa37d09040214858cce516792afaec5b1555ad6a

reuse GeometryExpanderOutput struct

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

M	source/blender/blenkernel/BKE_node.h
M	source/blender/blenkernel/intern/node.cc
M	source/blender/editors/space_node/node_edit.cc
M	source/blender/makesdna/DNA_node_types.h
M	source/blender/makesrna/intern/rna_nodetree.c
M	source/blender/nodes/geometry/nodes/node_geo_geometry_expander.cc

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

diff --git a/source/blender/blenkernel/BKE_node.h b/source/blender/blenkernel/BKE_node.h
index 0f50a7bc1f6..4c4231877bf 100644
--- a/source/blender/blenkernel/BKE_node.h
+++ b/source/blender/blenkernel/BKE_node.h
@@ -735,6 +735,9 @@ void nodeSetSocketAvailability(struct bNodeSocket *sock, bool is_available);
 
 int nodeSocketLinkLimit(const struct bNodeSocket *sock);
 
+void nodeGeometryExpanderUpdateOutputNameCache(struct GeometryExpanderOutput *expander_output,
+                                               const bNodeTree *ntree);
+
 /* Node Clipboard */
 void BKE_node_clipboard_init(const struct bNodeTree *ntree);
 void BKE_node_clipboard_clear(void);
diff --git a/source/blender/blenkernel/intern/node.cc b/source/blender/blenkernel/intern/node.cc
index 625973d8a92..03ade8b0cc1 100644
--- a/source/blender/blenkernel/intern/node.cc
+++ b/source/blender/blenkernel/intern/node.cc
@@ -71,7 +71,9 @@
 #include "BKE_node.h"
 
 #include "BLI_ghash.h"
+#include "BLI_string_ref.hh"
 #include "BLI_threads.h"
+
 #include "RNA_access.h"
 #include "RNA_define.h"
 
@@ -90,6 +92,8 @@
 
 #include "MOD_nodes.h"
 
+using blender::StringRef;
+
 #define NODE_DEFAULT_MAX_WIDTH 700
 
 /* Fallback types for undefined tree, nodes, sockets */
@@ -3926,6 +3930,36 @@ int nodeSocketLinkLimit(const bNodeSocket *sock)
   return sock->limit;
 }
 
+static std::string expander_output_to_name(const GeometryExpanderOutput &expander_output,
+                                           const bNodeTree &ntree)
+{
+  switch (expander_output.type) {
+    case GEOMETRY_EXPANDER_OUTPUT_TYPE_LOCAL: {
+      return expander_output.local_node_name + StringRef(" ▶ ") +
+             expander_output.local_socket_identifier;
+    }
+    case GEOMETRY_EXPANDER_OUTPUT_TYPE_INPUT: {
+      LISTBASE_FOREACH (const bNodeSocket *, socket, &ntree.inputs) {
+        if (socket->identifier == StringRef(expander_output.input_identifier)) {
+          return StringRef("Input ▶ ") + socket->name;
+        }
+      }
+      return "Unkown input";
+    }
+    case GEOMETRY_EXPANDER_OUTPUT_TYPE_BUILTIN: {
+      return StringRef("Built-in ▶ ") + expander_output.builtin_identifier;
+    }
+  }
+  return "";
+}
+
+void nodeGeometryExpanderUpdateOutputNameCache(struct GeometryExpanderOutput *expander_output,
+                                               const bNodeTree *ntree)
+{
+  const std::string name = expander_output_to_name(*expander_output, *ntree);
+  STRNCPY(expander_output->display_name_cache, name.c_str());
+}
+
 /* ************** Node Clipboard *********** */
 
 #define USE_NODE_CB_VALIDATE
diff --git a/source/blender/editors/space_node/node_edit.cc b/source/blender/editors/space_node/node_edit.cc
index 8039c79fcc2..8a22199986a 100644
--- a/source/blender/editors/space_node/node_edit.cc
+++ b/source/blender/editors/space_node/node_edit.cc
@@ -82,6 +82,7 @@
 #include "node_intern.h" /* own include */
 
 using blender::FunctionRef;
+using blender::MutableSpan;
 using blender::Span;
 using blender::StringRef;
 using blender::StringRefNull;
@@ -2989,35 +2990,18 @@ void NODE_OT_cryptomatte_layer_remove(wmOperatorType *ot)
 
 /* ****************** Geometry Expander Add Output  ******************* */
 
-namespace {
-struct AvailableAttribute {
-  eGeometryExpanderOutputType type;
-  eNodeSocketDatatype socket_type = SOCK_FLOAT;
-  std::string socket_name;
-
-  std::string local_node_name;
-  std::string local_socket_identifier;
-
-  std::string input_identifier;
-  std::string input_name;
-
-  std::string builtin_identifier;
-};
-}  // namespace
-
 static void foreach_available_attribute(
     bNodeTree *ntree,
     bNode *UNUSED(current_node),
-    FunctionRef<void(const AvailableAttribute &attribute)> callback)
+    FunctionRef<void(const GeometryExpanderOutput &attribute)> callback)
 {
   LISTBASE_FOREACH (bNodeSocket *, group_input, &ntree->inputs) {
     if (ELEM(group_input->type, SOCK_INT, SOCK_FLOAT, SOCK_VECTOR, SOCK_RGBA, SOCK_BOOLEAN)) {
-      AvailableAttribute attribute;
+      GeometryExpanderOutput attribute;
       attribute.type = GEOMETRY_EXPANDER_OUTPUT_TYPE_INPUT;
-      attribute.input_identifier = group_input->identifier;
-      attribute.input_name = group_input->name;
+      attribute.domain = ATTR_DOMAIN_POINT;
       attribute.socket_type = (eNodeSocketDatatype)group_input->type;
-      attribute.socket_name = StringRef("Input ▶ ") + group_input->name;
+      STRNCPY(attribute.input_identifier, group_input->identifier);
       callback(attribute);
     }
   }
@@ -3025,24 +3009,40 @@ static void foreach_available_attribute(
     LISTBASE_FOREACH (bNodeSocket *, node_output, &node->outputs) {
       if ((node_output->flag & SOCK_ADD_ATTRIBUTE_TO_GEOMETRY) &&
           ELEM(node_output->type, SOCK_INT, SOCK_FLOAT, SOCK_VECTOR, SOCK_RGBA, SOCK_BOOLEAN)) {
-        AvailableAttribute attribute;
+        GeometryExpanderOutput attribute;
         attribute.type = GEOMETRY_EXPANDER_OUTPUT_TYPE_LOCAL;
-        attribute.local_node_name = node->name;
-        attribute.local_socket_identifier = node_output->identifier;
         attribute.socket_type = (eNodeSocketDatatype)node_output->type;
-        attribute.socket_name = node->name + StringRef(" ▶ ") + node_output->name;
+        attribute.domain = ATTR_DOMAIN_POINT;
+        STRNCPY(attribute.local_node_name, node->name);
+        STRNCPY(attribute.local_socket_identifier, node_output->identifier);
         callback(attribute);
       }
     }
   }
+  {
+    GeometryExpanderOutput attribute;
+    attribute.type = GEOMETRY_EXPANDER_OUTPUT_TYPE_BUILTIN;
+    attribute.socket_type = SOCK_VECTOR;
+    attribute.domain = ATTR_DOMAIN_POINT;
+    STRNCPY(attribute.builtin_identifier, "position");
+    callback(attribute);
+  }
+  {
+    GeometryExpanderOutput attribute;
+    attribute.type = GEOMETRY_EXPANDER_OUTPUT_TYPE_BUILTIN;
+    attribute.socket_type = SOCK_INT;
+    attribute.domain = ATTR_DOMAIN_FACE;
+    STRNCPY(attribute.builtin_identifier, "material_index");
+    callback(attribute);
+  }
 }
 
-static Span<AvailableAttribute> get_updated_cached_available_attributes(bNodeTree *ntree,
-                                                                        bNode *node)
+static MutableSpan<GeometryExpanderOutput> get_updated_cached_available_attributes(
+    bNodeTree *ntree, bNode *node)
 {
-  static Vector<AvailableAttribute> cached_available_attributes;
+  static Vector<GeometryExpanderOutput> cached_available_attributes;
   cached_available_attributes.clear();
-  foreach_available_attribute(ntree, node, [&](const AvailableAttribute &attribute) {
+  foreach_available_attribute(ntree, node, [&](const GeometryExpanderOutput &attribute) {
     cached_available_attributes.append(attribute);
   });
   return cached_available_attributes;
@@ -3062,13 +3062,15 @@ static const EnumPropertyItem *node_geometry_expander_output_add_items(bContext
   bNode *node = nodeFindNodebyName(ntree, node_name);
   MEM_freeN(node_name);
 
-  Span<AvailableAttribute> attributes = get_updated_cached_available_attributes(ntree, node);
+  MutableSpan<GeometryExpanderOutput> attributes = get_updated_cached_available_attributes(ntree,
+                                                                                           node);
 
   for (const int i : attributes.index_range()) {
-    const AvailableAttribute &attribute = attributes[i];
+    GeometryExpanderOutput &attribute = attributes[i];
+    nodeGeometryExpanderUpdateOutputNameCache(&attribute, ntree);
     EnumPropertyItem item = {0};
     item.value = i;
-    item.name = attribute.socket_name.c_str();
+    item.name = attribute.display_name_cache;
     item.identifier = "test";
     RNA_enum_item_add(&items, &totitem, &item);
   }
@@ -3091,8 +3093,8 @@ static int node_geometry_expander_output_add_exec(bContext *C, wmOperator *op)
   NodeGeometryGeometryExpander *storage = (NodeGeometryGeometryExpander *)node->storage;
 
   const int item_value = RNA_enum_get(op->ptr, "item");
-  Span<AvailableAttribute> attributes = get_updated_cached_available_attributes(ntree, node);
-  const AvailableAttribute &attribute = attributes[item_value];
+  Span<GeometryExpanderOutput> attributes = get_updated_cached_available_attributes(ntree, node);
+  const GeometryExpanderOutput &attribute = attributes[item_value];
 
   auto to_identifier = [](int id) { return "out_" + std::to_string(id); };
 
@@ -3104,17 +3106,8 @@ static int node_geometry_expander_output_add_exec(bContext *C, wmOperator *op)
 
   GeometryExpanderOutput *expander_output = (GeometryExpanderOutput *)MEM_callocN(
       sizeof(GeometryExpanderOutput), __func__);
-
-  expander_output->type = attribute.type;
-  expander_output->domain = ATTR_DOMAIN_POINT;
-  expander_output->component_type = (int)GEO_COMPONENT_TYPE_MESH;
-  expander_output->socket_type = (int)attribute.socket_type;
-
-  STRNCPY(expander_output->socket_name, attribute.socket_name.c_str());
-  STRNCPY(expander_output->local_node_name, attribute.local_node_name.c_str());
-  STRNCPY(expander_output->local_socket_identifier, attribute.local_socket_identifier.c_str());
-  STRNCPY(expander_output->input_identifier, attribute.input_identifier.c_str());
-  STRNCPY(expander_output->builtin_identifier, attribute.builtin_identifier.c_str());
+  *expander_output = attribute;
+  STRNCPY(expander_output->socket_identifier, identifier.c_str());
 
   BLI_addtail(&storage->outputs, expander_output);
 
diff --git a/source/blender/makesdna/DNA_node_types.h b/source/blender/makesdna/DNA_node_types.h
index af0c47ca211..671e6da4e56 100644
--- a/source/blender/makesdna/DNA_node_types.h
+++ b/source/blender/makesdna/DNA_node_types.h
@@ -1440,7 +1440,9 @@ typedef struct GeometryExpanderOutput {
 
   /* Identifier of the corresponding socket in the geometry expander. */
   char socket_identifier[64];
-  char socket_name[64];
+
+  /* Derived from the other data. */
+  char display_name_cache[64];
 
   /* AttributeDomain. */
   int8_t domain;
diff --git a/source/blender/makesrna/intern/rna_nodetree.c b/source/blender/makesrna/intern/rna_nodetree.c
index 82f6a77af77..81e11e93fcd 100644
--- a/source/blender/makesrna/intern/rna_nodetree.c
+++ b/source/blender/makesrna/intern/rna_nodetree.c
@@ -2443,6 +2

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list