Commit: ebc81c6de419b5b27e58afda9fcebbe6f10ad436
Author: Jacques Lucke
Date:   Sun Nov 7 13:07:00 2021 +0100
Branches: temp-enum-socket
https://developer.blender.org/rBebc81c6de419b5b27e58afda9fcebbe6f10ad436

progress

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

M       release/scripts/startup/bl_operators/node.py
M       source/blender/blenkernel/intern/node.cc
M       source/blender/makesrna/intern/rna_nodetree.c
M       source/blender/nodes/function/nodes/node_fn_enum.cc
M       source/blender/nodes/intern/node_socket_declarations.cc

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

diff --git a/release/scripts/startup/bl_operators/node.py 
b/release/scripts/startup/bl_operators/node.py
index c9feeaf9aa2..86d6d8d38cf 100644
--- a/release/scripts/startup/bl_operators/node.py
+++ b/release/scripts/startup/bl_operators/node.py
@@ -327,7 +327,7 @@ class NODE_OT_enum_item_add(Operator):
         if node is None:
             return {'CANCELLED'}
         item = node.enum_items.new()
-        item.value = random.randint(0, 2 ** 31)
+        item.value = random.randint(0, 1e7)
         return {'FINISHED'}
 
 
diff --git a/source/blender/blenkernel/intern/node.cc 
b/source/blender/blenkernel/intern/node.cc
index 56fde3ac25f..258b3080c54 100644
--- a/source/blender/blenkernel/intern/node.cc
+++ b/source/blender/blenkernel/intern/node.cc
@@ -695,6 +695,11 @@ static void direct_link_node_socket(BlendDataReader 
*reader, bNodeSocket *sock)
   BLO_read_data_address(reader, &sock->default_value);
   sock->total_inputs = 0; /* Clear runtime data set before drawing. */
   sock->cache = nullptr;
+
+  if (sock->type == SOCK_ENUM) {
+    bNodeSocketValueEnum *socket_value = (bNodeSocketValueEnum 
*)sock->default_value;
+    socket_value->items = nullptr;
+  }
 }
 
 /* ntree itself has been read! */
diff --git a/source/blender/makesrna/intern/rna_nodetree.c 
b/source/blender/makesrna/intern/rna_nodetree.c
index 4494a553573..9a5976b1182 100644
--- a/source/blender/makesrna/intern/rna_nodetree.c
+++ b/source/blender/makesrna/intern/rna_nodetree.c
@@ -4572,6 +4572,21 @@ bool 
rna_NodeSocketMaterial_default_value_poll(PointerRNA *UNUSED(ptr), PointerR
   return ma->gp_style == NULL;
 }
 
