Blender V5.3
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
8#include "UI_resources.hh"
9
10#include "RNA_access.hh"
11#include "RNA_enum_types.hh"
12
14#include "BKE_grease_pencil.hh"
15#include "BKE_instances.hh"
16#include "BKE_mesh.hh"
18
19#include "NOD_rna_define.hh"
21
23
24#include "node_geometry_util.hh"
25
26#include <fmt/format.h>
27
29
31
33{
34 b.use_custom_socket_order();
35 b.allow_any_socket_order();
36 b.add_default_layout();
37 const bNode *node = b.node_or_null();
38
39 b.add_input<decl::Geometry>("Geometry"_ustr)
40 .description("Geometry to store a new attribute with the given name on");
41 b.add_output<decl::Geometry>("Geometry"_ustr).propagate_all_geometry().align_with_previous();
42 b.add_input<decl::Bool>("Selection"_ustr)
43 .default_value(true)
44 .hide_value()
45 .evaluated_geometry_field();
46 b.add_input<decl::String>("Name"_ustr).is_attribute_name().optional_label();
47
48 if (node != nullptr) {
49 const NodeGeometryStoreNamedAttribute &storage = node_storage(*node);
50 const eCustomDataType data_type = eCustomDataType(storage.data_type);
51 b.add_input(data_type, "Value"_ustr).evaluated_geometry_field();
52 }
53}
54
55static void node_layout(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
56{
57 layout.use_property_split_set(true);
58 layout.use_property_decorate_set(false);
59 layout.prop(ptr, "data_type", UI_ITEM_NONE, "", ICON_NONE);
60 layout.prop(ptr, "domain", UI_ITEM_NONE, "", ICON_NONE);
61}
62
63static void node_init(bNodeTree * /*tree*/, bNode *node)
64{
65 NodeGeometryStoreNamedAttribute *data = MEM_new<NodeGeometryStoreNamedAttribute>(__func__);
66 data->data_type = CD_PROP_FLOAT;
67 data->domain = int8_t(AttrDomain::Point);
68 node->storage = data;
69}
70
72{
73 const NodeDeclaration &declaration = *params.node_type().static_declaration;
74 search_link_ops_for_declarations(params, declaration.inputs);
75 search_link_ops_for_declarations(params, declaration.outputs);
76
77 if (params.in_out() == SOCK_IN) {
78 const std::optional<eCustomDataType> type = bke::socket_type_to_custom_data_type(
79 params.other_socket().type);
80 if (type && *type != CD_PROP_STRING) {
81 /* The input and output sockets have the same name. */
82 params.add_item(IFACE_("Value"), [type](LinkSearchOpParams &params) {
83 bNode &node = params.add_node("GeometryNodeStoreNamedAttribute"_ustr);
84 node_storage(node).data_type = *type;
85 params.update_and_connect_available_socket(node, "Value"_ustr);
86 });
87 }
88 }
89}
90
91/* Need to tag the drawings because it's not handled by try_capture_field_on_geometry
92 * automatically yet.*/
93static void grease_pencil_add_missing_update_tags(const std::string &name,
94 GeometrySet &geometry_set)
95{
97 if (name == "fill_id") {
98 for (GreasePencilDrawingBase *base : grease_pencil.drawings()) {
99 if (base->type != GP_DRAWING) {
100 continue;
101 }
102 bke::greasepencil::Drawing &drawing = reinterpret_cast<GreasePencilDrawing *>(base)->wrap();
103 drawing.tag_fills_changed();
104 }
105 }
106 else if (ELEM(name, "uv_rotation", "uv_translation", "uv_scale", "uv_shear")) {
107 for (GreasePencilDrawingBase *base : grease_pencil.drawings()) {
108 if (base->type != GP_DRAWING) {
109 continue;
110 }
111 bke::greasepencil::Drawing &drawing = reinterpret_cast<GreasePencilDrawing *>(base)->wrap();
113 }
114 }
115}
116
118{
119 GeometrySet geometry_set = params.extract_input<GeometrySet>("Geometry"_ustr);
120 const std::string name = params.extract_input<std::string>("Name"_ustr);
121
122 if (name.empty()) {
123 params.set_output("Geometry"_ustr, std::move(geometry_set));
124 return;
125 }
128 params.set_output("Geometry"_ustr, std::move(geometry_set));
129 return;
130 }
133 TIP_("Anonymous attributes cannot be created here"));
134 params.set_output("Geometry"_ustr, std::move(geometry_set));
135 return;
136 }
137
139
140 const NodeGeometryStoreNamedAttribute &storage = node_storage(params.node());
141 const eCustomDataType cd_type = eCustomDataType(storage.data_type);
142 const bke::AttrType data_type = *bke::custom_data_type_to_attr_type(cd_type);
143 const AttrDomain domain = AttrDomain(storage.domain);
144
145 const Field<bool> selection = params.extract_input<Field<bool>>("Selection"_ustr);
146
147 GField field = params.extract_input<GField>("Value"_ustr);
148 if (ELEM(data_type,
153 {
155 std::move(field), bke::attribute_type_to_cpp_type(data_type));
156 }
157
158 std::atomic<bool> failure = false;
159
160 /* Run on the instances component separately to only affect the top level of instances. */
161 if (domain == AttrDomain::Instance) {
162 if (geometry_set.has_instances()) {
163 GeometryComponent &component = geometry_set.get_component_for_write(
165
166 if (name == "position" && data_type == bke::AttrType::Float3) {
167 /* Special case for "position" which is no longer an attribute on instances. */
168 bke::Instances &instances = *geometry_set.get_instances_for_write();
169 bke::InstancesFieldContext context(instances);
170 fn::FieldEvaluator evaluator{context, instances.instances_num()};
171 evaluator.set_selection(selection);
173 evaluator.evaluate();
174 }
175 else {
176 if (!bke::try_capture_field_on_geometry(component, name, domain, selection, field)) {
177 if (component.attribute_domain_size(domain) != 0) {
178 failure.store(true);
179 }
180 }
181 }
182 }
183 }
184 else {
185 geometry::foreach_real_geometry(geometry_set, [&](GeometrySet &geometry_set) {
190 {
191 if (geometry_set.has(type)) {
192 GeometryComponent &component = geometry_set.get_component_for_write(type);
193 if (bke::try_capture_field_on_geometry(component, name, domain, selection, field)) {
194 if (component.type() == GeometryComponent::Type::Mesh) {
195 Mesh &mesh = *geometry_set.get_mesh_for_write();
197 bke::mesh_ensure_default_uv_attribute_on_add(mesh, name, domain, data_type);
198 }
199 if (component.type() == GeometryComponent::Type::GreasePencil) {
200 grease_pencil_add_missing_update_tags(name, geometry_set);
201 }
202 }
203 else if (component.attribute_domain_size(domain) != 0) {
204 failure.store(true);
205 }
206 }
207 }
208 });
209 }
210
211 if (failure) {
212 const char *domain_name = nullptr;
214 const char *type_name = nullptr;
216 const std::string message = fmt::format(
217 fmt::runtime(
218 RPT_("Failed to write to attribute \"{}\" with domain \"{}\" and type \"{}\"")),
219 name,
220 RPT_(domain_name),
221 RPT_(type_name));
223 }
224
225 params.set_output("Geometry"_ustr, std::move(geometry_set));
226}
227
228static void node_rna(StructRNA *srna)
229{
231 srna,
232 "data_type",
233 "Data Type",
234 "Type of data stored in attribute",
238 [](bContext * /*C*/, PointerRNA * /*ptr*/, PropertyRNA * /*prop*/, bool *r_free) {
239 *r_free = true;
242 });
243
245 "domain",
246 "Domain",
247 "Which domain to store the data in",
250 int(AttrDomain::Point));
251}
252
253static void node_register()
254{
255 static bke::bNodeType ntype;
256
258 &ntype, "GeometryNodeStoreNamedAttribute"_ustr, GEO_NODE_STORE_NAMED_ATTRIBUTE);
259 ntype.ui_name = "Store Named Attribute";
260 ntype.ui_description =
261 "Store the result of a field on a geometry as an attribute with the specified name";
262 ntype.enum_name_legacy = "STORE_NAMED_ATTRIBUTE";
265 "NodeGeometryStoreNamedAttribute",
268 ntype.initfunc = node_init;
269 ntype.declare = node_declare;
275
276 node_rna(ntype.rna_ext.srna);
277}
279
280} // namespace blender::nodes::node_geo_store_named_attribute_cc
Low-level operations for grease pencil.
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1353
#define NODE_CLASS_ATTRIBUTE
Definition BKE_node.hh:493
#define GEO_NODE_STORE_NAMED_ATTRIBUTE
#define RPT_(msgid)
#define TIP_(msgid)
#define IFACE_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_storage_enum_accessors(member)
#define UI_ITEM_NONE
#define ELEM(...)
GVArray try_convert(GVArray varray, const CPPType &to_type) const
int attribute_domain_size(AttrDomain domain) const
int instances_num() const
Definition instances.cc:408
void set_selection(Field< bool > selection)
int add_with_destination(GField field, GVMutableArray dst)
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
Vector< SocketDeclaration * > outputs
#define storage(slot, qualifiers)
bool attribute_name_is_anonymous(const StringRef name)
bool try_capture_field_on_geometry(MutableAttributeAccessor attributes, const fn::FieldContext &field_context, const StringRef name, AttrDomain domain, const fn::Field< bool > &selection, const fn::GField &field)
const DataTypeConversions & get_implicit_type_conversions()
bool allow_procedural_attribute_access(StringRef attribute_name)
std::optional< AttrType > custom_data_type_to_attr_type(eCustomDataType data_type)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
const char * no_procedural_access_message
void mesh_ensure_default_uv_attribute_on_add(Mesh &mesh, StringRef name, AttrDomain domain, bke::AttrType data_type)
void mesh_ensure_default_color_attribute_on_add(Mesh &mesh, StringRef name, AttrDomain domain, bke::AttrType data_type)
VMutableArray< float3 > instance_position_varray_for_write(Instances &instances)
Definition instances.cc:550
const CPPType & attribute_type_to_cpp_type(AttrType 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:6166
std::optional< eCustomDataType > socket_type_to_custom_data_type(eNodeSocketDatatype type)
Definition node.cc:5911
void foreach_real_geometry(bke::GeometrySet &geometry, FunctionRef< void(bke::GeometrySet &geometry_set)> fn)
BLI_INLINE Texture * wrap(MTLTexture *texture)
bool generic_attribute_type_supported(const EnumPropertyItem &item)
static void node_gather_link_searches(GatherLinkSearchOpParams &params)
static void node_layout(ui::Layout &layout, bContext *, PointerRNA *ptr)
static void grease_pencil_add_missing_update_tags(const std::string &name, GeometrySet &geometry_set)
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)
bool RNA_enum_name_from_value(const EnumPropertyItem *item, int value, const char **r_name)
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)
const EnumPropertyItem rna_enum_attribute_domain_items[]
signed char int8_t
Definition BKE_text.h:76
eNodeSocketDatatype type
GeometryComponent & get_component_for_write(GeometryComponent::Type component_type)
Instances * get_instances_for_write()
bool has(const GeometryComponent::Type component_type) const
GreasePencil * get_grease_pencil_for_write()
static constexpr int _160
Definition BKE_node.hh:246
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 use_property_decorate_set(bool is_decorate)
void use_property_split_set(bool is_sep)
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)