Blender V4.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
5#include "NOD_rna_define.hh"
6
7#include "UI_interface.hh"
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().hide_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 uiItemR(layout, ptr, "data_type", UI_ITEM_NONE, "", ICON_NONE);
37}
38
39static void node_init(bNodeTree * /*tree*/, bNode *node)
40{
41 NodeGeometryInputNamedAttribute *data = MEM_cnew<NodeGeometryInputNamedAttribute>(__func__);
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 const std::string name = params.extract_input<std::string>("Name");
83
84 if (name.empty()) {
85 params.set_default_remaining_outputs();
86 return;
87 }
89 params.error_message_add(NodeWarningType::Info, TIP_(bke::no_procedural_access_message));
90 params.set_default_remaining_outputs();
91 return;
92 }
94 params.error_message_add(NodeWarningType::Info,
95 TIP_("Anonymous attributes can't 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::Create(name, type));
105 params.set_output("Exists", bke::AttributeExistsFieldInput::Create(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, GEO_NODE_INPUT_NAMED_ATTRIBUTE, "Named Attribute", NODE_CLASS_INPUT);
128 ntype.declare = node_declare;
129 ntype.initfunc = node_init;
131 "NodeGeometryInputNamedAttribute",
135
136 node_rna(ntype.rna_ext.srna);
137}
139
140} // namespace blender::nodes::node_geo_input_named_attribute_cc
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1799
#define NODE_CLASS_INPUT
Definition BKE_node.hh:404
#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
void uiItemR(uiLayout *layout, PointerRNA *ptr, const char *propname, eUI_Item_Flag flag, const char *name, int icon)
static fn::Field< bool > Create(std::string name)
static fn::GField Create(std::string name, const CPPType &type, std::optional< std::string > socket_inspection_name=std::nullopt)
Vector< SocketDeclaration * > inputs
local_group_size(16, 16) .push_constant(Type b
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
bool attribute_name_is_anonymous(const StringRef name)
bool allow_procedural_attribute_access(StringRef attribute_name)
const char * no_procedural_access_message
void node_type_storage(bNodeType *ntype, const char *storagename, void(*freefunc)(bNode *node), void(*copyfunc)(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node))
Definition node.cc:4632
void node_register_type(bNodeType *ntype)
Definition node.cc:1708
const CPPType * custom_data_type_to_cpp_type(eCustomDataType type)
std::optional< eCustomDataType > socket_type_to_custom_data_type(eNodeSocketDatatype type)
Definition node.cc:4379
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, int type, const char *name, short nclass)
void node_free_standard_storage(bNode *node)
Definition node_util.cc:46
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:58
const EnumPropertyItem rna_enum_attribute_type_items[]
StructRNA * srna
Definition RNA_types.hh:780
Defines a node type.
Definition BKE_node.hh:218
void(* initfunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:267
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:339
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:238
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:363
NodeDeclareFunction declare
Definition BKE_node.hh:347
PointerRNA * ptr
Definition wm_files.cc:4126