[Bf-blender-cvs] [d12eff1a887] temp-enum-socket: improve enum storage

Jacques Lucke noreply at git.blender.org
Mon Nov 8 16:07:02 CET 2021


Commit: d12eff1a8873c489eeeea53dc8e61a878bf3db2d
Author: Jacques Lucke
Date:   Sat Nov 6 18:15:55 2021 +0100
Branches: temp-enum-socket
https://developer.blender.org/rBd12eff1a8873c489eeeea53dc8e61a878bf3db2d

improve enum storage

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

M	source/blender/blenkernel/intern/node.cc
M	source/blender/makesdna/DNA_node_types.h
M	source/blender/makesrna/intern/rna_nodetree.c
M	source/blender/nodes/NOD_static_types.h
M	source/blender/nodes/function/nodes/node_fn_enum.cc

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

diff --git a/source/blender/blenkernel/intern/node.cc b/source/blender/blenkernel/intern/node.cc
index 0b4bcc5740f..7a60a9a37ea 100644
--- a/source/blender/blenkernel/intern/node.cc
+++ b/source/blender/blenkernel/intern/node.cc
@@ -624,17 +624,18 @@ void ntreeBlendWrite(BlendWriter *writer, bNodeTree *ntree)
         }
         BLO_write_struct_by_name(writer, node->typeinfo->storagename, storage);
       }
-      else if (node->typeinfo != &NodeTypeUndefined) {
-        BLO_write_struct_by_name(writer, node->typeinfo->storagename, node->storage);
-      }
       else if (node->type == FN_NODE_ENUM) {
         NodeFunctionEnum *storage = (NodeFunctionEnum *)node->storage;
+        BLO_write_struct(writer, NodeFunctionEnum, storage);
         BLO_write_struct_list(writer, NodeFunctionEnumItem, &storage->items);
         LISTBASE_FOREACH (NodeFunctionEnumItem *, item, &storage->items) {
           BLO_write_string(writer, item->name);
           BLO_write_string(writer, item->description);
         }
       }
