2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2013 Blender Foundation.
19 * All rights reserved.
21 * Original Author: Joshua Leung
22 * Contributor(s): Based on original depsgraph.c code - Blender Foundation (2005-2013)
24 * ***** END GPL LICENSE BLOCK *****
27 /** \file blender/depsgraph/intern/builder/deg_builder_relations.cc
30 * Methods for constructing depsgraph
33 #include "intern/builder/deg_builder_relations.h"
37 #include <cstring> /* required for STREQ later on. */
39 #include "MEM_guardedalloc.h"
41 #include "BLI_utildefines.h"
42 #include "BLI_blenlib.h"
45 #include "DNA_action_types.h"
46 #include "DNA_anim_types.h"
47 #include "DNA_armature_types.h"
48 #include "DNA_camera_types.h"
49 #include "DNA_cachefile_types.h"
50 #include "DNA_constraint_types.h"
51 #include "DNA_curve_types.h"
52 #include "DNA_effect_types.h"
53 #include "DNA_gpencil_types.h"
54 #include "DNA_group_types.h"
55 #include "DNA_key_types.h"
56 #include "DNA_lamp_types.h"
57 #include "DNA_material_types.h"
58 #include "DNA_mask_types.h"
59 #include "DNA_mesh_types.h"
60 #include "DNA_meta_types.h"
61 #include "DNA_movieclip_types.h"
62 #include "DNA_node_types.h"
63 #include "DNA_particle_types.h"
64 #include "DNA_lightprobe_types.h"
65 #include "DNA_object_types.h"
66 #include "DNA_rigidbody_types.h"
67 #include "DNA_scene_types.h"
68 #include "DNA_texture_types.h"
69 #include "DNA_world_types.h"
70 #include "DNA_object_force.h"
72 #include "BKE_action.h"
73 #include "BKE_armature.h"
74 #include "BKE_animsys.h"
75 #include "BKE_constraint.h"
76 #include "BKE_curve.h"
77 #include "BKE_effect.h"
78 #include "BKE_collision.h"
79 #include "BKE_fcurve.h"
80 #include "BKE_group.h"
82 #include "BKE_library.h"
84 #include "BKE_material.h"
85 #include "BKE_mball.h"
86 #include "BKE_modifier.h"
88 #include "BKE_object.h"
89 #include "BKE_particle.h"
90 #include "BKE_rigidbody.h"
91 #include "BKE_sound.h"
92 #include "BKE_texture.h"
93 #include "BKE_tracking.h"
94 #include "BKE_world.h"
96 #include "RNA_access.h"
97 #include "RNA_types.h"
100 #include "DEG_depsgraph.h"
101 #include "DEG_depsgraph_build.h"
103 #include "intern/builder/deg_builder.h"
104 #include "intern/builder/deg_builder_pchanmap.h"
106 #include "intern/nodes/deg_node.h"
107 #include "intern/nodes/deg_node_component.h"
108 #include "intern/nodes/deg_node_operation.h"
110 #include "intern/depsgraph_intern.h"
111 #include "intern/depsgraph_types.h"
113 #include "util/deg_util_foreach.h"
119 struct BuilderWalkUserData {
120 DepsgraphRelationBuilder *builder;
125 static void modifier_walk(void *user_data,
126 struct Object * /*ob*/,
127 struct Object **obpoin,
130 BuilderWalkUserData *data = (BuilderWalkUserData *)user_data;
132 data->builder->build_object(data->bmain, data->scene, *obpoin);
136 void constraint_walk(bConstraint * /*con*/,
138 bool /*is_reference*/,
141 BuilderWalkUserData *data = (BuilderWalkUserData *)user_data;
144 if (GS(id->name) == ID_OB) {
145 data->builder->build_object(data->bmain, data->scene, (Object *)id);
152 /* ***************** */
153 /* Relations Builder */
155 /* TODO(sergey): This is somewhat weak, but we don't want neither false-positive
156 * time dependencies nor special exceptions in the depsgraph evaluation.
158 static bool python_driver_depends_on_time(ChannelDriver *driver)
160 if (driver->expression[0] == '\0') {
161 /* Empty expression depends on nothing. */
164 if (strchr(driver->expression, '(') != NULL) {
165 /* Function calls are considered dependent on a time. */
168 if (strstr(driver->expression, "frame") != NULL) {
169 /* Variable `frame` depends on time. */
170 /* TODO(sergey): This is a bit weak, but not sure about better way of
175 /* Possible indirect time relation s should be handled via variable
181 /* **** General purpose functions **** */
183 RNAPathKey::RNAPathKey(ID *id, const char *path) :
186 /* create ID pointer for root of path lookup */
188 RNA_id_pointer_create(id, &id_ptr);
189 /* try to resolve path... */
191 if (!RNA_path_resolve_full(&id_ptr, path, &this->ptr, &this->prop, &index)) {
192 this->ptr = PointerRNA_NULL;
197 DepsgraphRelationBuilder::DepsgraphRelationBuilder(Depsgraph *graph) :
202 TimeSourceDepsNode *DepsgraphRelationBuilder::find_node(
203 const TimeSourceKey &key) const
210 return m_graph->time_source;
214 ComponentDepsNode *DepsgraphRelationBuilder::find_node(
215 const ComponentKey &key) const
217 IDDepsNode *id_node = m_graph->find_id_node(key.id);
219 fprintf(stderr, "find_node component: Could not find ID %s\n",
220 (key.id != NULL) ? key.id->name : "<null>");
224 ComponentDepsNode *node = id_node->find_component(key.type, key.name);
228 OperationDepsNode *DepsgraphRelationBuilder::find_node(
229 const OperationKey &key) const
231 IDDepsNode *id_node = m_graph->find_id_node(key.id);
233 fprintf(stderr, "find_node operation: Could not find ID\n");
237 ComponentDepsNode *comp_node = id_node->find_component(key.component_type,
240 fprintf(stderr, "find_node operation: Could not find component\n");
244 OperationDepsNode *op_node = comp_node->find_operation(key.opcode,
248 fprintf(stderr, "find_node_operation: Failed for (%s, '%s')\n",
249 DEG_OPNAMES[key.opcode], key.name);
254 DepsNode *DepsgraphRelationBuilder::find_node(const RNAPathKey &key) const
256 return m_graph->find_node_from_pointer(&key.ptr, key.prop);
259 OperationDepsNode *DepsgraphRelationBuilder::has_node(
260 const OperationKey &key) const
262 IDDepsNode *id_node = m_graph->find_id_node(key.id);
266 ComponentDepsNode *comp_node = id_node->find_component(key.component_type,
271 return comp_node->has_operation(key.opcode, key.name, key.name_tag);
274 void DepsgraphRelationBuilder::add_time_relation(TimeSourceDepsNode *timesrc,
276 const char *description)
278 if (timesrc && node_to) {
279 m_graph->add_new_relation(timesrc, node_to, description);
282 DEG_DEBUG_PRINTF("add_time_relation(%p = %s, %p = %s, %s) Failed\n",
283 timesrc, (timesrc) ? timesrc->identifier().c_str() : "<None>",
284 node_to, (node_to) ? node_to->identifier().c_str() : "<None>",
289 void DepsgraphRelationBuilder::add_operation_relation(
290 OperationDepsNode *node_from,
291 OperationDepsNode *node_to,
292 const char *description)
294 if (node_from && node_to) {
295 m_graph->add_new_relation(node_from, node_to, description);
298 DEG_DEBUG_PRINTF("add_operation_relation(%p = %s, %p = %s, %s) Failed\n",
299 node_from, (node_from) ? node_from->identifier().c_str() : "<None>",
300 node_to, (node_to) ? node_to->identifier().c_str() : "<None>",
305 void DepsgraphRelationBuilder::add_collision_relations(const OperationKey &key, Scene *scene, Object *ob, Group *group, bool dupli, const char *name)
307 unsigned int numcollobj;
308 Object **collobjs = get_collisionobjects_ext(scene, ob, group, &numcollobj, eModifierType_Collision, dupli);
310 for (unsigned int i = 0; i < numcollobj; i++)
312 Object *ob1 = collobjs[i];
314 ComponentKey trf_key(&ob1->id, DEG_NODE_TYPE_TRANSFORM);
315 add_relation(trf_key, key, name);
317 ComponentKey coll_key(&ob1->id, DEG_NODE_TYPE_GEOMETRY);
318 add_relation(coll_key, key, name);
325 void DepsgraphRelationBuilder::add_forcefield_relations(const OperationKey &key, Scene *scene, Object *ob, ParticleSystem *psys, EffectorWeights *eff, bool add_absorption, const char *name)
327 ListBase *effectors = pdInitEffectors(NULL, scene, ob, psys, eff, false);
330 for (EffectorCache *eff = (EffectorCache *)effectors->first; eff; eff = eff->next) {
332 ComponentKey eff_key(&eff->ob->id, DEG_NODE_TYPE_TRANSFORM);
333 add_relation(eff_key, key, name);
338 ComponentKey eff_key(&eff->ob->id, DEG_NODE_TYPE_EVAL_PARTICLES);
339 add_relation(eff_key, key, name);
341 /* TODO: remove this when/if EVAL_PARTICLES is sufficient for up to date particles */
342 ComponentKey mod_key(&eff->ob->id, DEG_NODE_TYPE_GEOMETRY);
343 add_relation(mod_key, key, name);
345 else if (eff->psys != psys) {
346 OperationKey eff_key(&eff->ob->id, DEG_NODE_TYPE_EVAL_PARTICLES, DEG_OPCODE_PARTICLE_SYSTEM_EVAL, eff->psys->name);
347 add_relation(eff_key, key, name);
351 if (eff->pd->forcefield == PFIELD_SMOKEFLOW && eff->pd->f_source) {
352 ComponentKey trf_key(&eff->pd->f_source->id, DEG_NODE_TYPE_TRANSFORM);
353 add_relation(trf_key, key, "Smoke Force Domain");
355 ComponentKey eff_key(&eff->pd->f_source->id, DEG_NODE_TYPE_GEOMETRY);
356 add_relation(eff_key, key, "Smoke Force Domain");
359 if (add_absorption && (eff->pd->flag & PFIELD_VISIBILITY)) {
360 add_collision_relations(key, scene, ob, NULL, true, "Force Absorption");
365 pdEndEffectors(&effectors);
368 Depsgraph *DepsgraphRelationBuilder::getGraph()
373 /* **** Functions to build relations between entities **** */
375 void DepsgraphRelationBuilder::begin_build(Main *bmain)
377 /* LIB_TAG_DOIT is used to indicate whether node for given ID was already
380 BKE_main_id_tag_all(bmain, LIB_TAG_DOIT, false);
381 /* XXX nested node trees are notr included in tag-clearing above,
382 * so we need to do this manually.
384 FOREACH_NODETREE(bmain, nodetree, id) {
385 if (id != (ID *)nodetree) {
386 nodetree->id.tag &= ~LIB_TAG_DOIT;
388 } FOREACH_NODETREE_END
391 void DepsgraphRelationBuilder::build_group(Main *bmain,
396 ID *group_id = &group->id;
397 bool group_done = (group_id->tag & LIB_TAG_DOIT) != 0;
398 OperationKey object_local_transform_key(&object->id,
399 DEG_NODE_TYPE_TRANSFORM,
400 DEG_OPCODE_TRANSFORM_LOCAL);
401 LINKLIST_FOREACH (GroupObject *, go, &group->gobject) {
403 build_object(bmain, scene, go->ob);
405 ComponentKey dupli_transform_key(&go->ob->id, DEG_NODE_TYPE_TRANSFORM);
406 add_relation(dupli_transform_key, object_local_transform_key, "Dupligroup");
408 group_id->tag |= LIB_TAG_DOIT;
411 void DepsgraphRelationBuilder::build_object(Main *bmain, Scene *scene, Object *ob)
413 if (ob->id.tag & LIB_TAG_DOIT) {
416 ob->id.tag |= LIB_TAG_DOIT;
418 /* Object Transforms */
419 eDepsOperation_Code base_op = (ob->parent) ? DEG_OPCODE_TRANSFORM_PARENT : DEG_OPCODE_TRANSFORM_LOCAL;
420 OperationKey base_op_key(&ob->id, DEG_NODE_TYPE_TRANSFORM, base_op);
422 OperationKey local_transform_key(&ob->id, DEG_NODE_TYPE_TRANSFORM, DEG_OPCODE_TRANSFORM_LOCAL);
423 OperationKey parent_transform_key(&ob->id, DEG_NODE_TYPE_TRANSFORM, DEG_OPCODE_TRANSFORM_PARENT);
424 OperationKey final_transform_key(&ob->id, DEG_NODE_TYPE_TRANSFORM, DEG_OPCODE_TRANSFORM_FINAL);
426 OperationKey ob_ubereval_key(&ob->id, DEG_NODE_TYPE_TRANSFORM, DEG_OPCODE_TRANSFORM_OBJECT_UBEREVAL);
429 if (ob->parent != NULL) {
430 /* parent relationship */
431 build_object_parent(ob);
433 /* local -> parent */
434 add_relation(local_transform_key, parent_transform_key, "[ObLocal -> ObParent]");
437 if (ob->modifiers.first != NULL) {
438 BuilderWalkUserData data;
442 modifiers_foreachObjectLink(ob, modifier_walk, &data);
444 if (ob->constraints.first != NULL) {
445 BuilderWalkUserData data;
449 BKE_constraints_id_loop(&ob->constraints, constraint_walk, &data);
452 /* object constraints */
453 if (ob->constraints.first != NULL) {
454 OperationKey constraint_key(&ob->id,
455 DEG_NODE_TYPE_TRANSFORM,
456 DEG_OPCODE_TRANSFORM_CONSTRAINTS);
458 /* constraint relations */
459 // TODO: provide base op
460 // XXX: this is broken
461 build_constraints(scene,
463 DEG_NODE_TYPE_TRANSFORM,
468 /* operation order */
469 add_relation(base_op_key, constraint_key, "[ObBase-> Constraint Stack]");
470 add_relation(constraint_key, final_transform_key, "[ObConstraints -> Done]");
473 add_relation(constraint_key, ob_ubereval_key, "Temp Ubereval");
474 add_relation(ob_ubereval_key, final_transform_key, "Temp Ubereval");
477 /* NOTE: Keep an eye here, we skip some relations here to "streamline"
478 * dependencies and avoid transitive relations which causes overhead.
479 * But once we get rid of uber eval node this will need reconsideration.
481 if (ob->rigidbody_object == NULL) {
482 /* Rigid body will hook up another node inbetween, so skip
483 * relation here to avoid transitive relation.
485 add_relation(base_op_key, ob_ubereval_key, "Temp Ubereval");
487 add_relation(ob_ubereval_key, final_transform_key, "Temp Ubereval");
491 build_animdata(&ob->id);
493 // XXX: This should be hooked up by the build_animdata code
494 if (needs_animdata_node(&ob->id)) {
495 ComponentKey adt_key(&ob->id, DEG_NODE_TYPE_ANIMATION);
496 add_relation(adt_key, local_transform_key, "Object Animation");
501 ID *obdata_id = (ID *)ob->data;
503 /* ob data animation */
504 build_animdata(obdata_id);
506 /* type-specific data... */
508 case OB_MESH: /* Geometry */
515 build_obdata_geom(bmain, scene, ob);
519 case OB_ARMATURE: /* Pose */
520 if (ID_IS_LINKED_DATABLOCK(ob) && ob->proxy_from != NULL) {
524 build_rig(bmain, scene, ob);
528 case OB_LAMP: /* Lamp */
532 case OB_CAMERA: /* Camera */
537 build_lightprobe(ob);
541 Key *key = BKE_key_from_object(ob);
543 ComponentKey geometry_key((ID *)ob->data, DEG_NODE_TYPE_GEOMETRY);
544 ComponentKey key_key(&key->id, DEG_NODE_TYPE_GEOMETRY);
545 add_relation(key_key, geometry_key, "Shapekeys");
549 /* Particle systems. */
550 if (ob->particlesystem.first != NULL) {
551 build_particles(scene, ob);
555 if (ob->gpd != NULL) {
556 build_gpencil(ob->gpd);
559 /* Object that this is a proxy for. */
560 if (ob->proxy != NULL) {
561 ob->proxy->proxy_from = ob;
562 build_object(bmain, scene, ob->proxy);
563 /* TODO(sergey): This is an inverted relation, matches old depsgraph
564 * behavior and need to be investigated if it still need to be inverted.
566 ComponentKey ob_pose_key(&ob->id, DEG_NODE_TYPE_EVAL_POSE);
567 ComponentKey proxy_pose_key(&ob->proxy->id, DEG_NODE_TYPE_EVAL_POSE);
568 add_relation(ob_pose_key, proxy_pose_key, "Proxy");
571 /* Object dupligroup. */
572 if (ob->dup_group != NULL) {
573 build_group(bmain, scene, ob, ob->dup_group);
577 void DepsgraphRelationBuilder::build_object_parent(Object *ob)
579 /* XXX: for now, need to use the component key (not just direct to the parent op),
580 * or else the matrix doesn't get reset/
582 // XXX: @sergey - it would be good if we got that backwards flushing working
583 // when tagging for updates.
584 //OperationKey ob_key(&ob->id, DEG_NODE_TYPE_TRANSFORM, DEG_OPCODE_TRANSFORM_PARENT);
585 ComponentKey ob_key(&ob->id, DEG_NODE_TYPE_TRANSFORM);
587 /* type-specific links */
588 switch (ob->partype) {
589 case PARSKEL: /* Armature Deform (Virtual Modifier) */
591 ComponentKey parent_key(&ob->parent->id, DEG_NODE_TYPE_TRANSFORM);
592 add_relation(parent_key, ob_key, "Armature Deform Parent");
596 case PARVERT1: /* Vertex Parent */
599 ComponentKey parent_key(&ob->parent->id, DEG_NODE_TYPE_GEOMETRY);
600 add_relation(parent_key, ob_key, "Vertex Parent");
602 /* XXX not sure what this is for or how you could be done properly - lukas */
603 OperationDepsNode *parent_node = find_operation_node(parent_key);
604 if (parent_node != NULL) {
605 parent_node->customdata_mask |= CD_MASK_ORIGINDEX;
608 ComponentKey transform_key(&ob->parent->id, DEG_NODE_TYPE_TRANSFORM);
609 add_relation(transform_key, ob_key, "Vertex Parent TFM");
613 case PARBONE: /* Bone Parent */
615 ComponentKey parent_bone_key(&ob->parent->id,
618 OperationKey parent_transform_key(&ob->parent->id,
619 DEG_NODE_TYPE_TRANSFORM,
620 DEG_OPCODE_TRANSFORM_FINAL);
621 add_relation(parent_bone_key, ob_key, "Bone Parent");
622 add_relation(parent_transform_key, ob_key, "Armature Parent");
628 if (ob->parent->type == OB_LATTICE) {
629 /* Lattice Deform Parent - Virtual Modifier */
630 // XXX: no virtual modifiers should be left!
631 ComponentKey parent_key(&ob->parent->id, DEG_NODE_TYPE_TRANSFORM);
632 ComponentKey geom_key(&ob->parent->id, DEG_NODE_TYPE_GEOMETRY);
634 add_relation(parent_key, ob_key, "Lattice Deform Parent");
635 add_relation(geom_key, ob_key, "Lattice Deform Parent Geom");
637 else if (ob->parent->type == OB_CURVE) {
638 Curve *cu = (Curve *)ob->parent->data;
640 if (cu->flag & CU_PATH) {
642 ComponentKey parent_key(&ob->parent->id, DEG_NODE_TYPE_GEOMETRY);
643 add_relation(parent_key, ob_key, "Curve Follow Parent");
645 ComponentKey transform_key(&ob->parent->id, DEG_NODE_TYPE_TRANSFORM);
646 add_relation(transform_key, ob_key, "Curve Follow TFM");
649 /* Standard Parent */
650 ComponentKey parent_key(&ob->parent->id, DEG_NODE_TYPE_TRANSFORM);
651 add_relation(parent_key, ob_key, "Curve Parent");
655 /* Standard Parent */
656 ComponentKey parent_key(&ob->parent->id, DEG_NODE_TYPE_TRANSFORM);
657 add_relation(parent_key, ob_key, "Parent");
663 /* exception case: parent is duplivert */
664 if ((ob->type == OB_MBALL) && (ob->parent->transflag & OB_DUPLIVERTS)) {
665 //dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA | DAG_RL_OB_OB, "Duplivert");
669 void DepsgraphRelationBuilder::build_constraints(Scene *scene, ID *id,
670 eDepsNode_Type component_type,
671 const char *component_subdata,
672 ListBase *constraints,
673 RootPChanMap *root_map)
675 OperationKey constraint_op_key(
679 (component_type == DEG_NODE_TYPE_BONE)
680 ? DEG_OPCODE_BONE_CONSTRAINTS
681 : DEG_OPCODE_TRANSFORM_CONSTRAINTS);
682 /* Add dependencies for each constraint in turn. */
683 for (bConstraint *con = (bConstraint *)constraints->first; con; con = con->next) {
684 const bConstraintTypeInfo *cti = BKE_constraint_typeinfo_get(con);
685 /* Invalid constraint type. */
689 /* Special case for camera tracking -- it doesn't use targets to
692 /* TODO: we can now represent dependencies in a much richer manner,
693 * so review how this is done.
696 CONSTRAINT_TYPE_FOLLOWTRACK,
697 CONSTRAINT_TYPE_CAMERASOLVER,
698 CONSTRAINT_TYPE_OBJECTSOLVER))
700 bool depends_on_camera = false;
701 if (cti->type == CONSTRAINT_TYPE_FOLLOWTRACK) {
702 bFollowTrackConstraint *data = (bFollowTrackConstraint *)con->data;
704 (data->flag & FOLLOWTRACK_ACTIVECLIP)) && data->track[0])
706 depends_on_camera = true;
708 if (data->depth_ob) {
709 ComponentKey depth_transform_key(&data->depth_ob->id,
710 DEG_NODE_TYPE_TRANSFORM);
711 ComponentKey depth_geometry_key(&data->depth_ob->id,
712 DEG_NODE_TYPE_GEOMETRY);
713 add_relation(depth_transform_key, constraint_op_key, cti->name);
714 add_relation(depth_geometry_key, constraint_op_key, cti->name);
717 else if (cti->type == CONSTRAINT_TYPE_OBJECTSOLVER) {
718 depends_on_camera = true;
720 if (depends_on_camera && scene->camera) {
721 ComponentKey camera_key(&scene->camera->id, DEG_NODE_TYPE_TRANSFORM);
722 add_relation(camera_key, constraint_op_key, cti->name);
724 /* TODO(sergey): This is more a TimeSource -> MovieClip ->
725 * Constraint dependency chain.
727 TimeSourceKey time_src_key;
728 add_relation(time_src_key, constraint_op_key, "[TimeSrc -> Animation]");
730 else if (cti->type == CONSTRAINT_TYPE_TRANSFORM_CACHE) {
731 /* TODO(kevin): This is more a TimeSource -> CacheFile -> Constraint
734 TimeSourceKey time_src_key;
735 add_relation(time_src_key, constraint_op_key, "[TimeSrc -> Animation]");
736 bTransformCacheConstraint *data = (bTransformCacheConstraint *)con->data;
737 if (data->cache_file) {
738 ComponentKey cache_key(&data->cache_file->id, DEG_NODE_TYPE_CACHE);
739 add_relation(cache_key, constraint_op_key, cti->name);
742 else if (cti->get_constraint_targets) {
743 ListBase targets = {NULL, NULL};
744 cti->get_constraint_targets(con, &targets);
745 LINKLIST_FOREACH (bConstraintTarget *, ct, &targets) {
746 if (ct->tar == NULL) {
750 CONSTRAINT_TYPE_KINEMATIC,
751 CONSTRAINT_TYPE_SPLINEIK))
753 /* Ignore IK constraints - these are handled separately
757 else if (ELEM(con->type,
758 CONSTRAINT_TYPE_FOLLOWPATH,
759 CONSTRAINT_TYPE_CLAMPTO))
761 /* These constraints require path geometry data. */
762 ComponentKey target_key(&ct->tar->id, DEG_NODE_TYPE_GEOMETRY);
763 add_relation(target_key, constraint_op_key, cti->name);
764 ComponentKey target_transform_key(&ct->tar->id,
765 DEG_NODE_TYPE_TRANSFORM);
766 add_relation(target_transform_key, constraint_op_key, cti->name);
768 else if ((ct->tar->type == OB_ARMATURE) && (ct->subtarget[0])) {
770 if (&ct->tar->id == id) {
772 eDepsOperation_Code target_key_opcode;
773 /* Using "done" here breaks in-chain deps, while using
774 * "ready" here breaks most production rigs instead.
775 * So, we do a compromise here, and only do this when an
776 * IK chain conflict may occur.
778 if (root_map->has_common_root(component_subdata,
781 target_key_opcode = DEG_OPCODE_BONE_READY;
784 target_key_opcode = DEG_OPCODE_BONE_DONE;
786 OperationKey target_key(&ct->tar->id,
790 add_relation(target_key, constraint_op_key, cti->name);
793 /* Different armature - we can safely use the result
796 OperationKey target_key(&ct->tar->id,
799 DEG_OPCODE_BONE_DONE);
800 add_relation(target_key, constraint_op_key, cti->name);
803 else if (ELEM(ct->tar->type, OB_MESH, OB_LATTICE) &&
807 /* NOTE: for now, we don't need to represent vertex groups
810 ComponentKey target_key(&ct->tar->id, DEG_NODE_TYPE_GEOMETRY);
811 add_relation(target_key, constraint_op_key, cti->name);
812 if (ct->tar->type == OB_MESH) {
813 OperationDepsNode *node2 = find_operation_node(target_key);
815 node2->customdata_mask |= CD_MASK_MDEFORMVERT;
819 else if (con->type == CONSTRAINT_TYPE_SHRINKWRAP) {
820 /* Constraints which requires the target object surface. */
821 ComponentKey target_key(&ct->tar->id, DEG_NODE_TYPE_GEOMETRY);
822 add_relation(target_key, constraint_op_key, cti->name);
823 /* NOTE: obdata eval now doesn't necessarily depend on the
824 * object's transform.
826 ComponentKey target_transform_key(&ct->tar->id,
827 DEG_NODE_TYPE_TRANSFORM);
828 add_relation(target_transform_key, constraint_op_key, cti->name);
831 /* Standard object relation. */
832 // TODO: loc vs rot vs scale?
833 if (&ct->tar->id == id) {
834 /* Constraint targetting own object:
835 * - This case is fine IFF we're dealing with a bone
836 * constraint pointing to its own armature. In that
837 * case, it's just transform -> bone.
838 * - If however it is a real self targetting case, just
839 * make it depend on the previous constraint (or the
840 * pre-constraint state).
842 if ((ct->tar->type == OB_ARMATURE) &&
843 (component_type == DEG_NODE_TYPE_BONE))
845 OperationKey target_key(&ct->tar->id,
846 DEG_NODE_TYPE_TRANSFORM,
847 DEG_OPCODE_TRANSFORM_FINAL);
848 add_relation(target_key, constraint_op_key, cti->name);
851 OperationKey target_key(&ct->tar->id,
852 DEG_NODE_TYPE_TRANSFORM,
853 DEG_OPCODE_TRANSFORM_LOCAL);
854 add_relation(target_key, constraint_op_key, cti->name);
858 /* Normal object dependency. */
859 OperationKey target_key(&ct->tar->id,
860 DEG_NODE_TYPE_TRANSFORM,
861 DEG_OPCODE_TRANSFORM_FINAL);
862 add_relation(target_key, constraint_op_key, cti->name);
865 /* Constraints which needs world's matrix for transform.
866 * TODO(sergey): More constraints here?
869 CONSTRAINT_TYPE_ROTLIKE,
870 CONSTRAINT_TYPE_SIZELIKE,
871 CONSTRAINT_TYPE_LOCLIKE,
872 CONSTRAINT_TYPE_TRANSLIKE))
874 /* TODO(sergey): Add used space check. */
875 ComponentKey target_transform_key(&ct->tar->id,
876 DEG_NODE_TYPE_TRANSFORM);
877 add_relation(target_transform_key, constraint_op_key, cti->name);
880 if (cti->flush_constraint_targets) {
881 cti->flush_constraint_targets(con, &targets, 1);
887 void DepsgraphRelationBuilder::build_animdata(ID *id)
889 AnimData *adt = BKE_animdata_from_id(id);
894 ComponentKey adt_key(id, DEG_NODE_TYPE_ANIMATION);
897 if (adt->action || adt->nla_tracks.first) {
898 /* wire up dependency to time source */
899 TimeSourceKey time_src_key;
900 add_relation(time_src_key, adt_key, "[TimeSrc -> Animation]");
902 // XXX: Hook up specific update callbacks for special properties which may need it...
904 // XXX: animdata "hierarchy" - top-level overrides need to go after lower-down
908 LINKLIST_FOREACH (FCurve *, fcu, &adt->drivers) {
909 OperationKey driver_key(id,
910 DEG_NODE_TYPE_PARAMETERS,
912 fcu->rna_path ? fcu->rna_path : "",
915 /* create the driver's relations to targets */
916 build_driver(id, fcu);
918 /* Special case for array drivers: we can not multithread them because
919 * of the way how they work internally: animation system will write the
920 * whole array back to RNA even when changing individual array value.
922 * Some tricky things here:
923 * - array_index is -1 for single channel drivers, meaning we only have
924 * to do some magic when array_index is not -1.
925 * - We do relation from next array index to a previous one, so we don't
926 * have to deal with array index 0.
928 * TODO(sergey): Avoid liner lookup somehow.
930 if (fcu->array_index > 0) {
931 FCurve *fcu_prev = NULL;
932 LINKLIST_FOREACH (FCurve *, fcu_candidate, &adt->drivers) {
933 /* Writing to different RNA paths is */
934 const char *rna_path = fcu->rna_path ? fcu->rna_path : "";
935 if (!STREQ(fcu_candidate->rna_path, rna_path)) {
938 /* We only do relation from previous fcurve to previous one. */
939 if (fcu_candidate->array_index >= fcu->array_index) {
942 /* Choose fcurve with highest possible array index. */
943 if (fcu_prev == NULL ||
944 fcu_candidate->array_index > fcu_prev->array_index)
946 fcu_prev = fcu_candidate;
949 if (fcu_prev != NULL) {
950 OperationKey prev_driver_key(id,
951 DEG_NODE_TYPE_PARAMETERS,
953 fcu_prev->rna_path ? fcu_prev->rna_path : "",
954 fcu_prev->array_index);
955 OperationKey driver_key(id,
956 DEG_NODE_TYPE_PARAMETERS,
958 fcu->rna_path ? fcu->rna_path : "",
960 add_relation(prev_driver_key, driver_key, "[Driver Order]");
964 /* prevent driver from occurring before own animation... */
965 if (adt->action || adt->nla_tracks.first) {
966 add_relation(adt_key, driver_key, "[AnimData Before Drivers]");
971 void DepsgraphRelationBuilder::build_driver(ID *id, FCurve *fcu)
973 ChannelDriver *driver = fcu->driver;
974 OperationKey driver_key(id,
975 DEG_NODE_TYPE_PARAMETERS,
977 fcu->rna_path ? fcu->rna_path : "",
979 bPoseChannel *pchan = NULL;
980 const char *rna_path = fcu->rna_path ? fcu->rna_path : "";
981 const ID_Type id_type = GS(id->name);
983 /* Create dependency between driver and data affected by it. */
984 /* - direct property relationship... */
985 //RNAPathKey affected_key(id, fcu->rna_path);
986 //add_relation(driver_key, affected_key, "[Driver -> Data] DepsRel");
988 /* Driver -> data components (for interleaved evaluation
989 * bones/constraints/modifiers).
991 // XXX: this probably should probably be moved out into a separate function.
992 if (strstr(rna_path, "pose.bones[") != NULL) {
993 /* interleaved drivers during bone eval */
994 /* TODO: ideally, if this is for a constraint, it goes to said
997 Object *ob = (Object *)id;
998 char *bone_name = BLI_str_quoted_substrN(rna_path, "pose.bones[");
999 pchan = BKE_pose_channel_find_name(ob->pose, bone_name);
1000 if (bone_name != NULL) {
1001 MEM_freeN(bone_name);
1004 if (pchan != NULL) {
1005 OperationKey bone_key(id,
1008 DEG_OPCODE_BONE_LOCAL);
1009 add_relation(driver_key, bone_key, "[Driver -> Bone]");
1013 "Couldn't find bone name for driver path - '%s'\n",
1017 else if (id_type == ID_AR && strstr(rna_path, "bones[")) {
1018 /* Drivers on armature-level bone settings (i.e. bbone stuff),
1019 * which will affect the evaluation of corresponding pose bones.
1021 IDDepsNode *arm_node = m_graph->find_id_node(id);
1022 char *bone_name = BLI_str_quoted_substrN(rna_path, "bones[");
1023 if (arm_node != NULL && bone_name != NULL) {
1024 /* Find objects which use this, and make their eval callbacks
1027 foreach (DepsRelation *rel, arm_node->outlinks) {
1028 IDDepsNode *to_node = (IDDepsNode *)rel->to;
1029 /* We only care about objects with pose data which use this. */
1030 if (GS(to_node->id_orig->name) == ID_OB) {
1031 Object *ob = (Object *)to_node->id_orig;
1032 // NOTE: ob->pose may be NULL
1033 bPoseChannel *pchan = BKE_pose_channel_find_name(ob->pose,
1035 if (pchan != NULL) {
1036 OperationKey bone_key(&ob->id,
1039 DEG_OPCODE_BONE_LOCAL);
1040 add_relation(driver_key,
1042 "[Arm Bone -> Driver -> Bone]");
1046 /* Free temp data. */
1047 MEM_freeN(bone_name);
1052 "Couldn't find armature bone name for driver path - '%s'\n",
1056 else if (id_type == ID_OB && strstr(rna_path, "modifiers[")) {
1057 OperationKey modifier_key(id,
1058 DEG_NODE_TYPE_GEOMETRY,
1059 DEG_OPCODE_GEOMETRY_UBEREVAL);
1060 if (has_node(modifier_key)) {
1061 add_relation(driver_key, modifier_key, "[Driver -> Modifier]");
1064 printf("Unexisting driver RNA path: %s\n", rna_path);
1067 else if (id_type == ID_KE && strstr(rna_path, "key_blocks[")) {
1068 /* Shape key driver - hook into the base geometry operation. */
1069 // XXX: double check where this points
1070 Key *shape_key = (Key *)id;
1071 ComponentKey geometry_key(shape_key->from, DEG_NODE_TYPE_GEOMETRY);
1072 add_relation(driver_key, geometry_key, "[Driver -> ShapeKey Geom]");
1074 else if (strstr(rna_path, "key_blocks[")) {
1075 ComponentKey geometry_key(id, DEG_NODE_TYPE_GEOMETRY);
1076 add_relation(driver_key, geometry_key, "[Driver -> ShapeKey Geom]");
1082 /* Assume that driver affects a transform. */
1083 OperationKey local_transform_key(id,
1084 DEG_NODE_TYPE_TRANSFORM,
1085 DEG_OPCODE_TRANSFORM_LOCAL);
1086 add_relation(driver_key,
1087 local_transform_key,
1088 "[Driver -> Transform]");
1093 ComponentKey geometry_key(id, DEG_NODE_TYPE_GEOMETRY);
1094 add_relation(driver_key,
1096 "[Driver -> Shapekey Geometry]");
1101 ComponentKey ntree_key(id, DEG_NODE_TYPE_SHADING);
1102 add_relation(driver_key,
1104 "[Driver -> NTree Shading Update]");
1111 /* Ensure that affected prop's update callbacks will be triggered once
1114 /* TODO: Implement this once the functionality to add these links exists
1117 /* XXX: the data itself could also set this, if it were to be truly
1118 * initialised later?
1120 /* Loop over variables to get the target relationships. */
1121 LINKLIST_FOREACH (DriverVar *, dvar, &driver->variables) {
1122 /* Only used targets. */
1123 DRIVER_TARGETS_USED_LOOPER(dvar)
1125 if (dtar->id == NULL) {
1128 /* Special handling for directly-named bones. */
1129 if ((dtar->flag & DTAR_FLAG_STRUCT_REF) && (dtar->pchan_name[0])) {
1130 Object *ob = (Object *)dtar->id;
1131 bPoseChannel *target_pchan =
1132 BKE_pose_channel_find_name(ob->pose, dtar->pchan_name);
1133 if (target_pchan != NULL) {
1134 /* Get node associated with bone. */
1135 // XXX: watch the space!
1136 /* Some cases can't use final bone transform, for example:
1137 * - Driving the bone with itself (addressed here)
1138 * - Relations inside an IK chain (TODO?)
1140 if (dtar->id == id &&
1142 STREQ(pchan->name, target_pchan->name))
1146 OperationKey target_key(dtar->id,
1149 DEG_OPCODE_BONE_DONE);
1150 add_relation(target_key,
1152 "[Bone Target -> Driver]");
1155 else if (dtar->flag & DTAR_FLAG_STRUCT_REF) {
1156 /* Get node associated with the object's transforms. */
1157 if (dtar->id == id) {
1158 /* Ignore input dependency if we're driving properties of
1159 * the same ID, otherwise we'll be ending up in a cyclic
1164 OperationKey target_key(dtar->id,
1165 DEG_NODE_TYPE_TRANSFORM,
1166 DEG_OPCODE_TRANSFORM_FINAL);
1167 add_relation(target_key, driver_key, "[Target -> Driver]");
1169 else if (dtar->rna_path && strstr(dtar->rna_path, "pose.bones[")) {
1170 /* Workaround for ensuring that local bone transforms don't end
1171 * up having to wait for pose eval to finish (to prevent cycles).
1173 Object *ob = (Object *)dtar->id;
1174 char *bone_name = BLI_str_quoted_substrN(dtar->rna_path,
1176 bPoseChannel *target_pchan =
1177 BKE_pose_channel_find_name(ob->pose, bone_name);
1178 if (bone_name != NULL) {
1179 MEM_freeN(bone_name);
1182 if (target_pchan != NULL) {
1183 if (dtar->id == id &&
1185 STREQ(pchan->name, target_pchan->name))
1189 OperationKey bone_key(dtar->id,
1192 DEG_OPCODE_BONE_LOCAL);
1193 add_relation(bone_key, driver_key, "[RNA Bone -> Driver]");
1197 if (dtar->id == id) {
1198 /* Ignore input dependency if we're driving properties of
1199 * the same ID, otherwise we'll be ending up in a cyclic
1204 /* Resolve path to get node. */
1205 RNAPathKey target_key(dtar->id,
1206 dtar->rna_path ? dtar->rna_path : "");
1207 add_relation(target_key, driver_key, "[RNA Target -> Driver]");
1210 DRIVER_TARGETS_LOOPER_END
1212 /* It's quite tricky to detect if the driver actually depends on time or
1213 * not, so for now we'll be quite conservative here about optimization and
1214 * consider all python drivers to be depending on time.
1216 if ((driver->type == DRIVER_TYPE_PYTHON) &&
1217 python_driver_depends_on_time(driver))
1219 TimeSourceKey time_src_key;
1220 add_relation(time_src_key, driver_key, "[TimeSrc -> Driver]");
1224 void DepsgraphRelationBuilder::build_world(World *world)
1226 ID *world_id = &world->id;
1227 if (world_id->tag & LIB_TAG_DOIT) {
1230 world_id->tag |= LIB_TAG_DOIT;
1232 build_animdata(world_id);
1234 /* TODO: other settings? */
1237 build_texture_stack(world->mtex);
1239 /* world's nodetree */
1240 if (world->nodetree != NULL) {
1241 build_nodetree(world->nodetree);
1242 ComponentKey ntree_key(&world->nodetree->id, DEG_NODE_TYPE_SHADING);
1243 ComponentKey world_key(world_id, DEG_NODE_TYPE_SHADING);
1244 add_relation(ntree_key, world_key, "NTree->World Shading Update");
1248 void DepsgraphRelationBuilder::build_rigidbody(Scene *scene)
1250 RigidBodyWorld *rbw = scene->rigidbody_world;
1252 OperationKey init_key(&scene->id, DEG_NODE_TYPE_TRANSFORM, DEG_OPCODE_RIGIDBODY_REBUILD);
1253 OperationKey sim_key(&scene->id, DEG_NODE_TYPE_TRANSFORM, DEG_OPCODE_RIGIDBODY_SIM);
1255 /* rel between the two sim-nodes */
1256 add_relation(init_key, sim_key, "Rigidbody [Init -> SimStep]");
1258 /* set up dependencies between these operations and other builtin nodes --------------- */
1260 /* time dependency */
1261 TimeSourceKey time_src_key;
1262 add_relation(time_src_key, init_key, "TimeSrc -> Rigidbody Reset/Rebuild (Optional)");
1264 /* objects - simulation participants */
1266 LINKLIST_FOREACH (GroupObject *, go, &rbw->group->gobject) {
1267 Object *ob = go->ob;
1268 if (ob == NULL || ob->type != OB_MESH) {
1272 /* hook up evaluation order...
1273 * 1) flushing rigidbody results follows base transforms being applied
1274 * 2) rigidbody flushing can only be performed after simulation has been run
1276 * 3) simulation needs to know base transforms to figure out what to do
1277 * XXX: there's probably a difference between passive and active
1278 * - passive don't change, so may need to know full transform...
1280 OperationKey rbo_key(&ob->id, DEG_NODE_TYPE_TRANSFORM, DEG_OPCODE_RIGIDBODY_TRANSFORM_COPY);
1282 eDepsOperation_Code trans_opcode = ob->parent ? DEG_OPCODE_TRANSFORM_PARENT : DEG_OPCODE_TRANSFORM_LOCAL;
1283 OperationKey trans_op(&ob->id, DEG_NODE_TYPE_TRANSFORM, trans_opcode);
1285 add_relation(sim_key, rbo_key, "Rigidbody Sim Eval -> RBO Sync");
1287 /* if constraints exist, those depend on the result of the rigidbody sim
1288 * - This allows constraints to modify the result of the sim (i.e. clamping)
1289 * while still allowing the sim to depend on some changes to the objects.
1290 * Also, since constraints are hooked up to the final nodes, this link
1291 * means that we can also fit in there too...
1292 * - Later, it might be good to include a constraint in the stack allowing us
1293 * to control whether rigidbody eval gets interleaved into the constraint stack
1295 if (ob->constraints.first) {
1296 OperationKey constraint_key(&ob->id,
1297 DEG_NODE_TYPE_TRANSFORM,
1298 DEG_OPCODE_TRANSFORM_CONSTRAINTS);
1299 add_relation(rbo_key, constraint_key, "RBO Sync -> Ob Constraints");
1302 /* Final object transform depends on rigidbody.
1304 * NOTE: Currently we consider final here an ubereval node.
1305 * If it is gone we'll need to reconsider relation here.
1307 OperationKey uber_key(&ob->id,
1308 DEG_NODE_TYPE_TRANSFORM,
1309 DEG_OPCODE_TRANSFORM_OBJECT_UBEREVAL);
1310 add_relation(rbo_key, uber_key, "RBO Sync -> Uber (Temp)");
1313 /* Needed to get correct base values. */
1314 add_relation(trans_op, sim_key, "Base Ob Transform -> Rigidbody Sim Eval");
1319 if (rbw->constraints) {
1320 LINKLIST_FOREACH (GroupObject *, go, &rbw->constraints->gobject) {
1321 Object *ob = go->ob;
1322 if (ob == NULL || !ob->rigidbody_constraint) {
1326 RigidBodyCon *rbc = ob->rigidbody_constraint;
1328 /* final result of the constraint object's transform controls how the
1329 * constraint affects the physics sim for these objects
1331 ComponentKey trans_key(&ob->id, DEG_NODE_TYPE_TRANSFORM);
1332 OperationKey ob1_key(&rbc->ob1->id, DEG_NODE_TYPE_TRANSFORM, DEG_OPCODE_RIGIDBODY_TRANSFORM_COPY);
1333 OperationKey ob2_key(&rbc->ob2->id, DEG_NODE_TYPE_TRANSFORM, DEG_OPCODE_RIGIDBODY_TRANSFORM_COPY);
1335 /* - constrained-objects sync depends on the constraint-holder */
1336 add_relation(trans_key, ob1_key, "RigidBodyConstraint -> RBC.Object_1");
1337 add_relation(trans_key, ob2_key, "RigidBodyConstraint -> RBC.Object_2");
1339 /* - ensure that sim depends on this constraint's transform */
1340 add_relation(trans_key, sim_key, "RigidBodyConstraint Transform -> RB Simulation");
1345 void DepsgraphRelationBuilder::build_particles(Scene *scene, Object *ob)
1347 TimeSourceKey time_src_key;
1348 OperationKey obdata_ubereval_key(&ob->id,
1349 DEG_NODE_TYPE_GEOMETRY,
1350 DEG_OPCODE_GEOMETRY_UBEREVAL);
1351 OperationKey eval_init_key(&ob->id,
1352 DEG_NODE_TYPE_EVAL_PARTICLES,
1353 DEG_OPCODE_PARTICLE_SYSTEM_EVAL_INIT);
1354 /* TODO(sergey): Are all particle systems depends on time?
1355 * Hair without dynamics i.e.
1357 add_relation(time_src_key, eval_init_key, "TimeSrc -> PSys");
1359 /* particle systems */
1360 LINKLIST_FOREACH (ParticleSystem *, psys, &ob->particlesystem) {
1361 ParticleSettings *part = psys->part;
1363 /* Build particle settings relations.
1365 * NOTE: The call itself ensures settings are only build once.
1367 build_particle_settings(part);
1369 /* This particle system. */
1370 OperationKey psys_key(&ob->id,
1371 DEG_NODE_TYPE_EVAL_PARTICLES,
1372 DEG_OPCODE_PARTICLE_SYSTEM_EVAL,
1375 /* Update particle system when settings changes. */
1376 OperationKey particle_settings_key(&part->id,
1377 DEG_NODE_TYPE_PARAMETERS,
1378 DEG_OPCODE_PARTICLE_SETTINGS_EVAL);
1379 OperationKey particle_settings_recalc_clear_key(
1381 DEG_NODE_TYPE_PARAMETERS,
1382 DEG_OPCODE_PARTICLE_SETTINGS_RECALC_CLEAR);
1383 OperationKey psys_settings_key(&ob->id,
1384 DEG_NODE_TYPE_EVAL_PARTICLES,
1385 DEG_OPCODE_PARTICLE_SETTINGS_EVAL,
1387 add_relation(particle_settings_key, psys_settings_key, "Particle Settings Change");
1388 add_relation(psys_settings_key, psys_key, "Particle Settings Update");
1389 add_relation(psys_key,
1390 particle_settings_recalc_clear_key,
1391 "Particle Settings Recalc Clear");
1393 /* XXX: if particle system is later re-enabled, we must do full rebuild? */
1394 if (!psys_check_enabled(ob, psys, G.is_rendering))
1397 add_relation(eval_init_key, psys_key, "Init -> PSys");
1399 /* TODO(sergey): Currently particle update is just a placeholder,
1400 * hook it to the ubereval node so particle system is getting updated
1403 add_relation(psys_key, obdata_ubereval_key, "PSys -> UberEval");
1406 if (part->type != PART_HAIR) {
1407 add_collision_relations(psys_key,
1410 part->collision_group,
1412 "Particle Collision");
1414 else if ((psys->flag & PSYS_HAIR_DYNAMICS) &&
1415 psys->clmd != NULL &&
1416 psys->clmd->coll_parms != NULL)
1418 add_collision_relations(psys_key,
1421 psys->clmd->coll_parms->group,
1427 add_forcefield_relations(psys_key,
1431 part->effector_weights,
1432 part->type == PART_HAIR,
1437 LINKLIST_FOREACH (BoidState *, state, &part->boids->states) {
1438 LINKLIST_FOREACH (BoidRule *, rule, &state->rules) {
1439 Object *ruleob = NULL;
1440 if (rule->type == eBoidRuleType_Avoid)
1441 ruleob = ((BoidRuleGoalAvoid *)rule)->ob;
1442 else if (rule->type == eBoidRuleType_FollowLeader)
1443 ruleob = ((BoidRuleFollowLeader *)rule)->ob;
1446 ComponentKey ruleob_key(&ruleob->id, DEG_NODE_TYPE_TRANSFORM);
1447 add_relation(ruleob_key, psys_key, "Boid Rule");
1453 if (part->ren_as == PART_DRAW_OB && part->dup_ob) {
1454 ComponentKey dup_ob_key(&part->dup_ob->id, DEG_NODE_TYPE_TRANSFORM);
1455 add_relation(dup_ob_key, psys_key, "Particle Object Visualization");
1459 /* Particle depends on the object transform, so that channel is to be ready
1462 * TODO(sergey): This relation should be altered once real granular update
1465 ComponentKey transform_key(&ob->id, DEG_NODE_TYPE_TRANSFORM);
1466 add_relation(transform_key, obdata_ubereval_key, "Partcile Eval");
1468 /* TODO(sergey): Do we need a point cache operations here? */
1471 void DepsgraphRelationBuilder::build_particle_settings(ParticleSettings *part)
1473 ID *part_id = &part->id;
1474 if (part_id->tag & LIB_TAG_DOIT) {
1477 part_id->tag |= LIB_TAG_DOIT;
1479 /* Animation data relations. */
1480 build_animdata(&part->id);
1482 OperationKey eval_key(part_id,
1483 DEG_NODE_TYPE_PARAMETERS,
1484 DEG_OPCODE_PARTICLE_SETTINGS_EVAL);
1485 OperationKey recalc_clear_key(part_id,
1486 DEG_NODE_TYPE_PARAMETERS,
1487 DEG_OPCODE_PARTICLE_SETTINGS_RECALC_CLEAR);
1488 add_relation(eval_key, recalc_clear_key, "Particle Settings Clear Recalc");
1491 void DepsgraphRelationBuilder::build_cloth(Scene * /*scene*/,
1493 ModifierData * /*md*/)
1495 OperationKey cache_key(&object->id,
1496 DEG_NODE_TYPE_CACHE,
1497 DEG_OPCODE_GEOMETRY_CLOTH_MODIFIER);
1498 /* Cache component affects on modifier. */
1499 OperationKey modifier_key(&object->id,
1500 DEG_NODE_TYPE_GEOMETRY,
1501 DEG_OPCODE_GEOMETRY_UBEREVAL);
1502 add_relation(cache_key, modifier_key, "Cloth Cache -> Cloth");
1506 void DepsgraphRelationBuilder::build_shapekeys(ID *obdata, Key *key)
1508 ComponentKey obdata_key(obdata, DEG_NODE_TYPE_GEOMETRY);
1510 /* attach animdata to geometry */
1511 build_animdata(&key->id);
1514 // TODO: this should really be handled in build_animdata, since many of these cases will need it
1515 if (key->adt->action || key->adt->nla_tracks.first) {
1516 ComponentKey adt_key(&key->id, DEG_NODE_TYPE_ANIMATION);
1517 add_relation(adt_key, obdata_key, "Animation");
1520 /* NOTE: individual shapekey drivers are handled above already */
1523 /* attach to geometry */
1524 // XXX: aren't shapekeys now done as a pseudo-modifier on object?
1525 //ComponentKey key_key(&key->id, DEG_NODE_TYPE_GEOMETRY); // FIXME: this doesn't exist
1526 //add_relation(key_key, obdata_key, "Shapekeys");
1530 * ObData Geometry Evaluation
1531 * ==========================
1533 * The evaluation of geometry on objects is as follows:
1534 * - The actual evaluated of the derived geometry (e.g. DerivedMesh, DispList, etc.)
1535 * occurs in the Geometry component of the object which references this. This includes
1536 * modifiers, and the temporary "ubereval" for geometry.
1537 * - Therefore, each user of a piece of shared geometry data ends up evaluating its own
1538 * version of the stuff, complete with whatever modifiers it may use.
1540 * - The datablocks for the geometry data - "obdata" (e.g. ID_ME, ID_CU, ID_LT, etc.) are used for
1541 * 1) calculating the bounding boxes of the geometry data,
1542 * 2) aggregating inward links from other objects (e.g. for text on curve, etc.)
1543 * and also for the links coming from the shapekey datablocks
1544 * - Animation/Drivers affecting the parameters of the geometry are made to trigger
1545 * updates on the obdata geometry component, which then trigger downstream
1546 * re-evaluation of the individual instances of this geometry.
1548 // TODO: Materials and lighting should probably get their own component, instead of being lumped under geometry?
1549 void DepsgraphRelationBuilder::build_obdata_geom(Main *bmain, Scene *scene, Object *ob)
1551 ID *obdata = (ID *)ob->data;
1553 /* Init operation of object-level geometry evaluation. */
1554 OperationKey geom_init_key(&ob->id, DEG_NODE_TYPE_GEOMETRY, DEG_OPCODE_PLACEHOLDER, "Eval Init");
1556 /* get nodes for result of obdata's evaluation, and geometry evaluation on object */
1557 ComponentKey obdata_geom_key(obdata, DEG_NODE_TYPE_GEOMETRY);
1558 ComponentKey geom_key(&ob->id, DEG_NODE_TYPE_GEOMETRY);
1560 /* link components to each other */
1561 add_relation(obdata_geom_key, geom_key, "Object Geometry Base Data");
1563 OperationKey obdata_ubereval_key(&ob->id,
1564 DEG_NODE_TYPE_GEOMETRY,
1565 DEG_OPCODE_GEOMETRY_UBEREVAL);
1567 /* Special case: modifiers and DerivedMesh creation queries scene for various
1568 * things like data mask to be used. We add relation here to ensure object is
1569 * never evaluated prior to Scene's CoW is ready.
1571 OperationKey scene_key(&scene->id,
1572 DEG_NODE_TYPE_PARAMETERS,
1573 DEG_OPCODE_PLACEHOLDER,
1575 add_relation(scene_key, obdata_ubereval_key, "CoW Relation");
1578 if (ob->modifiers.first != NULL) {
1579 LINKLIST_FOREACH (ModifierData *, md, &ob->modifiers) {
1580 const ModifierTypeInfo *mti = modifierType_getInfo((ModifierType)md->type);
1582 if (mti->updateDepsgraph) {
1583 DepsNodeHandle handle = create_node_handle(obdata_ubereval_key);
1584 mti->updateDepsgraph(
1589 reinterpret_cast< ::DepsNodeHandle* >(&handle));
1592 if (BKE_object_modifier_use_time(ob, md)) {
1593 TimeSourceKey time_src_key;
1594 add_relation(time_src_key, obdata_ubereval_key, "Time Source");
1596 /* Hacky fix for T45633 (Animated modifiers aren't updated)
1598 * This check works because BKE_object_modifier_use_time() tests
1599 * for either the modifier needing time, or that it is animated.
1601 /* XXX: Remove this hack when these links are added as part of build_animdata() instead */
1602 if (modifier_dependsOnTime(md) == false && needs_animdata_node(&ob->id)) {
1603 ComponentKey animation_key(&ob->id, DEG_NODE_TYPE_ANIMATION);
1604 add_relation(animation_key, obdata_ubereval_key, "Modifier Animation");
1608 if (md->type == eModifierType_Cloth) {
1609 build_cloth(scene, ob, md);
1616 for (int a = 1; a <= ob->totcol; a++) {
1617 Material *ma = give_current_material(ob, a);
1621 if (ob->type == OB_MESH) {
1622 OperationKey material_key(&ma->id,
1623 DEG_NODE_TYPE_SHADING,
1624 DEG_OPCODE_MATERIAL_UPDATE);
1625 OperationKey shading_key(&ob->id, DEG_NODE_TYPE_SHADING, DEG_OPCODE_SHADING);
1626 add_relation(material_key, shading_key, "Material Update");
1632 /* geometry collision */
1633 if (ELEM(ob->type, OB_MESH, OB_CURVE, OB_LATTICE)) {
1634 // add geometry collider relations
1637 /* Make sure uber update is the last in the dependencies.
1639 * TODO(sergey): Get rid of this node.
1641 if (ob->type != OB_ARMATURE) {
1642 /* Armatures does no longer require uber node. */
1643 OperationKey obdata_ubereval_key(&ob->id, DEG_NODE_TYPE_GEOMETRY, DEG_OPCODE_GEOMETRY_UBEREVAL);
1644 add_relation(geom_init_key, obdata_ubereval_key, "Object Geometry UberEval");
1647 if (obdata->tag & LIB_TAG_DOIT) {
1650 obdata->tag |= LIB_TAG_DOIT;
1652 /* Link object data evaluation node to exit operation. */
1653 OperationKey obdata_geom_eval_key(obdata, DEG_NODE_TYPE_GEOMETRY, DEG_OPCODE_PLACEHOLDER, "Geometry Eval");
1654 OperationKey obdata_geom_done_key(obdata, DEG_NODE_TYPE_GEOMETRY, DEG_OPCODE_PLACEHOLDER, "Eval Done");
1655 add_relation(obdata_geom_eval_key, obdata_geom_done_key, "ObData Geom Eval Done");
1657 /* type-specific node/links */
1660 /* NOTE: This is compatibility code to support particle systems
1662 * for viewport being properly rendered in final render mode.
1663 * This relation is similar to what dag_object_time_update_flags()
1664 * was doing for mesh objects with particle system/
1666 * Ideally we need to get rid of this relation.
1668 if (ob->particlesystem.first != NULL) {
1669 TimeSourceKey time_key;
1670 OperationKey obdata_ubereval_key(&ob->id, DEG_NODE_TYPE_GEOMETRY, DEG_OPCODE_GEOMETRY_UBEREVAL);
1671 add_relation(time_key, obdata_ubereval_key, "Legacy particle time");
1677 Object *mom = BKE_mball_basis_find(scene, ob);
1679 /* motherball - mom depends on children! */
1681 /* non-motherball -> cannot be directly evaluated! */
1682 ComponentKey mom_key(&mom->id, DEG_NODE_TYPE_GEOMETRY);
1683 ComponentKey transform_key(&ob->id, DEG_NODE_TYPE_TRANSFORM);
1684 add_relation(geom_key, mom_key, "Metaball Motherball");
1685 add_relation(transform_key, mom_key, "Metaball Motherball");
1693 Curve *cu = (Curve *)obdata;
1695 /* curve's dependencies */
1696 // XXX: these needs geom data, but where is geom stored?
1698 ComponentKey bevob_key(&cu->bevobj->id, DEG_NODE_TYPE_GEOMETRY);
1699 build_object(bmain, scene, cu->bevobj);
1700 add_relation(bevob_key, geom_key, "Curve Bevel");
1703 ComponentKey taperob_key(&cu->taperobj->id, DEG_NODE_TYPE_GEOMETRY);
1704 build_object(bmain, scene, cu->taperobj);
1705 add_relation(taperob_key, geom_key, "Curve Taper");
1707 if (ob->type == OB_FONT) {
1708 if (cu->textoncurve) {
1709 ComponentKey textoncurve_key(&cu->textoncurve->id, DEG_NODE_TYPE_GEOMETRY);
1710 build_object(bmain, scene, cu->textoncurve);
1711 add_relation(textoncurve_key, geom_key, "Text on Curve");
1717 case OB_SURF: /* Nurbs Surface */
1722 case OB_LATTICE: /* Lattice */
1729 Key *key = BKE_key_from_object(ob);
1731 build_shapekeys(obdata, key);
1734 if (needs_animdata_node(obdata)) {
1735 ComponentKey animation_key(obdata, DEG_NODE_TYPE_ANIMATION);
1736 ComponentKey parameters_key(obdata, DEG_NODE_TYPE_PARAMETERS);
1737 add_relation(animation_key, parameters_key, "Geom Parameters");
1738 /* Evaluation usually depends on animation.
1739 * TODO(sergey): Need to re-hook it after granular update is implemented..
1741 add_relation(animation_key, obdata_geom_eval_key, "Animation");
1746 // TODO: Link scene-camera links in somehow...
1747 void DepsgraphRelationBuilder::build_camera(Object *ob)
1749 Camera *cam = (Camera *)ob->data;
1750 ID *camera_id = &cam->id;
1751 if (camera_id->tag & LIB_TAG_DOIT) {
1754 camera_id->tag |= LIB_TAG_DOIT;
1756 ComponentKey object_parameters_key(&ob->id, DEG_NODE_TYPE_PARAMETERS);
1757 ComponentKey camera_parameters_key(camera_id, DEG_NODE_TYPE_PARAMETERS);
1759 add_relation(camera_parameters_key, object_parameters_key,
1760 "Camera -> Object");
1762 if (needs_animdata_node(camera_id)) {
1763 ComponentKey animation_key(camera_id, DEG_NODE_TYPE_ANIMATION);
1764 add_relation(animation_key, camera_parameters_key, "Camera Parameters");
1768 if (cam->dof_ob != NULL) {
1769 ComponentKey dof_ob_key(&cam->dof_ob->id, DEG_NODE_TYPE_TRANSFORM);
1770 add_relation(dof_ob_key, object_parameters_key, "Camera DOF");
1775 void DepsgraphRelationBuilder::build_lamp(Object *ob)
1777 Lamp *la = (Lamp *)ob->data;
1778 ID *lamp_id = &la->id;
1779 if (lamp_id->tag & LIB_TAG_DOIT) {
1782 lamp_id->tag |= LIB_TAG_DOIT;
1784 ComponentKey object_parameters_key(&ob->id, DEG_NODE_TYPE_PARAMETERS);
1785 ComponentKey lamp_parameters_key(lamp_id, DEG_NODE_TYPE_PARAMETERS);
1787 add_relation(lamp_parameters_key, object_parameters_key,
1790 if (needs_animdata_node(lamp_id)) {
1791 ComponentKey animation_key(lamp_id, DEG_NODE_TYPE_ANIMATION);
1792 add_relation(animation_key, lamp_parameters_key, "Lamp Parameters");
1795 /* lamp's nodetree */
1797 build_nodetree(la->nodetree);
1798 ComponentKey nodetree_key(&la->nodetree->id, DEG_NODE_TYPE_SHADING);
1799 add_relation(nodetree_key, lamp_parameters_key, "NTree->Lamp Parameters");
1803 build_texture_stack(la->mtex);
1805 if (DEG_depsgraph_use_copy_on_write()) {
1806 /* Make sure copy on write of lamp data is always properly updated for
1809 OperationKey ob_copy_on_write_key(&ob->id,
1810 DEG_NODE_TYPE_COPY_ON_WRITE,
1811 DEG_OPCODE_COPY_ON_WRITE);
1812 OperationKey lamp_copy_on_write_key(lamp_id,
1813 DEG_NODE_TYPE_COPY_ON_WRITE,
1814 DEG_OPCODE_COPY_ON_WRITE);
1815 add_relation(lamp_copy_on_write_key, ob_copy_on_write_key, "Eval Order");
1819 void DepsgraphRelationBuilder::build_nodetree(bNodeTree *ntree)
1821 if (ntree == NULL) {
1824 ID *ntree_id = &ntree->id;
1825 build_animdata(ntree_id);
1826 ComponentKey shading_key(ntree_id, DEG_NODE_TYPE_SHADING);
1827 /* nodetree's nodes... */
1828 LINKLIST_FOREACH (bNode *, bnode, &ntree->nodes) {
1830 if (GS(bnode->id->name) == ID_MA) {
1831 build_material((Material *)bnode->id);
1833 else if (bnode->type == ID_TE) {
1834 build_texture((Tex *)bnode->id);
1836 else if (bnode->type == NODE_GROUP) {
1837 bNodeTree *group_ntree = (bNodeTree *)bnode->id;
1838 if ((group_ntree->id.tag & LIB_TAG_DOIT) == 0) {
1839 build_nodetree(group_ntree);
1840 group_ntree->id.tag |= LIB_TAG_DOIT;
1842 ComponentKey group_shading_key(&group_ntree->id,
1843 DEG_NODE_TYPE_SHADING);
1844 add_relation(group_shading_key, shading_key, "Group Node");
1849 if (needs_animdata_node(ntree_id)) {
1850 ComponentKey animation_key(ntree_id, DEG_NODE_TYPE_ANIMATION);
1851 add_relation(animation_key, shading_key, "NTree Parameters");
1854 OperationKey shading_update_key(ntree_id,
1855 DEG_NODE_TYPE_SHADING,
1856 DEG_OPCODE_MATERIAL_UPDATE);
1857 OperationKey shading_parameters_key(ntree_id,
1858 DEG_NODE_TYPE_SHADING_PARAMETERS,
1859 DEG_OPCODE_MATERIAL_UPDATE);
1860 add_relation(shading_parameters_key, shading_update_key, "NTree Shading Parameters");
1863 /* Recursively build graph for material */
1864 void DepsgraphRelationBuilder::build_material(Material *ma)
1866 ID *ma_id = &ma->id;
1867 if (ma_id->tag & LIB_TAG_DOIT) {
1870 ma_id->tag |= LIB_TAG_DOIT;
1873 build_animdata(ma_id);
1876 build_texture_stack(ma->mtex);
1878 /* material's nodetree */
1879 if (ma->nodetree != NULL) {
1880 build_nodetree(ma->nodetree);
1881 OperationKey ntree_key(&ma->nodetree->id,
1882 DEG_NODE_TYPE_SHADING,
1883 DEG_OPCODE_MATERIAL_UPDATE);
1884 OperationKey material_key(&ma->id,
1885 DEG_NODE_TYPE_SHADING,
1886 DEG_OPCODE_MATERIAL_UPDATE);
1887 add_relation(ntree_key, material_key, "Material's NTree");
1891 /* Recursively build graph for texture */
1892 void DepsgraphRelationBuilder::build_texture(Tex *tex)
1894 ID *tex_id = &tex->id;
1895 if (tex_id->tag & LIB_TAG_DOIT) {
1898 tex_id->tag |= LIB_TAG_DOIT;
1900 /* texture itself */
1901 build_animdata(tex_id);
1903 /* texture's nodetree */
1904 build_nodetree(tex->nodetree);
1907 /* Texture-stack attached to some shading datablock */
1908 void DepsgraphRelationBuilder::build_texture_stack(MTex **texture_stack)
1912 /* for now assume that all texture-stacks have same number of max items */
1913 for (i = 0; i < MAX_MTEX; i++) {
1914 MTex *mtex = texture_stack[i];
1915 if (mtex && mtex->tex)
1916 build_texture(mtex->tex);
1920 void DepsgraphRelationBuilder::build_compositor(Scene *scene)
1922 /* For now, just a plain wrapper? */
1923 build_nodetree(scene->nodetree);
1926 void DepsgraphRelationBuilder::build_gpencil(bGPdata *gpd)
1929 build_animdata(&gpd->id);
1931 // TODO: parent object (when that feature is implemented)
1934 bool DepsgraphRelationBuilder::needs_animdata_node(ID *id)
1936 AnimData *adt = BKE_animdata_from_id(id);
1938 return (adt->action != NULL) || (adt->nla_tracks.first != NULL);
1943 void DepsgraphRelationBuilder::build_cachefile(CacheFile *cache_file) {
1945 build_animdata(&cache_file->id);
1948 void DepsgraphRelationBuilder::build_mask(Mask *mask)
1950 ID *mask_id = &mask->id;
1951 /* F-Curve animation. */
1952 build_animdata(mask_id);
1953 /* Own mask animation. */
1954 OperationKey mask_animation_key(mask_id,
1955 DEG_NODE_TYPE_ANIMATION,
1956 DEG_OPCODE_MASK_ANIMATION);
1957 TimeSourceKey time_src_key;
1958 add_relation(time_src_key, mask_animation_key, "TimeSrc -> Mask Animation");
1959 /* Final mask evaluation. */
1960 ComponentKey parameters_key(mask_id, DEG_NODE_TYPE_PARAMETERS);
1961 add_relation(mask_animation_key, parameters_key, "Mask Animation -> Mask Eval");
1964 void DepsgraphRelationBuilder::build_movieclip(MovieClip *clip)
1967 build_animdata(&clip->id);
1970 void DepsgraphRelationBuilder::build_lightprobe(Object *object)
1972 LightProbe *probe = (LightProbe *)object->data;
1973 ID *probe_id = &probe->id;
1974 if (probe_id->tag & LIB_TAG_DOIT) {
1977 probe_id->tag |= LIB_TAG_DOIT;
1978 build_animdata(&probe->id);
1980 OperationKey probe_key(probe_id,
1981 DEG_NODE_TYPE_PARAMETERS,
1982 DEG_OPCODE_PLACEHOLDER,
1984 OperationKey object_key(&object->id,
1985 DEG_NODE_TYPE_PARAMETERS,
1986 DEG_OPCODE_PLACEHOLDER,
1988 add_relation(probe_key, object_key, "LightProbe Update");
1991 void DepsgraphRelationBuilder::build_copy_on_write_relations()
1993 GHASH_FOREACH_BEGIN(IDDepsNode *, id_node, m_graph->id_hash)
1995 build_copy_on_write_relations(id_node);
1997 GHASH_FOREACH_END();
2000 void DepsgraphRelationBuilder::build_copy_on_write_relations(IDDepsNode *id_node)
2002 ID *id_orig = id_node->id_orig;
2004 TimeSourceKey time_source_key;
2005 OperationKey copy_on_write_key(id_orig,
2006 DEG_NODE_TYPE_COPY_ON_WRITE,
2007 DEG_OPCODE_COPY_ON_WRITE);
2008 /* XXX: This is a quick hack to make Alt-A to work. */
2009 // add_relation(time_source_key, copy_on_write_key, "Fluxgate capacitor hack");
2010 /* Resat of code is using rather low level trickery, so need to get some
2011 * explicit pointers.
2013 DepsNode *node_cow = find_node(copy_on_write_key);
2014 OperationDepsNode *op_cow = node_cow->get_exit_operation();
2015 /* Plug any other components to this one. */
2016 GHASH_FOREACH_BEGIN(ComponentDepsNode *, comp_node, id_node->components)
2018 if (comp_node->type == DEG_NODE_TYPE_COPY_ON_WRITE) {
2019 /* Copy-on-write component never depends on itself. */
2022 if (!comp_node->depends_on_cow()) {
2023 /* Component explicitly requests to not add relation. */
2026 /* All entry operations of each component should wait for a proper
2029 OperationDepsNode *op_entry = comp_node->get_entry_operation();
2030 if (op_entry != NULL) {
2031 m_graph->add_new_relation(op_cow, op_entry, "CoW Dependency");
2033 /* All dangling operations should also be executed after copy-on-write. */
2034 GHASH_FOREACH_BEGIN(OperationDepsNode *, op_node, comp_node->operations_map)
2036 if (op_node->inlinks.size() == 0) {
2037 m_graph->add_new_relation(op_cow, op_node, "CoW Dependency");
2040 GHASH_FOREACH_END();
2041 /* NOTE: We currently ignore implicit relations to an external
2042 * datablocks for copy-on-write operations. This means, for example,
2043 * copy-on-write component of Object will not wait for copy-on-write
2044 * component of it's Mesh. This is because pointers are all known
2045 * already so remapping will happen all correct. And then If some object
2046 * evaluation step needs geometry, it will have transitive dependency
2047 * to Mesh copy-on-write already.
2050 GHASH_FOREACH_END();