Blender V5.0
node_geo_input_named_attribute.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "NOD_rna_define.hh"
6
8#include "UI_resources.hh"
9
11
12#include "RNA_enum_types.hh"
13
14#include "node_geometry_util.hh"
15
17
19
21{
22 const bNode *node = b.node_or_null();
23
24 b.add_input<decl::String>("Name").is_attribute_name().optional_label();
25
26 if (node != nullptr) {
27 const NodeGeometryInputNamedAttribute &storage = node_storage(*node);
28 const eCustomDataType data_type = eCustomDataType(storage.data_type);
29 b.add_output(data_type, "Attribute").field_source();
30 }
31 b.add_output<decl::Bool>("Exists").field_source();
32}
33
34static void node_layout(uiLayout *layout, bContext * /*C*/, PointerRNA *ptr)
35{
36 layout->prop(ptr, "data_type", UI_ITEM_NONE, "", ICON_NONE);
37}
38
39static void node_init(bNodeTree * /*tree*/, bNode *node)
40{
42 data->data_type = CD_PROP_FLOAT;
43 node->storage = data;
44}
45
47{
48 const NodeDeclaration &declaration = *params.node_type().static_declaration;
50
51 const blender::bke::bNodeType &node_type = params.node_type();
52 if (params.in_out() == SOCK_OUT) {
53 const std::optional<eCustomDataType> type = bke::socket_type_to_custom_data_type(
54 eNodeSocketDatatype(params.other_socket().type));
55 if (type && *type != CD_PROP_STRING) {
56 /* The input and output sockets have the same name. */
57 params.add_item(IFACE_("Attribute"), [node_type, type](LinkSearchOpParams &params) {
58 bNode &node = params.add_node(node_type);
59 node_storage(node).data_type = *type;
60 params.update_and_connect_available_socket(node, "Attribute");
61 });
62 if (params.node_tree().typeinfo->validate_link(
63 SOCK_BOOLEAN, eNodeSocketDatatype(params.other_socket().type)))
64 {
65 params.add_item(
66 IFACE_("Exists"),
67 [node_type](LinkSearchOpParams &params) {
68 bNode &node = params.add_node(node_type);
69 params.update_and_connect_available_socket(node, "Exists");
70 },
71 -1);
72 }
73 }
74 }
75}
76
78{
79 const NodeGeometryInputNamedAttribute &storage = node_storage(params.node());
80 const eCustomDataType data_type = eCustomDataType(storage.data_type);
81
82 std::string name = params.extract_input<std::string>("Name");
83
84 if (name.empty()) {
85 params.set_default_remaining_outputs();
86 return;
87 }
90 params.set_default_remaining_outputs();
91 return;
92 }
94 params.error_message_add(NodeWarningType::Info,
95 TIP_("Anonymous attributes cannot be accessed by name"));
96 params.set_default_remaining_outputs();
97 return;
98 }
99
100 params.used_named_attribute(name, NamedAttributeUsage::Read);
101
102 const CPPType &type = *bke::custom_data_type_to_cpp_type(data_type);
103
104 params.set_output<GField>("Attribute", AttributeFieldInput::from(name, type));
105 params.set_output("Exists", bke::AttributeExistsFieldInput::from(std::move(name)));
106}
107
108static void node_rna(StructRNA *srna)
109{
111 "data_type",
112 "Data Type",
113 "The data type used to read the attribute values",
118}
119
120static void node_register()
121{
122 static blender::bke::bNodeType ntype;
123
124 geo_node_type_base(&ntype, "GeometryNodeInputNamedAttribute", GEO_NODE_INPUT_NAMED_ATTRIBUTE);
125 ntype.ui_name = "Named Attribute";
126 ntype.ui_description = "Retrieve the data of a specified attribute";
127 ntype.enum_name_legacy = "INPUT_ATTRIBUTE";
128 ntype.nclass = NODE_CLASS_INPUT;
132 ntype.declare = node_declare;
133 ntype.initfunc = node_init;
135 "NodeGeometryInputNamedAttribute",
139
140 node_rna(ntype.rna_ext.srna);
141}
143
144} // namespace blender::nodes::node_geo_input_named_attribute_cc
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1240
#define NODE_CLASS_INPUT
Definition BKE_node.hh:447
#define GEO_NODE_INPUT_NAMED_ATTRIBUTE
#define TIP_(msgid)
#define IFACE_(msgid)
@ CD_PROP_FLOAT
@ CD_PROP_STRING
@ SOCK_OUT
eNodeSocketDatatype
@ SOCK_BOOLEAN
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_storage_enum_accessors(member)
#define UI_ITEM_NONE
BMesh const char void * data
static fn::Field< bool > from(std::string name)
static fn::GField from(std::string name, const CPPType &type, std::optional< std::string > socket_inspection_name=std::nullopt)
Vector< SocketDeclaration * > inputs
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void * MEM_callocN(size_t len, const char *str)
Definition mallocn.cc:118
bool attribute_name_is_anonymous(const StringRef name)
bool allow_procedural_attribute_access(StringRef attribute_name)
void node_register_type(bNodeType &ntype)
Definition node.cc:2416
const char * no_procedural_access_message
const CPPType * custom_data_type_to_cpp_type(eCustomDataType type)
void node_type_storage(bNodeType &ntype, std::optional< StringRefNull > storagename, void(*freefunc)(bNode *node), void(*copyfunc)(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node))
Definition node.cc:5414
std::optional< eCustomDataType > socket_type_to_custom_data_type(eNodeSocketDatatype type)
Definition node.cc:5144
const EnumPropertyItem * attribute_type_type_with_socket_fn(bContext *, PointerRNA *, PropertyRNA *, bool *r_free)
static void node_layout(uiLayout *layout, bContext *, PointerRNA *ptr)
static void node_gather_link_searches(GatherLinkSearchOpParams &params)
void search_link_ops_for_declarations(GatherLinkSearchOpParams &params, Span< SocketDeclaration * > declarations)
PropertyRNA * RNA_def_node_enum(StructRNA *srna, const char *identifier, const char *ui_name, const char *ui_description, const EnumPropertyItem *static_items, const EnumRNAAccessors accessors, std::optional< int > default_value, const EnumPropertyItemFunc item_func, const bool allow_animation)
void geo_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
void node_free_standard_storage(bNode *node)
Definition node_util.cc:42
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:54
const char * name
const EnumPropertyItem rna_enum_attribute_type_items[]
StructRNA * srna
void * storage
Defines a node type.
Definition BKE_node.hh:238
std::string ui_description
Definition BKE_node.hh:244
void(* initfunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:289
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:354
const char * enum_name_legacy
Definition BKE_node.hh:247
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:259
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:378
NodeDeclareFunction declare
Definition BKE_node.hh:362
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< blender::StringRef > name_opt, int icon, std::optional< blender::StringRef > placeholder=std::nullopt)
PointerRNA * ptr
Definition wm_files.cc:4238