Blender V4.5
node_geo_store_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 <atomic>
6
7#include "UI_interface.hh"
8#include "UI_resources.hh"
9
10#include "RNA_access.hh"
11#include "RNA_enum_types.hh"
12
13#include "BKE_instances.hh"
14#include "BKE_mesh.hh"
16
17#include "NOD_rna_define.hh"
19
20#include "node_geometry_util.hh"
21
22#include <fmt/format.h>
23
25
27
29{
30 b.use_custom_socket_order();
31 b.allow_any_socket_order();
32 b.add_default_layout();
33 const bNode *node = b.node_or_null();
34
35 b.add_input<decl::Geometry>("Geometry");
36 b.add_output<decl::Geometry>("Geometry").propagate_all().align_with_previous();
37 b.add_input<decl::Bool>("Selection").default_value(true).hide_value().field_on_all();
38 b.add_input<decl::String>("Name").is_attribute_name().hide_label();
39
40 if (node != nullptr) {
41 const NodeGeometryStoreNamedAttribute &storage = node_storage(*node);
42 const eCustomDataType data_type = eCustomDataType(storage.data_type);
43 b.add_input(data_type, "Value").field_on_all();
44 }
45}
46
47static void node_layout(uiLayout *layout, bContext * /*C*/, PointerRNA *ptr)
48{
49 uiLayoutSetPropSep(layout, true);
50 uiLayoutSetPropDecorate(layout, false);
51 layout->prop(ptr, "data_type", UI_ITEM_NONE, "", ICON_NONE);
52 layout->prop(ptr, "domain", UI_ITEM_NONE, "", ICON_NONE);
53}
54
55static void node_init(bNodeTree * /*tree*/, bNode *node)
56{
58 data->data_type = CD_PROP_FLOAT;
59 data->domain = int8_t(AttrDomain::Point);
60 node->storage = data;
61}
62
64{
65 const NodeDeclaration &declaration = *params.node_type().static_declaration;
68
69 if (params.in_out() == SOCK_IN) {
70 const std::optional<eCustomDataType> type = bke::socket_type_to_custom_data_type(
71 eNodeSocketDatatype(params.other_socket().type));
72 if (type && *type != CD_PROP_STRING) {
73 /* The input and output sockets have the same name. */
74 params.add_item(IFACE_("Value"), [type](LinkSearchOpParams &params) {
75 bNode &node = params.add_node("GeometryNodeStoreNamedAttribute");
76 node_storage(node).data_type = *type;
77 params.update_and_connect_available_socket(node, "Value");
78 });
79 }
80 }
81}
82
84{
85 GeometrySet geometry_set = params.extract_input<GeometrySet>("Geometry");
86 const std::string name = params.extract_input<std::string>("Name");
87
88 if (name.empty()) {
89 params.set_output("Geometry", std::move(geometry_set));
90 return;
91 }
94 params.set_output("Geometry", std::move(geometry_set));
95 return;
96 }
98 params.error_message_add(NodeWarningType::Info,
99 TIP_("Anonymous attributes can't be created here"));
100 params.set_output("Geometry", std::move(geometry_set));
101 return;
102 }
103
104 params.used_named_attribute(name, NamedAttributeUsage::Write);
105
106 const NodeGeometryStoreNamedAttribute &storage = node_storage(params.node());
107 const eCustomDataType data_type = eCustomDataType(storage.data_type);
108 const AttrDomain domain = AttrDomain(storage.domain);
109
110 const Field<bool> selection = params.extract_input<Field<bool>>("Selection");
111
112 GField field = params.extract_input<GField>("Value");
115 std::move(field), *bke::custom_data_type_to_cpp_type(data_type));
116 }
117
118 std::atomic<bool> failure = false;
119
120 /* Run on the instances component separately to only affect the top level of instances. */
121 if (domain == AttrDomain::Instance) {
122 if (geometry_set.has_instances()) {
123 GeometryComponent &component = geometry_set.get_component_for_write(
124 GeometryComponent::Type::Instance);
125
126 if (name == "position" && data_type == CD_PROP_FLOAT3) {
127 /* Special case for "position" which is no longer an attribute on instances. */
128 bke::Instances &instances = *geometry_set.get_instances_for_write();
129 bke::InstancesFieldContext context(instances);
130 fn::FieldEvaluator evaluator{context, instances.instances_num()};
131 evaluator.set_selection(selection);
133 evaluator.evaluate();
134 }
135 else {
136 if (!bke::try_capture_field_on_geometry(component, name, domain, selection, field)) {
137 if (component.attribute_domain_size(domain) != 0) {
138 failure.store(true);
139 }
140 }
141 }
142 }
143 }
144 else {
145 geometry_set.modify_geometry_sets([&](GeometrySet &geometry_set) {
146 for (const GeometryComponent::Type type : {GeometryComponent::Type::Mesh,
147 GeometryComponent::Type::PointCloud,
148 GeometryComponent::Type::Curve,
149 GeometryComponent::Type::GreasePencil})
150 {
151 if (geometry_set.has(type)) {
152 GeometryComponent &component = geometry_set.get_component_for_write(type);
153 if (bke::try_capture_field_on_geometry(component, name, domain, selection, field)) {
154 if (component.type() == GeometryComponent::Type::Mesh) {
155 Mesh &mesh = *geometry_set.get_mesh_for_write();
156 bke::mesh_ensure_default_color_attribute_on_add(mesh, name, domain, data_type);
157 }
158 }
159 else if (component.attribute_domain_size(domain) != 0) {
160 failure.store(true);
161 }
162 }
163 }
164 });
165 }
166
167 if (failure) {
168 const char *domain_name = nullptr;
170 const char *type_name = nullptr;
172 const std::string message = fmt::format(
173 fmt::runtime(
174 TIP_("Failed to write to attribute \"{}\" with domain \"{}\" and type \"{}\"")),
175 name,
176 TIP_(domain_name),
177 TIP_(type_name));
178 params.error_message_add(NodeWarningType::Warning, message);
179 }
180
181 params.set_output("Geometry", std::move(geometry_set));
182}
183
184static void node_rna(StructRNA *srna)
185{
187 srna,
188 "data_type",
189 "Data Type",
190 "Type of data stored in attribute",
194 [](bContext * /*C*/, PointerRNA * /*ptr*/, PropertyRNA * /*prop*/, bool *r_free) {
195 *r_free = true;
198 });
199
201 "domain",
202 "Domain",
203 "Which domain to store the data in",
206 int(AttrDomain::Point));
207}
208
209static void node_register()
210{
211 static blender::bke::bNodeType ntype;
212
213 geo_node_type_base(&ntype, "GeometryNodeStoreNamedAttribute", GEO_NODE_STORE_NAMED_ATTRIBUTE);
214 ntype.ui_name = "Store Named Attribute";
215 ntype.ui_description =
216 "Store the result of a field on a geometry as an attribute with the specified name";
217 ntype.enum_name_legacy = "STORE_NAMED_ATTRIBUTE";
220 "NodeGeometryStoreNamedAttribute",
223 blender::bke::node_type_size(ntype, 140, 100, 700);
224 ntype.initfunc = node_init;
225 ntype.declare = node_declare;
230
231 node_rna(ntype.rna_ext.srna);
232}
234
235} // namespace blender::nodes::node_geo_store_named_attribute_cc
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1215
#define NODE_CLASS_ATTRIBUTE
Definition BKE_node.hh:448
#define GEO_NODE_STORE_NAMED_ATTRIBUTE
#define ELEM(...)
#define TIP_(msgid)
#define IFACE_(msgid)
@ CD_PROP_BYTE_COLOR
@ CD_PROP_FLOAT
@ CD_PROP_FLOAT3
@ CD_PROP_FLOAT2
@ CD_PROP_STRING
@ SOCK_IN
eNodeSocketDatatype
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_storage_enum_accessors(member)
void uiLayoutSetPropSep(uiLayout *layout, bool is_sep)
#define UI_ITEM_NONE
void uiLayoutSetPropDecorate(uiLayout *layout, bool is_sep)
BMesh const char void * data
GVArray try_convert(GVArray varray, const CPPType &to_type) const
int attribute_domain_size(AttrDomain domain) const
int instances_num() const
Definition instances.cc:398
void set_selection(Field< bool > selection)
Definition FN_field.hh:383
int add_with_destination(GField field, GVMutableArray dst)
Definition field.cc:738
Vector< SocketDeclaration * > inputs
Vector< SocketDeclaration * > outputs
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)
const DataTypeConversions & get_implicit_type_conversions()
bool allow_procedural_attribute_access(StringRef attribute_name)
void node_type_size(bNodeType &ntype, int width, int minwidth, int maxwidth)
Definition node.cc:5573
void node_register_type(bNodeType &ntype)
Definition node.cc:2748
const char * no_procedural_access_message
void mesh_ensure_default_color_attribute_on_add(Mesh &mesh, StringRef id, AttrDomain domain, eCustomDataType data_type)
bool try_capture_field_on_geometry(MutableAttributeAccessor attributes, const fn::FieldContext &field_context, const StringRef attribute_id, AttrDomain domain, const fn::Field< bool > &selection, const fn::GField &field)
const CPPType * custom_data_type_to_cpp_type(eCustomDataType type)
VMutableArray< float3 > instance_position_varray_for_write(Instances &instances)
Definition instances.cc:534
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:5603
std::optional< eCustomDataType > socket_type_to_custom_data_type(eNodeSocketDatatype type)
Definition node.cc:5355
bool generic_attribute_type_supported(const EnumPropertyItem &item)
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)
const EnumPropertyItem * enum_items_filter(const EnumPropertyItem *original_item_array, FunctionRef< bool(const EnumPropertyItem &item)> fn)
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
bool RNA_enum_name_from_value(const EnumPropertyItem *item, int value, const char **r_name)
const EnumPropertyItem rna_enum_attribute_domain_items[]
const EnumPropertyItem rna_enum_attribute_type_items[]
StructRNA * srna
Definition RNA_types.hh:909
void * storage
GeometryComponent & get_component_for_write(GeometryComponent::Type component_type)
Instances * get_instances_for_write()
bool has(const GeometryComponent::Type component_type) const
void modify_geometry_sets(ForeachSubGeometryCallback callback)
Defines a node type.
Definition BKE_node.hh:226
std::string ui_description
Definition BKE_node.hh:232
void(* initfunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:277
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:347
const char * enum_name_legacy
Definition BKE_node.hh:235
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:247
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:371
NodeDeclareFunction declare
Definition BKE_node.hh:355
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:4226