Blender V5.3
depsgraph_query.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2013 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include <cstring> /* XXX: `memcpy`. */
12
13#include "BLI_listbase.hh"
14
15#include "BKE_action.hh" /* XXX: BKE_pose_channel_find_name */
16#include "BKE_idtype.hh"
17#include "BKE_lib_id.hh"
18#include "BKE_main.hh"
19
20#include "DNA_layer_types.h"
21#include "DNA_object_types.h"
22#include "DNA_scene_types.h"
23
24#include "RNA_access.hh"
25#include "RNA_path.hh"
26#include "RNA_prototypes.hh"
27
28#include "DEG_depsgraph.hh"
30
31#include "intern/depsgraph.hh"
35
36namespace blender {
37
38namespace deg {
39
40static const ID *get_original_id(const ID *id)
41{
42 if (id == nullptr) {
43 return nullptr;
44 }
45 if (id->orig_id == nullptr) {
46 return id;
47 }
48 BLI_assert((id->tag & ID_TAG_COPIED_ON_EVAL) != 0);
49 return id_cast<ID *>(id->orig_id);
50}
51
52static ID *get_original_id(ID *id)
53{
54 const ID *const_id = id;
55 return const_cast<ID *>(get_original_id(const_id));
56}
57
58static const ID *get_evaluated_id(const Depsgraph *deg_graph, const ID *id)
59{
60 if (id == nullptr) {
61 return nullptr;
62 }
63 /* TODO(sergey): This is a duplicate of Depsgraph::get_cow_id(),
64 * but here we never do assert, since we don't know nature of the
65 * incoming ID data-block. */
66 const IDNode *id_node = deg_graph->find_id_node(id);
67 if (id_node == nullptr) {
68 return id;
69 }
70 return id_node->id_cow;
71}
72
73static ID *get_evaluated_id(const Depsgraph *deg_graph, ID *id)
74{
75 const ID *const_id = id;
76 return const_cast<ID *>(get_evaluated_id(deg_graph, const_id));
77}
78
79} // namespace deg
80
81Scene *DEG_get_input_scene(const Depsgraph *graph)
82{
83 const deg::Depsgraph *deg_graph = reinterpret_cast<const deg::Depsgraph *>(graph);
84 return deg_graph->scene;
85}
86
87ViewLayer *DEG_get_input_view_layer(const Depsgraph *graph)
88{
89 const deg::Depsgraph *deg_graph = reinterpret_cast<const deg::Depsgraph *>(graph);
90 return deg_graph->view_layer;
91}
92
93Main *DEG_get_bmain(const Depsgraph *graph)
94{
95 const deg::Depsgraph *deg_graph = reinterpret_cast<const deg::Depsgraph *>(graph);
96 return deg_graph->bmain;
97}
98
99eEvaluationMode DEG_get_mode(const Depsgraph *graph)
100{
101 const deg::Depsgraph *deg_graph = reinterpret_cast<const deg::Depsgraph *>(graph);
102 return deg_graph->mode;
103}
104
105float DEG_get_ctime(const Depsgraph *graph)
106{
107 const deg::Depsgraph *deg_graph = reinterpret_cast<const deg::Depsgraph *>(graph);
108 return deg_graph->ctime;
109}
110
111bool DEG_id_type_updated(const Depsgraph *graph, short id_type)
112{
113 const deg::Depsgraph *deg_graph = reinterpret_cast<const deg::Depsgraph *>(graph);
114 return deg_graph->id_type_updated[BKE_idtype_idcode_to_index(id_type)] != 0;
115}
116
117bool DEG_id_type_any_updated(const Depsgraph *graph)
118{
119 const deg::Depsgraph *deg_graph = reinterpret_cast<const deg::Depsgraph *>(graph);
120
121 /* Loop over all ID types. */
122 for (char id_type_index : deg_graph->id_type_updated) {
123 if (id_type_index) {
124 return true;
125 }
126 }
127
128 return false;
129}
130
131bool DEG_id_is_user_modified(const Depsgraph *graph, const ID *id)
132{
133 const deg::Depsgraph *deg_graph = reinterpret_cast<const deg::Depsgraph *>(graph);
134 const deg::IDNode *id_node = deg_graph->find_id_node(deg::get_original_id(id));
135 if (id_node == nullptr) {
136 return false;
137 }
138 return id_node->is_user_modified;
139}
140
141bool DEG_scene_component_is_user_modified(const Depsgraph *graph,
142 const Scene *scene,
143 const eDepsSceneComponentType component_type)
144{
145 const deg::Depsgraph *deg_graph = reinterpret_cast<const deg::Depsgraph *>(graph);
146 const deg::IDNode *id_node = deg_graph->find_id_node(deg::get_original_id(&scene->id));
147 if (id_node == nullptr) {
148 return false;
149 }
150 const deg::ComponentNode *component_node = id_node->find_component(
151 deg::nodeTypeFromSceneComponent(component_type));
152 if (component_node == nullptr) {
153 return false;
154 }
155 return component_node->is_user_modified;
156}
157
158bool DEG_id_type_any_exists(const Depsgraph *depsgraph, short id_type)
159{
160 const deg::Depsgraph *deg_graph = reinterpret_cast<const deg::Depsgraph *>(depsgraph);
161 return deg_graph->id_type_exist[BKE_idtype_idcode_to_index(id_type)] != 0;
162}
163
164uint32_t DEG_get_eval_flags_for_id(const Depsgraph *graph, const ID *id)
165{
166 if (graph == nullptr) {
167 /* Happens when converting objects to mesh from a python script
168 * after modifying scene graph.
169 *
170 * Currently harmless because it's only called for temporary
171 * objects which are out of the DAG anyway. */
172 return 0;
173 }
174
175 const deg::Depsgraph *deg_graph = reinterpret_cast<const deg::Depsgraph *>(graph);
176 const deg::IDNode *id_node = deg_graph->find_id_node(deg::get_original_id(id));
177 if (id_node == nullptr) {
178 /* TODO(sergey): Does it mean we need to check set scene? */
179 return 0;
180 }
181
182 return id_node->eval_flags;
183}
184
185void DEG_get_customdata_mask_for_object(const Depsgraph *graph,
186 Object *ob,
187 CustomData_MeshMasks *r_mask)
188{
189 if (graph == nullptr) {
190 /* Happens when converting objects to mesh from a python script
191 * after modifying scene graph.
192 *
193 * Currently harmless because it's only called for temporary
194 * objects which are out of the DAG anyway. */
195 return;
196 }
197
198 const deg::Depsgraph *deg_graph = reinterpret_cast<const deg::Depsgraph *>(graph);
199 const deg::IDNode *id_node = deg_graph->find_id_node(DEG_get_original(&ob->id));
200 if (id_node == nullptr) {
201 /* TODO(sergey): Does it mean we need to check set scene? */
202 return;
203 }
204
205 r_mask->vmask |= id_node->customdata_masks.vert_mask;
206 r_mask->emask |= id_node->customdata_masks.edge_mask;
207 r_mask->fmask |= id_node->customdata_masks.face_mask;
208 r_mask->lmask |= id_node->customdata_masks.loop_mask;
209 r_mask->pmask |= id_node->customdata_masks.poly_mask;
210}
211
212Scene *DEG_get_evaluated_scene(const Depsgraph *graph)
213{
214 const deg::Depsgraph *deg_graph = reinterpret_cast<const deg::Depsgraph *>(graph);
215 Scene *scene_cow = deg_graph->scene_cow;
216 /* TODO(sergey): Shall we expand data-block here? Or is it OK to assume
217 * that caller is OK with just a pointer in case scene is not updated yet? */
218 BLI_assert(scene_cow != nullptr && deg::deg_eval_copy_is_expanded(&scene_cow->id));
219 return scene_cow;
220}
221
223{
224 const deg::Depsgraph *deg_graph = reinterpret_cast<const deg::Depsgraph *>(graph);
225 Scene *scene_cow = DEG_get_evaluated_scene(graph);
226 if (scene_cow == nullptr) {
227 return nullptr; /* Happens with new, not-yet-built/evaluated graphs. */
228 }
229 /* Do name-based lookup. */
230 /* TODO(sergey): Can this be optimized? */
231 ViewLayer *view_layer_orig = deg_graph->view_layer;
232 ViewLayer *view_layer_cow = static_cast<ViewLayer *>(
233 BLI_findstring(&scene_cow->view_layers, view_layer_orig->name, offsetof(ViewLayer, name)));
234 BLI_assert(view_layer_cow != nullptr);
235 return view_layer_cow;
236}
237
238ID *DEG_get_evaluated_id(const Depsgraph *depsgraph, ID *id)
239{
240 return deg::get_evaluated_id(reinterpret_cast<const deg::Depsgraph *>(depsgraph), id);
241}
242
243const ID *DEG_get_evaluated_id(const Depsgraph *depsgraph, const ID *id)
244{
245 return DEG_get_evaluated_id(depsgraph, const_cast<ID *>(id));
246}
247
248void DEG_get_evaluated_rna_pointer(const Depsgraph *depsgraph,
249 PointerRNA *ptr,
250 PointerRNA *r_ptr_eval)
251{
252 if ((ptr == nullptr) || (r_ptr_eval == nullptr)) {
253 return;
254 }
255 ID *orig_id = ptr->owner_id;
256 ID *cow_id = DEG_get_evaluated_id(depsgraph, orig_id);
257 if (ptr->owner_id == ptr->data) {
258 /* For ID pointers, it's easy... */
259 r_ptr_eval->owner_id = cow_id;
260 r_ptr_eval->data = static_cast<void *>(cow_id);
261 r_ptr_eval->type = ptr->type;
262 }
263 else if (ptr->type == RNA_PoseBone) {
264 /* HACK: Since bone keyframing is quite commonly used,
265 * speed things up for this case by doing a special lookup
266 * for bones */
267 const Object *ob_eval = id_cast<Object *>(cow_id);
268 bPoseChannel *pchan = static_cast<bPoseChannel *>(ptr->data);
269 bPoseChannel *pchan_eval = BKE_pose_channel_find_name(ob_eval->pose, pchan->name);
270 r_ptr_eval->owner_id = cow_id;
271 r_ptr_eval->data = pchan_eval;
272 r_ptr_eval->type = ptr->type;
273 }
274 else {
275 /* For everything else, try to get RNA Path of the BMain-pointer,
276 * then use that to look up what the evaluated one should be
277 * given the evaluated ID pointer as the new lookup point */
278 /* TODO: Find a faster alternative, or implement support for other
279 * common types too above (e.g. modifiers) */
280 if (const std::optional<std::string> path = RNA_path_from_ID_to_struct(ptr)) {
281 PointerRNA cow_id_ptr = RNA_id_pointer_create(cow_id);
282 if (!RNA_path_resolve(&cow_id_ptr, path->c_str(), r_ptr_eval, nullptr)) {
283 /* Couldn't find evaluated copy of data */
284 fprintf(stderr,
285 "%s: Couldn't resolve RNA path ('%s') relative to evaluated ID (%p) for '%s'\n",
286 __func__,
287 path->c_str(),
288 static_cast<void *>(cow_id),
289 orig_id->name);
290 }
291 }
292 else {
293 /* Path resolution failed - XXX: Hide this behind a debug flag */
294 fprintf(stderr,
295 "%s: Couldn't get RNA path for %s relative to %s\n",
296 __func__,
298 orig_id->name);
299 }
300 }
301}
302
304{
305 return deg::get_original_id(id);
306}
307
308const ID *DEG_get_original_id(const ID *id)
309{
310 return deg::get_original_id(id);
311}
312
313Depsgraph *DEG_get_depsgraph_by_id(const ID &id)
314{
315 return id.runtime->depsgraph;
316}
317
318bool DEG_is_original_id(const ID *id)
319{
320 /* Some explanation of the logic.
321 *
322 * What we want here is to be able to tell whether given ID is a result of dependency graph
323 * evaluation or not.
324 *
325 * All the data-blocks which are created by copy-on-evaluation mechanism will have will be tagged
326 * with ID_TAG_COPIED_ON_EVAL tag. Those data-blocks can not be original.
327 *
328 * Localization is usually happening from evaluated data-block, or will have some special pointer
329 * magic which will make them to act as evaluated.
330 *
331 * NOTE: We consider ID evaluated if ANY of those flags is set. We do NOT require ALL of them. */
333 return false;
334 }
335 return true;
336}
337
338bool DEG_is_evaluated_id(const ID *id)
339{
340 return !DEG_is_original_id(id);
341}
342
343bool DEG_is_fully_evaluated(const Depsgraph *depsgraph)
344{
345 const deg::Depsgraph *deg_graph = reinterpret_cast<const deg::Depsgraph *>(depsgraph);
346 /* Check whether relations are up to date. */
347 if (deg_graph->need_update_relations) {
348 return false;
349 }
350 /* Check whether IDs are up to date. */
351 if (!deg_graph->entry_tags.is_empty()) {
352 return false;
353 }
354 return true;
355}
356
357bool DEG_id_is_fully_evaluated(const Depsgraph *depsgraph, const ID *id_eval)
358{
359 const deg::Depsgraph *deg_graph = reinterpret_cast<const deg::Depsgraph *>(depsgraph);
360 /* Only us the original ID pointer to look up the IDNode, do not dereference it. */
361 const ID *id_orig = deg::get_original_id(id_eval);
362 const deg::IDNode *id_node = deg_graph->find_id_node(id_orig);
363 if (!id_node) {
364 return false;
365 }
366 for (deg::ComponentNode *component : id_node->components.values()) {
367 for (deg::OperationNode *operation : component->operations) {
368 if (operation->flag & deg::DEPSOP_FLAG_NEEDS_UPDATE) {
369 return false;
370 }
371 }
372 }
373 return true;
374}
375
376static bool operation_needs_update(const ID &id,
377 const deg::NodeType component_type,
378 const deg::OperationCode opcode)
379{
380 const Depsgraph *depsgraph = DEG_get_depsgraph_by_id(id);
381 if (!depsgraph) {
382 return false;
383 }
384 const deg::Depsgraph &deg_graph = *reinterpret_cast<const deg::Depsgraph *>(depsgraph);
385 /* Only us the original ID pointer to look up the IDNode, do not dereference it. */
386 const ID *id_orig = deg::get_original_id(&id);
387 if (!id_orig) {
388 return false;
389 }
390 const deg::IDNode *id_node = deg_graph.find_id_node(id_orig);
391 if (!id_node) {
392 return false;
393 };
394 const deg::ComponentNode *component_node = id_node->find_component(component_type);
395 if (!component_node) {
396 return false;
397 }
398 const deg::OperationNode *operation_node = component_node->find_operation(opcode);
399 if (!operation_node) {
400 return false;
401 }
402 /* Technically, there is potential for a race condition here, because the depsgraph evaluation
403 * might update this flag, but it's very unlikely to cause issues right now. Maybe this should
404 * become an atomic eventually. */
405 const bool needs_update = operation_node->flag & deg::DEPSOP_FLAG_NEEDS_UPDATE;
406 return needs_update;
407}
408
414
420
426
427std::optional<double> DEG_get_last_evaluation_time(const Depsgraph *depsgraph)
428{
429 if (!DEG_is_fully_evaluated(depsgraph)) {
430 return std::nullopt;
431 }
432 const deg::Depsgraph &deg_graph = *reinterpret_cast<const deg::Depsgraph *>(depsgraph);
433 return deg_graph.debug.total_evaluation_time();
434}
435
436} // namespace blender
Blender kernel action and pose functionality.
Object is a sort of wrapper for general info.
double total_evaluation_time() const
Definition deg_debug.cc:51
#define offsetof(t, d)
NodeType nodeTypeFromSceneComponent(eDepsSceneComponentType component)
Definition deg_node.cc:114
bool deg_eval_copy_is_expanded(const ID *id_cow)
static const ID * get_evaluated_id(const Depsgraph *deg_graph, const ID *id)
static const ID * get_original_id(const ID *id)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
std::optional< double > DEG_get_last_evaluation_time(const Depsgraph *depsgraph)
ID * DEG_get_original_id(ID *id)
bool RNA_path_resolve(const PointerRNA *ptr, const char *path, PointerRNA *r_ptr, PropertyRNA **r_prop)
Definition rna_path.cc:529
bool DEG_is_fully_evaluated(const Depsgraph *depsgraph)
PointerRNA RNA_id_pointer_create(ID *id)
bool DEG_id_is_fully_evaluated(const Depsgraph *depsgraph, const ID *id_eval)
bool DEG_scene_component_is_user_modified(const Depsgraph *graph, const Scene *scene, eDepsSceneComponentType component_type)
std::optional< std::string > RNA_path_from_ID_to_struct(const PointerRNA *ptr)
Definition rna_path.cc:1078
void * BLI_findstring(const ListBase *listbase, const char *id, int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:615
ID * DEG_get_evaluated_id(const Depsgraph *depsgraph, ID *id)
uint32_t DEG_get_eval_flags_for_id(const Depsgraph *graph, const ID *id)
void DEG_get_customdata_mask_for_object(const Depsgraph *graph, Object *object, CustomData_MeshMasks *r_mask)
bool DEG_id_is_user_modified(const Depsgraph *graph, const ID *id)
Depsgraph * DEG_get_depsgraph_by_id(const ID &id)
static bool operation_needs_update(const ID &id, const deg::NodeType component_type, const deg::OperationCode opcode)
unsigned int uint32_t
Definition BKE_text.h:81
bool DEG_id_type_any_exists(const Depsgraph *depsgraph, short id_type)
bool DEG_object_transform_is_evaluated(const Object &object)
ViewLayer * DEG_get_input_view_layer(const Depsgraph *graph)
bool DEG_id_type_any_updated(const Depsgraph *depsgraph)
Main * DEG_get_bmain(const Depsgraph *graph)
int BKE_idtype_idcode_to_index(short idcode)
Definition idtype.cc:338
ViewLayer * DEG_get_evaluated_view_layer(const Depsgraph *graph)
void DEG_get_evaluated_rna_pointer(const Depsgraph *depsgraph, PointerRNA *ptr, PointerRNA *r_ptr_eval)
eEvaluationMode DEG_get_mode(const Depsgraph *graph)
bool DEG_is_evaluated_id(const ID *id)
const char * RNA_struct_identifier(const StructRNA *type)
T * DEG_get_original(T *id)
bool DEG_collection_geometry_is_evaluated(const Collection &collection)
Scene * DEG_get_input_scene(const Depsgraph *graph)
@ ID_TAG_COPIED_ON_EVAL
Definition DNA_ID.h:1000
@ ID_TAG_LOCALIZED
Definition DNA_ID.h:990
bool DEG_id_type_updated(const Depsgraph *depsgraph, short id_type)
bool DEG_is_original_id(const ID *id)
Scene * DEG_get_evaluated_scene(const Depsgraph *graph)
bool DEG_object_geometry_is_evaluated(const Object &object)
float DEG_get_ctime(const Depsgraph *graph)
bPoseChannel * BKE_pose_channel_find_name(const bPose *pose, const char *name)
char name[258]
Definition DNA_ID.h:433
struct bPose * pose
StructRNA * type
Definition RNA_types.hh:63
ListBaseT< ViewLayer > view_layers
OperationNode * find_operation(OperationIDKey key) const
IDNode * find_id_node(const ID *id) const
Definition depsgraph.cc:102
char id_type_updated[INDEX_ID_MAX]
Definition depsgraph.hh:119
eEvaluationMode mode
Definition depsgraph.hh:145
char id_type_exist[INDEX_ID_MAX]
Definition depsgraph.hh:124
DepsgraphDebug debug
Definition depsgraph.hh:168
Set< OperationNode * > entry_tags
Definition depsgraph.hh:129
DEGCustomDataMeshMasks customdata_masks
Map< ComponentIDKey, ComponentNode * > components
ComponentNode * find_component(NodeType type, StringRef name="") const