Blender V5.3
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
6
7#include "NOD_rna_define.hh"
8
10#include "UI_resources.hh"
11
13
14#include "RNA_enum_types.hh"
15
16#include "node_geometry_util.hh"
17
19
21
23{
24 const bNode *node = b.node_or_null();
25
26 b.add_input<decl::String>("Name"_ustr).is_attribute_name().optional_label();
27
28 if (node != nullptr) {
29 const NodeGeometryInputNamedAttribute &storage = node_storage(*node);
30 const eCustomDataType data_type = eCustomDataType(storage.data_type);
31 b.add_output(data_type, "Attribute"_ustr).structure_type(StructureType::Field);
32 }
33 b.add_output<decl::Bool>("Exists"_ustr).structure_type(StructureType::Field);
34}
35
36static void node_layout(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
37{
38 layout.prop(ptr, "data_type", UI_ITEM_NONE, "", ICON_NONE);
39}
40
41static void node_init(bNodeTree * /*tree*/, bNode *node)
42{
43 NodeGeometryInputNamedAttribute *data = MEM_new<NodeGeometryInputNamedAttribute>(__func__);
44 data->data_type = CD_PROP_FLOAT;
45 node->storage = data;
46}
47
49{
50 const NodeDeclaration &declaration = *params.node_type().static_declaration;
51 search_link_ops_for_declarations(params, declaration.inputs);
52
53 const bke::bNodeType &node_type = params.node_type();
54 if (params.in_out() == SOCK_OUT) {
55 const std::optional<eCustomDataType> type = bke::socket_type_to_custom_data_type(
56 params.other_socket().type);
57 if (type && *type != CD_PROP_STRING) {
58 /* The input and output sockets have the same name. */
59 params.add_item(IFACE_("Attribute"), [node_type, type](LinkSearchOpParams &params) {
60 bNode &node = params.add_node(node_type);
61 node_storage(node).data_type = *type;
62 params.update_and_connect_available_socket(node, "Attribute"_ustr);
63 });
64 if (params.node_tree().typeinfo->validate_link(SOCK_BOOLEAN, params.other_socket().type)) {
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"_ustr);
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"_ustr);
83
84 if (name.empty()) {
86 return;
87 }
91 return;
92 }
95 TIP_("Anonymous attributes cannot be accessed by name"));
97 return;
98 }
99
101
102 const CPPType &type = *bke::custom_data_type_to_cpp_type(data_type);
103
104 params.set_output<GField>("Attribute"_ustr, AttributeFieldInput::from(name, type));
105 params.set_output("Exists"_ustr, 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 bke::bNodeType ntype;
123
125 &ntype, "GeometryNodeInputNamedAttribute"_ustr, GEO_NODE_INPUT_NAMED_ATTRIBUTE);
126 ntype.ui_name = "Named Attribute";
127 ntype.ui_description = "Retrieve the data of a specified attribute";
128 ntype.enum_name_legacy = "INPUT_ATTRIBUTE";
129 ntype.nclass = NODE_CLASS_INPUT;
133 ntype.declare = node_declare;
134 ntype.initfunc = node_init;
136 "NodeGeometryInputNamedAttribute",
140
141 node_rna(ntype.rna_ext.srna);
142}
144
145} // namespace blender::nodes::node_geo_input_named_attribute_cc
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1353
#define NODE_CLASS_INPUT
Definition BKE_node.hh:478
#define GEO_NODE_INPUT_NAMED_ATTRIBUTE
#define TIP_(msgid)
#define IFACE_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_storage_enum_accessors(member)
#define UI_ITEM_NONE
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)
const bke::bNodeType & node_type() const
void add_item(std::string socket_name, SocketLinkOperation::LinkSocketFn fn, int weight=0)
void error_message_add(const NodeWarningType type, StringRef message) const
T extract_input(const UString identifier)
void used_named_attribute(StringRef attribute_name, NamedAttributeUsage usage)
void set_output(const UString identifier, T &&value)
void update_and_connect_available_socket(bNode &new_node, UString socket_name)
Vector< SocketDeclaration * > inputs
#define storage(slot, qualifiers)
bool attribute_name_is_anonymous(const StringRef name)
const CPPType * custom_data_type_to_cpp_type(eCustomDataType type)
bool allow_procedural_attribute_access(StringRef attribute_name)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
const char * no_procedural_access_message
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:6166
std::optional< eCustomDataType > socket_type_to_custom_data_type(eNodeSocketDatatype type)
Definition node.cc:5911
const EnumPropertyItem * attribute_type_type_with_socket_fn(bContext *, PointerRNA *, PropertyRNA *, bool *r_free)
static void node_gather_link_searches(GatherLinkSearchOpParams &params)
static void node_layout(ui::Layout &layout, bContext *, PointerRNA *ptr)
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)
BMesh const char void * data
const EnumPropertyItem rna_enum_attribute_type_items[]
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:57
void node_free_standard_storage(bNode *node)
Definition node_util.cc:45
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
eNodeSocketDatatype type
bke::bNodeTreeType * typeinfo
bool(*) validate_link(eNodeSocketDatatype from, eNodeSocketDatatype to)
Definition BKE_node.hh:566
Defines a node type.
Definition BKE_node.hh:265
void(*) draw_buttons(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:290
std::string ui_description
Definition BKE_node.hh:271
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:385
void(*) initfunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:320
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:409
NodeDeclareFunction declare
Definition BKE_node.hh:393
nodes::NodeDeclaration * static_declaration
Definition BKE_node.hh:402
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)