Blender V5.3
depsgraph_query_iter.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2017 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11/* Silence warnings from copying deprecated fields. */
12#define DNA_DEPRECATED_ALLOW
13
14#include "BKE_duplilist.hh"
15#include "BKE_idprop.hh"
16#include "BKE_layer.hh"
17#include "BKE_modifier.hh"
18#include "BKE_node.hh"
19#include "BKE_object.hh"
20#include "BKE_object_types.hh"
21
22#include "BLI_listbase.hh"
23#include "BLI_math_matrix_c.hh"
24#include "BLI_math_vector_c.hh"
25#include "BLI_utildefines.hh"
26
27#include "DNA_object_types.h"
28#include "DNA_scene_types.h"
29
30#include "DEG_depsgraph.hh"
32
33#include "intern/depsgraph.hh"
35
36#ifndef NDEBUG
38#endif
39
40namespace blender {
41
42/* If defined, all working data will be set to an invalid state, helping
43 * to catch issues when areas accessing data which is considered to be no
44 * longer available. */
45#undef INVALIDATE_WORK_DATA
46
47#ifndef NDEBUG
48# define INVALIDATE_WORK_DATA
49#endif
50
53
54/* ************************ DEG ITERATORS ********************* */
55
56namespace {
57
58void deg_invalidate_iterator_work_data(DEGObjectIterData *data)
59{
60#ifdef INVALIDATE_WORK_DATA
61 BLI_assert(data != nullptr);
62 memset(static_cast<void *>(&data->temp_dupli_object), 0xff, sizeof(data->temp_dupli_object));
63#else
64 (void)data;
65#endif
66}
67
68bool deg_object_hide_original(eEvaluationMode eval_mode, const Object *ob, const DupliObject *dob)
69{
70 /* Automatic hiding if this object is being instanced on verts/faces/frames
71 * by its parent. Ideally this should not be needed, but due to the wrong
72 * dependency direction in the data design there is no way to keep the object
73 * visible otherwise. The better solution eventually would be for objects
74 * to specify which object they instance, instead of through parenting.
75 *
76 * This function should not be used for meta-balls. They have custom visibility rules, as hiding
77 * the base meta-ball will also hide all the other balls in the group. */
78 if (eval_mode == DAG_EVAL_RENDER || dob) {
79 const int hide_original_types = OB_DUPLIVERTS | OB_DUPLIFACES;
80
81 if (!dob || !(dob->type & hide_original_types)) {
82 if (ob->parent && (ob->parent->transflag & hide_original_types)) {
83 return true;
84 }
85 }
86 }
87
88 return false;
89}
90
91void deg_iterator_duplis_init(DEGObjectIterData *data, Object *object)
92{
93 /* This should have been set to default values at `DEG_iterator_objects_begin`, or at the end of
94 * the previous `deg_iterator_duplis_step` cycle. */
95 BLI_assert(data->dupli_parent == nullptr);
96 BLI_assert(data->dupli_object_next == nullptr);
97 BLI_assert(data->dupli_object_next_index == -1);
98 if (data->dupli_list.is_empty()) {
99 return;
100 }
101 data->dupli_parent = object;
102 data->dupli_object_next = &data->dupli_list.first();
103 data->dupli_object_next_index = 0;
104}
105
106/* Returns false when iterator is exhausted. */
107bool deg_iterator_duplis_step(DEGObjectIterData *data)
108{
109 if (data->dupli_list.is_empty()) {
110 return false;
111 }
112
113 while (data->dupli_object_next != nullptr) {
114 DupliObject *dob = data->dupli_object_next;
115 Object *obd = dob->ob;
116
117 if (++data->dupli_object_next_index < data->dupli_list.size()) {
118 data->dupli_object_next = &data->dupli_list[data->dupli_object_next_index];
119 }
120 else {
121 data->dupli_object_next = nullptr;
122 data->dupli_object_next_index = -1;
123 }
124
125 if (!DEG_iterator_dupli_is_visible(dob, data->eval_mode)) {
126 continue;
127 }
128
129 /* TODO: Can this be removed? included_objects is already passed to object_duplilist(). */
130 DEGObjectIterSettings *settings = data->settings;
131 if (settings->included_objects) {
132 Object *object_orig = DEG_get_original(obd);
133 if (!settings->included_objects->contains(object_orig)) {
134 continue;
135 }
136 }
137
138 if (data->dupli_object_current) {
140 &data->temp_dupli_object);
141 }
142
143 data->dupli_object_current = dob;
144
146 data->dupli_object_current,
147 data->eval_mode,
148 true,
149 &data->temp_dupli_object,
150 &data->temp_dupli_object_runtime))
151 {
152 data->next_object = &data->temp_dupli_object;
153 return true;
154 }
155 }
156
157 /* Even if the `dupli_list` is not empty, it may happen that none of its entry is displayed (e.g.
158 * #DEG_iterator_dupli_is_visible return `false` for all of the duplis). In such cases,
159 * `dupli_object_current` will also be `nullptr`, and nothing needs to be freed here.
160 * See also #149673 for a reproducible case. */
161 if (data->dupli_object_current) {
162 DEG_iterator_temp_object_free_properties(data->dupli_object_current, &data->temp_dupli_object);
163 }
164 data->dupli_list.clear();
165 data->dupli_parent = nullptr;
166 data->dupli_object_next = nullptr;
167 data->dupli_object_next_index = -1;
168 data->dupli_object_current = nullptr;
169 deg_invalidate_iterator_work_data(data);
170 return false;
171}
172
173/* Returns false when iterator is exhausted. */
174bool deg_iterator_objects_step(DEGObjectIterData *data)
175{
176 deg::Depsgraph *deg_graph = reinterpret_cast<deg::Depsgraph *>(data->graph);
177
178 for (; data->id_node_index < data->num_id_nodes; data->id_node_index++) {
179 deg::IDNode *id_node = deg_graph->id_nodes[data->id_node_index];
180
181 /* Use the build time visibility so that the ID is not appearing/disappearing throughout
182 * animation export. */
183 if (!id_node->is_visible_on_build) {
184 continue;
185 }
186
187 const ID_Type id_type = GS(id_node->id_orig->name);
188
189 if (id_type != ID_OB) {
190 continue;
191 }
192
193 switch (id_node->linked_state) {
195 if ((data->flag & DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY) == 0) {
196 continue;
197 }
198 break;
200 if ((data->flag & DEG_ITER_OBJECT_FLAG_LINKED_VIA_SET) == 0) {
201 continue;
202 }
203 break;
205 if ((data->flag & DEG_ITER_OBJECT_FLAG_LINKED_INDIRECTLY) == 0) {
206 continue;
207 }
208 break;
209 }
210
211 Object *object = id_cast<Object *>(id_node->id_cow);
212 Object *object_orig = DEG_get_original(object);
213
214 DEGObjectIterSettings *settings = data->settings;
215 if (settings->included_objects) {
216 if (!settings->included_objects->contains(object_orig)) {
217 continue;
218 }
219 }
220
221 /* NOTE: The object might be invisible after the latest depsgraph evaluation, in which case
222 * going into its evaluated state might not be safe. For example, its evaluated mesh state
223 * might point to a freed data-block if the mesh is animated.
224 * So it is required to perform the visibility checks prior to looking into any deeper into the
225 * object. */
226
228
229 object->runtime->select_id = object_orig->runtime->select_id;
230
231 const bool use_preview = object_orig == data->object_orig_with_preview;
232 if (use_preview) {
233 object_duplilist_preview(data->graph, object, data->settings->viewer_path, data->dupli_list);
234 deg_iterator_duplis_init(data, object);
235 data->id_node_index++;
236 return true;
237 }
238
239 int ob_visibility = OB_VISIBLE_ALL;
241 ob_visibility = BKE_object_visibility(object, data->eval_mode);
242
243 if (!DEG_iterator_object_is_visible(data->eval_mode, object)) {
244 continue;
245 }
246 }
247
248 if (ob_visibility & OB_VISIBLE_INSTANCES) {
249 if ((data->flag & DEG_ITER_OBJECT_FLAG_DUPLI) &&
250 ((object->transflag & OB_DUPLI) || object->runtime->geometry_set_eval != nullptr))
251 {
253 object_duplilist(data->graph, object, data->settings->included_objects, data->dupli_list);
254 deg_iterator_duplis_init(data, object);
255 }
256 }
257
258 if (ob_visibility & (OB_VISIBLE_SELF | OB_VISIBLE_PARTICLES)) {
260 data->next_object = object;
261 }
262 data->id_node_index++;
263 return true;
264 }
265 return false;
266}
267
268} // namespace
269
271{
272 BLI_assert(this != &other);
273
274 this->settings = other.settings;
275 this->graph = other.graph;
276 this->flag = other.flag;
277 this->eval_mode = other.eval_mode;
279 this->next_object = other.next_object;
280 this->dupli_parent = other.dupli_parent;
281 this->dupli_list = std::move(other.dupli_list);
288 this->id_node_index = other.id_node_index;
289 this->num_id_nodes = other.num_id_nodes;
290}
291
293{
294 if (viewer_path.path.is_empty()) {
295 return nullptr;
296 }
297 const ViewerPathElem *elem = viewer_path.path.first();
298 if (elem->type != VIEWER_PATH_ELEM_TYPE_ID) {
299 return nullptr;
300 }
301 const IDViewerPathElem *id_elem = reinterpret_cast<const IDViewerPathElem *>(elem);
302 if (id_elem->id == nullptr) {
303 return nullptr;
304 }
305 if (GS(id_elem->id->name) != ID_OB) {
306 return nullptr;
307 }
308 Object *object = reinterpret_cast<Object *>(id_elem->id);
309 if (elem->next == nullptr || elem->next->type != VIEWER_PATH_ELEM_TYPE_MODIFIER) {
310 return nullptr;
311 }
312 const ModifierViewerPathElem *modifier_elem = reinterpret_cast<const ModifierViewerPathElem *>(
313 elem->next);
315 if (md == nullptr) {
316 return nullptr;
317 }
318 if (!(md->mode & eModifierMode_Realtime)) {
319 return nullptr;
320 }
321 return reinterpret_cast<Object *>(id_elem->id);
322}
323
325{
326 Depsgraph *depsgraph = data->graph;
327 deg::Depsgraph *deg_graph = reinterpret_cast<deg::Depsgraph *>(depsgraph);
328 const size_t num_id_nodes = deg_graph->id_nodes.size();
329
330 iter->data = data;
331
332 if (num_id_nodes == 0) {
333 iter->valid = false;
334 return;
335 }
336
337 data->next_object = nullptr;
338 data->dupli_parent = nullptr;
339 data->dupli_list.clear();
340 data->dupli_object_next = nullptr;
341 data->dupli_object_next_index = -1;
342 data->dupli_object_current = nullptr;
343 data->id_node_index = 0;
344 data->num_id_nodes = num_id_nodes;
345 data->eval_mode = DEG_get_mode(depsgraph);
346 deg_invalidate_iterator_work_data(data);
347
348 /* Determine if the preview of any object should be in the iterator. */
349 const ViewerPath *viewer_path = data->settings->viewer_path;
350 if (viewer_path != nullptr) {
351 data->object_orig_with_preview = find_object_with_preview_geometry(*viewer_path);
352 }
353
355}
356
358{
359 DEGObjectIterData *data = static_cast<DEGObjectIterData *>(iter->data);
360 while (true) {
361 if (data->next_object != nullptr) {
362 iter->current = data->next_object;
363 data->next_object = nullptr;
364 return;
365 }
366 if (deg_iterator_duplis_step(data)) {
367 continue;
368 }
369 if (deg_iterator_objects_step(data)) {
370 continue;
372 iter->valid = false;
373 break;
374 }
375}
376
378{
379 DEGObjectIterData *data = static_cast<DEGObjectIterData *>(iter->data);
380 if (data != nullptr) {
381 /* Force crash in case the iterator data is referenced and accessed down
382 * the line. (#51718) */
383 deg_invalidate_iterator_work_data(data);
384 }
385}
386
387/* ************************ DEG ID ITERATOR ********************* */
388
389static void DEG_iterator_ids_step(BLI_Iterator *iter, deg::IDNode *id_node, bool only_updated)
390{
391 ID *id_cow = id_node->id_cow;
392
393 /* Use the build time visibility so that the ID is not appearing/disappearing throughout
394 * animation export.
395 * When the dependency graph is asked for updates report all IDs, as the user of those updates
396 * might need to react to updates coming from IDs which do change visibility throughout the
397 * life-time of the graph. */
398 if (!only_updated && !id_node->is_visible_on_build) {
399 iter->skip = true;
400 return;
401 }
402
403 if (only_updated && !(id_cow->recalc & ID_RECALC_ALL)) {
404 /* Node-tree is considered part of the data-block. */
405 bNodeTree *ntree = bke::node_tree_from_id(id_cow);
406 if (ntree == nullptr) {
407 iter->skip = true;
408 return;
409 }
410 if ((ntree->id.recalc & ID_RECALC_NTREE_OUTPUT) == 0) {
411 iter->skip = true;
412 return;
413 }
414 }
415
416 iter->current = id_cow;
417 iter->skip = false;
418}
419
421{
422 Depsgraph *depsgraph = data->graph;
423 deg::Depsgraph *deg_graph = reinterpret_cast<deg::Depsgraph *>(depsgraph);
424 const size_t num_id_nodes = deg_graph->id_nodes.size();
425
426 iter->data = data;
427
428 if ((num_id_nodes == 0) || (data->only_updated && !DEG_id_type_any_updated(depsgraph))) {
429 iter->valid = false;
430 return;
431 }
432
433 data->id_node_index = 0;
434 data->num_id_nodes = num_id_nodes;
435
436 deg::IDNode *id_node = deg_graph->id_nodes[data->id_node_index];
437 DEG_iterator_ids_step(iter, id_node, data->only_updated);
438
439 if (iter->skip) {
441 }
442}
443
445{
446 DEGIDIterData *data = static_cast<DEGIDIterData *>(iter->data);
447 Depsgraph *depsgraph = data->graph;
448 deg::Depsgraph *deg_graph = reinterpret_cast<deg::Depsgraph *>(depsgraph);
449
450 do {
451 iter->skip = false;
452
453 ++data->id_node_index;
454 if (data->id_node_index == data->num_id_nodes) {
455 iter->valid = false;
456 return;
457 }
458
459 deg::IDNode *id_node = deg_graph->id_nodes[data->id_node_index];
460 DEG_iterator_ids_step(iter, id_node, data->only_updated);
461 } while (iter->skip);
462}
463
465
467{
468 if (ob->type == OB_MBALL) {
469 return true;
470 }
471 return !deg_object_hide_original(eval_mode, ob, nullptr);
472}
473
475{
476 if (dupli->no_draw) {
477 return false;
478 }
479 if (dupli->ob_data && GS(dupli->ob_data->name) == ID_MB) {
480 return false;
481 }
482 if (dupli->ob->type != OB_MBALL && deg_object_hide_original(eval_mode, dupli->ob, dupli)) {
483 return false;
484 }
485
486 return true;
487}
488
490 const DupliObject *dupli,
491 eEvaluationMode eval_mode,
492 bool do_matrix_setup,
493 Object *r_temp_object,
494 bke::ObjectRuntime *r_temp_runtime)
495{
496 *r_temp_object = dna::shallow_copy(*dupli->ob);
497 r_temp_object->runtime = r_temp_runtime;
498 *r_temp_object->runtime = *dupli->ob->runtime;
499
500 r_temp_object->base_flag = dupli_parent->base_flag | BASE_FROM_DUPLI;
501 r_temp_object->base_local_view_bits = dupli_parent->base_local_view_bits;
502 r_temp_object->runtime->local_collections_bits = dupli_parent->runtime->local_collections_bits;
503 r_temp_object->dt = std::min(r_temp_object->dt, dupli_parent->dt);
504 copy_v4_v4(r_temp_object->color, dupli_parent->color);
505 r_temp_object->runtime->select_id = dupli_parent->runtime->select_id;
506 if (dupli->ob->data != dupli->ob_data) {
508 }
509
510 /* Duplicated elements shouldn't care whether their original collection is visible or not. */
512
513 /* TODO: Could this be computed in DEG_iterator_dupli_is_visible,
514 * before setting up the shallow copy? */
515 int ob_visibility = BKE_object_visibility(r_temp_object, eval_mode);
516 if ((ob_visibility & (OB_VISIBLE_SELF | OB_VISIBLE_PARTICLES)) == 0) {
517 return false;
518 }
519
520 if (do_matrix_setup) {
521 /* This could be avoided by refactoring make_dupli() in order to track all negative scaling
522 * recursively. */
523 bool is_neg_scale = is_negative_m4(dupli->mat);
524 SET_FLAG_FROM_TEST(r_temp_object->transflag, is_neg_scale, OB_NEG_SCALE);
525
526 copy_m4_m4(r_temp_object->runtime->object_to_world.ptr(), dupli->mat);
527 invert_m4_m4(r_temp_object->runtime->world_to_object.ptr(),
528 r_temp_object->object_to_world().ptr());
529 }
530
532
533 return true;
534}
535
536static void ensure_id_properties_freed(const IDProperty *dupli_idprops,
537 IDProperty **temp_dupli_idprops)
538{
539 if (*temp_dupli_idprops == nullptr) {
540 /* No ID properties in temp data-block -- no leak is possible. */
541 return;
542 }
543 if (*temp_dupli_idprops == dupli_idprops) {
544 /* Temp copy of object did not modify ID properties. */
545 return;
546 }
547 /* Free memory which is owned by temporary storage which is about to get overwritten. */
548 IDP_FreeProperty(*temp_dupli_idprops);
549 *temp_dupli_idprops = nullptr;
550}
551
557
558} // namespace blender
General operations, lookup, etc. for blender objects.
Object is a sort of wrapper for general info.
#define SET_FLAG_FROM_TEST(value, test, flag)
#define BLI_assert(a)
int64_t size() const
blender::ID_Type ID_Type
bNodeTree * node_tree_from_id(ID *id)
Definition node.cc:5337
bool deg_eval_copy_is_expanded(const ID *id_cow)
bool deg_validate_eval_copy_datablock(ID *id_cow)
@ DEG_ID_LINKED_INDIRECTLY
internal::ShallowDataConstRef< T > shallow_copy(const T &other)
Definition DNA_defs.h:147
bool DEG_iterator_temp_object_from_dupli(const Object *dupli_parent, const DupliObject *dupli, eEvaluationMode eval_mode, bool do_matrix_setup, Object *r_temp_object, bke::ObjectRuntime *r_temp_runtime)
void DEG_iterator_temp_object_free_properties(const DupliObject *dupli, Object *temp_object)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void DEG_iterator_ids_end(BLI_Iterator *iter)
bool DEG_iterator_object_is_visible(eEvaluationMode eval_mode, const Object *ob)
@ BASE_ENABLED_AND_MAYBE_VISIBLE_IN_VIEWPORT
void DEG_iterator_objects_next(BLI_Iterator *iter)
bool invert_m4_m4(float inverse[4][4], const float mat[4][4])
bool is_negative_m4(const float mat[4][4])
ModifierData * BKE_modifiers_findby_persistent_uid(const Object *ob, int persistent_uid)
bool DEG_iterator_temp_object_from_dupli(const Object *dupli_parent, const DupliObject *dupli, eEvaluationMode eval_mode, bool do_matrix_setup, Object *r_temp_object, bke::ObjectRuntime *r_temp_runtime)
bool DEG_id_type_any_updated(const Depsgraph *depsgraph)
BMesh const char void * data
static void DEG_iterator_ids_step(BLI_Iterator *iter, deg::IDNode *id_node, bool only_updated)
void object_duplilist(Depsgraph *depsgraph, Object *ob, Set< const Object * > *include_objects, DupliList &r_duplilist)
int BKE_object_visibility(const Object *ob, int dag_eval_mode)
Definition object.cc:2063
void DEG_iterator_ids_next(BLI_Iterator *iter)
@ DEG_ITER_OBJECT_FLAG_LINKED_VIA_SET
@ DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY
@ DEG_ITER_OBJECT_FLAG_LINKED_INDIRECTLY
void DEG_iterator_objects_end(BLI_Iterator *iter)
eEvaluationMode DEG_get_mode(const Depsgraph *graph)
static Object * find_object_with_preview_geometry(const ViewerPath &viewer_path)
bool DEG_iterator_dupli_is_visible(const DupliObject *dupli, eEvaluationMode eval_mode)
T * DEG_get_original(T *id)
void BKE_object_replace_data_on_shallow_copy(Object *ob, ID *new_data)
Definition object.cc:5748
void IDP_FreeProperty(IDProperty *prop)
Definition idprop.cc:1403
void object_duplilist_preview(Depsgraph *depsgraph, Object *ob, const ViewerPath *viewer_path, DupliList &r_duplilist)
@ ID_RECALC_ALL
Definition DNA_ID.h:1190
@ ID_RECALC_NTREE_OUTPUT
Definition DNA_ID.h:1155
ID_Type GS(const char *name)
Definition DNA_ID.h:410
static void ensure_id_properties_freed(const IDProperty *dupli_idprops, IDProperty **temp_dupli_idprops)
MINLINE void copy_v4_v4(float r[4], const float a[4])
void DEG_iterator_ids_begin(BLI_Iterator *iter, DEGIDIterData *data)
void copy_m4_m4(float m1[4][4], const float m2[4][4])
void DEG_iterator_objects_begin(BLI_Iterator *iter, DEGObjectIterData *data)
@ OB_VISIBLE_ALL
@ OB_VISIBLE_PARTICLES
@ OB_VISIBLE_SELF
@ OB_VISIBLE_INSTANCES
void DEG_iterator_temp_object_free_properties(const DupliObject *dupli, Object *temp_object)
bke::ObjectRuntime temp_dupli_object_runtime
DEGObjectIterSettings * settings
void transfer_from(DEGObjectIterData &other)
char name[258]
Definition DNA_ID.h:433
IDProperty * system_properties
Definition DNA_ID.h:490
IDProperty * properties
Definition DNA_ID.h:481
unsigned int recalc
Definition DNA_ID.h:446
const c_style_mat & ptr() const
bke::ObjectRuntime * runtime
unsigned short base_local_view_bits
eObject_TransFlag transflag
struct ViewerPathElem * next
ListBaseT< ViewerPathElem > path