Blender V5.3
deg_builder_rna.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2019 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
10
11#include <cstring>
12
13#include "BLI_listbase.hh"
14#include "BLI_string.hh"
15#include "BLI_utildefines.hh"
16
17#include "DNA_action_types.h"
19#include "DNA_key_types.h"
20#include "DNA_object_types.h"
21
22#include "BKE_constraint.h"
23
24#include "RNA_access.hh"
25#include "RNA_prototypes.hh"
26
28#include "intern/depsgraph.hh"
33
34namespace blender::deg {
35
36/* ********************************* ID Data ******************************** */
37
39 public:
40 explicit RNANodeQueryIDData(const ID *id) : id_(id) {}
41
46
48 {
50 return constraint_to_pchan_map_->lookup_default(constraint, nullptr);
51 }
52
54 {
55 if (constraint_to_pchan_map_ != nullptr) {
56 return;
57 }
59 const Object *object = reinterpret_cast<const Object *>(id_);
61 if (object->pose != nullptr) {
62 for (const bPoseChannel &pchan : object->pose->chanbase) {
63 for (const bConstraint &constraint : pchan.constraints) {
64 constraint_to_pchan_map_->add_new(&constraint, &pchan);
65 }
66 }
67 }
68 }
69
70 protected:
71 /* ID this data corresponds to. */
72 const ID *id_;
73
74 /* indexed by bConstraint*, returns pose channel which contains that
75 * constraint. */
77};
78
79/* ***************************** Node Identifier **************************** */
80
90
92{
93 return id != nullptr && type != NodeType::UNDEFINED;
94}
95
96/* ********************************** Query ********************************* */
97
99 : depsgraph_(depsgraph), builder_(builder)
100{
101}
102
104
106 const PropertyRNA *prop,
107 RNAPointerSource source)
108{
109 const RNANodeIdentifier node_identifier = construct_node_identifier(ptr, prop, source);
110 if (!node_identifier.is_valid()) {
111 return nullptr;
112 }
113 IDNode *id_node = depsgraph_->find_id_node(node_identifier.id);
114 if (id_node == nullptr) {
115 return nullptr;
116 }
117 ComponentNode *comp_node = id_node->find_component(node_identifier.type,
118 node_identifier.component_name);
119 if (comp_node == nullptr) {
120 return nullptr;
121 }
122 if (node_identifier.operation_code == OperationCode::OPERATION) {
123 return comp_node;
124 }
125 return comp_node->find_operation(node_identifier.operation_code,
126 node_identifier.operation_name,
127 node_identifier.operation_name_tag);
128}
129
130bool RNANodeQuery::contains(const char *prop_identifier, const char *rna_path_component)
131{
132 const char *substr = strstr(prop_identifier, rna_path_component);
133 if (substr == nullptr) {
134 return false;
135 }
136
137 /* If `substr != prop_identifier`, it means that the sub-string is found further in
138 * `prop_identifier`, and that thus index -1 is a valid memory location. */
139 const bool start_ok = substr == prop_identifier || substr[-1] == '.';
140 if (!start_ok) {
141 return false;
142 }
143
144 const size_t component_len = strlen(rna_path_component);
145 const bool end_ok = ELEM(substr[component_len], '\0', '.', '[');
146 return end_ok;
147}
148
150 const PropertyRNA *prop,
151 RNAPointerSource source)
152{
153 RNANodeIdentifier node_identifier;
154 if (!ptr->has_type()) {
155 return node_identifier;
156 }
157 /* Set default values for returns. */
158 node_identifier.id = ptr->owner_id;
159 node_identifier.component_name = "";
161 node_identifier.operation_name = "";
162 node_identifier.operation_name_tag = -1;
163 /* Handling of commonly known scenarios. */
164 if (rna_prop_affects_parameters_node(ptr, prop)) {
165 /* Custom properties of bones are placed in their components to improve granularity. */
166 if (RNA_struct_is_a(ptr->type, RNA_PoseBone)) {
167 const bPoseChannel *pchan = static_cast<const bPoseChannel *>(ptr->data);
168 node_identifier.type = NodeType::BONE;
169 node_identifier.component_name = pchan->name;
170 }
171 else {
172 node_identifier.type = NodeType::PARAMETERS;
173 }
175 node_identifier.operation_name = RNA_property_identifier(prop);
176 return node_identifier;
177 }
178 if (ptr->type == RNA_PoseBone) {
179 const bPoseChannel *pchan = static_cast<const bPoseChannel *>(ptr->data);
180 /* Bone - generally, we just want the bone component. */
181 node_identifier.type = NodeType::BONE;
182 node_identifier.component_name = pchan->name;
183 /* However check property name for special handling. */
184 if (prop != nullptr) {
185 Object *object = reinterpret_cast<Object *>(node_identifier.id);
186 const char *prop_name = RNA_property_identifier(prop);
187 /* B-Bone properties should connect to the final operation. */
188 if (STRPREFIX(prop_name, "bbone_")) {
189 if (builder_->check_pchan_has_bbone_segments(object, pchan)) {
191 }
192 else {
194 }
195 }
196 /* Final transform properties go to the Done node for the exit. */
197 else if (STR_ELEM(prop_name, "head", "tail", "length") || STRPREFIX(prop_name, "matrix")) {
198 if (source == RNAPointerSource::EXIT) {
200 }
201 }
202 /* Bone visibility has its own depsgraph node. */
203 else if (STREQ(prop_name, "hide")) {
205 }
206 /* And other properties can always go to the entry operation. */
207 else {
209 }
210 }
211 return node_identifier;
212 }
213 if (ptr->type == RNA_Bone) {
214 /* Armature-level bone mapped to Armature Eval, and thus Pose Init.
215 * Drivers have special code elsewhere that links them to the pose
216 * bone components, instead of using this generic code. */
217 node_identifier.type = NodeType::ARMATURE;
219 /* If trying to look up via an Object, e.g. due to lookup via
220 * obj.pose.bones[].bone in a driver attached to the Object,
221 * redirect to its data. */
222 if (GS(node_identifier.id->name) == ID_OB) {
223 node_identifier.id = id_cast<Object *>(node_identifier.id)->data;
224 }
225 return node_identifier;
226 }
227
228 const char *prop_identifier = prop != nullptr ?
229 RNA_property_identifier(const_cast<PropertyRNA *>(prop)) :
230 "";
231
232 if (RNA_struct_is_a(ptr->type, RNA_Constraint)) {
233 const Object *object = reinterpret_cast<const Object *>(ptr->owner_id);
234 const bConstraint *constraint = static_cast<const bConstraint *>(ptr->data);
236 /* Check whether is object or bone constraint. */
237 /* NOTE: Currently none of the area can address transform of an object
238 * at a given constraint, but for rigging one might use constraint
239 * influence to be used to drive some corrective shape keys or so. */
240 const bPoseChannel *pchan = id_data->get_pchan_for_constraint(constraint);
241 if (pchan == nullptr) {
242 node_identifier.type = NodeType::TRANSFORM;
244 }
245 else {
246 node_identifier.type = NodeType::BONE;
248 node_identifier.component_name = pchan->name;
249 }
250 return node_identifier;
251 }
252 if (ELEM(ptr->type, RNA_ConstraintTarget, RNA_ConstraintTargetBone)) {
253 Object *object = reinterpret_cast<Object *>(ptr->owner_id);
254 bConstraintTarget *tgt = static_cast<bConstraintTarget *>(ptr->data);
255 /* Check whether is object or bone constraint. */
256 bPoseChannel *pchan = nullptr;
257 bConstraint *con = BKE_constraint_find_from_target(object, tgt, &pchan);
258 if (con != nullptr) {
259 if (pchan != nullptr) {
260 node_identifier.type = NodeType::BONE;
262 node_identifier.component_name = pchan->name;
263 }
264 else {
265 node_identifier.type = NodeType::TRANSFORM;
267 }
268 return node_identifier;
269 }
270 }
271 else if (RNA_struct_is_a(ptr->type, RNA_Modifier) &&
272 (contains(prop_identifier, "show_viewport") ||
273 contains(prop_identifier, "show_render")))
274 {
275 node_identifier.type = NodeType::GEOMETRY;
277 return node_identifier;
278 }
279 else if (RNA_struct_is_a(ptr->type, RNA_Mesh) || RNA_struct_is_a(ptr->type, RNA_Modifier) ||
280 RNA_struct_is_a(ptr->type, RNA_Spline) || RNA_struct_is_a(ptr->type, RNA_TextBox) ||
281 RNA_struct_is_a(ptr->type, RNA_AnnotationLayer) ||
282 RNA_struct_is_a(ptr->type, RNA_LatticePoint) ||
283 RNA_struct_is_a(ptr->type, RNA_MeshUVLoop) ||
284 RNA_struct_is_a(ptr->type, RNA_MeshLoopColor) ||
285 RNA_struct_is_a(ptr->type, RNA_VertexGroupElement) ||
286 RNA_struct_is_a(ptr->type, RNA_ShaderFx) ||
287 (prop &&
288 RNA_property_flag(const_cast<PropertyRNA *>(prop)) & PROP_FORCE_GEOMETRY_EVAL) != 0)
289 {
290 /* When modifier is used as FROM operation this is likely referencing to
291 * the property (for example, modifier's influence).
292 * But when it's used as TO operation, this is geometry component. */
293 switch (source) {
295 node_identifier.type = NodeType::GEOMETRY;
296 break;
298 node_identifier.type = NodeType::PARAMETERS;
300 break;
301 }
302 return node_identifier;
303 }
304 else if (ptr->type == RNA_Object) {
305 /* Transforms props? */
306 if (prop != nullptr) {
307 /* TODO(sergey): How to optimize this? */
308 if (contains(prop_identifier, "location") || contains(prop_identifier, "matrix_basis") ||
309 contains(prop_identifier, "matrix_channel") ||
310 contains(prop_identifier, "matrix_inverse") ||
311 contains(prop_identifier, "matrix_local") ||
312 contains(prop_identifier, "matrix_parent_inverse") ||
313 contains(prop_identifier, "matrix_world") ||
314 contains(prop_identifier, "rotation_axis_angle") ||
315 contains(prop_identifier, "rotation_euler") ||
316 contains(prop_identifier, "rotation_mode") ||
317 contains(prop_identifier, "rotation_quaternion") || contains(prop_identifier, "scale") ||
318 contains(prop_identifier, "delta_location") ||
319 contains(prop_identifier, "delta_rotation_euler") ||
320 contains(prop_identifier, "delta_rotation_quaternion") ||
321 contains(prop_identifier, "delta_scale"))
322 {
323 node_identifier.type = NodeType::TRANSFORM;
324 return node_identifier;
325 }
326 if (contains(prop_identifier, "data")) {
327 /* We access object.data, most likely a geometry.
328 * Might be a bone tho. */
329 node_identifier.type = NodeType::GEOMETRY;
330 return node_identifier;
331 }
332 if (STR_ELEM(prop_identifier, "hide_viewport", "hide_render")) {
333 node_identifier.type = NodeType::OBJECT_FROM_LAYER;
334 return node_identifier;
335 }
336 if (STREQ(prop_identifier, "dimensions")) {
337 node_identifier.type = NodeType::PARAMETERS;
339 return node_identifier;
340 }
341 }
342 }
343 else if (ptr->type == RNA_ShapeKey) {
344 KeyBlock *key_block = static_cast<KeyBlock *>(ptr->data);
345 node_identifier.id = ptr->owner_id;
346 node_identifier.type = NodeType::PARAMETERS;
348 node_identifier.operation_name = key_block->name;
349 return node_identifier;
350 }
351 else if (ptr->type == RNA_Key) {
352 node_identifier.id = ptr->owner_id;
353 node_identifier.type = NodeType::GEOMETRY;
354 return node_identifier;
355 }
356 else if (RNA_struct_is_a(ptr->type, RNA_Strip) ||
357 RNA_struct_is_a(ptr->type, RNA_StripModifier) ||
358 ELEM(ptr->type, RNA_StripTransform, RNA_StripCrop, RNA_StripColorBalanceData))
359 {
360 /* Sequencer strip or related nested data. */
361 node_identifier.type = NodeType::SEQUENCER;
362 return node_identifier;
363 }
364 else if (RNA_struct_is_a(ptr->type, RNA_NodeSocket)) {
365 node_identifier.type = NodeType::NTREE_OUTPUT;
366 return node_identifier;
367 }
368 else if (RNA_struct_is_a(ptr->type, RNA_ShaderNode)) {
369 node_identifier.type = NodeType::SHADING;
370 return node_identifier;
371 }
372 else if (ELEM(ptr->type, RNA_Curve, RNA_TextCurve)) {
373 node_identifier.id = ptr->owner_id;
374 node_identifier.type = NodeType::GEOMETRY;
375 return node_identifier;
376 }
377 else if (ELEM(ptr->type, RNA_BezierSplinePoint, RNA_SplinePoint)) {
378 node_identifier.id = ptr->owner_id;
379 node_identifier.type = NodeType::GEOMETRY;
380 return node_identifier;
381 }
382 else if (RNA_struct_is_a(ptr->type, RNA_ImageUser)) {
383 if (GS(node_identifier.id->name) == ID_NT) {
384 node_identifier.type = NodeType::IMAGE_ANIMATION;
386 return node_identifier;
387 }
388 }
389 else if (ELEM(ptr->type, RNA_MeshVertex, RNA_MeshEdge, RNA_MeshLoop, RNA_MeshPolygon)) {
390 node_identifier.type = NodeType::GEOMETRY;
391 return node_identifier;
392 }
393 else if (ptr->owner_id && GS(ptr->owner_id->name) == ID_SCE &&
394 RNA_struct_search_closest_ancestor_by_type(ptr, RNA_SceneCompositorEffect))
395 {
396 node_identifier.type = NodeType::COMPOSITOR;
398 return node_identifier;
399 }
400 if (prop != nullptr) {
401 /* All unknown data effectively falls under "parameter evaluation". */
402 node_identifier.type = NodeType::PARAMETERS;
404 node_identifier.operation_name = "";
405 node_identifier.operation_name_tag = -1;
406 return node_identifier;
407 }
408 return node_identifier;
409}
410
412{
413 std::unique_ptr<RNANodeQueryIDData> &id_data = id_data_map_.lookup_or_add_cb(
414 id, [&]() { return std::make_unique<RNANodeQueryIDData>(id); });
415 return id_data.get();
416}
417
419{
420 return prop != nullptr && RNA_property_is_idprop(prop) &&
421 /* ID properties in the geometry nodes modifier don't affect that parameters node.
422 * Instead they affect the modifier and therefore the geometry node directly. */
423 !RNA_struct_is_a(ptr->type, RNA_NodesModifier);
424}
425
426} // namespace blender::deg
#define STR_ELEM(...)
Object is a sort of wrapper for general info.
#define STRPREFIX(a, b)
#define ELEM(...)
#define STREQ(a, b)
Map(Allocator allocator={}) noexcept
Definition BLI_map.hh:186
virtual bool check_pchan_has_bbone_segments(const Object *object, const bPoseChannel *pchan)
const bPoseChannel * get_pchan_for_constraint(const bConstraint *constraint)
Map< const bConstraint *, const bPoseChannel * > * constraint_to_pchan_map_
Node * find_node(const PointerRNA *ptr, const PropertyRNA *prop, RNAPointerSource source)
Map< const ID *, std::unique_ptr< RNANodeQueryIDData > > id_data_map_
RNANodeIdentifier construct_node_identifier(const PointerRNA *ptr, const PropertyRNA *prop, RNAPointerSource source)
static bool contains(const char *prop_identifier, const char *rna_path_component)
DepsgraphBuilder * builder_
RNANodeQuery(Depsgraph *depsgraph, DepsgraphBuilder *builder)
RNANodeQueryIDData * ensure_id_data(const ID *id)
bool rna_prop_affects_parameters_node(const PointerRNA *ptr, const PropertyRNA *prop)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
bool RNA_struct_is_a(const StructRNA *type, const StructRNA *srna)
const char * RNA_property_identifier(const PropertyRNA *prop)
std::optional< AncestorPointerRNA > RNA_struct_search_closest_ancestor_by_type(const PointerRNA *ptr, const StructRNA *srna)
struct bConstraint * BKE_constraint_find_from_target(struct Object *ob, struct bConstraintTarget *tgt, struct bPoseChannel **r_pchan)
int RNA_property_flag(PropertyRNA *prop)
bool RNA_property_is_idprop(const PropertyRNA *prop)
ID_Type GS(const char *name)
Definition DNA_ID.h:410
@ PROP_FORCE_GEOMETRY_EVAL
Definition RNA_types.hh:546
char name[258]
Definition DNA_ID.h:433
bool has_type() const
Definition RNA_types.hh:180
StructRNA * type
Definition RNA_types.hh:63
ListBaseT< struct bConstraint > constraints
OperationNode * find_operation(OperationIDKey key) const
IDNode * find_id_node(const ID *id) const
Definition depsgraph.cc:102
ComponentNode * find_component(NodeType type, StringRef name="") const