+      else if (node->typeinfo != &NodeTypeUndefined) {
+        BLO_write_struct_by_name(writer, node->typeinfo->storagename, node->storage);
+      }
     }
 
     if (node->type == CMP_NODE_OUTPUT_FILE) {
@@ -814,9 +815,10 @@ void ntreeBlendReadData(BlendDataReader *reader, bNodeTree *ntree)
         }
         case FN_NODE_ENUM: {
           NodeFunctionEnum *storage = (NodeFunctionEnum *)node->storage;
+          BLO_read_data_address(reader, &storage->owner_node);
           BLO_read_list(reader, &storage->items);
           LISTBASE_FOREACH (NodeFunctionEnumItem *, item, &storage->items) {
-            BLO_read_data_address(reader, &item->owner);
+            BLO_read_data_address(reader, &item->owner_node);
             BLO_read_data_address(reader, &item->name);
             BLO_read_data_address(reader, &item->description);
           }
diff --git a/source/blender/makesdna/DNA_node_types.h b/source/blender/makesdna/DNA_node_types.h
index 69b5004dda8..5ec04065a6f 100644
--- a/source/blender/makesdna/DNA_node_types.h
+++ b/source/blender/makesdna/DNA_node_types.h
@@ -1601,7 +1601,8 @@ typedef struct NodeGeometryViewer {
 } NodeGeometryViewer;
 
 typedef struct NodeFunctionEnumItem {
-  bNode *owner;
+  struct NodeFunctionEnumItem *next, *prev;
+  bNode *owner_node;
   int value;
   char _pad[4];
   char *name;
@@ -1609,6 +1610,7 @@ typedef struct NodeFunctionEnumItem {
 } NodeFunctionEnumItem;
 
 typedef struct NodeFunctionEnum {
+  bNode *owner_node;
   /** NodeFunctionEnumItem. */
   ListBase items;
 } NodeFunctionEnum;
diff --git a/source/blender/makesrna/intern/rna_nodetree.c b/source/blender/makesrna/intern/rna_nodetree.c
index 9ecfec54e07..f2558f30ffb 100644
--- a/source/blender/makesrna/intern/rna_nodetree.c
+++ b/source/blender/makesrna/intern/rna_nodetree.c
@@ -4571,6 +4571,16 @@ bool rna_NodeSocketMaterial_default_value_poll(PointerRNA *UNUSED(ptr), PointerR
   return ma->gp_style == NULL;
 }
 
+static NodeFunctionEnumItem *rna_NodeFunctionEnumItems_items_new(bNode *node)
+{
+  NodeFunctionEnum *storage = node->storage;
+  NodeFunctionEnumItem *item = MEM_callocN(sizeof(NodeFunctionEnumItem), __func__);
+  item->owner_node = node;
+  BLI_addtail(&storage->items, item);
+  printf("%d\n", BLI_listbase_count(&storage->items));
+  return item;
+}
+
 #else
 
 static const EnumPropertyItem prop_image_layer_items[] = {
@@ -5066,16 +5076,54 @@ static void def_fn_input_string(StructRNA *srna)
   RNA_def_property_update(prop, NC_NODE | NA_EDITED, "rna_Node_update");
 }
 
-static void def_fn_enum(StructRNA *srna)
+static void rna_def_fn_enum_item(BlenderRNA *brna)
 {
+  StructRNA *srna;
   PropertyRNA *prop;
 
+  srna = RNA_def_struct(brna, "NodeFunctionEnumItem", NULL);
+  RNA_def_struct_ui_text(srna, "Node Function Enum Item", "");
+
+  prop = RNA_def_property(srna, "value", PROP_INT, PROP_NONE);
+  RNA_def_property_ui_text(prop, "Value", "Internal identifier if the enum item");
+
+  prop = RNA_def_property(srna, "name", PROP_STRING, PROP_NONE);
+  RNA_def_property_ui_text(prop, "Name", "Display name of the enum item");
+
+  prop = RNA_def_property(srna, "description", PROP_STRING, PROP_NONE);
+  RNA_def_property_ui_text(prop, "Description", "Tooltip for the enum item");
+}
+
+static void rna_def_fn_enum_items_api(BlenderRNA *brna, PropertyRNA *cprop)
+{
+  StructRNA *srna;
+  FunctionRNA *func;
+  PropertyRNA *parm;
+
+  RNA_def_property_srna(cprop, "NodeFunctionEnumItems");
+  srna = RNA_def_struct(brna, "NodeFunctionEnumItems", NULL);
+  RNA_def_struct_sdna(srna, "bNode");
+  RNA_def_struct_ui_text(srna, "Enum Items", "");
+
+  func = RNA_def_function(srna, "new", "rna_NodeFunctionEnumItems_items_new");
+  RNA_def_function_ui_description(func, "Add a new enum item");
+  parm = RNA_def_pointer(func, "item", "NodeFunctionEnumItem", "", "New enum item");
+  RNA_def_function_return(func, parm);
+}
+
+static void def_fn_enum(BlenderRNA *brna, StructRNA *srna)
+{
+  PropertyRNA *prop;
+
+  rna_def_fn_enum_item(brna);
+
   RNA_def_struct_sdna_from(srna, "NodeFunctionEnum", "storage");
 
-  // prop = RNA_def_property(srna, "value", PROP_INT, PROP_NONE);
-  // RNA_def_property_int_sdna(prop, NULL, "some_value");
-  // RNA_def_property_ui_text(prop, "Integer", "Input value used for unconnected socket");
-  // RNA_def_property_update(prop, NC_NODE | NA_EDITED, "rna_Node_update");
+  prop = RNA_def_property(srna, "enum_items", PROP_COLLECTION, PROP_NONE);
+  RNA_def_property_collection_sdna(prop, NULL, "items", NULL);
+  RNA_def_property_struct_type(prop, "NodeFunctionEnumItem");
+  RNA_def_property_ui_text(prop, "Items", "");
+  rna_def_fn_enum_items_api(brna, prop);
 }
 
 /* -- Shader Nodes ---------------------------------------------------------- */
@@ -13171,6 +13219,9 @@ void RNA_def_nodetree(BlenderRNA *brna)
         /* needs brna argument, can't use NOD_static_types.h */ \
         def_cmp_output_file(brna, srna); \
       } \
+      if (ID == FN_NODE_ENUM) { \
+        def_fn_enum(brna, srna); \
+      } \
     }
 
   /* hack, don't want to add include path to RNA just for this, since in the future RNA types
diff --git a/source/blender/nodes/NOD_static_types.h b/source/blender/nodes/NOD_static_types.h
index c67b65970cc..b157d77a57e 100644
--- a/source/blender/nodes/NOD_static_types.h
+++ b/source/blender/nodes/NOD_static_types.h
@@ -268,7 +268,7 @@ DefNode(FunctionNode, FN_NODE_LEGACY_RANDOM_FLOAT, 0, "LEGACY_RANDOM_FLOAT", Leg
 DefNode(FunctionNode, FN_NODE_ALIGN_EULER_TO_VECTOR, def_fn_align_euler_to_vector, "ALIGN_EULER_TO_VECTOR", AlignEulerToVector, "Align Euler To Vector", "")
 DefNode(FunctionNode, FN_NODE_BOOLEAN_MATH,  def_boolean_math,  "BOOLEAN_MATH",  BooleanMath,  "Boolean Math", "")
 DefNode(FunctionNode, FN_NODE_COMPARE_FLOATS, def_float_compare, "COMPARE_FLOATS", CompareFloats, "Compare Floats", "")
-DefNode(FunctionNode, FN_NODE_ENUM, def_fn_enum, "ENUM", Enum, "Enum", "")
+DefNode(FunctionNode, FN_NODE_ENUM, 0, "ENUM", Enum, "Enum", "")
 DefNode(FunctionNode, FN_NODE_FLOAT_TO_INT, def_float_to_int, "FLOAT_TO_INT", FloatToInt, "Float to Integer", "")
 DefNode(FunctionNode, FN_NODE_INPUT_BOOL, def_fn_input_bool, "INPUT_BOOL", InputBool, "Boolean", "")
 DefNode(FunctionNode, FN_NODE_INPUT_COLOR, def_fn_input_color, "INPUT_COLOR", InputColor, "Color", "")
diff --git a/source/blender/nodes/function/nodes/node_fn_enum.cc b/source/blender/nodes/function/nodes/node_fn_enum.cc
index 4c424ba1fd3..42c781c2404 100644
--- a/source/blender/nodes/function/nodes/node_fn_enum.cc
+++ b/source/blender/nodes/function/nodes/node_fn_enum.cc
@@ -35,6 +35,7 @@ static void fn_node_enum_declare(NodeDeclarationBuilder &b)
 static void fn_node_enum_init(bNodeTree *UNUSED(tree), bNode *node)
 {
   NodeFunctionEnum *data = (NodeFunctionEnum *)MEM_callocN(sizeof(NodeFunctionEnum), __func__);
+  data->owner_node = node;
   node->storage = data;
 }
 
@@ -61,10 +62,11 @@ static void fn_node_enum_copy_storage(bNodeTree *UNUSED(dest_ntree),
 {
   const NodeFunctionEnum *src_storage = (const NodeFunctionEnum *)src_node->storage;
   NodeFunctionEnum *dst_storage = (NodeFunctionEnum *)MEM_dupallocN(src_storage);
+  dst_storage->owner_node = dst_node;
   BLI_listbase_clear(&dst_storage->items);
   LISTBASE_FOREACH (const NodeFunctionEnumItem *, src_item, &src_storage->items) {
     NodeFunctionEnumItem *dst_item = (NodeFunctionEnumItem *)MEM_dupallocN(src_item);
-    dst_item->owner = dst_node;
+    dst_item->owner_node = dst_node;
     dst_item->name = (char *)MEM_dupallocN(src_item->name);
     dst_item->description = (char *)MEM_dupallocN(src_item->description);
     BLI_addtail(&dst_storage->items, dst_item);



More information about the Bf-blender-cvs mailing list