diff options
author | Edgar Roman Cervantes <redvant> | 2021-02-20 01:03:14 +0300 |
---|---|---|
committer | Hans Goudey <h.goudey@me.com> | 2021-02-20 01:03:14 +0300 |
commit | a961a2189cb38ffb368d6781aa57177bfefe0e36 (patch) | |
tree | 9fe23cadf50386676c42655e2b02e61144694384 /source | |
parent | ee1c674775fd60352502f4d6062dbd4637865f9d (diff) |
Geometry Nodes: Add string input node
This commit adds a simple string input node, intended for use in the
attribute workflow to make using the same attribute name in multiple
places easier. The node is function node similar to the existing vector
input node.
Ref T84971
Differential Revision: https://developer.blender.org/D10316
Diffstat (limited to 'source')
-rw-r--r-- | source/blender/blenkernel/BKE_node.h | 1 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/node.cc | 13 | ||||
-rw-r--r-- | source/blender/makesdna/DNA_node_types.h | 4 | ||||
-rw-r--r-- | source/blender/makesrna/intern/rna_nodetree.c | 47 | ||||
-rw-r--r-- | source/blender/nodes/CMakeLists.txt | 1 | ||||
-rw-r--r-- | source/blender/nodes/NOD_function.h | 1 | ||||
-rw-r--r-- | source/blender/nodes/NOD_static_types.h | 1 | ||||
-rw-r--r-- | source/blender/nodes/function/nodes/node_fn_input_string.cc | 84 |
8 files changed, 152 insertions, 0 deletions
diff --git a/source/blender/blenkernel/BKE_node.h b/source/blender/blenkernel/BKE_node.h index 77d850ec2f6..8652adecaf9 100644 --- a/source/blender/blenkernel/BKE_node.h +++ b/source/blender/blenkernel/BKE_node.h @@ -1388,6 +1388,7 @@ int ntreeTexExecTree(struct bNodeTree *ntree, #define FN_NODE_OBJECT_TRANSFORMS 1205 #define FN_NODE_RANDOM_FLOAT 1206 #define FN_NODE_INPUT_VECTOR 1207 +#define FN_NODE_INPUT_STRING 1208 /** \} */ diff --git a/source/blender/blenkernel/intern/node.cc b/source/blender/blenkernel/intern/node.cc index 228d3e0d825..b96adce7cca 100644 --- a/source/blender/blenkernel/intern/node.cc +++ b/source/blender/blenkernel/intern/node.cc @@ -523,6 +523,13 @@ void ntreeBlendWrite(BlendWriter *writer, bNodeTree *ntree) BLO_write_struct_by_name(writer, node->typeinfo->storagename, node->storage); MEM_SAFE_FREE(nc->matte_id); } + else if (node->type == FN_NODE_INPUT_STRING) { + NodeInputString *storage = (NodeInputString *)node->storage; + if (storage->string) { + BLO_write_string(writer, storage->string); + } + 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); } @@ -685,6 +692,11 @@ void ntreeBlendReadData(BlendDataReader *reader, bNodeTree *ntree) iuser->scene = nullptr; break; } + case FN_NODE_INPUT_STRING: { + NodeInputString *storage = (NodeInputString *)node->storage; + BLO_read_data_address(reader, &storage->string); + break; + } default: break; } @@ -4806,6 +4818,7 @@ static void registerFunctionNodes() register_node_type_fn_combine_strings(); register_node_type_fn_float_compare(); register_node_type_fn_group_instance_id(); + register_node_type_fn_input_string(); register_node_type_fn_input_vector(); register_node_type_fn_object_transforms(); register_node_type_fn_random_float(); diff --git a/source/blender/makesdna/DNA_node_types.h b/source/blender/makesdna/DNA_node_types.h index 9633ef8e121..e72e9632483 100644 --- a/source/blender/makesdna/DNA_node_types.h +++ b/source/blender/makesdna/DNA_node_types.h @@ -1140,6 +1140,10 @@ typedef struct NodeInputVector { float vector[3]; } NodeInputVector; +typedef struct NodeInputString { + char *string; +} NodeInputString; + typedef struct NodeGeometryRotatePoints { /* GeometryNodeRotatePointsType */ uint8_t type; diff --git a/source/blender/makesrna/intern/rna_nodetree.c b/source/blender/makesrna/intern/rna_nodetree.c index b372ff4c273..65de8c479b8 100644 --- a/source/blender/makesrna/intern/rna_nodetree.c +++ b/source/blender/makesrna/intern/rna_nodetree.c @@ -4113,6 +4113,38 @@ void rna_ShaderNodePointDensity_density_minmax(bNode *self, RE_point_density_minmax(depsgraph, pd, r_min, r_max); } +static void rna_NodeInputString_string_get(PointerRNA *ptr, char *value) +{ + bNode *node = (bNode *)ptr->data; + NodeInputString *storage = node->storage; + + strcpy(value, (storage->string) ? storage->string : ""); +} + +static int rna_NodeInputString_string_length(PointerRNA *ptr) +{ + bNode *node = (bNode *)ptr->data; + NodeInputString *storage = node->storage; + + return (storage->string) ? strlen(storage->string) : 0; +} + +static void rna_NodeInputString_string_set(PointerRNA *ptr, const char *value) +{ + bNode *node = (bNode *)ptr->data; + NodeInputString *storage = node->storage; + + if (storage->string) { + MEM_freeN(storage->string); + } + + if (value && value[0]) { + storage->string = BLI_strdup(value); + } + else { + storage->string = NULL; + } +} #else static const EnumPropertyItem prop_image_layer_items[] = { @@ -4539,6 +4571,21 @@ static void def_fn_input_vector(StructRNA *srna) RNA_def_property_update(prop, NC_NODE | NA_EDITED, "rna_Node_update"); } +static void def_fn_input_string(StructRNA *srna) +{ + PropertyRNA *prop; + + RNA_def_struct_sdna_from(srna, "NodeInputString", "storage"); + + prop = RNA_def_property(srna, "string", PROP_STRING, PROP_NONE); + RNA_def_property_string_funcs(prop, + "rna_NodeInputString_string_get", + "rna_NodeInputString_string_length", + "rna_NodeInputString_string_set"); + RNA_def_property_ui_text(prop, "String", ""); + RNA_def_property_update(prop, NC_NODE | NA_EDITED, "rna_Node_update"); +} + /* -- Shader Nodes ---------------------------------------------------------- */ static void def_sh_output(StructRNA *srna) diff --git a/source/blender/nodes/CMakeLists.txt b/source/blender/nodes/CMakeLists.txt index c9eb0c43bba..8c5081555fc 100644 --- a/source/blender/nodes/CMakeLists.txt +++ b/source/blender/nodes/CMakeLists.txt @@ -135,6 +135,7 @@ set(SRC function/nodes/node_fn_combine_strings.cc function/nodes/node_fn_float_compare.cc function/nodes/node_fn_group_instance_id.cc + function/nodes/node_fn_input_string.cc function/nodes/node_fn_input_vector.cc function/nodes/node_fn_object_transforms.cc function/nodes/node_fn_random_float.cc diff --git a/source/blender/nodes/NOD_function.h b/source/blender/nodes/NOD_function.h index b5279f7d914..6b184f04af7 100644 --- a/source/blender/nodes/NOD_function.h +++ b/source/blender/nodes/NOD_function.h @@ -24,6 +24,7 @@ void register_node_type_fn_boolean_math(void); void register_node_type_fn_combine_strings(void); void register_node_type_fn_float_compare(void); void register_node_type_fn_group_instance_id(void); +void register_node_type_fn_input_string(void); void register_node_type_fn_input_vector(void); void register_node_type_fn_object_transforms(void); void register_node_type_fn_random_float(void); diff --git a/source/blender/nodes/NOD_static_types.h b/source/blender/nodes/NOD_static_types.h index 962ad70eda4..35550bdec48 100644 --- a/source/blender/nodes/NOD_static_types.h +++ b/source/blender/nodes/NOD_static_types.h @@ -267,6 +267,7 @@ DefNode(FunctionNode, FN_NODE_COMBINE_STRINGS, 0, "COMBINE_STRINGS DefNode(FunctionNode, FN_NODE_OBJECT_TRANSFORMS, 0, "OBJECT_TRANSFORMS", ObjectTransforms, "Object Transforms", "") DefNode(FunctionNode, FN_NODE_RANDOM_FLOAT, 0, "RANDOM_FLOAT", RandomFloat, "Random Float", "") DefNode(FunctionNode, FN_NODE_INPUT_VECTOR, def_fn_input_vector, "INPUT_VECTOR", InputVector, "Vector", "") +DefNode(FunctionNode, FN_NODE_INPUT_STRING, def_fn_input_string, "INPUT_STRING", InputString, "String", "") DefNode(GeometryNode, GEO_NODE_TRIANGULATE, def_geo_triangulate, "TRIANGULATE", Triangulate, "Triangulate", "") DefNode(GeometryNode, GEO_NODE_EDGE_SPLIT, 0, "EDGE_SPLIT", EdgeSplit, "Edge Split", "") diff --git a/source/blender/nodes/function/nodes/node_fn_input_string.cc b/source/blender/nodes/function/nodes/node_fn_input_string.cc new file mode 100644 index 00000000000..f16bdef2f38 --- /dev/null +++ b/source/blender/nodes/function/nodes/node_fn_input_string.cc @@ -0,0 +1,84 @@ +/* + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "node_function_util.hh" + +#include "UI_interface.h" +#include "UI_resources.h" + +static bNodeSocketTemplate fn_node_input_string_out[] = { + {SOCK_STRING, N_("String")}, + {-1, ""}, +}; + +static void fn_node_input_string_layout(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr) +{ + uiItemR(layout, ptr, "string", 0, "", ICON_NONE); +} + +static void fn_node_input_string_expand_in_mf_network( + blender::nodes::NodeMFNetworkBuilder &builder) +{ + bNode &bnode = builder.bnode(); + NodeInputString *node_storage = static_cast<NodeInputString *>(bnode.storage); + std::string string = std::string((node_storage->string) ? node_storage->string : ""); + + builder.construct_and_set_matching_fn<blender::fn::CustomMF_Constant<std::string>>(string); +} + +static void fn_node_input_string_init(bNodeTree *UNUSED(ntree), bNode *node) +{ + node->storage = MEM_callocN(sizeof(NodeInputString), __func__); +} + +static void fn_node_input_string_free(bNode *node) +{ + NodeInputString *storage = (NodeInputString *)node->storage; + if (storage == nullptr) { + return; + } + if (storage->string != nullptr) { + MEM_freeN(storage->string); + } + MEM_freeN(storage); +} + +static void fn_node_string_copy(bNodeTree *UNUSED(dest_ntree), + bNode *dest_node, + const bNode *src_node) +{ + NodeInputString *source_storage = (NodeInputString *)src_node->storage; + NodeInputString *destination_storage = (NodeInputString *)MEM_dupallocN(source_storage); + + if (source_storage->string) { + destination_storage->string = (char *)MEM_dupallocN(source_storage->string); + } + + dest_node->storage = destination_storage; +} + +void register_node_type_fn_input_string() +{ + static bNodeType ntype; + + fn_node_type_base(&ntype, FN_NODE_INPUT_STRING, "String", NODE_CLASS_INPUT, 0); + node_type_socket_templates(&ntype, nullptr, fn_node_input_string_out); + node_type_init(&ntype, fn_node_input_string_init); + node_type_storage(&ntype, "NodeInputString", fn_node_input_string_free, fn_node_string_copy); + ntype.expand_in_mf_network = fn_node_input_string_expand_in_mf_network; + ntype.draw_buttons = fn_node_input_string_layout; + nodeRegisterType(&ntype); +} |