Blender V5.3
node_socket_value_iter.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BKE_curves.hh"
10#include "BKE_grease_pencil.hh"
11#include "BKE_instances.hh"
14
16
20
22
24 const VisitParams &params_;
25
26 public:
27 RecursiveVisitor(const VisitParams &visitors) : params_(visitors) {}
28
30 {
31 if (value.is_single()) {
32 const GPointer value_ptr = value.get();
33 if (this->check_GPointer(value_ptr)) {
34 return true;
35 }
36 }
37 else if (value.is_field()) {
38 const fn::GField &field = *value.get_if<fn::GField>();
39 if (this->check_GField(field)) {
40 return true;
41 }
42 }
43 else if (value.is_list()) {
44 const nodes::GListPtr &list = *value.get_if<nodes::GListPtr>();
45 if (list) {
46 if (this->check_GList(*list)) {
47 return true;
48 }
49 }
50 }
51 return false;
52 }
53
55 {
56 if (value.is_single()) {
57 GMutablePointer value_ptr = value.get();
58 this->edit_GPointer(value_ptr);
59 return;
60 }
61 if (value.is_field()) {
62 fn::GField field = value.extract<fn::GField>();
63 this->edit_GField(field);
64 value.emplace<fn::GField>(std::move(field));
65 return;
66 }
67 if (value.is_list()) {
69 if (list) {
70 this->edit_GList(list.get_for_write());
71 }
72 value.emplace<nodes::GListPtr>(std::move(list));
73 return;
74 }
75 }
76
77 bool check_GPointer(const GPointer value_ptr)
78 {
79 const CPPType &type = *value_ptr.type();
80 if (type.is<GeometrySet>()) {
81 return this->check_GeometrySet(*value_ptr.get<GeometrySet>());
82 }
83 if (type.is<nodes::BundlePtr>()) {
84 const nodes::BundlePtr &bundle_ptr = *value_ptr.get<nodes::BundlePtr>();
85 if (bundle_ptr) {
86 return this->check_Bundle(*bundle_ptr);
87 }
88 return false;
89 }
90 if (type.is<nodes::ClosurePtr>()) {
91 if (params_.check_non_editable) {
92 const nodes::ClosurePtr &closure_ptr = *value_ptr.get<nodes::ClosurePtr>();
93 if (closure_ptr) {
94 return this->check_Closure(*closure_ptr);
95 }
96 }
97 return false;
98 }
99 return false;
100 }
101
103 {
104 const CPPType &type = *value.type();
105 if (type.is<GeometrySet>()) {
106 this->edit_GeometrySet(*value.get<GeometrySet>());
107 return;
108 }
109 if (type.is<nodes::BundlePtr>()) {
110 nodes::BundlePtr &bundle_ptr = *value.get<nodes::BundlePtr>();
111 if (bundle_ptr) {
112 nodes::Bundle &bundle = bundle_ptr.ensure_mutable_inplace();
113 this->edit_Bundle(bundle);
114 }
115 return;
116 }
117 if (type.is<nodes::ClosurePtr>()) {
118 /* Can't edit closures. */
119 return;
120 }
121 }
122
123 bool check_GeometrySet(const GeometrySet &geometry_set)
124 {
132 {
133 if (!geometry_set.has(type)) {
134 continue;
135 }
136 const GeometryComponent &component = *geometry_set.get_component(type);
137 if (this->check_GeometryComponent(component)) {
138 return true;
139 }
140 }
141 if (geometry_set.has_bundle()) {
142 if (this->check_Bundle(*geometry_set.bundle())) {
143 return true;
144 }
145 }
146 return false;
147 }
148
149 void edit_GeometrySet(GeometrySet &geometry_set)
150 {
151 if (geometry_set.has_bundle()) {
152 if (this->check_Bundle(*geometry_set.bundle())) {
153 this->edit_Bundle(geometry_set.bundle_for_write());
154 }
155 }
163 {
164 if (!geometry_set.has(type)) {
165 continue;
166 }
167 {
168 const GeometryComponent &component = *geometry_set.get_component(type);
169 if (!this->check_GeometryComponent(component)) {
170 continue;
171 }
172 }
173 GeometryComponent &component = geometry_set.get_component_for_write(type);
174 this->edit_GeometryComponent(component);
175 }
176 }
177
179 {
180 if (const std::optional<bke::AttributeAccessor> attributes = component.attributes()) {
181 if (this->check_AttributeAccessor(*attributes)) {
182 return true;
183 }
184 }
185 if (component.type() == GeometryComponent::Type::Instance) {
186 const InstancesComponent &instance_component = static_cast<const InstancesComponent &>(
187 component);
188 if (const Instances *instances = instance_component.get()) {
189 for (const InstanceReference &reference : instances->references()) {
190 if (this->check_InstanceReference(reference)) {
191 return true;
192 }
193 }
194 }
195 }
196 if (component.type() == GeometryComponent::Type::GreasePencil) {
197 const GreasePencilComponent &grease_pencil_component =
198 static_cast<const GreasePencilComponent &>(component);
199 if (const GreasePencil *grease_pencil = grease_pencil_component.get()) {
200 for (const greasepencil::Layer *layer : grease_pencil->layers()) {
201 if (this->check_Layer(*grease_pencil, *layer)) {
202 return true;
203 }
204 }
205 }
206 }
207 return false;
208 }
209
211 {
212 if (std::optional<bke::MutableAttributeAccessor> attributes = component.attributes_for_write())
213 {
214 this->edit_AttributeAccessor(*attributes);
215 }
216 if (component.type() == GeometryComponent::Type::Instance) {
217 InstancesComponent &instance_component = static_cast<InstancesComponent &>(component);
218 if (Instances *instances = instance_component.get_for_write()) {
219 for (InstanceReference &reference : instances->references_for_write()) {
220 this->edit_InstanceReference(reference);
221 }
222 }
223 }
224 if (component.type() == GeometryComponent::Type::GreasePencil) {
225 GreasePencilComponent &grease_pencil_component = static_cast<GreasePencilComponent &>(
226 component);
227 if (GreasePencil *grease_pencil = grease_pencil_component.get_for_write()) {
228 for (greasepencil::Layer *layer : grease_pencil->layers_for_write()) {
230 }
231 }
232 }
233 }
234
236 {
237 if (params_.check_AttributeAccessor) {
238 if (params_.check_AttributeAccessor(accessor)) {
239 return true;
240 }
241 }
242 return false;
243 }
244
246 {
247 if (params_.edit_AttributeAccessor) {
248 params_.edit_AttributeAccessor(accessor);
249 }
250 }
251
253 {
254 if (const greasepencil::Drawing *drawing = grease_pencil.get_eval_drawing(layer)) {
255 const CurvesGeometry &curves = drawing->strokes();
256 if (this->check_AttributeAccessor(curves.attributes())) {
257 return true;
258 }
259 }
260 return false;
261 }
262
264 {
265 if (greasepencil::Drawing *drawing = grease_pencil.get_eval_drawing(layer)) {
266 CurvesGeometry &curves = drawing->strokes_for_write();
267 MutableAttributeAccessor attributes = curves.attributes_for_write();
268 this->edit_AttributeAccessor(attributes);
269 }
270 }
271
273 {
275 if (this->check_GeometrySet(reference.geometry_set())) {
276 return true;
277 }
278 return false;
279 }
280 if (!params_.ignore_non_geometry_instances) {
281 GeometrySet geometry_set;
282 reference.to_geometry_set(geometry_set);
283 if (this->check_GeometrySet(geometry_set)) {
284 return true;
285 }
286 }
287 return false;
288 }
289
291 {
293 this->edit_GeometrySet(reference.geometry_set());
294 }
295 }
296
297 bool check_Bundle(const nodes::Bundle &bundle)
298 {
299 for (const auto &item : bundle.items()) {
300 if (const auto *socket_value = std::get_if<nodes::BundleItemSocketValue>(&item.value.value))
301 {
302 if (this->check_SocketValueVariant(socket_value->value)) {
303 return true;
304 }
305 }
306 }
307 return false;
308 }
309
311 {
312 for (auto &&item : bundle.items()) {
313 if (auto *socket_value = std::get_if<nodes::BundleItemSocketValue>(&item.value.value)) {
314 this->edit_SocketValueVariant(socket_value->value);
315 }
316 }
317 }
318
319 bool check_Closure(const nodes::Closure &closure)
320 {
321 for (const SocketValueVariant *value : closure.captured_values()) {
322 if (this->check_SocketValueVariant(*value)) {
323 return true;
324 }
325 }
326 for (const SocketValueVariant &default_value : closure.default_input_values()) {
327 if (this->check_SocketValueVariant(default_value)) {
328 return true;
329 }
330 }
331 return false;
332 }
333
334 bool check_GField(const fn::GField &field)
335 {
336 if (params_.check_GField) {
337 if (params_.check_GField(field)) {
338 return true;
339 }
340 }
341 return false;
342 }
343
345 {
346 if (params_.edit_GField) {
347 params_.edit_GField(field);
348 }
349 }
350
352 {
353 const CPPType &type = list.cpp_type();
354 if (type.is<SocketValueVariant>()) {
355 bool need_edit = false;
356 list.typed<SocketValueVariant>().foreach([&](const SocketValueVariant &value_variant) {
357 if (!need_edit) {
358 need_edit = this->check_SocketValueVariant(value_variant);
359 }
360 });
361 return need_edit;
362 }
363 if (type.is<GeometrySet>()) {
364 bool need_edit = false;
365 list.typed<GeometrySet>().foreach([&](const GeometrySet &geometry) {
366 if (!need_edit) {
367 need_edit = this->check_GeometrySet(geometry);
368 }
369 });
370 return need_edit;
371 }
372 if (type.is<nodes::BundlePtr>()) {
373 bool need_edit = false;
374 list.typed<nodes::BundlePtr>().foreach([&](const nodes::BundlePtr &bundle_ptr) {
375 if (!bundle_ptr) {
376 return;
377 }
378 if (!need_edit) {
379 need_edit = this->check_Bundle(*bundle_ptr);
380 }
381 });
382 return need_edit;
383 }
384 if (type.is<nodes::ClosurePtr>()) {
385 bool need_edit = false;
386 list.typed<nodes::ClosurePtr>().foreach([&](const nodes::ClosurePtr &closure_ptr) {
387 if (!closure_ptr) {
388 return;
389 }
390 if (!need_edit) {
391 need_edit = this->check_Closure(*closure_ptr);
392 }
393 });
394 return need_edit;
395 }
396 return false;
397 }
398
400 {
401 const CPPType &type = list.cpp_type();
402 if (type.is<SocketValueVariant>()) {
403 list.typed<SocketValueVariant>().foreach_for_write([&](SocketValueVariant &value_variant) {
404 this->edit_SocketValueVariant(value_variant);
405 });
406 }
407 else if (type.is<GeometrySet>()) {
408 list.typed<GeometrySet>().foreach_for_write(
410 }
411 else if (type.is<nodes::BundlePtr>()) {
412 list.typed<nodes::BundlePtr>().foreach_for_write([&](nodes::BundlePtr &bundle_ptr) {
413 if (bundle_ptr) {
414 this->edit_Bundle(bundle_ptr.ensure_mutable_inplace());
415 }
416 });
417 }
418 }
419};
420
422{
423 RecursiveVisitor visitor{params};
424 visitor.edit_SocketValueVariant(value);
425}
426
427void edit_recursive(GeometrySet &value, const VisitParams &params)
428{
429 RecursiveVisitor visitor{params};
430 visitor.edit_GeometrySet(value);
431}
432
433void check_recursive(const SocketValueVariant &value, const VisitParams &params)
434{
435 RecursiveVisitor visitor{params};
436 visitor.check_SocketValueVariant(value);
437}
438
439void check_recursive(const GeometrySet &value, const VisitParams &params)
440{
441 RecursiveVisitor visitor{params};
442 visitor.check_GeometrySet(value);
443}
444
445} // namespace blender::bke::socket_value_visitor
Low-level operations for curves.
Low-level operations for grease pencil.
bool is() const
const CPPType * type() const
const CPPType * type() const
const void * get() const
virtual std::optional< AttributeAccessor > attributes() const
virtual std::optional< MutableAttributeAccessor > attributes_for_write()
void to_geometry_set(GeometrySet &r_geometry_set) const
Definition instances.cc:115
void edit_InstanceReference(bke::InstanceReference &reference)
bool check_AttributeAccessor(const bke::AttributeAccessor &accessor)
bool check_GeometryComponent(const GeometryComponent &component)
void edit_Layer(GreasePencil &grease_pencil, greasepencil::Layer &layer)
void edit_AttributeAccessor(bke::MutableAttributeAccessor &accessor)
bool check_Layer(const GreasePencil &grease_pencil, const greasepencil::Layer &layer)
bool check_InstanceReference(const bke::InstanceReference &reference)
bool check_SocketValueVariant(const SocketValueVariant &value)
BundleItemMap::ItemIterator items() const
Span< bke::SocketValueVariant > default_input_values() const
Span< const bke::SocketValueVariant * > captured_values() const
#define layer
void edit_recursive(SocketValueVariant &value, const VisitParams &params)
void check_recursive(const SocketValueVariant &value, const VisitParams &params)
ImplicitSharingPtr< Closure > ClosurePtr
ImplicitSharingPtr< Bundle > BundlePtr
GeometryComponent & get_component_for_write(GeometryComponent::Type component_type)
nodes::Bundle & bundle_for_write()
bool has(const GeometryComponent::Type component_type) const
const nodes::Bundle * bundle() const
const GeometryComponent * get_component(GeometryComponent::Type component_type) const