+static void rna_nodeFunctionEnum_update_enum(Main *bmain, bNodeTree *ntree, 
bNode *node)
+{
+  ntree->update |= NTREE_UPDATE_FIELD_INFERENCING;
+  nodeUpdate(ntree, node);
+  ED_node_tag_update_nodetree(bmain, ntree, node);
+  ntreeUpdateTree(bmain, ntree);
+}
+
+static void rna_NodeFunctionEnumItem_update(Main *bmain, Scene *UNUSED(scene), 
PointerRNA *ptr)
+{
+  NodeFunctionEnumItem *item = (NodeFunctionEnumItem *)ptr->data;
+  bNode *node = item->owner_node;
+  rna_nodeFunctionEnum_update_enum(bmain, (bNodeTree *)ptr->owner_id, node);
+}
+
 static NodeFunctionEnumItem *rna_NodeFunctionEnumItems_items_new(ID *id, bNode 
*node, bContext *C)
 {
   NodeFunctionEnum *storage = node->storage;
@@ -4583,8 +4598,8 @@ static NodeFunctionEnumItem 
*rna_NodeFunctionEnumItems_items_new(ID *id, bNode *
   RNA_pointer_create(id, &RNA_Node, node, &node_ptr);
 
   Main *bmain = CTX_data_main(C);
-  Scene *scene = CTX_data_scene(C);
-  rna_Node_socket_update(bmain, scene, &node_ptr);
+  bNodeTree *ntree = (bNodeTree *)id;
+  rna_nodeFunctionEnum_update_enum(bmain, ntree, node);
   return item;
 }
 
@@ -4602,6 +4617,13 @@ static const EnumPropertyItem 
*rna_NodeSocketEnum_items(bContext *UNUSED(C),
   return storage->items;
 }
 
+static void rna_NodeSocketEnum_set(PointerRNA *ptr, int value)
+{
+  bNodeSocket *socket = ptr->data;
+  bNodeSocketValueEnum *storage = (bNodeSocketValueEnum 
*)socket->default_value;
+  storage->value = value;
+}
+
 #else
 
 static const EnumPropertyItem prop_image_layer_items[] = {
@@ -5107,12 +5129,15 @@ static void rna_def_fn_enum_item(BlenderRNA *brna)
 
   prop = RNA_def_property(srna, "value", PROP_INT, PROP_NONE);
   RNA_def_property_ui_text(prop, "Value", "Internal identifier if the enum 
item");
+  RNA_def_property_update(prop, NC_NODE | NA_EDITED, 
"rna_NodeFunctionEnumItem_update");
 
   prop = RNA_def_property(srna, "name", PROP_STRING, PROP_NONE);
   RNA_def_property_ui_text(prop, "Name", "Display name of the enum item");
+  RNA_def_property_update(prop, NC_NODE | NA_EDITED, 
"rna_NodeFunctionEnumItem_update");
 
   prop = RNA_def_property(srna, "description", PROP_STRING, PROP_NONE);
   RNA_def_property_ui_text(prop, "Description", "Tooltip for the enum item");
+  RNA_def_property_update(prop, NC_NODE | NA_EDITED, 
"rna_NodeFunctionEnumItem_update");
 }
 
 static void rna_def_fn_enum_items_api(BlenderRNA *brna, PropertyRNA *cprop)
@@ -12059,8 +12084,8 @@ static void rna_def_node_socket_enum(BlenderRNA *brna,
 
   prop = RNA_def_property(srna, "default_value", PROP_ENUM, PROP_NONE);
   RNA_def_property_enum_sdna(prop, NULL, "value");
-  RNA_def_property_enum_funcs(prop, NULL, NULL, "rna_NodeSocketEnum_items");
-  RNA_def_property_enum_items(prop, DummyRNA_NULL_items);
+  RNA_def_property_enum_funcs(prop, NULL, "rna_NodeSocketEnum_set", 
"rna_NodeSocketEnum_items");
+  RNA_def_property_enum_items(prop, DummyRNA_DEFAULT_items);
   RNA_def_property_ui_text(prop, "Default Value", "Input value used for 
unconnected socket");
   RNA_def_property_update(prop, NC_NODE | NA_EDITED, 
"rna_NodeSocketStandard_value_update");
   RNA_def_property_flag(prop, PROP_CONTEXT_UPDATE);
diff --git a/source/blender/nodes/function/nodes/node_fn_enum.cc 
b/source/blender/nodes/function/nodes/node_fn_enum.cc
index 587c0d5338d..32fca040129 100644
--- a/source/blender/nodes/function/nodes/node_fn_enum.cc
+++ b/source/blender/nodes/function/nodes/node_fn_enum.cc
@@ -16,6 +16,7 @@
 
 #include <cmath>
 
+#include "RNA_define.h"
 #include "RNA_enum_types.h"
 
 #include "UI_interface.h"
@@ -32,25 +33,37 @@ static void fn_node_enum_declare(NodeDeclarationBuilder &b)
     return;
   }
 
-  static EnumPropertyItem my_items[] = {
-      {0, "GRAVITY", 0, "Gravity", "Applies gravity to the simulation"},
-      {1, "INFLATE", 0, "Inflate", "Inflates the cloth"},
-      {2, "EXPAND", 0, "Expand", "Expands the cloth's dimensions"},
-      {3, "PINCH", 0, "Pinch", "Pulls the cloth to the cursor's start 
position"},
-      {4,
-       "SCALE",
-       0,
-       "Scale",
-       "Scales the mesh as a soft body using the origin of the object as 
scale"},
-      {0, NULL, 0, NULL, NULL},
-  };
-
-  b.add_input<decl::Enum>("Enum").static_items(my_items).hide_label();
+  EnumPropertyItem *items = nullptr;
+  int tot_items = 0;
 
   const NodeFunctionEnum *storage = (const NodeFunctionEnum *)node->storage;
   LISTBASE_FOREACH (const NodeFunctionEnumItem *, item, &storage->items) {
     b.add_output<decl::Bool>(N_("Bool"), "item_" + 
std::to_string(item->value));
+    EnumPropertyItem enum_item = {0};
+    enum_item.identifier = BLI_strdup(item->name ? item->name : "");
+    enum_item.name = enum_item.identifier;
+    enum_item.description = BLI_strdup(item->description ? item->description : 
"");
+    enum_item.value = item->value;
+    RNA_enum_item_add(&items, &tot_items, &enum_item);
   }
+
+  std::shared_ptr<decl::EnumItems> socket_items;
+  if (items == nullptr) {
+    socket_items = std::make_shared<decl::EnumItems>();
+  }
+  else {
+    socket_items = std::make_shared<decl::EnumItems>(items, [tot_items, 
items]() {
+      for (const int i : IndexRange(tot_items)) {
+        EnumPropertyItem &enum_item = items[i];
+        MEM_freeN((void *)enum_item.identifier);
+        MEM_freeN((void *)enum_item.description);
+      }
+      MEM_freeN(items);
+    });
+  }
+
+  RNA_enum_item_end(&items, &tot_items);
+  
b.add_input<decl::Enum>("Enum").dynamic_items(std::move(socket_items)).hide_label();
 };
 
 static bool fn_node_enum_draw_socket(uiLayout *layout,
@@ -117,6 +130,12 @@ static void fn_node_enum_free_storage(bNode *node)
 {
   NodeFunctionEnum *storage = (NodeFunctionEnum *)node->storage;
   LISTBASE_FOREACH_MUTABLE (NodeFunctionEnumItem *, item, &storage->items) {
+    if (item->name) {
+      MEM_freeN(item->name);
+    }
+    if (item->description) {
+      MEM_freeN(item->description);
+    }
     MEM_freeN(item);
   }
   MEM_freeN(storage);
diff --git a/source/blender/nodes/intern/node_socket_declarations.cc 
b/source/blender/nodes/intern/node_socket_declarations.cc
index b9966277b66..9a4bd58a280 100644
--- a/source/blender/nodes/intern/node_socket_declarations.cc
+++ b/source/blender/nodes/intern/node_socket_declarations.cc
@@ -286,6 +286,7 @@ EnumItems::EnumItems() : items_(DummyRNA_NULL_items)
 EnumItems::EnumItems(const EnumPropertyItem *items, std::function<void()> 
free_fn)
     : items_(items), free_fn_(std::move(free_fn))
 {
+  BLI_assert(items != nullptr);
 }
 
 EnumItems::~EnumItems()
@@ -327,6 +328,7 @@ bool Enum::matches(const bNodeSocket &socket) const
 
 EnumBuilder &EnumBuilder::static_items(const EnumPropertyItem *items)
 {
+  BLI_assert(items != nullptr);
   decl_->items_ = std::make_shared<EnumItems>(items, nullptr);
   return *this;
 }

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
List details, subscription details or unsubscribe:
https://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to