From 393269266d53187f3087a41b675091b8919881e4 Mon Sep 17 00:00:00 2001 From: Weikang Qiu Date: Sun, 16 Apr 2023 15:23:53 -0400 Subject: [PATCH 1/3] A demo for subtype list of the input vector node --- source/blender/makesdna/DNA_node_types.h | 1 + source/blender/makesrna/intern/rna_nodetree.c | 35 ++++++++++++++++--- .../function/nodes/node_fn_input_vector.cc | 34 ++++++++++++++++-- 3 files changed, 64 insertions(+), 6 deletions(-) diff --git a/source/blender/makesdna/DNA_node_types.h b/source/blender/makesdna/DNA_node_types.h index 8245637943a..21ac544792f 100644 --- a/source/blender/makesdna/DNA_node_types.h +++ b/source/blender/makesdna/DNA_node_types.h @@ -1325,6 +1325,7 @@ typedef struct NodeInputInt { typedef struct NodeInputVector { float vector[3]; + int subtype; } NodeInputVector; typedef struct NodeInputColor { diff --git a/source/blender/makesrna/intern/rna_nodetree.c b/source/blender/makesrna/intern/rna_nodetree.c index 2399cfe74e3..b366b9d4798 100644 --- a/source/blender/makesrna/intern/rna_nodetree.c +++ b/source/blender/makesrna/intern/rna_nodetree.c @@ -7,6 +7,7 @@ #include #include #include +#include #include "BLI_math.h" #include "BLI_utildefines.h" @@ -5163,15 +5164,41 @@ static void def_fn_input_int(StructRNA *srna) static void def_fn_input_vector(StructRNA *srna) { + const EnumPropertyItem *rna_input_vector_subtype = rna_enum_property_subtype_number_array_items; + PropertyRNA *prop; RNA_def_struct_sdna_from(srna, "NodeInputVector", "storage"); - prop = RNA_def_property(srna, "vector", PROP_FLOAT, PROP_XYZ); - RNA_def_property_array(prop, 3); - RNA_def_property_float_sdna(prop, NULL, "vector"); - RNA_def_property_ui_text(prop, "Vector", ""); + prop = RNA_def_property(srna, "subtype", PROP_ENUM, PROP_NONE); + RNA_def_property_enum_sdna(prop, NULL, "subtype"); + RNA_def_property_enum_items(prop, rna_input_vector_subtype); + RNA_def_property_ui_text(prop, "Subtype", ""); RNA_def_property_update(prop, NC_NODE | NA_EDITED, "rna_Node_update"); + + + static char prop_names[20][30]; + + int prop_ind = 0; + for (const EnumPropertyItem *item = rna_input_vector_subtype; item->identifier != NULL; item++) { + char *prop_name = prop_names[prop_ind++]; + strcpy(prop_name, "vector_"); + int i; + const char *identifier = item->identifier; + for (i = 0; i < strlen(identifier); i++) { + if (isalpha(identifier[i])) + prop_name[7 + i] = tolower(identifier[i]); + else + prop_name[7 + i] = '_'; + } + prop_name[7 + i] = '\0'; + + prop = RNA_def_property(srna, prop_name, PROP_FLOAT, item->value); + RNA_def_property_array(prop, 3); + RNA_def_property_float_sdna(prop, NULL, "vector"); + RNA_def_property_ui_text(prop, "Vector", ""); + RNA_def_property_update(prop, NC_NODE | NA_EDITED, "rna_Node_update"); + } } static void def_fn_input_string(StructRNA *srna) diff --git a/source/blender/nodes/function/nodes/node_fn_input_vector.cc b/source/blender/nodes/function/nodes/node_fn_input_vector.cc index 084eb3372d5..512c19fc761 100644 --- a/source/blender/nodes/function/nodes/node_fn_input_vector.cc +++ b/source/blender/nodes/function/nodes/node_fn_input_vector.cc @@ -3,21 +3,49 @@ #include "node_function_util.hh" #include "BLI_hash.h" +#include "RNA_types.h" +#include "RNA_enum_types.h" #include "UI_interface.h" #include "UI_resources.h" namespace blender::nodes::node_fn_input_vector_cc { +NODE_STORAGE_FUNCS(NodeInputVector) + static void node_declare(NodeDeclarationBuilder &b) { b.add_output(N_("Vector")); } +static void node_layout_share(uiLayout* layout, bContext* /*C*/, PointerRNA* ptr) +{ + const NodeInputVector &data = node_storage(*static_cast(ptr->data)); + + uiLayout *col = uiLayoutColumn(layout, true); + std::string prop_name; + for (const EnumPropertyItem *item = rna_enum_property_subtype_number_array_items; + item->identifier != NULL; + item++) { + if (item->value == data.subtype) { + std::string identifier(item->identifier); + transform(identifier.begin(), identifier.end(), identifier.begin(), std::tolower); + prop_name = "vector_" + identifier; + break; + } + } + uiItemR(col, ptr, prop_name.c_str(), UI_ITEM_R_EXPAND, "", ICON_NONE); +} + static void node_layout(uiLayout *layout, bContext * /*C*/, PointerRNA *ptr) { - uiLayout *col = uiLayoutColumn(layout, true); - uiItemR(col, ptr, "vector", UI_ITEM_R_EXPAND, "", ICON_NONE); + node_layout_share(layout, nullptr, ptr); +} + +static void node_layout_ex(uiLayout* layout, bContext* /*C*/, PointerRNA* ptr) +{ + uiItemR(layout, ptr, "subtype", UI_ITEM_R_SPLIT_EMPTY_NAME, IFACE_("Subtype"), ICON_NONE); + node_layout_share(layout, nullptr, ptr); } static void node_build_multi_function(NodeMultiFunctionBuilder &builder) @@ -31,6 +59,7 @@ static void node_build_multi_function(NodeMultiFunctionBuilder &builder) static void node_init(bNodeTree * /*tree*/, bNode *node) { NodeInputVector *data = MEM_cnew(__func__); + data->subtype = PROP_XYZ; node->storage = data; } @@ -49,5 +78,6 @@ void register_node_type_fn_input_vector() &ntype, "NodeInputVector", node_free_standard_storage, node_copy_standard_storage); ntype.build_multi_function = file_ns::node_build_multi_function; ntype.draw_buttons = file_ns::node_layout; + ntype.draw_buttons_ex = file_ns::node_layout_ex; nodeRegisterType(&ntype); } -- 2.30.2 From 384d734534d1d97a2e9d76976ebe428d1ae4176f Mon Sep 17 00:00:00 2001 From: Weikang-Qiu Date: Tue, 18 Apr 2023 18:35:35 -0400 Subject: [PATCH 2/3] Input vector node subtype - callback --- source/blender/makesrna/RNA_access.h | 1 + source/blender/makesrna/intern/rna_access.cc | 12 +++++++ source/blender/makesrna/intern/rna_nodetree.c | 36 +++++-------------- .../function/nodes/node_fn_input_vector.cc | 30 +++++++--------- 4 files changed, 33 insertions(+), 46 deletions(-) diff --git a/source/blender/makesrna/RNA_access.h b/source/blender/makesrna/RNA_access.h index 14d7c6691f6..35d7cbb723d 100644 --- a/source/blender/makesrna/RNA_access.h +++ b/source/blender/makesrna/RNA_access.h @@ -358,6 +358,7 @@ char *RNA_property_string_get_alloc( PointerRNA *ptr, PropertyRNA *prop, char *fixedbuf, int fixedlen, int *r_len); void RNA_property_string_set(PointerRNA *ptr, PropertyRNA *prop, const char *value); void RNA_property_string_set_bytes(PointerRNA *ptr, PropertyRNA *prop, const char *value, int len); +void RNA_property_subtype_set(struct ID* id, void* data, const char *propname, PropertySubType value); eStringPropertySearchFlag RNA_property_string_search_flag(PropertyRNA *prop); /** diff --git a/source/blender/makesrna/intern/rna_access.cc b/source/blender/makesrna/intern/rna_access.cc index c0179998bdb..26e687e3369 100644 --- a/source/blender/makesrna/intern/rna_access.cc +++ b/source/blender/makesrna/intern/rna_access.cc @@ -5248,6 +5248,18 @@ void RNA_string_get(PointerRNA *ptr, const char *name, char *value) } } +void RNA_property_subtype_set(struct ID *id, + void *data, + const char *propname, + PropertySubType value) +{ + PointerRNA ptr; + + RNA_pointer_create(id, &RNA_Node, data, &ptr); + PropertyRNA *prop = RNA_struct_find_property(&ptr, propname); + prop->subtype = value; +} + char *RNA_string_get_alloc( PointerRNA *ptr, const char *name, char *fixedbuf, int fixedlen, int *r_len) { diff --git a/source/blender/makesrna/intern/rna_nodetree.c b/source/blender/makesrna/intern/rna_nodetree.c index b366b9d4798..f8f7bb0ed38 100644 --- a/source/blender/makesrna/intern/rna_nodetree.c +++ b/source/blender/makesrna/intern/rna_nodetree.c @@ -7,7 +7,6 @@ #include #include #include -#include #include "BLI_math.h" #include "BLI_utildefines.h" @@ -5164,41 +5163,22 @@ static void def_fn_input_int(StructRNA *srna) static void def_fn_input_vector(StructRNA *srna) { - const EnumPropertyItem *rna_input_vector_subtype = rna_enum_property_subtype_number_array_items; - PropertyRNA *prop; RNA_def_struct_sdna_from(srna, "NodeInputVector", "storage"); - prop = RNA_def_property(srna, "subtype", PROP_ENUM, PROP_NONE); + prop = RNA_def_property(srna, "vector", PROP_FLOAT, PROP_XYZ); + RNA_def_property_array(prop, 3); + RNA_def_property_float_sdna(prop, NULL, "vector"); + RNA_def_property_ui_text(prop, "Vector", ""); + RNA_def_property_update(prop, NC_NODE | NA_EDITED, "rna_Node_update"); + + const EnumPropertyItem *rna_input_vector_subtype = rna_enum_property_subtype_number_array_items; + prop = RNA_def_property(srna, "subtype", PROP_ENUM, PROP_XYZ); RNA_def_property_enum_sdna(prop, NULL, "subtype"); RNA_def_property_enum_items(prop, rna_input_vector_subtype); RNA_def_property_ui_text(prop, "Subtype", ""); RNA_def_property_update(prop, NC_NODE | NA_EDITED, "rna_Node_update"); - - - static char prop_names[20][30]; - - int prop_ind = 0; - for (const EnumPropertyItem *item = rna_input_vector_subtype; item->identifier != NULL; item++) { - char *prop_name = prop_names[prop_ind++]; - strcpy(prop_name, "vector_"); - int i; - const char *identifier = item->identifier; - for (i = 0; i < strlen(identifier); i++) { - if (isalpha(identifier[i])) - prop_name[7 + i] = tolower(identifier[i]); - else - prop_name[7 + i] = '_'; - } - prop_name[7 + i] = '\0'; - - prop = RNA_def_property(srna, prop_name, PROP_FLOAT, item->value); - RNA_def_property_array(prop, 3); - RNA_def_property_float_sdna(prop, NULL, "vector"); - RNA_def_property_ui_text(prop, "Vector", ""); - RNA_def_property_update(prop, NC_NODE | NA_EDITED, "rna_Node_update"); - } } static void def_fn_input_string(StructRNA *srna) diff --git a/source/blender/nodes/function/nodes/node_fn_input_vector.cc b/source/blender/nodes/function/nodes/node_fn_input_vector.cc index 512c19fc761..8934cada381 100644 --- a/source/blender/nodes/function/nodes/node_fn_input_vector.cc +++ b/source/blender/nodes/function/nodes/node_fn_input_vector.cc @@ -3,8 +3,8 @@ #include "node_function_util.hh" #include "BLI_hash.h" -#include "RNA_types.h" -#include "RNA_enum_types.h" + +#include "RNA_access.h" #include "UI_interface.h" #include "UI_resources.h" @@ -18,23 +18,10 @@ static void node_declare(NodeDeclarationBuilder &b) b.add_output(N_("Vector")); } -static void node_layout_share(uiLayout* layout, bContext* /*C*/, PointerRNA* ptr) +static void node_layout_share(uiLayout *layout, bContext * /*C*/, PointerRNA *ptr) { - const NodeInputVector &data = node_storage(*static_cast(ptr->data)); - uiLayout *col = uiLayoutColumn(layout, true); - std::string prop_name; - for (const EnumPropertyItem *item = rna_enum_property_subtype_number_array_items; - item->identifier != NULL; - item++) { - if (item->value == data.subtype) { - std::string identifier(item->identifier); - transform(identifier.begin(), identifier.end(), identifier.begin(), std::tolower); - prop_name = "vector_" + identifier; - break; - } - } - uiItemR(col, ptr, prop_name.c_str(), UI_ITEM_R_EXPAND, "", ICON_NONE); + uiItemR(col, ptr, "vector", UI_ITEM_R_EXPAND, "", ICON_NONE); } static void node_layout(uiLayout *layout, bContext * /*C*/, PointerRNA *ptr) @@ -42,12 +29,18 @@ static void node_layout(uiLayout *layout, bContext * /*C*/, PointerRNA *ptr) node_layout_share(layout, nullptr, ptr); } -static void node_layout_ex(uiLayout* layout, bContext* /*C*/, PointerRNA* ptr) +static void node_layout_ex(uiLayout *layout, bContext * /*C*/, PointerRNA *ptr) { uiItemR(layout, ptr, "subtype", UI_ITEM_R_SPLIT_EMPTY_NAME, IFACE_("Subtype"), ICON_NONE); node_layout_share(layout, nullptr, ptr); } +static void node_update(bNodeTree *ntree, bNode *node) { + NodeInputVector *data = static_cast(node->storage); + + RNA_property_subtype_set(&ntree->id, node, "vector", static_cast(data->subtype)); +} + static void node_build_multi_function(NodeMultiFunctionBuilder &builder) { const bNode &bnode = builder.node(); @@ -79,5 +72,6 @@ void register_node_type_fn_input_vector() ntype.build_multi_function = file_ns::node_build_multi_function; ntype.draw_buttons = file_ns::node_layout; ntype.draw_buttons_ex = file_ns::node_layout_ex; + ntype.updatefunc = file_ns::node_update; nodeRegisterType(&ntype); } -- 2.30.2 From 843668eef0097a02b7de9cfb9d3a370d5a336a05 Mon Sep 17 00:00:00 2001 From: Weikang-Qiu Date: Fri, 21 Apr 2023 18:51:42 -0400 Subject: [PATCH 3/3] Refractor of function RNA_property_subtype_set --- source/blender/makesrna/RNA_access.h | 3 ++- source/blender/makesrna/intern/rna_access.cc | 18 ++++++------------ source/blender/makesrna/intern/rna_nodetree.c | 9 ++++++++- source/blender/nodes/function/CMakeLists.txt | 1 + .../function/nodes/node_fn_input_vector.cc | 5 ++++- 5 files changed, 21 insertions(+), 15 deletions(-) diff --git a/source/blender/makesrna/RNA_access.h b/source/blender/makesrna/RNA_access.h index 35d7cbb723d..7b41165354f 100644 --- a/source/blender/makesrna/RNA_access.h +++ b/source/blender/makesrna/RNA_access.h @@ -358,7 +358,6 @@ char *RNA_property_string_get_alloc( PointerRNA *ptr, PropertyRNA *prop, char *fixedbuf, int fixedlen, int *r_len); void RNA_property_string_set(PointerRNA *ptr, PropertyRNA *prop, const char *value); void RNA_property_string_set_bytes(PointerRNA *ptr, PropertyRNA *prop, const char *value, int len); -void RNA_property_subtype_set(struct ID* id, void* data, const char *propname, PropertySubType value); eStringPropertySearchFlag RNA_property_string_search_flag(PropertyRNA *prop); /** @@ -593,6 +592,8 @@ void RNA_collection_clear(PointerRNA *ptr, const char *name); } \ ((void)0) +void RNA_property_subtype_set(PointerRNA *ptr, const char *propname, PropertySubType value); + /** * Check if the #IDproperty exists, for operators. * diff --git a/source/blender/makesrna/intern/rna_access.cc b/source/blender/makesrna/intern/rna_access.cc index 26e687e3369..8c13199a28d 100644 --- a/source/blender/makesrna/intern/rna_access.cc +++ b/source/blender/makesrna/intern/rna_access.cc @@ -5248,18 +5248,6 @@ void RNA_string_get(PointerRNA *ptr, const char *name, char *value) } } -void RNA_property_subtype_set(struct ID *id, - void *data, - const char *propname, - PropertySubType value) -{ - PointerRNA ptr; - - RNA_pointer_create(id, &RNA_Node, data, &ptr); - PropertyRNA *prop = RNA_struct_find_property(&ptr, propname); - prop->subtype = value; -} - char *RNA_string_get_alloc( PointerRNA *ptr, const char *name, char *fixedbuf, int fixedlen, int *r_len) { @@ -5392,6 +5380,12 @@ bool RNA_collection_is_empty(PointerRNA *ptr, const char *name) return false; } +void RNA_property_subtype_set(PointerRNA *ptr, const char *propname, PropertySubType value) +{ + PropertyRNA *prop = RNA_struct_find_property(ptr, propname); + prop->subtype = value; +} + bool RNA_property_is_set_ex(PointerRNA *ptr, PropertyRNA *prop, bool use_ghost) { prop = rna_ensure_property(prop); diff --git a/source/blender/makesrna/intern/rna_nodetree.c b/source/blender/makesrna/intern/rna_nodetree.c index f8f7bb0ed38..460ff10d065 100644 --- a/source/blender/makesrna/intern/rna_nodetree.c +++ b/source/blender/makesrna/intern/rna_nodetree.c @@ -2448,6 +2448,13 @@ static void rna_Node_update_relations(Main *bmain, Scene *scene, PointerRNA *ptr DEG_relations_tag_update(bmain); } +static void rna_Node_update_individual(Main *UNUSED(bmain), Scene *UNUSED(scene), PointerRNA *ptr) +{ + bNodeTree *ntree = (bNodeTree *)ptr->owner_id; + bNode *node = (bNode *)ptr->data; + node->typeinfo->updatefunc(ntree, node); +} + static void rna_Node_socket_value_update(ID *id, bNode *UNUSED(node), bContext *C) { BKE_ntree_update_tag_all((bNodeTree *)id); @@ -5178,7 +5185,7 @@ static void def_fn_input_vector(StructRNA *srna) RNA_def_property_enum_sdna(prop, NULL, "subtype"); RNA_def_property_enum_items(prop, rna_input_vector_subtype); RNA_def_property_ui_text(prop, "Subtype", ""); - RNA_def_property_update(prop, NC_NODE | NA_EDITED, "rna_Node_update"); + RNA_def_property_update(prop, NC_NODE | NA_EDITED, "rna_Node_update_individual"); } static void def_fn_input_string(StructRNA *srna) diff --git a/source/blender/nodes/function/CMakeLists.txt b/source/blender/nodes/function/CMakeLists.txt index 756178ffe5a..04cf5225d4b 100644 --- a/source/blender/nodes/function/CMakeLists.txt +++ b/source/blender/nodes/function/CMakeLists.txt @@ -14,6 +14,7 @@ set(INC ../../makesrna ../../windowmanager ../../../../intern/guardedalloc + ${CMAKE_BINARY_DIR}/source/blender/makesrna ) set(INC_SYS diff --git a/source/blender/nodes/function/nodes/node_fn_input_vector.cc b/source/blender/nodes/function/nodes/node_fn_input_vector.cc index 8934cada381..bcf846fdfee 100644 --- a/source/blender/nodes/function/nodes/node_fn_input_vector.cc +++ b/source/blender/nodes/function/nodes/node_fn_input_vector.cc @@ -5,6 +5,7 @@ #include "BLI_hash.h" #include "RNA_access.h" +#include "RNA_prototypes.h" #include "UI_interface.h" #include "UI_resources.h" @@ -38,7 +39,9 @@ static void node_layout_ex(uiLayout *layout, bContext * /*C*/, PointerRNA *ptr) static void node_update(bNodeTree *ntree, bNode *node) { NodeInputVector *data = static_cast(node->storage); - RNA_property_subtype_set(&ntree->id, node, "vector", static_cast(data->subtype)); + PointerRNA ptr; + RNA_pointer_create(&ntree->id, &RNA_Node, node, &ptr); + RNA_property_subtype_set(&ptr, "vector", static_cast(data->subtype)); } static void node_build_multi_function(NodeMultiFunctionBuilder &builder) -- 2.30.2