Blender V5.3
node_geo_attribute_capture.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#include "UI_resources.hh"
7
13
14#include "RNA_enum_types.hh"
15
16#include "BLO_read_write.hh"
17
18#include "BKE_library.hh"
19#include "BKE_screen.hh"
20
22
23#include "node_geometry_util.hh"
24
25namespace blender {
26
28
30
32{
33 const bNodeTree *tree = b.tree_or_null();
34 const bNode *node = b.node_or_null();
35 b.use_custom_socket_order();
36 b.allow_any_socket_order();
37
38 b.add_default_layout();
39
40 b.add_input<decl::Geometry>("Geometry"_ustr)
42 "Geometry to evaluate the given fields and store the resulting attributes on. All "
43 "geometry types except volumes are supported");
44 b.add_output<decl::Geometry>("Geometry"_ustr).propagate_all_geometry().align_with_previous();
45 b.add_input<decl::Bool>("Selection"_ustr)
46 .default_value(true)
47 .hide_value()
48 .evaluated_geometry_field();
49 b.add_output<decl::Bool>("Selection"_ustr).anonymous_attribute_output().align_with_previous();
50 if (node != nullptr) {
51 const NodeGeometryAttributeCapture &storage = node_storage(*node);
52 for (const NodeGeometryAttributeCaptureItem &item :
53 Span(storage.capture_items, storage.capture_items_num))
54 {
55 const UString name(item.name);
56 const eCustomDataType data_type = eCustomDataType(item.data_type);
57 const UString input_identifier(
59 const UString output_identifier(
61 b.add_input(data_type, name, input_identifier)
62 .evaluated_geometry_field()
63 .socket_name_ptr(&tree->id, *CaptureAttributeItemsAccessor::item_srna, &item, "name");
64 b.add_output(data_type, name, output_identifier)
65 .anonymous_attribute_output()
66 .align_with_previous();
67 }
68 }
69 b.add_input<decl::Extend>(""_ustr, "__extend__"_ustr)
70 .structure_type(StructureType::Field)
71 .custom_draw(socket_items::ui::draw_extend_socket_fn<CaptureAttributeItemsAccessor>());
72 b.add_output<decl::Extend>(""_ustr, "__extend__"_ustr)
73 .structure_type(StructureType::Field)
74 .align_with_previous();
75}
76
77static void node_layout(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
78{
79 layout.use_property_split_set(true);
80 layout.use_property_decorate_set(false);
81 layout.prop(ptr, "domain", UI_ITEM_NONE, "", ICON_NONE);
82}
83
84static void node_init(bNodeTree * /*tree*/, bNode *node)
85{
86 NodeGeometryAttributeCapture *data = MEM_new<NodeGeometryAttributeCapture>(__func__);
87 data->domain = int8_t(AttrDomain::Point);
88 node->storage = data;
89}
90
91static void node_layout_ex(ui::Layout &layout, bContext *C, PointerRNA *ptr)
92{
93 bNodeTree &tree = *reinterpret_cast<bNodeTree *>(ptr->owner_id);
94 bNode &node = *static_cast<bNode *>(ptr->data);
95
96 layout.prop(ptr, "domain", UI_ITEM_NONE, "", ICON_NONE);
97
98 if (ui::Layout *panel = layout.panel(
99 C, "capture_attribute_items", false, IFACE_("Capture Items")))
100 {
101 socket_items::ui::draw_items_list_with_operators<CaptureAttributeItemsAccessor>(
102 C, panel, tree, node);
103 socket_items::ui::draw_active_item_props<CaptureAttributeItemsAccessor>(
104 tree, node, [&](PointerRNA *item_ptr) {
105 panel->use_property_split_set(true);
106 panel->use_property_decorate_set(false);
107 panel->prop(item_ptr, "data_type", UI_ITEM_NONE, std::nullopt, ICON_NONE);
108 });
109 }
110}
111
116
117static void clean_unused_attributes(const AttributeFilter &attribute_filter,
118 const VectorSet<std::string> &keep,
119 GeometryComponent &component)
120{
121 std::optional<MutableAttributeAccessor> attributes = component.attributes_for_write();
122 if (!attributes.has_value()) {
123 return;
124 }
125
126 Vector<std::string> unused_ids;
127 attributes->foreach_attribute([&](const bke::AttributeIter &iter) {
129 return;
130 }
131 if (keep.contains(iter.name)) {
132 return;
133 }
134 if (!attribute_filter.allow_skip(iter.name)) {
135 return;
136 }
137 unused_ids.append(iter.name);
138 });
139
140 for (const std::string &unused_id : unused_ids) {
141 attributes->remove(unused_id);
142 }
143}
144
146{
147 GeometrySet geometry_set = params.extract_input<GeometrySet>("Geometry"_ustr);
148
149 if (!params.output_is_required("Geometry"_ustr)) {
150 params.error_message_add(
152 TIP_("The attribute output cannot be used without the geometry output"));
154 return;
155 }
156
157 const NodeGeometryAttributeCapture &storage = node_storage(params.node());
158 const AttrDomain domain = AttrDomain(storage.domain);
159 const Field<bool> selection = params.extract_input<Field<bool>>("Selection"_ustr);
160
161 Vector<GField> fields;
162 VectorSet<std::string> out_attribute_names;
163 for (const NodeGeometryAttributeCaptureItem &item :
164 Span{storage.capture_items, storage.capture_items_num})
165 {
166 const std::string input_identifier =
168 const std::string output_identifier =
170 std::optional<std::string> attribute_id = params.get_output_anonymous_attribute_id_if_needed(
171 UString(output_identifier));
172 if (!attribute_id) {
173 continue;
174 }
175 fields.append(params.extract_input<GField>(UString(input_identifier)));
176 out_attribute_names.add_new(std::move(*attribute_id));
177 }
178
179 {
180 std::optional<std::string> selection_attribute_id =
181 params.get_output_anonymous_attribute_id_if_needed("Selection"_ustr);
182 if (selection_attribute_id) {
183 fields.append(selection);
184 out_attribute_names.add_new(std::move(*selection_attribute_id));
185 }
186 }
187
188 if (fields.is_empty()) {
189 params.set_output("Geometry"_ustr, geometry_set);
191 return;
192 }
193
194 const Array<StringRef> out_attribute_name_refs = out_attribute_names.as_span();
195
196 const auto capture_on = [&](GeometryComponent &component) {
198 component, out_attribute_name_refs, domain, selection, fields);
199 /* Changing of the anonymous attributes may require removing attributes that are no longer
200 * needed. */
202 params.get_attribute_filter("Geometry"_ustr), out_attribute_names, component);
203 };
204
205 /* Run on the instances component separately to only affect the top level of instances. */
206 if (domain == AttrDomain::Instance) {
207 if (geometry_set.has_instances()) {
209 }
210 }
211 else {
216
217 geometry::foreach_real_geometry(geometry_set, [&](GeometrySet &geometry_set) {
218 for (const GeometryComponent::Type type : types) {
219 if (geometry_set.has(type)) {
220 capture_on(geometry_set.get_component_for_write(type));
221 }
222 }
223 });
224 }
225
226 params.set_output("Geometry"_ustr, geometry_set);
227}
228
234
236{
238 MEM_delete(reinterpret_cast<NodeGeometryAttributeCapture *>(node->storage));
239}
240
241static void node_copy_storage(bNodeTree * /*dst_tree*/, bNode *dst_node, const bNode *src_node)
242{
243 const NodeGeometryAttributeCapture &src_storage = node_storage(*src_node);
244 NodeGeometryAttributeCapture *dst_storage = MEM_new<NodeGeometryAttributeCapture>(
245 __func__, dna::shallow_copy(src_storage));
246 dst_node->storage = dst_storage;
247
249}
250
252{
253 const eNodeSocketDatatype type = params.other_socket().type;
254 if (type == SOCK_GEOMETRY) {
255 params.add_item(IFACE_("Geometry"), [](LinkSearchOpParams &params) {
256 bNode &node = params.add_node("GeometryNodeCaptureAttribute"_ustr);
257 params.connect_available_socket(node, "Geometry"_ustr);
258 });
259 }
261 return;
262 }
263
264 params.add_item(IFACE_("Value"), [type](LinkSearchOpParams &params) {
265 bNode &node = params.add_node("GeometryNodeCaptureAttribute"_ustr);
266 const auto *item =
268 params.node_tree, node, type, params.socket.name);
269 const std::string identifier =
270 params.socket.in_out == SOCK_IN ?
273 params.update_and_connect_available_socket_by_identifier(node, UString(identifier));
274 });
275}
276
278 const bNode &node,
279 const bNodeSocket &output_socket)
280{
281 return &node.input_socket(output_socket.index());
282}
283
284static void node_blend_write(const bNodeTree & /*tree*/, const bNode &node, BlendWriter &writer)
285{
287}
288
293
294static void node_register()
295{
296 static bke::bNodeType ntype;
297 geo_node_type_base(&ntype, "GeometryNodeCaptureAttribute"_ustr, GEO_NODE_CAPTURE_ATTRIBUTE);
298 ntype.ui_name = "Capture Attribute";
299 ntype.ui_description =
300 "Store the result of a field on a geometry and output the data as a node socket. Allows "
301 "remembering or interpolating data as the geometry changes, such as positions before "
302 "deformation";
303 ntype.enum_name_legacy = "CAPTURE_ATTRIBUTE";
306 ntype, "NodeGeometryAttributeCapture", node_free_storage, node_copy_storage);
307 ntype.initfunc = node_init;
308 ntype.declare = node_declare;
319}
321
322} // namespace nodes::node_geo_attribute_capture_cc
323
324namespace nodes {
325
326StructRNA **CaptureAttributeItemsAccessor::item_srna = &RNA_NodeGeometryCaptureAttributeItem;
327
329{
330 writer->write_string(item.name);
331}
332
337
338} // namespace nodes
339} // namespace blender
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1353
#define NODE_CLASS_ATTRIBUTE
Definition BKE_node.hh:493
#define GEO_NODE_CAPTURE_ATTRIBUTE
#define TIP_(msgid)
#define IFACE_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define C
Definition RandGen.cpp:29
#define UI_ITEM_NONE
constexpr Span()=default
UString()=default
bool contains(const Key &key) const
Span< Key > as_span() const
void add_new(const Key &key)
void append(const T &value)
virtual std::optional< MutableAttributeAccessor > attributes_for_write()
void add_item(std::string socket_name, SocketLinkOperation::LinkSocketFn fn, int weight=0)
void error_message_add(const NodeWarningType type, StringRef message) const
NodeAttributeFilter get_attribute_filter(const UString output_identifier) const
std::optional< std::string > get_output_anonymous_attribute_id_if_needed(const UString output_identifier, const bool force_create=false)
T extract_input(const UString identifier)
bool output_is_required(const UString identifier) const
void set_output(const UString identifier, T &&value)
void connect_available_socket(bNode &new_node, UString socket_name)
#define storage(slot, qualifiers)
bool attribute_name_is_anonymous(const StringRef name)
bool try_capture_fields_on_geometry(MutableAttributeAccessor attributes, const fn::FieldContext &field_context, Span< StringRef > names, AttrDomain domain, const fn::Field< bool > &selection, Span< fn::GField > fields)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
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
internal::ShallowDataConstRef< T > shallow_copy(const T &other)
Definition DNA_defs.h:147
void foreach_real_geometry(bke::GeometrySet &geometry, FunctionRef< void(bke::GeometrySet &geometry_set)> fn)
static void node_geo_exec(GeoNodeExecParams params)
static void node_declare(NodeDeclarationBuilder &b)
static void node_layout_ex(ui::Layout &layout, bContext *C, PointerRNA *ptr)
static void clean_unused_attributes(const AttributeFilter &attribute_filter, const VectorSet< std::string > &keep, GeometryComponent &component)
static void node_blend_read(bNodeTree &, bNode &node, BlendDataReader &reader)
static void node_blend_write(const bNodeTree &, const bNode &node, BlendWriter &writer)
static bool node_insert_link(bke::NodeInsertLinkParams &params)
static void node_gather_link_searches(GatherLinkSearchOpParams &params)
static const bNodeSocket * node_internally_linked_input(const bNodeTree &, const bNode &node, const bNodeSocket &output_socket)
static void node_layout(ui::Layout &layout, bContext *, PointerRNA *ptr)
static void node_copy_storage(bNodeTree *, bNode *dst_node, const bNode *src_node)
void blend_write(BlendWriter *writer, const bNode &node)
void blend_read_data(BlendDataReader *reader, bNode &node)
void copy_array(const bNode &src_node, bNode &dst_node)
Accessor::ItemT * add_item_with_socket_type_and_name(bNodeTree &ntree, bNode &node, const eNodeSocketDatatype socket_type, const char *name, std::optional< int > dimensions=std::nullopt)
bool try_add_item_via_any_extend_socket(bNodeTree &ntree, bNode &extend_node, bNode &storage_node, bNodeLink &link, const std::optional< StringRef > socket_identifier=std::nullopt, typename Accessor::ItemT **r_new_item=nullptr)
BMesh Iterators.
BMesh const char void * data
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
void BLO_read_string(BlendDataReader *reader, char **ptr_p)
Definition readfile.cc:6653
signed char int8_t
Definition BKE_text.h:76
void write_string(const char *data)
eNodeSocketDatatype type
eNodeTree_Type type
bool allow_skip(const StringRef name) const
GeometryComponent & get_component_for_write(GeometryComponent::Type component_type)
bool has(const GeometryComponent::Type component_type) const
Defines a node type.
Definition BKE_node.hh:265
NodeInternallyLinkedInputFunction internally_linked_input
Definition BKE_node.hh:415
void(*) draw_buttons(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:290
NodeBlendWriteFunction blend_write_storage_content
Definition BKE_node.hh:421
void(*) register_operators()
Definition BKE_node.hh:448
std::string ui_description
Definition BKE_node.hh:271
NodeBlendDataReadFunction blend_data_read_storage_content
Definition BKE_node.hh:422
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:385
void(*) draw_buttons_ex(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:292
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
bool(*) insert_link(NodeInsertLinkParams &params)
Definition BKE_node.hh:364
static std::string input_socket_identifier_for_item(const NodeGeometryAttributeCaptureItem &item)
static void blend_read_data_item(BlendDataReader *reader, ItemT &item)
static bool supports_socket_type(const eNodeSocketDatatype socket_type, const int)
static std::string output_socket_identifier_for_item(const NodeGeometryAttributeCaptureItem &item)
static void blend_write_item(BlendWriter *writer, const ItemT &item)
void use_property_decorate_set(bool is_decorate)
void use_property_split_set(bool is_sep)
PanelLayout panel(const bContext *C, StringRef idname, bool default_closed)
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)