830d811c4c1a49350d57262b787261abd6be5007
[blender.git] / source / blender / depsgraph / intern / builder / deg_builder_relations.cc
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
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.
8  *
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.
13  *
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.
17  *
18  * The Original Code is Copyright (C) 2013 Blender Foundation.
19  * All rights reserved.
20  *
21  * Original Author: Joshua Leung
22  * Contributor(s): Based on original depsgraph.c code - Blender Foundation (2005-2013)
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/depsgraph/intern/builder/deg_builder_relations.cc
28  *  \ingroup depsgraph
29  *
30  * Methods for constructing depsgraph
31  */
32
33 #include "intern/builder/deg_builder_relations.h"
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <cstring>  /* required for STREQ later on. */
38
39 #include "MEM_guardedalloc.h"
40
41 #include "BLI_utildefines.h"
42 #include "BLI_blenlib.h"
43
44 extern "C" {
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_types.h"
71
72 #include "BKE_action.h"
73 #include "BKE_armature.h"
74 #include "BKE_animsys.h"
75 #include "BKE_collection.h"
76 #include "BKE_constraint.h"
77 #include "BKE_curve.h"
78 #include "BKE_effect.h"
79 #include "BKE_collision.h"
80 #include "BKE_fcurve.h"
81 #include "BKE_key.h"
82 #include "BKE_library.h"
83 #include "BKE_main.h"
84 #include "BKE_material.h"
85 #include "BKE_mball.h"
86 #include "BKE_modifier.h"
87 #include "BKE_node.h"
88 #include "BKE_object.h"
89 #include "BKE_particle.h"
90 #include "BKE_rigidbody.h"
91 #include "BKE_sound.h"
92 #include "BKE_tracking.h"
93 #include "BKE_world.h"
94
95 #include "RNA_access.h"
96 #include "RNA_types.h"
97 } /* extern "C" */
98
99 #include "DEG_depsgraph.h"
100 #include "DEG_depsgraph_build.h"
101
102 #include "intern/builder/deg_builder.h"
103 #include "intern/builder/deg_builder_pchanmap.h"
104
105 #include "intern/nodes/deg_node.h"
106 #include "intern/nodes/deg_node_component.h"
107 #include "intern/nodes/deg_node_id.h"
108 #include "intern/nodes/deg_node_operation.h"
109 #include "intern/nodes/deg_node_time.h"
110
111 #include "intern/depsgraph_intern.h"
112 #include "intern/depsgraph_types.h"
113
114 #include "util/deg_util_foreach.h"
115
116 namespace DEG {
117
118 /* ***************** */
119 /* Relations Builder */
120
121 /* TODO(sergey): This is somewhat weak, but we don't want neither false-positive
122  * time dependencies nor special exceptions in the depsgraph evaluation.
123  */
124 static bool python_driver_depends_on_time(ChannelDriver *driver)
125 {
126         if (driver->expression[0] == '\0') {
127                 /* Empty expression depends on nothing. */
128                 return false;
129         }
130         if (strchr(driver->expression, '(') != NULL) {
131                 /* Function calls are considered dependent on a time. */
132                 return true;
133         }
134         if (strstr(driver->expression, "frame") != NULL) {
135                 /* Variable `frame` depends on time. */
136                 /* TODO(sergey): This is a bit weak, but not sure about better way of
137                  * handling this.
138                  */
139                 return true;
140         }
141         /* Possible indirect time relation s should be handled via variable
142          * targets.
143          */
144         return false;
145 }
146
147 static bool particle_system_depends_on_time(ParticleSystem *psys)
148 {
149         ParticleSettings *part = psys->part;
150         /* Non-hair particles we always consider dependent on time. */
151         if (part->type != PART_HAIR) {
152                 return true;
153         }
154         /* Dynamics always depends on time. */
155         if (psys->flag & PSYS_HAIR_DYNAMICS) {
156                 return true;
157         }
158         /* TODO(sergey): Check what else makes hair dependent on time. */
159         return false;
160 }
161
162 static bool object_particles_depends_on_time(Object *object)
163 {
164         if (object->type != OB_MESH) {
165                 return false;
166         }
167         LISTBASE_FOREACH (ParticleSystem *, psys, &object->particlesystem) {
168                 if (particle_system_depends_on_time(psys)) {
169                         return true;
170                 }
171         }
172         return false;
173 }
174
175 static bool check_id_has_anim_component(ID *id)
176 {
177         AnimData *adt = BKE_animdata_from_id(id);
178         if (adt == NULL) {
179                 return false;
180         }
181         return (adt->action != NULL) ||
182                (!BLI_listbase_is_empty(&adt->nla_tracks));
183 }
184
185 /* **** General purpose functions ****  */
186
187 DepsgraphRelationBuilder::DepsgraphRelationBuilder(Main *bmain,
188                                                    Depsgraph *graph)
189     : bmain_(bmain),
190       graph_(graph),
191       scene_(NULL)
192 {
193 }
194
195 TimeSourceDepsNode *DepsgraphRelationBuilder::get_node(
196         const TimeSourceKey &key) const
197 {
198         if (key.id) {
199                 /* XXX TODO */
200                 return NULL;
201         }
202         else {
203                 return graph_->time_source;
204         }
205 }
206
207 ComponentDepsNode *DepsgraphRelationBuilder::get_node(
208         const ComponentKey &key) const
209 {
210         IDDepsNode *id_node = graph_->find_id_node(key.id);
211         if (!id_node) {
212                 fprintf(stderr, "find_node component: Could not find ID %s\n",
213                         (key.id != NULL) ? key.id->name : "<null>");
214                 return NULL;
215         }
216
217         ComponentDepsNode *node = id_node->find_component(key.type, key.name);
218         return node;
219 }
220
221 OperationDepsNode *DepsgraphRelationBuilder::get_node(
222         const OperationKey &key) const
223 {
224         OperationDepsNode *op_node = find_node(key);
225         if (op_node == NULL) {
226                 fprintf(stderr, "find_node_operation: Failed for (%s, '%s')\n",
227                         operationCodeAsString(key.opcode), key.name);
228         }
229         return op_node;
230 }
231
232 DepsNode *DepsgraphRelationBuilder::get_node(const RNAPathKey &key) const
233 {
234         return graph_->find_node_from_pointer(&key.ptr, key.prop);
235 }
236
237 OperationDepsNode *DepsgraphRelationBuilder::find_node(
238         const OperationKey &key) const
239 {
240         IDDepsNode *id_node = graph_->find_id_node(key.id);
241         if (!id_node) {
242                 return NULL;
243         }
244         ComponentDepsNode *comp_node = id_node->find_component(key.component_type,
245                                                                key.component_name);
246         if (!comp_node) {
247                 return NULL;
248         }
249         return comp_node->find_operation(key.opcode, key.name, key.name_tag);
250 }
251
252 bool DepsgraphRelationBuilder::has_node(const OperationKey &key) const
253 {
254         return find_node(key) != NULL;
255 }
256
257 DepsRelation *DepsgraphRelationBuilder::add_time_relation(
258         TimeSourceDepsNode *timesrc,
259         DepsNode *node_to,
260         const char *description,
261         bool check_unique)
262 {
263         if (timesrc && node_to) {
264                 return graph_->add_new_relation(timesrc, node_to, description, check_unique);
265         }
266         else {
267                 DEG_DEBUG_PRINTF((::Depsgraph *)graph_,
268                                  BUILD, "add_time_relation(%p = %s, %p = %s, %s) Failed\n",
269                                  timesrc,   (timesrc) ? timesrc->identifier().c_str() : "<None>",
270                                  node_to,   (node_to) ? node_to->identifier().c_str() : "<None>",
271                                  description);
272         }
273         return NULL;
274 }
275
276 DepsRelation *DepsgraphRelationBuilder::add_operation_relation(
277         OperationDepsNode *node_from,
278         OperationDepsNode *node_to,
279         const char *description,
280         bool check_unique)
281 {
282         if (node_from && node_to) {
283                 return graph_->add_new_relation(node_from,
284                                                 node_to,
285                                                 description,
286                                                 check_unique);
287         }
288         else {
289                 DEG_DEBUG_PRINTF((::Depsgraph *)graph_,
290                                  BUILD, "add_operation_relation(%p = %s, %p = %s, %s) Failed\n",
291                                  node_from, (node_from) ? node_from->identifier().c_str() : "<None>",
292                                  node_to,   (node_to)   ? node_to->identifier().c_str() : "<None>",
293                                  description);
294         }
295         return NULL;
296 }
297
298 void DepsgraphRelationBuilder::add_collision_relations(
299         const OperationKey &key,
300         Scene *scene,
301         Object *object,
302         Collection *collection,
303         bool dupli,
304         const char *name)
305 {
306         unsigned int numcollobj;
307         Object **collobjs = get_collisionobjects_ext(scene,
308                                                      object,
309                                                      collection,
310                                                      &numcollobj,
311                                                      eModifierType_Collision,
312                                                      dupli);
313         for (unsigned int i = 0; i < numcollobj; i++) {
314                 Object *ob1 = collobjs[i];
315
316                 ComponentKey trf_key(&ob1->id, DEG_NODE_TYPE_TRANSFORM);
317                 add_relation(trf_key, key, name);
318
319                 ComponentKey coll_key(&ob1->id, DEG_NODE_TYPE_GEOMETRY);
320                 add_relation(coll_key, key, name);
321         }
322         if (collobjs != NULL) {
323                 MEM_freeN(collobjs);
324         }
325 }
326
327 void DepsgraphRelationBuilder::add_forcefield_relations(
328         const OperationKey &key,
329         Scene *scene,
330         Object *object,
331         ParticleSystem *psys,
332         EffectorWeights *eff,
333         bool add_absorption,
334         const char *name)
335 {
336         ListBase *effectors = pdInitEffectors(NULL, scene, object, psys, eff, false);
337         if (effectors != NULL) {
338                 LISTBASE_FOREACH (EffectorCache *, eff, effectors) {
339                         if (eff->ob != object) {
340                                 ComponentKey eff_key(&eff->ob->id, DEG_NODE_TYPE_TRANSFORM);
341                                 add_relation(eff_key, key, name);
342                         }
343                         if (eff->psys != NULL) {
344                                 if (eff->ob != object) {
345                                         ComponentKey eff_key(&eff->ob->id, DEG_NODE_TYPE_EVAL_PARTICLES);
346                                         add_relation(eff_key, key, name);
347
348                                         /* TODO: remove this when/if EVAL_PARTICLES is sufficient
349                                          * for up to date particles.
350                                          */
351                                         ComponentKey mod_key(&eff->ob->id, DEG_NODE_TYPE_GEOMETRY);
352                                         add_relation(mod_key, key, name);
353                                 }
354                                 else if (eff->psys != psys) {
355                                         OperationKey eff_key(&eff->ob->id,
356                                                              DEG_NODE_TYPE_EVAL_PARTICLES,
357                                                              DEG_OPCODE_PARTICLE_SYSTEM_EVAL,
358                                                              eff->psys->name);
359                                         add_relation(eff_key, key, name);
360                                 }
361                         }
362                         if (eff->pd->forcefield == PFIELD_SMOKEFLOW && eff->pd->f_source) {
363                                 ComponentKey trf_key(&eff->pd->f_source->id,
364                                                      DEG_NODE_TYPE_TRANSFORM);
365                                 add_relation(trf_key, key, "Smoke Force Domain");
366
367                                 ComponentKey eff_key(&eff->pd->f_source->id,
368                                                      DEG_NODE_TYPE_GEOMETRY);
369                                 add_relation(eff_key, key, "Smoke Force Domain");
370                         }
371                         if (add_absorption && (eff->pd->flag & PFIELD_VISIBILITY)) {
372                                 add_collision_relations(key,
373                                                         scene,
374                                                         object,
375                                                         NULL,
376                                                         true,
377                                                         "Force Absorption");
378                         }
379                 }
380         }
381
382         pdEndEffectors(&effectors);
383 }
384
385 Depsgraph *DepsgraphRelationBuilder::getGraph()
386 {
387         return graph_;
388 }
389
390 /* **** Functions to build relations between entities  **** */
391
392 void DepsgraphRelationBuilder::begin_build()
393 {
394 }
395
396 void DepsgraphRelationBuilder::build_id(ID *id)
397 {
398         if (id == NULL) {
399                 return;
400         }
401         switch (GS(id->name)) {
402                 case ID_GR:
403                         build_collection(NULL, (Collection *)id);
404                         break;
405                 case ID_OB:
406                         build_object(NULL, (Object *)id);
407                         break;
408                 case ID_NT:
409                         build_nodetree((bNodeTree *)id);
410                         break;
411                 case ID_MA:
412                         build_material((Material *)id);
413                         break;
414                 case ID_TE:
415                         build_texture((Tex *)id);
416                         break;
417                 case ID_WO:
418                         build_world((World *)id);
419                         break;
420                 case ID_MSK:
421                         build_mask((Mask *)id);
422                         break;
423                 case ID_MC:
424                         build_movieclip((MovieClip *)id);
425                         break;
426                 default:
427                         fprintf(stderr, "Unhandled ID %s\n", id->name);
428         }
429 }
430
431 void DepsgraphRelationBuilder::build_collection(Object *object, Collection *collection)
432 {
433         const int restrict_flag = (graph_->mode == DAG_EVAL_VIEWPORT) ?
434                 COLLECTION_RESTRICT_VIEW : COLLECTION_RESTRICT_RENDER;
435         if (collection->flag & restrict_flag) {
436                 return;
437         }
438
439         const bool group_done = built_map_.checkIsBuiltAndTag(collection);
440         OperationKey object_local_transform_key(object != NULL ? &object->id : NULL,
441                                                 DEG_NODE_TYPE_TRANSFORM,
442                                                 DEG_OPCODE_TRANSFORM_LOCAL);
443         if (!group_done) {
444                 LISTBASE_FOREACH (CollectionObject *, cob, &collection->gobject) {
445                         build_object(NULL, cob->ob);
446                 }
447                 LISTBASE_FOREACH (CollectionChild *, child, &collection->children) {
448                         build_collection(NULL, child->collection);
449                 }
450         }
451         if (object != NULL) {
452                 const ListBase group_objects = BKE_collection_object_cache_get(collection);
453                 LISTBASE_FOREACH (Base *, base, &group_objects) {
454                         ComponentKey dupli_transform_key(&base->object->id, DEG_NODE_TYPE_TRANSFORM);
455                         add_relation(dupli_transform_key, object_local_transform_key, "Dupligroup");
456                 }
457         }
458 }
459
460 void DepsgraphRelationBuilder::build_object(Base *base, Object *object)
461 {
462         if (built_map_.checkIsBuiltAndTag(object)) {
463                 if (base != NULL) {
464                         build_object_flags(base, object);
465                 }
466                 return;
467         }
468         /* Object Transforms */
469         eDepsOperation_Code base_op = (object->parent) ? DEG_OPCODE_TRANSFORM_PARENT
470                                                        : DEG_OPCODE_TRANSFORM_LOCAL;
471         OperationKey base_op_key(&object->id, DEG_NODE_TYPE_TRANSFORM, base_op);
472         OperationKey local_transform_key(&object->id,
473                                          DEG_NODE_TYPE_TRANSFORM,
474                                          DEG_OPCODE_TRANSFORM_LOCAL);
475         OperationKey parent_transform_key(&object->id,
476                                           DEG_NODE_TYPE_TRANSFORM,
477                                           DEG_OPCODE_TRANSFORM_PARENT);
478         OperationKey final_transform_key(&object->id,
479                                          DEG_NODE_TYPE_TRANSFORM,
480                                          DEG_OPCODE_TRANSFORM_FINAL);
481         OperationKey ob_ubereval_key(&object->id,
482                                      DEG_NODE_TYPE_TRANSFORM,
483                                      DEG_OPCODE_TRANSFORM_OBJECT_UBEREVAL);
484         /* Various flags, flushing from bases/collections. */
485         build_object_flags(base, object);
486         /* Parenting. */
487         if (object->parent != NULL) {
488                 /* Make sure parent object's relations are built. */
489                 build_object(NULL, object->parent);
490                 /* Parent relationship. */
491                 build_object_parent(object);
492                 /* Local -> parent. */
493                 add_relation(local_transform_key,
494                              parent_transform_key,
495                              "ObLocal -> ObParent");
496         }
497         /* Modifiers. */
498         if (object->modifiers.first != NULL) {
499                 BuilderWalkUserData data;
500                 data.builder = this;
501                 modifiers_foreachIDLink(object, modifier_walk, &data);
502         }
503         /* Constraints. */
504         if (object->constraints.first != NULL) {
505                 BuilderWalkUserData data;
506                 data.builder = this;
507                 BKE_constraints_id_loop(&object->constraints, constraint_walk, &data);
508         }
509         /* Object constraints. */
510         if (object->constraints.first != NULL) {
511                 OperationKey constraint_key(&object->id,
512                                             DEG_NODE_TYPE_TRANSFORM,
513                                             DEG_OPCODE_TRANSFORM_CONSTRAINTS);
514                 /* Constraint relations. */
515                 build_constraints(&object->id,
516                                   DEG_NODE_TYPE_TRANSFORM,
517                                   "",
518                                   &object->constraints,
519                                   NULL);
520                 /* operation order */
521                 add_relation(base_op_key, constraint_key, "ObBase-> Constraint Stack");
522                 add_relation(constraint_key, final_transform_key, "ObConstraints -> Done");
523                 // XXX
524                 add_relation(constraint_key, ob_ubereval_key, "Temp Ubereval");
525                 add_relation(ob_ubereval_key, final_transform_key, "Temp Ubereval");
526         }
527         else {
528                 /* NOTE: Keep an eye here, we skip some relations here to "streamline"
529                  * dependencies and avoid transitive relations which causes overhead.
530                  * But once we get rid of uber eval node this will need reconsideration.
531                  */
532                 if (object->rigidbody_object == NULL) {
533                         /* Rigid body will hook up another node inbetween, so skip
534                          * relation here to avoid transitive relation.
535                          */
536                         add_relation(base_op_key, ob_ubereval_key, "Temp Ubereval");
537                 }
538                 add_relation(ob_ubereval_key, final_transform_key, "Temp Ubereval");
539         }
540         /* Animation data */
541         build_animdata(&object->id);
542         /* Object data. */
543         build_object_data(object);
544         /* Particle systems. */
545         if (object->particlesystem.first != NULL) {
546                 build_particles(object);
547         }
548         /* Grease pencil. */
549         if (object->gpd != NULL) {
550                 build_gpencil(object->gpd);
551         }
552         /* Proxy object to copy from. */
553         if (object->proxy_from != NULL) {
554                 build_object(NULL, object->proxy_from);
555                 ComponentKey ob_pose_key(&object->proxy_from->id, DEG_NODE_TYPE_EVAL_POSE);
556                 ComponentKey proxy_pose_key(&object->id, DEG_NODE_TYPE_EVAL_POSE);
557                 add_relation(ob_pose_key, proxy_pose_key, "Proxy Pose");
558
559                 ComponentKey ob_transform_key(&object->proxy_from->id, DEG_NODE_TYPE_TRANSFORM);
560                 ComponentKey proxy_transform_key(&object->id, DEG_NODE_TYPE_TRANSFORM);
561                 add_relation(ob_transform_key, proxy_transform_key, "Proxy Transform");
562         }
563
564         /* Object dupligroup. */
565         if (object->dup_group != NULL) {
566                 build_collection(object, object->dup_group);
567         }
568 }
569
570 void DepsgraphRelationBuilder::build_object_flags(Base *base, Object *object)
571 {
572         if (base == NULL) {
573                 return;
574         }
575         OperationKey view_layer_done_key(&scene_->id,
576                                          DEG_NODE_TYPE_LAYER_COLLECTIONS,
577                                          DEG_OPCODE_VIEW_LAYER_EVAL);
578         OperationKey object_flags_key(&object->id,
579                                       DEG_NODE_TYPE_LAYER_COLLECTIONS,
580                                       DEG_OPCODE_OBJECT_BASE_FLAGS);
581         add_relation(view_layer_done_key, object_flags_key, "Base flags flush");
582 }
583
584 void DepsgraphRelationBuilder::build_object_data(Object *object)
585 {
586         if (object->data == NULL) {
587                 return;
588         }
589         ID *obdata_id = (ID *)object->data;
590         /* Object data animation. */
591         if (!built_map_.checkIsBuilt(obdata_id)) {
592                 build_animdata(obdata_id);
593         }
594         /* type-specific data. */
595         switch (object->type) {
596                 case OB_MESH:
597                 case OB_CURVE:
598                 case OB_FONT:
599                 case OB_SURF:
600                 case OB_MBALL:
601                 case OB_LATTICE:
602                 {
603                         build_object_data_geometry(object);
604                         break;
605                 }
606                 case OB_ARMATURE:
607                         if (ID_IS_LINKED(object) && object->proxy_from != NULL) {
608                                 build_proxy_rig(object);
609                         }
610                         else {
611                                  build_rig(object);
612                         }
613                         break;
614                 case OB_LAMP:
615                         build_object_data_lamp(object);
616                         break;
617                 case OB_CAMERA:
618                         build_object_data_camera(object);
619                         break;
620                 case OB_LIGHTPROBE:
621                         build_object_data_lightprobe(object);
622                         break;
623         }
624         Key *key = BKE_key_from_object(object);
625         if (key != NULL) {
626                 ComponentKey geometry_key((ID *)object->data, DEG_NODE_TYPE_GEOMETRY);
627                 ComponentKey key_key(&key->id, DEG_NODE_TYPE_GEOMETRY);
628                 add_relation(key_key, geometry_key, "Shapekeys");
629                 build_nested_shapekey(&object->id, key);
630         }
631 }
632
633 void DepsgraphRelationBuilder::build_object_data_camera(Object *object)
634 {
635         Camera *camera = (Camera *)object->data;
636         build_camera(camera);
637         ComponentKey object_parameters_key(&object->id, DEG_NODE_TYPE_PARAMETERS);
638         ComponentKey camera_parameters_key(&camera->id, DEG_NODE_TYPE_PARAMETERS);
639         add_relation(camera_parameters_key, object_parameters_key, "Camera -> Object");
640 }
641
642 void DepsgraphRelationBuilder::build_object_data_lamp(Object *object)
643 {
644         Lamp *lamp = (Lamp *)object->data;
645         build_lamp(lamp);
646         ComponentKey object_parameters_key(&object->id, DEG_NODE_TYPE_PARAMETERS);
647         ComponentKey lamp_parameters_key(&lamp->id, DEG_NODE_TYPE_PARAMETERS);
648         add_relation(lamp_parameters_key, object_parameters_key, "Lamp -> Object");
649 }
650
651 void DepsgraphRelationBuilder::build_object_data_lightprobe(Object *object)
652 {
653         LightProbe *probe = (LightProbe *)object->data;
654         build_lightprobe(probe);
655         OperationKey probe_key(&probe->id,
656                                DEG_NODE_TYPE_PARAMETERS,
657                                DEG_OPCODE_LIGHT_PROBE_EVAL);
658         OperationKey object_key(&object->id,
659                                 DEG_NODE_TYPE_PARAMETERS,
660                                 DEG_OPCODE_LIGHT_PROBE_EVAL);
661         add_relation(probe_key, object_key, "LightProbe Update");
662 }
663
664 void DepsgraphRelationBuilder::build_object_parent(Object *object)
665 {
666         /* XXX: for now, need to use the component key (not just direct to the parent op),
667          * or else the matrix doesn't get reset/
668          */
669         // XXX: @sergey - it would be good if we got that backwards flushing working
670         // when tagging for updates.
671         //OperationKey ob_key(&object->id, DEG_NODE_TYPE_TRANSFORM, DEG_OPCODE_TRANSFORM_PARENT);
672         ComponentKey ob_key(&object->id, DEG_NODE_TYPE_TRANSFORM);
673
674         /* type-specific links */
675         switch (object->partype) {
676                 case PARSKEL:  /* Armature Deform (Virtual Modifier) */
677                 {
678                         ComponentKey parent_key(&object->parent->id, DEG_NODE_TYPE_TRANSFORM);
679                         add_relation(parent_key, ob_key, "Armature Deform Parent");
680                         break;
681                 }
682
683                 case PARVERT1: /* Vertex Parent */
684                 case PARVERT3:
685                 {
686                         ComponentKey parent_key(&object->parent->id, DEG_NODE_TYPE_GEOMETRY);
687                         add_relation(parent_key, ob_key, "Vertex Parent");
688
689                         /* XXX not sure what this is for or how you could be done properly - lukas */
690                         OperationDepsNode *parent_node = find_operation_node(parent_key);
691                         if (parent_node != NULL) {
692                                 parent_node->customdata_mask |= CD_MASK_ORIGINDEX;
693                         }
694
695                         ComponentKey transform_key(&object->parent->id, DEG_NODE_TYPE_TRANSFORM);
696                         add_relation(transform_key, ob_key, "Vertex Parent TFM");
697                         break;
698                 }
699
700                 case PARBONE: /* Bone Parent */
701                 {
702                         ComponentKey parent_bone_key(&object->parent->id,
703                                                      DEG_NODE_TYPE_BONE,
704                                                      object->parsubstr);
705                         OperationKey parent_transform_key(&object->parent->id,
706                                                           DEG_NODE_TYPE_TRANSFORM,
707                                                           DEG_OPCODE_TRANSFORM_FINAL);
708                         add_relation(parent_bone_key, ob_key, "Bone Parent");
709                         add_relation(parent_transform_key, ob_key, "Armature Parent");
710                         break;
711                 }
712
713                 default:
714                 {
715                         if (object->parent->type == OB_LATTICE) {
716                                 /* Lattice Deform Parent - Virtual Modifier */
717                                 // XXX: no virtual modifiers should be left!
718                                 ComponentKey parent_key(&object->parent->id, DEG_NODE_TYPE_TRANSFORM);
719                                 ComponentKey geom_key(&object->parent->id, DEG_NODE_TYPE_GEOMETRY);
720
721                                 add_relation(parent_key, ob_key, "Lattice Deform Parent");
722                                 add_relation(geom_key, ob_key, "Lattice Deform Parent Geom");
723                         }
724                         else if (object->parent->type == OB_CURVE) {
725                                 Curve *cu = (Curve *)object->parent->data;
726
727                                 if (cu->flag & CU_PATH) {
728                                         /* Follow Path */
729                                         ComponentKey parent_key(&object->parent->id, DEG_NODE_TYPE_GEOMETRY);
730                                         add_relation(parent_key, ob_key, "Curve Follow Parent");
731
732                                         ComponentKey transform_key(&object->parent->id, DEG_NODE_TYPE_TRANSFORM);
733                                         add_relation(transform_key, ob_key, "Curve Follow TFM");
734                                 }
735                                 else {
736                                         /* Standard Parent */
737                                         ComponentKey parent_key(&object->parent->id, DEG_NODE_TYPE_TRANSFORM);
738                                         add_relation(parent_key, ob_key, "Curve Parent");
739                                 }
740                         }
741                         else {
742                                 /* Standard Parent */
743                                 ComponentKey parent_key(&object->parent->id, DEG_NODE_TYPE_TRANSFORM);
744                                 add_relation(parent_key, ob_key, "Parent");
745                         }
746                         break;
747                 }
748         }
749
750         /* exception case: parent is duplivert */
751         if ((object->type == OB_MBALL) && (object->parent->transflag & OB_DUPLIVERTS)) {
752                 //dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA | DAG_RL_OB_OB, "Duplivert");
753         }
754 }
755
756 void DepsgraphRelationBuilder::build_constraints(ID *id,
757                                                  eDepsNode_Type component_type,
758                                                  const char *component_subdata,
759                                                  ListBase *constraints,
760                                                  RootPChanMap *root_map)
761 {
762         OperationKey constraint_op_key(
763                 id,
764                 component_type,
765                 component_subdata,
766                 (component_type == DEG_NODE_TYPE_BONE)
767                         ? DEG_OPCODE_BONE_CONSTRAINTS
768                         : DEG_OPCODE_TRANSFORM_CONSTRAINTS);
769         /* Add dependencies for each constraint in turn. */
770         for (bConstraint *con = (bConstraint *)constraints->first; con; con = con->next) {
771                 const bConstraintTypeInfo *cti = BKE_constraint_typeinfo_get(con);
772                 /* Invalid constraint type. */
773                 if (cti == NULL) {
774                         continue;
775                 }
776                 /* Special case for camera tracking -- it doesn't use targets to
777                  * define relations.
778                  */
779                 /* TODO: we can now represent dependencies in a much richer manner,
780                  * so review how this is done.
781                  */
782                 if (ELEM(cti->type,
783                          CONSTRAINT_TYPE_FOLLOWTRACK,
784                          CONSTRAINT_TYPE_CAMERASOLVER,
785                          CONSTRAINT_TYPE_OBJECTSOLVER))
786                 {
787                         bool depends_on_camera = false;
788                         if (cti->type == CONSTRAINT_TYPE_FOLLOWTRACK) {
789                                 bFollowTrackConstraint *data = (bFollowTrackConstraint *)con->data;
790                                 if (((data->clip) ||
791                                      (data->flag & FOLLOWTRACK_ACTIVECLIP)) && data->track[0])
792                                 {
793                                         depends_on_camera = true;
794                                 }
795                                 if (data->depth_ob) {
796                                         ComponentKey depth_transform_key(&data->depth_ob->id,
797                                                                          DEG_NODE_TYPE_TRANSFORM);
798                                         ComponentKey depth_geometry_key(&data->depth_ob->id,
799                                                                         DEG_NODE_TYPE_GEOMETRY);
800                                         add_relation(depth_transform_key, constraint_op_key, cti->name);
801                                         add_relation(depth_geometry_key, constraint_op_key, cti->name);
802                                 }
803                         }
804                         else if (cti->type == CONSTRAINT_TYPE_OBJECTSOLVER) {
805                                 depends_on_camera = true;
806                         }
807                         if (depends_on_camera && scene_->camera != NULL) {
808                                 ComponentKey camera_key(&scene_->camera->id, DEG_NODE_TYPE_TRANSFORM);
809                                 add_relation(camera_key, constraint_op_key, cti->name);
810                         }
811                         /* TODO(sergey): This is more a TimeSource -> MovieClip ->
812                          * Constraint dependency chain.
813                          */
814                         TimeSourceKey time_src_key;
815                         add_relation(time_src_key, constraint_op_key, "TimeSrc -> Animation");
816                 }
817                 else if (cti->type == CONSTRAINT_TYPE_TRANSFORM_CACHE) {
818                         /* TODO(kevin): This is more a TimeSource -> CacheFile -> Constraint
819                          * dependency chain.
820                          */
821                         TimeSourceKey time_src_key;
822                         add_relation(time_src_key, constraint_op_key, "TimeSrc -> Animation");
823                         bTransformCacheConstraint *data = (bTransformCacheConstraint *)con->data;
824                         if (data->cache_file) {
825                                 ComponentKey cache_key(&data->cache_file->id, DEG_NODE_TYPE_CACHE);
826                                 add_relation(cache_key, constraint_op_key, cti->name);
827                         }
828                 }
829                 else if (cti->get_constraint_targets) {
830                         ListBase targets = {NULL, NULL};
831                         cti->get_constraint_targets(con, &targets);
832                         LISTBASE_FOREACH (bConstraintTarget *, ct, &targets) {
833                                 if (ct->tar == NULL) {
834                                         continue;
835                                 }
836                                 if (ELEM(con->type,
837                                          CONSTRAINT_TYPE_KINEMATIC,
838                                          CONSTRAINT_TYPE_SPLINEIK))
839                                 {
840                                         /* Ignore IK constraints - these are handled separately
841                                          * (on pose level).
842                                          */
843                                 }
844                                 else if (ELEM(con->type,
845                                               CONSTRAINT_TYPE_FOLLOWPATH,
846                                               CONSTRAINT_TYPE_CLAMPTO))
847                                 {
848                                         /* These constraints require path geometry data. */
849                                         ComponentKey target_key(&ct->tar->id, DEG_NODE_TYPE_GEOMETRY);
850                                         add_relation(target_key, constraint_op_key, cti->name);
851                                         ComponentKey target_transform_key(&ct->tar->id,
852                                                                           DEG_NODE_TYPE_TRANSFORM);
853                                         add_relation(target_transform_key, constraint_op_key, cti->name);
854                                 }
855                                 else if ((ct->tar->type == OB_ARMATURE) && (ct->subtarget[0])) {
856                                         /* bone */
857                                         if (&ct->tar->id == id) {
858                                                 /* same armature  */
859                                                 eDepsOperation_Code target_key_opcode;
860                                                 /* Using "done" here breaks in-chain deps, while using
861                                                  * "ready" here breaks most production rigs instead.
862                                                  * So, we do a compromise here, and only do this when an
863                                                  * IK chain conflict may occur.
864                                                  */
865                                                 if (root_map->has_common_root(component_subdata,
866                                                                               ct->subtarget))
867                                                 {
868                                                         target_key_opcode = DEG_OPCODE_BONE_READY;
869                                                 }
870                                                 else {
871                                                         target_key_opcode = DEG_OPCODE_BONE_DONE;
872                                                 }
873                                                 OperationKey target_key(&ct->tar->id,
874                                                                         DEG_NODE_TYPE_BONE,
875                                                                         ct->subtarget,
876                                                                         target_key_opcode);
877                                                 add_relation(target_key, constraint_op_key, cti->name);
878                                         }
879                                         else {
880                                                 /* Different armature - we can safely use the result
881                                                  * of that.
882                                                  */
883                                                 OperationKey target_key(&ct->tar->id,
884                                                                         DEG_NODE_TYPE_BONE,
885                                                                         ct->subtarget,
886                                                                         DEG_OPCODE_BONE_DONE);
887                                                 add_relation(target_key, constraint_op_key, cti->name);
888                                         }
889                                 }
890                                 else if (ELEM(ct->tar->type, OB_MESH, OB_LATTICE) &&
891                                          (ct->subtarget[0]))
892                                 {
893                                         /* Vertex group. */
894                                         /* NOTE: for now, we don't need to represent vertex groups
895                                          * separately.
896                                          */
897                                         ComponentKey target_key(&ct->tar->id, DEG_NODE_TYPE_GEOMETRY);
898                                         add_relation(target_key, constraint_op_key, cti->name);
899                                         if (ct->tar->type == OB_MESH) {
900                                                 OperationDepsNode *node2 = find_operation_node(target_key);
901                                                 if (node2 != NULL) {
902                                                         node2->customdata_mask |= CD_MASK_MDEFORMVERT;
903                                                 }
904                                         }
905                                 }
906                                 else if (con->type == CONSTRAINT_TYPE_SHRINKWRAP) {
907                                         /* Constraints which requires the target object surface. */
908                                         ComponentKey target_key(&ct->tar->id, DEG_NODE_TYPE_GEOMETRY);
909                                         add_relation(target_key, constraint_op_key, cti->name);
910                                         /* NOTE: obdata eval now doesn't necessarily depend on the
911                                          * object's transform.
912                                          */
913                                         ComponentKey target_transform_key(&ct->tar->id,
914                                                                           DEG_NODE_TYPE_TRANSFORM);
915                                         add_relation(target_transform_key, constraint_op_key, cti->name);
916                                 }
917                                 else {
918                                         /* Standard object relation. */
919                                         // TODO: loc vs rot vs scale?
920                                         if (&ct->tar->id == id) {
921                                                 /* Constraint targetting own object:
922                                                  * - This case is fine IFF we're dealing with a bone
923                                                  *   constraint pointing to its own armature. In that
924                                                  *   case, it's just transform -> bone.
925                                                  * - If however it is a real self targetting case, just
926                                                  *   make it depend on the previous constraint (or the
927                                                  *   pre-constraint state).
928                                                  */
929                                                 if ((ct->tar->type == OB_ARMATURE) &&
930                                                     (component_type == DEG_NODE_TYPE_BONE))
931                                                 {
932                                                         OperationKey target_key(&ct->tar->id,
933                                                                                 DEG_NODE_TYPE_TRANSFORM,
934                                                                                 DEG_OPCODE_TRANSFORM_FINAL);
935                                                         add_relation(target_key, constraint_op_key, cti->name);
936                                                 }
937                                                 else {
938                                                         OperationKey target_key(&ct->tar->id,
939                                                                                 DEG_NODE_TYPE_TRANSFORM,
940                                                                                 DEG_OPCODE_TRANSFORM_LOCAL);
941                                                         add_relation(target_key, constraint_op_key, cti->name);
942                                                 }
943                                         }
944                                         else {
945                                                 /* Normal object dependency. */
946                                                 OperationKey target_key(&ct->tar->id,
947                                                                         DEG_NODE_TYPE_TRANSFORM,
948                                                                         DEG_OPCODE_TRANSFORM_FINAL);
949                                                 add_relation(target_key, constraint_op_key, cti->name);
950                                         }
951                                 }
952                                 /* Constraints which needs world's matrix for transform.
953                                  * TODO(sergey): More constraints here?
954                                  */
955                                 if (ELEM(con->type,
956                                          CONSTRAINT_TYPE_ROTLIKE,
957                                          CONSTRAINT_TYPE_SIZELIKE,
958                                          CONSTRAINT_TYPE_LOCLIKE,
959                                          CONSTRAINT_TYPE_TRANSLIKE))
960                                 {
961                                         /* TODO(sergey): Add used space check. */
962                                         ComponentKey target_transform_key(&ct->tar->id,
963                                                                           DEG_NODE_TYPE_TRANSFORM);
964                                         add_relation(target_transform_key, constraint_op_key, cti->name);
965                                 }
966                         }
967                         if (cti->flush_constraint_targets) {
968                                 cti->flush_constraint_targets(con, &targets, 1);
969                         }
970                 }
971         }
972 }
973
974 void DepsgraphRelationBuilder::build_animdata(ID *id)
975 {
976         /* Animation curves and NLA. */
977         build_animdata_curves(id);
978         /* Drivers. */
979         build_animdata_drivers(id);
980 }
981
982 void DepsgraphRelationBuilder::build_animdata_curves(ID *id)
983 {
984         AnimData *adt = BKE_animdata_from_id(id);
985         if (adt == NULL) {
986                 return;
987         }
988         if (adt->action == NULL && adt->nla_tracks.first == NULL) {
989                 return;
990         }
991         /* Wire up dependency to time source. */
992         ComponentKey adt_key(id, DEG_NODE_TYPE_ANIMATION);
993         TimeSourceKey time_src_key;
994         add_relation(time_src_key, adt_key, "TimeSrc -> Animation");
995         /* Relation from action itself. */
996         if (adt->action != NULL &&
997             !built_map_.checkIsBuiltAndTag(&adt->action->id))
998         {
999                 ComponentKey action_key(&adt->action->id, DEG_NODE_TYPE_ANIMATION);
1000                 add_relation(action_key, adt_key, "Action -> Animation");
1001         }
1002         /* Get source operations. */
1003         DepsNode *node_from = get_node(adt_key);
1004         BLI_assert(node_from != NULL);
1005         if (node_from == NULL) {
1006                 return;
1007         }
1008         OperationDepsNode *operation_from = node_from->get_exit_operation();
1009         BLI_assert(operation_from != NULL);
1010         /* Build relations from animation operation to properties it changes. */
1011         if (adt->action != NULL) {
1012                 build_animdata_curves_targets(id, adt_key,
1013                                       operation_from,
1014                                       &adt->action->curves);
1015         }
1016         LISTBASE_FOREACH(NlaTrack *, nlt, &adt->nla_tracks) {
1017                 build_animdata_nlastrip_targets(id, adt_key,
1018                                                 operation_from,
1019                                                 &nlt->strips);
1020         }
1021 }
1022
1023 void DepsgraphRelationBuilder::build_animdata_curves_targets(
1024         ID *id, ComponentKey &adt_key,
1025         OperationDepsNode *operation_from,
1026         ListBase *curves)
1027 {
1028         /* Iterate over all curves and build relations. */
1029         PointerRNA id_ptr;
1030         RNA_id_pointer_create(id, &id_ptr);
1031         LISTBASE_FOREACH(FCurve *, fcu, curves) {
1032                 PointerRNA ptr;
1033                 PropertyRNA *prop;
1034                 int index;
1035                 if (!RNA_path_resolve_full(&id_ptr, fcu->rna_path,
1036                                            &ptr, &prop, &index))
1037                 {
1038                         continue;
1039                 }
1040                 DepsNode *node_to = graph_->find_node_from_pointer(&ptr, prop);
1041                 if (node_to == NULL) {
1042                         continue;
1043                 }
1044                 OperationDepsNode *operation_to = node_to->get_entry_operation();
1045                 /* NOTE: Special case for bones, avoid relation from animation to
1046                  * each of the bones. Bone evaluation could only start from pose
1047                  * init anyway.
1048                  */
1049                 if (operation_to->opcode == DEG_OPCODE_BONE_LOCAL) {
1050                         OperationKey pose_init_key(id,
1051                                                    DEG_NODE_TYPE_EVAL_POSE,
1052                                                    DEG_OPCODE_POSE_INIT);
1053                         add_relation(adt_key, pose_init_key, "Animation -> Prop", true);
1054                         continue;
1055                 }
1056                 graph_->add_new_relation(operation_from, operation_to,
1057                                          "Animation -> Prop",
1058                                          true);
1059                 /* It is possible that animation is writing to a nested ID datablock,
1060                  * need to make sure animation is evaluated after target ID is copied.
1061                  */
1062                 const IDDepsNode *id_node_from = operation_from->owner->owner;
1063                 const IDDepsNode *id_node_to = operation_to->owner->owner;
1064                 if (id_node_from != id_node_to) {
1065                         ComponentKey cow_key(id_node_to->id_orig,
1066                                              DEG_NODE_TYPE_COPY_ON_WRITE);
1067                         add_relation(cow_key, adt_key, "Target CoW -> Animation", true);
1068                 }
1069         }
1070 }
1071
1072 void DepsgraphRelationBuilder::build_animdata_nlastrip_targets(
1073         ID *id, ComponentKey &adt_key,
1074         OperationDepsNode *operation_from,
1075         ListBase *strips)
1076 {
1077         LISTBASE_FOREACH(NlaStrip *, strip, strips) {
1078                 if (strip->act != NULL) {
1079                         build_animdata_curves_targets(id, adt_key,
1080                                                       operation_from,
1081                                                       &strip->act->curves);
1082                 }
1083                 else if (strip->strips.first != NULL) {
1084                         build_animdata_nlastrip_targets(id, adt_key,
1085                                                         operation_from,
1086                                                         &strip->strips);
1087                 }
1088         }
1089 }
1090
1091 void DepsgraphRelationBuilder::build_animdata_drivers(ID *id)
1092 {
1093         AnimData *adt = BKE_animdata_from_id(id);
1094         if (adt == NULL) {
1095                 return;
1096         }
1097         ComponentKey adt_key(id, DEG_NODE_TYPE_ANIMATION);
1098         LISTBASE_FOREACH (FCurve *, fcu, &adt->drivers) {
1099                 OperationKey driver_key(id,
1100                                         DEG_NODE_TYPE_PARAMETERS,
1101                                         DEG_OPCODE_DRIVER,
1102                                         fcu->rna_path ? fcu->rna_path : "",
1103                                         fcu->array_index);
1104
1105                 /* create the driver's relations to targets */
1106                 build_driver(id, fcu);
1107                 /* Special case for array drivers: we can not multithread them because
1108                  * of the way how they work internally: animation system will write the
1109                  * whole array back to RNA even when changing individual array value.
1110                  *
1111                  * Some tricky things here:
1112                  * - array_index is -1 for single channel drivers, meaning we only have
1113                  *   to do some magic when array_index is not -1.
1114                  * - We do relation from next array index to a previous one, so we don't
1115                  *   have to deal with array index 0.
1116                  *
1117                  * TODO(sergey): Avoid liner lookup somehow.
1118                  */
1119                 if (fcu->array_index > 0) {
1120                         FCurve *fcu_prev = NULL;
1121                         LISTBASE_FOREACH (FCurve *, fcu_candidate, &adt->drivers) {
1122                                 /* Writing to different RNA paths is  */
1123                                 const char *rna_path = fcu->rna_path ? fcu->rna_path : "";
1124                                 if (!STREQ(fcu_candidate->rna_path, rna_path)) {
1125                                         continue;
1126                                 }
1127                                 /* We only do relation from previous fcurve to previous one. */
1128                                 if (fcu_candidate->array_index >= fcu->array_index) {
1129                                         continue;
1130                                 }
1131                                 /* Choose fcurve with highest possible array index. */
1132                                 if (fcu_prev == NULL ||
1133                                     fcu_candidate->array_index > fcu_prev->array_index)
1134                                 {
1135                                         fcu_prev = fcu_candidate;
1136                                 }
1137                         }
1138                         if (fcu_prev != NULL) {
1139                                 OperationKey prev_driver_key(id,
1140                                                              DEG_NODE_TYPE_PARAMETERS,
1141                                                              DEG_OPCODE_DRIVER,
1142                                                              fcu_prev->rna_path ? fcu_prev->rna_path : "",
1143                                                              fcu_prev->array_index);
1144                                 OperationKey driver_key(id,
1145                                                         DEG_NODE_TYPE_PARAMETERS,
1146                                                         DEG_OPCODE_DRIVER,
1147                                                         fcu->rna_path ? fcu->rna_path : "",
1148                                                         fcu->array_index);
1149                                 add_relation(prev_driver_key, driver_key, "Driver Order");
1150                         }
1151                 }
1152
1153                 /* prevent driver from occurring before own animation... */
1154                 if (adt->action || adt->nla_tracks.first) {
1155                         add_relation(adt_key, driver_key, "AnimData Before Drivers");
1156                 }
1157         }
1158 }
1159
1160 void DepsgraphRelationBuilder::build_driver(ID *id, FCurve *fcu)
1161 {
1162         ChannelDriver *driver = fcu->driver;
1163         OperationKey driver_key(id,
1164                                 DEG_NODE_TYPE_PARAMETERS,
1165                                 DEG_OPCODE_DRIVER,
1166                                 fcu->rna_path ? fcu->rna_path : "",
1167                                 fcu->array_index);
1168         /* Driver -> data components (for interleaved evaluation
1169          * bones/constraints/modifiers).
1170          */
1171         build_driver_data(id, fcu);
1172         /* Loop over variables to get the target relationships. */
1173         build_driver_variables(id, fcu);
1174         /* It's quite tricky to detect if the driver actually depends on time or
1175          * not, so for now we'll be quite conservative here about optimization and
1176          * consider all python drivers to be depending on time.
1177          */
1178         if ((driver->type == DRIVER_TYPE_PYTHON) &&
1179             python_driver_depends_on_time(driver))
1180         {
1181                 TimeSourceKey time_src_key;
1182                 add_relation(time_src_key, driver_key, "TimeSrc -> Driver");
1183         }
1184 }
1185
1186 void DepsgraphRelationBuilder::build_driver_data(ID *id, FCurve *fcu)
1187 {
1188         OperationKey driver_key(id,
1189                                 DEG_NODE_TYPE_PARAMETERS,
1190                                 DEG_OPCODE_DRIVER,
1191                                 fcu->rna_path ? fcu->rna_path : "",
1192                                 fcu->array_index);
1193         const char *rna_path = fcu->rna_path ? fcu->rna_path : "";
1194         const RNAPathKey self_key(id, rna_path);
1195         if (GS(id->name) == ID_AR && strstr(rna_path, "bones[")) {
1196                 /* Drivers on armature-level bone settings (i.e. bbone stuff),
1197                  * which will affect the evaluation of corresponding pose bones.
1198                  */
1199                 IDDepsNode *arm_node = graph_->find_id_node(id);
1200                 char *bone_name = BLI_str_quoted_substrN(rna_path, "bones[");
1201                 if (arm_node != NULL && bone_name != NULL) {
1202                         /* Find objects which use this, and make their eval callbacks
1203                          * depend on this.
1204                          */
1205                         foreach (DepsRelation *rel, arm_node->outlinks) {
1206                                 IDDepsNode *to_node = (IDDepsNode *)rel->to;
1207                                 /* We only care about objects with pose data which use this. */
1208                                 if (GS(to_node->id_orig->name) == ID_OB) {
1209                                         Object *object = (Object *)to_node->id_orig;
1210                                         // NOTE: object->pose may be NULL
1211                                         bPoseChannel *pchan = BKE_pose_channel_find_name(object->pose,
1212                                                                                          bone_name);
1213                                         if (pchan != NULL) {
1214                                                 OperationKey bone_key(&object->id,
1215                                                                       DEG_NODE_TYPE_BONE,
1216                                                                       pchan->name,
1217                                                                       DEG_OPCODE_BONE_LOCAL);
1218                                                 add_relation(driver_key,
1219                                                              bone_key,
1220                                                              "Arm Bone -> Driver -> Bone");
1221                                         }
1222                                 }
1223                         }
1224                         /* Free temp data. */
1225                         MEM_freeN(bone_name);
1226                         bone_name = NULL;
1227                 }
1228                 else {
1229                         fprintf(stderr,
1230                                 "Couldn't find armature bone name for driver path - '%s'\n",
1231                                 rna_path);
1232                 }
1233         }
1234         else {
1235                 RNAPathKey target_key(id, rna_path);
1236                 add_relation(driver_key, target_key, "Driver -> Target");
1237                 /* Similar to the case with f-curves, driver might drive a nested
1238                  * datablock, which means driver execution should wait for that
1239                  * datablock to be copied.
1240                  */
1241                 {
1242                         PointerRNA id_ptr;
1243                         PointerRNA ptr;
1244                         RNA_id_pointer_create(id, &id_ptr);
1245                         if (RNA_path_resolve_full(&id_ptr, fcu->rna_path, &ptr, NULL, NULL)) {
1246                                 if (id_ptr.id.data != ptr.id.data) {
1247                                         ComponentKey cow_key((ID *)ptr.id.data,
1248                                                              DEG_NODE_TYPE_COPY_ON_WRITE);
1249                                         add_relation(cow_key,
1250                                                      driver_key,
1251                                                      "Target CoW -> Driver",
1252                                                      true);
1253                                 }
1254                         }
1255                 }
1256                 if (RNA_pointer_is_null(&target_key.ptr)) {
1257                         /* TODO(sergey): This would only mean that driver is broken.
1258                          * so we can't create relation anyway. However, we need to avoid
1259                          * adding drivers which are known to be buggy to a dependency
1260                          * graph, in order to save computational power.
1261                          */
1262                 }
1263                 else {
1264                         if (target_key.prop != NULL &&
1265                             RNA_property_is_idprop(target_key.prop))
1266                         {
1267                                 OperationKey parameters_key(id,
1268                                                             DEG_NODE_TYPE_PARAMETERS,
1269                                                             DEG_OPCODE_PARAMETERS_EVAL);
1270                                 add_relation(target_key,
1271                                              parameters_key,
1272                                              "Driver Target -> Properties");
1273                         }
1274                         add_relation(driver_key, target_key, "Driver -> Target");
1275                 }
1276         }
1277 }
1278
1279 void DepsgraphRelationBuilder::build_driver_variables(ID *id, FCurve *fcu)
1280 {
1281         ChannelDriver *driver = fcu->driver;
1282         OperationKey driver_key(id,
1283                                 DEG_NODE_TYPE_PARAMETERS,
1284                                 DEG_OPCODE_DRIVER,
1285                                 fcu->rna_path ? fcu->rna_path : "",
1286                                 fcu->array_index);
1287         const char *rna_path = fcu->rna_path ? fcu->rna_path : "";
1288         const RNAPathKey self_key(id, rna_path);
1289
1290         LISTBASE_FOREACH (DriverVar *, dvar, &driver->variables) {
1291                 /* Only used targets. */
1292                 DRIVER_TARGETS_USED_LOOPER(dvar)
1293                 {
1294                         if (dtar->id == NULL) {
1295                                 continue;
1296                         }
1297                         build_id(dtar->id);
1298                         /* Special handling for directly-named bones. */
1299                         if ((dtar->flag & DTAR_FLAG_STRUCT_REF) &&
1300                             (((Object *)dtar->id)->type == OB_ARMATURE) &&
1301                             (dtar->pchan_name[0]))
1302                         {
1303                                 Object *object = (Object *)dtar->id;
1304                                 bPoseChannel *target_pchan =
1305                                         BKE_pose_channel_find_name(object->pose,
1306                                                                    dtar->pchan_name);
1307                                 if (target_pchan == NULL) {
1308                                         continue;
1309                                 }
1310                                 OperationKey variable_key(dtar->id,
1311                                                           DEG_NODE_TYPE_BONE,
1312                                                           target_pchan->name,
1313                                                           DEG_OPCODE_BONE_DONE);
1314                                 if (is_same_bone_dependency(variable_key, self_key)) {
1315                                         continue;
1316                                 }
1317                                 add_relation(variable_key, driver_key, "Bone Target -> Driver");
1318                         }
1319                         else if (dtar->flag & DTAR_FLAG_STRUCT_REF) {
1320                                 /* Get node associated with the object's transforms. */
1321                                 if (dtar->id == id) {
1322                                         /* Ignore input dependency if we're driving properties of
1323                                          * the same ID, otherwise we'll be ending up in a cyclic
1324                                          * dependency here.
1325                                          */
1326                                         continue;
1327                                 }
1328                                 OperationKey target_key(dtar->id,
1329                                                         DEG_NODE_TYPE_TRANSFORM,
1330                                                         DEG_OPCODE_TRANSFORM_FINAL);
1331                                 add_relation(target_key, driver_key, "Target -> Driver");
1332                         }
1333                         else if (dtar->rna_path) {
1334                                 RNAPathKey variable_key(dtar->id, dtar->rna_path);
1335                                 if (RNA_pointer_is_null(&variable_key.ptr)) {
1336                                         continue;
1337                                 }
1338                                 if (is_same_bone_dependency(variable_key, self_key) ||
1339                                     is_same_nodetree_node_dependency(variable_key, self_key) ||
1340                                     is_same_shapekey_dependency(variable_key, self_key))
1341                                 {
1342                                         continue;
1343                                 }
1344                                 add_relation(variable_key, driver_key, "RNA Target -> Driver");
1345                         }
1346                         else {
1347                                 if (dtar->id == id) {
1348                                         /* Ignore input dependency if we're driving properties of
1349                                          * the same ID, otherwise we'll be ending up in a cyclic
1350                                          * dependency here.
1351                                          */
1352                                         continue;
1353                                 }
1354                                 /* Resolve path to get node. */
1355                                 RNAPathKey target_key(dtar->id,
1356                                                       dtar->rna_path ? dtar->rna_path : "");
1357                                 add_relation(target_key, driver_key, "RNA Target -> Driver");
1358                         }
1359                 }
1360                 DRIVER_TARGETS_LOOPER_END
1361         }
1362 }
1363
1364 void DepsgraphRelationBuilder::build_world(World *world)
1365 {
1366         if (built_map_.checkIsBuiltAndTag(world)) {
1367                 return;
1368         }
1369         build_animdata(&world->id);
1370         /* TODO: other settings? */
1371         /* world's nodetree */
1372         if (world->nodetree != NULL) {
1373                 build_nodetree(world->nodetree);
1374                 ComponentKey ntree_key(&world->nodetree->id, DEG_NODE_TYPE_SHADING);
1375                 ComponentKey world_key(&world->id, DEG_NODE_TYPE_SHADING);
1376                 add_relation(ntree_key, world_key, "NTree->World Shading Update");
1377                 build_nested_nodetree(&world->id, world->nodetree);
1378         }
1379 }
1380
1381 void DepsgraphRelationBuilder::build_rigidbody(Scene *scene)
1382 {
1383         RigidBodyWorld *rbw = scene->rigidbody_world;
1384
1385         OperationKey init_key(&scene->id, DEG_NODE_TYPE_TRANSFORM, DEG_OPCODE_RIGIDBODY_REBUILD);
1386         OperationKey sim_key(&scene->id, DEG_NODE_TYPE_TRANSFORM, DEG_OPCODE_RIGIDBODY_SIM);
1387
1388         /* rel between the two sim-nodes */
1389         add_relation(init_key, sim_key, "Rigidbody [Init -> SimStep]");
1390
1391         /* set up dependencies between these operations and other builtin nodes --------------- */
1392
1393         /* time dependency */
1394         TimeSourceKey time_src_key;
1395         add_relation(time_src_key, init_key, "TimeSrc -> Rigidbody Reset/Rebuild (Optional)");
1396
1397         /* objects - simulation participants */
1398         if (rbw->group) {
1399                 const ListBase group_objects = BKE_collection_object_cache_get(rbw->group);
1400                 LISTBASE_FOREACH (Base *, base, &group_objects) {
1401                         Object *object = base->object;
1402                         if (object == NULL || object->type != OB_MESH) {
1403                                 continue;
1404                         }
1405
1406                         /* hook up evaluation order...
1407                          * 1) flushing rigidbody results follows base transforms being applied
1408                          * 2) rigidbody flushing can only be performed after simulation has been run
1409                          *
1410                          * 3) simulation needs to know base transforms to figure out what to do
1411                          *    XXX: there's probably a difference between passive and active
1412                          *         - passive don't change, so may need to know full transform...
1413                          */
1414                         OperationKey rbo_key(&object->id, DEG_NODE_TYPE_TRANSFORM, DEG_OPCODE_RIGIDBODY_TRANSFORM_COPY);
1415
1416                         eDepsOperation_Code trans_opcode = object->parent ? DEG_OPCODE_TRANSFORM_PARENT : DEG_OPCODE_TRANSFORM_LOCAL;
1417                         OperationKey trans_op(&object->id, DEG_NODE_TYPE_TRANSFORM, trans_opcode);
1418
1419                         add_relation(sim_key, rbo_key, "Rigidbody Sim Eval -> RBO Sync");
1420
1421                         /* if constraints exist, those depend on the result of the rigidbody sim
1422                          * - This allows constraints to modify the result of the sim (i.e. clamping)
1423                          *   while still allowing the sim to depend on some changes to the objects.
1424                          *   Also, since constraints are hooked up to the final nodes, this link
1425                          *   means that we can also fit in there too...
1426                          * - Later, it might be good to include a constraint in the stack allowing us
1427                          *   to control whether rigidbody eval gets interleaved into the constraint stack
1428                          */
1429                         if (object->constraints.first) {
1430                                 OperationKey constraint_key(&object->id,
1431                                                             DEG_NODE_TYPE_TRANSFORM,
1432                                                             DEG_OPCODE_TRANSFORM_CONSTRAINTS);
1433                                 add_relation(rbo_key, constraint_key, "RBO Sync -> Ob Constraints");
1434                         }
1435                         else {
1436                                 /* Final object transform depends on rigidbody.
1437                                  *
1438                                  * NOTE: Currently we consider final here an ubereval node.
1439                                  * If it is gone we'll need to reconsider relation here.
1440                                  */
1441                                 OperationKey uber_key(&object->id,
1442                                                       DEG_NODE_TYPE_TRANSFORM,
1443                                                       DEG_OPCODE_TRANSFORM_OBJECT_UBEREVAL);
1444                                 add_relation(rbo_key, uber_key, "RBO Sync -> Uber (Temp)");
1445                         }
1446
1447                         /* Needed to get correct base values. */
1448                         add_relation(trans_op, sim_key, "Base Ob Transform -> Rigidbody Sim Eval");
1449                 }
1450         }
1451
1452         /* constraints */
1453         if (rbw->constraints) {
1454                 const ListBase constraint_objects = BKE_collection_object_cache_get(rbw->constraints);
1455                 LISTBASE_FOREACH (Base *, base, &constraint_objects) {
1456                         Object *object = base->object;
1457                         if (object == NULL || !object->rigidbody_constraint) {
1458                                 continue;
1459                         }
1460
1461                         RigidBodyCon *rbc = object->rigidbody_constraint;
1462
1463                         /* final result of the constraint object's transform controls how the
1464                          * constraint affects the physics sim for these objects
1465                          */
1466                         ComponentKey trans_key(&object->id, DEG_NODE_TYPE_TRANSFORM);
1467                         OperationKey ob1_key(&rbc->ob1->id, DEG_NODE_TYPE_TRANSFORM, DEG_OPCODE_RIGIDBODY_TRANSFORM_COPY);
1468                         OperationKey ob2_key(&rbc->ob2->id, DEG_NODE_TYPE_TRANSFORM, DEG_OPCODE_RIGIDBODY_TRANSFORM_COPY);
1469
1470                         /* - constrained-objects sync depends on the constraint-holder */
1471                         add_relation(trans_key, ob1_key, "RigidBodyConstraint -> RBC.Object_1");
1472                         add_relation(trans_key, ob2_key, "RigidBodyConstraint -> RBC.Object_2");
1473
1474                         /* - ensure that sim depends on this constraint's transform */
1475                         add_relation(trans_key, sim_key, "RigidBodyConstraint Transform -> RB Simulation");
1476                 }
1477         }
1478 }
1479
1480 void DepsgraphRelationBuilder::build_particles(Object *object)
1481 {
1482         TimeSourceKey time_src_key;
1483         OperationKey obdata_ubereval_key(&object->id,
1484                                          DEG_NODE_TYPE_GEOMETRY,
1485                                          DEG_OPCODE_GEOMETRY_UBEREVAL);
1486         OperationKey eval_init_key(&object->id,
1487                                    DEG_NODE_TYPE_EVAL_PARTICLES,
1488                                    DEG_OPCODE_PARTICLE_SYSTEM_EVAL_INIT);
1489
1490         /* Particle systems. */
1491         LISTBASE_FOREACH (ParticleSystem *, psys, &object->particlesystem) {
1492                 ParticleSettings *part = psys->part;
1493
1494                 /* Build particle settings relations.
1495                  *
1496                  * NOTE: The call itself ensures settings are only build once.
1497                  */
1498                 build_particle_settings(part);
1499
1500                 /* This particle system. */
1501                 OperationKey psys_key(&object->id,
1502                                       DEG_NODE_TYPE_EVAL_PARTICLES,
1503                                       DEG_OPCODE_PARTICLE_SYSTEM_EVAL,
1504                                       psys->name);
1505
1506                 /* Update particle system when settings changes. */
1507                 OperationKey particle_settings_key(&part->id,
1508                                                    DEG_NODE_TYPE_PARAMETERS,
1509                                                    DEG_OPCODE_PARTICLE_SETTINGS_EVAL);
1510                 add_relation(particle_settings_key, eval_init_key, "Particle Settings Change");
1511                 add_relation(eval_init_key, psys_key, "Init -> PSys");
1512                 /* TODO(sergey): Currently particle update is just a placeholder,
1513                  * hook it to the ubereval node so particle system is getting updated
1514                  * on playback.
1515                  */
1516                 add_relation(psys_key, obdata_ubereval_key, "PSys -> UberEval");
1517                 /* Collisions */
1518                 if (part->type != PART_HAIR) {
1519                         add_collision_relations(psys_key,
1520                                                 scene_,
1521                                                 object,
1522                                                 part->collision_group,
1523                                                 true,
1524                                                 "Particle Collision");
1525                 }
1526                 else if ((psys->flag & PSYS_HAIR_DYNAMICS) &&
1527                          psys->clmd != NULL &&
1528                          psys->clmd->coll_parms != NULL)
1529                 {
1530                         add_collision_relations(psys_key,
1531                                                 scene_,
1532                                                 object,
1533                                                 psys->clmd->coll_parms->group,
1534                                                 true,
1535                                                 "Hair Collision");
1536                 }
1537                 /* Effectors. */
1538                 add_forcefield_relations(psys_key,
1539                                          scene_,
1540                                          object,
1541                                          psys,
1542                                          part->effector_weights,
1543                                          part->type == PART_HAIR,
1544                                          "Particle Field");
1545                 /* Boids .*/
1546                 if (part->boids) {
1547                         LISTBASE_FOREACH (BoidState *, state, &part->boids->states) {
1548                                 LISTBASE_FOREACH (BoidRule *, rule, &state->rules) {
1549                                         Object *ruleob = NULL;
1550                                         if (rule->type == eBoidRuleType_Avoid) {
1551                                                 ruleob = ((BoidRuleGoalAvoid *)rule)->ob;
1552                                         }
1553                                         else if (rule->type == eBoidRuleType_FollowLeader) {
1554                                                 ruleob = ((BoidRuleFollowLeader *)rule)->ob;
1555                                         }
1556                                         if (ruleob) {
1557                                                 ComponentKey ruleob_key(&ruleob->id,
1558                                                                         DEG_NODE_TYPE_TRANSFORM);
1559                                                 add_relation(ruleob_key, psys_key, "Boid Rule");
1560                                         }
1561                                 }
1562                         }
1563                 }
1564                 switch (part->ren_as) {
1565                         case PART_DRAW_OB:
1566                                 if (part->dup_ob != NULL) {
1567                                         /* Make sure object's relations are all built.  */
1568                                         build_object(NULL, part->dup_ob);
1569                                         /* Build relation for the particle visualization. */
1570                                         build_particles_visualization_object(object,
1571                                                                              psys,
1572                                                                              part->dup_ob);
1573                                 }
1574                                 break;
1575                         case PART_DRAW_GR:
1576                                 if (part->dup_group != NULL) {
1577                                         build_collection(NULL, part->dup_group);
1578                                         LISTBASE_FOREACH (CollectionObject *, go, &part->dup_group->gobject) {
1579                                                 build_particles_visualization_object(object,
1580                                                                                      psys,
1581                                                                                      go->ob);
1582                                         }
1583                                 }
1584                                 break;
1585                 }
1586         }
1587
1588         /* Particle depends on the object transform, so that channel is to be ready
1589          * first.
1590          *
1591          * TODO(sergey): This relation should be altered once real granular update
1592          * is implemented.
1593          */
1594         ComponentKey transform_key(&object->id, DEG_NODE_TYPE_TRANSFORM);
1595         add_relation(transform_key, obdata_ubereval_key, "Partcile Eval");
1596
1597         OperationKey point_cache_reset_key(&object->id,
1598                                            DEG_NODE_TYPE_CACHE,
1599                                            DEG_OPCODE_POINT_CACHE_RESET);
1600         add_relation(transform_key, point_cache_reset_key, "Object Transform -> Point Cache Reset");
1601         add_relation(point_cache_reset_key, obdata_ubereval_key, "Point Cache Reset -> UberEval");
1602 }
1603
1604 void DepsgraphRelationBuilder::build_particle_settings(ParticleSettings *part)
1605 {
1606         if (built_map_.checkIsBuiltAndTag(part)) {
1607                 return;
1608         }
1609         /* Animation data relations. */
1610         build_animdata(&part->id);
1611 }
1612
1613 void DepsgraphRelationBuilder::build_particles_visualization_object(
1614         Object *object,
1615         ParticleSystem *psys,
1616         Object *draw_object)
1617 {
1618         OperationKey psys_key(&object->id,
1619                               DEG_NODE_TYPE_EVAL_PARTICLES,
1620                               DEG_OPCODE_PARTICLE_SYSTEM_EVAL,
1621                               psys->name);
1622         OperationKey obdata_ubereval_key(&object->id,
1623                                          DEG_NODE_TYPE_GEOMETRY,
1624                                          DEG_OPCODE_GEOMETRY_UBEREVAL);
1625         ComponentKey dup_ob_key(&draw_object->id, DEG_NODE_TYPE_TRANSFORM);
1626         add_relation(dup_ob_key, psys_key, "Particle Object Visualization");
1627         if (draw_object->type == OB_MBALL) {
1628                 ComponentKey dup_geometry_key(&draw_object->id, DEG_NODE_TYPE_GEOMETRY);
1629                 add_relation(obdata_ubereval_key,
1630                              dup_geometry_key,
1631                              "Particle MBall Visualization");
1632         }
1633 }
1634
1635 void DepsgraphRelationBuilder::build_cloth(Object *object,
1636                                            ModifierData * /*md*/)
1637 {
1638         OperationKey cache_key(&object->id,
1639                                DEG_NODE_TYPE_CACHE,
1640                                DEG_OPCODE_GEOMETRY_CLOTH_MODIFIER);
1641         /* Cache component affects on modifier. */
1642         OperationKey modifier_key(&object->id,
1643                                   DEG_NODE_TYPE_GEOMETRY,
1644                                   DEG_OPCODE_GEOMETRY_UBEREVAL);
1645         add_relation(cache_key, modifier_key, "Cloth Cache -> Cloth");
1646 }
1647
1648 /* Shapekeys */
1649 void DepsgraphRelationBuilder::build_shapekeys(ID *obdata, Key *key)
1650 {
1651         ComponentKey obdata_key(obdata, DEG_NODE_TYPE_GEOMETRY);
1652
1653         /* attach animdata to geometry */
1654         build_animdata(&key->id);
1655
1656         if (key->adt) {
1657                 // TODO: this should really be handled in build_animdata, since many of these cases will need it
1658                 if (key->adt->action || key->adt->nla_tracks.first) {
1659                         ComponentKey adt_key(&key->id, DEG_NODE_TYPE_ANIMATION);
1660                         add_relation(adt_key, obdata_key, "Animation");
1661                 }
1662
1663                 /* NOTE: individual shapekey drivers are handled above already */
1664         }
1665
1666         /* attach to geometry */
1667         // XXX: aren't shapekeys now done as a pseudo-modifier on object?
1668         //ComponentKey key_key(&key->id, DEG_NODE_TYPE_GEOMETRY); // FIXME: this doesn't exist
1669         //add_relation(key_key, obdata_key, "Shapekeys");
1670 }
1671
1672 /**
1673  * ObData Geometry Evaluation
1674  * ==========================
1675  *
1676  * The evaluation of geometry on objects is as follows:
1677  * - The actual evaluated of the derived geometry (e.g. DerivedMesh, DispList)
1678  *   occurs in the Geometry component of the object which references this.
1679  *   This includes modifiers, and the temporary "ubereval" for geometry.
1680  *   Therefore, each user of a piece of shared geometry data ends up evaluating
1681  *   its own version of the stuff, complete with whatever modifiers it may use.
1682  *
1683  * - The datablocks for the geometry data - "obdata" (e.g. ID_ME, ID_CU, ID_LT.)
1684  *   are used for
1685  *     1) calculating the bounding boxes of the geometry data,
1686  *     2) aggregating inward links from other objects (e.g. for text on curve)
1687  *        and also for the links coming from the shapekey datablocks
1688  * - Animation/Drivers affecting the parameters of the geometry are made to
1689  *   trigger updates on the obdata geometry component, which then trigger
1690  *   downstream re-evaluation of the individual instances of this geometry.
1691  */
1692 void DepsgraphRelationBuilder::build_object_data_geometry(Object *object)
1693 {
1694         ID *obdata = (ID *)object->data;
1695         /* Init operation of object-level geometry evaluation. */
1696         OperationKey geom_init_key(&object->id,
1697                                    DEG_NODE_TYPE_GEOMETRY,
1698                                                            DEG_OPCODE_PLACEHOLDER,
1699                                                            "Eval Init");
1700         /* Get nodes for result of obdata's evaluation, and geometry evaluation
1701          * on object.
1702          */
1703         ComponentKey obdata_geom_key(obdata, DEG_NODE_TYPE_GEOMETRY);
1704         ComponentKey geom_key(&object->id, DEG_NODE_TYPE_GEOMETRY);
1705         /* Link components to each other. */
1706         add_relation(obdata_geom_key, geom_key, "Object Geometry Base Data");
1707         OperationKey obdata_ubereval_key(&object->id,
1708                                          DEG_NODE_TYPE_GEOMETRY,
1709                                          DEG_OPCODE_GEOMETRY_UBEREVAL);
1710         /* Special case: modifiers evaluation queries scene for various things like
1711          * data mask to be used. We add relation here to ensure object is never
1712          * evaluated prior to Scene's CoW is ready.
1713          */
1714         OperationKey scene_key(&scene_->id,
1715                                DEG_NODE_TYPE_PARAMETERS,
1716                                DEG_OPCODE_PLACEHOLDER,
1717                                "Scene Eval");
1718         DepsRelation *rel = add_relation(scene_key, obdata_ubereval_key, "CoW Relation");
1719         rel->flag |= DEPSREL_FLAG_NO_FLUSH;
1720         /* Modifiers */
1721         if (object->modifiers.first != NULL) {
1722                 ModifierUpdateDepsgraphContext ctx = {};
1723                 ctx.scene = scene_;
1724                 ctx.object = object;
1725                 LISTBASE_FOREACH (ModifierData *, md, &object->modifiers) {
1726                         const ModifierTypeInfo *mti = modifierType_getInfo((ModifierType)md->type);
1727                         if (mti->updateDepsgraph) {
1728                                 DepsNodeHandle handle = create_node_handle(obdata_ubereval_key);
1729                                 ctx.node = reinterpret_cast< ::DepsNodeHandle* >(&handle);
1730                                 mti->updateDepsgraph(md, &ctx);
1731                         }
1732                         if (BKE_object_modifier_use_time(object, md)) {
1733                                 TimeSourceKey time_src_key;
1734                                 add_relation(time_src_key, obdata_ubereval_key, "Time Source");
1735                         }
1736                         if (md->type == eModifierType_Cloth) {
1737                                 build_cloth(object, md);
1738                         }
1739                 }
1740         }
1741         /* Materials. */
1742         if (object->totcol) {
1743                 for (int a = 1; a <= object->totcol; a++) {
1744                         Material *ma = give_current_material(object, a);
1745                         if (ma != NULL) {
1746                                 build_material(ma);
1747
1748                                 if (object->type == OB_MESH) {
1749                                         OperationKey material_key(&ma->id,
1750                                                                   DEG_NODE_TYPE_SHADING,
1751                                                                   DEG_OPCODE_MATERIAL_UPDATE);
1752                                         OperationKey shading_key(&object->id,
1753                                                                  DEG_NODE_TYPE_SHADING,
1754                                                                  DEG_OPCODE_SHADING);
1755                                         add_relation(material_key, shading_key, "Material Update");
1756                                 }
1757                         }
1758                 }
1759         }
1760         /* Geometry collision. */
1761         if (ELEM(object->type, OB_MESH, OB_CURVE, OB_LATTICE)) {
1762                 // add geometry collider relations
1763         }
1764         /* Make sure uber update is the last in the dependencies.
1765          *
1766          * TODO(sergey): Get rid of this node.
1767          */
1768         if (object->type != OB_ARMATURE) {
1769                 /* Armatures does no longer require uber node. */
1770                 OperationKey obdata_ubereval_key(&object->id,
1771                                                  DEG_NODE_TYPE_GEOMETRY,
1772                                                  DEG_OPCODE_GEOMETRY_UBEREVAL);
1773                 add_relation(geom_init_key,
1774                              obdata_ubereval_key,
1775                              "Object Geometry UberEval");
1776         }
1777         if (object->type == OB_MBALL) {
1778                         Object *mom = BKE_mball_basis_find(scene_, object);
1779                         ComponentKey mom_geom_key(&mom->id, DEG_NODE_TYPE_GEOMETRY);
1780                         /* motherball - mom depends on children! */
1781                         if (mom == object) {
1782                                 ComponentKey mom_transform_key(&mom->id,
1783                                                                DEG_NODE_TYPE_TRANSFORM);
1784                                 add_relation(mom_transform_key,
1785                                              mom_geom_key,
1786                                              "Metaball Motherball Transform -> Geometry");
1787                         }
1788                         else {
1789                                 ComponentKey transform_key(&object->id, DEG_NODE_TYPE_TRANSFORM);
1790                                 add_relation(geom_key, mom_geom_key, "Metaball Motherball");
1791                                 add_relation(transform_key, mom_geom_key, "Metaball Motherball");
1792                         }
1793         }
1794         /* NOTE: This is compatibility code to support particle systems
1795          *
1796          * for viewport being properly rendered in final render mode.
1797          * This relation is similar to what dag_object_time_update_flags()
1798          * was doing for mesh objects with particle system.
1799          *
1800          * Ideally we need to get rid of this relation.
1801          */
1802         if (object_particles_depends_on_time(object)) {
1803                 TimeSourceKey time_key;
1804                 OperationKey obdata_ubereval_key(&object->id,
1805                                                  DEG_NODE_TYPE_GEOMETRY,
1806                                                  DEG_OPCODE_GEOMETRY_UBEREVAL);
1807                 add_relation(time_key, obdata_ubereval_key, "Legacy particle time");
1808         }
1809         /* Object data datablock. */
1810         build_object_data_geometry_datablock((ID *)object->data);
1811 }
1812
1813 void DepsgraphRelationBuilder::build_object_data_geometry_datablock(ID *obdata)
1814 {
1815         if (built_map_.checkIsBuiltAndTag(obdata)) {
1816                 return;
1817         }
1818         /* Animation. */
1819         build_animdata(obdata);
1820         /* ShapeKeys. */
1821         Key *key = BKE_key_from_id(obdata);
1822         if (key != NULL) {
1823                 build_shapekeys(obdata, key);
1824         }
1825         /* Link object data evaluation node to exit operation. */
1826         OperationKey obdata_geom_eval_key(obdata,
1827                                           DEG_NODE_TYPE_GEOMETRY,
1828                                           DEG_OPCODE_PLACEHOLDER,
1829                                           "Geometry Eval");
1830         OperationKey obdata_geom_done_key(obdata,
1831                                           DEG_NODE_TYPE_GEOMETRY,
1832                                           DEG_OPCODE_PLACEHOLDER,
1833                                           "Eval Done");
1834         add_relation(obdata_geom_eval_key,
1835                      obdata_geom_done_key,
1836                                  "ObData Geom Eval Done");
1837         /* Type-specific links. */
1838         const ID_Type id_type = GS(obdata->name);
1839         switch (id_type) {
1840                 case ID_ME:
1841                         break;
1842                 case ID_MB:
1843                         break;
1844                 case ID_CU:
1845                 {
1846                         Curve *cu = (Curve *)obdata;
1847                         if (cu->bevobj != NULL) {
1848                                 ComponentKey bevob_geom_key(&cu->bevobj->id,
1849                                                             DEG_NODE_TYPE_GEOMETRY);
1850                                 add_relation(bevob_geom_key,
1851                                              obdata_geom_eval_key,
1852                                              "Curve Bevel Geometry");
1853                                 ComponentKey bevob_key(&cu->bevobj->id,
1854                                                        DEG_NODE_TYPE_TRANSFORM);
1855                                 add_relation(bevob_key,
1856                                              obdata_geom_eval_key,
1857                                              "Curve Bevel Transform");
1858                                 build_object(NULL, cu->bevobj);
1859                         }
1860                         if (cu->taperobj != NULL) {
1861                                 ComponentKey taperob_key(&cu->taperobj->id,
1862                                                          DEG_NODE_TYPE_GEOMETRY);
1863                                 add_relation(taperob_key, obdata_geom_eval_key, "Curve Taper");
1864                                 build_object(NULL, cu->taperobj);
1865                         }
1866                         if (cu->textoncurve != NULL) {
1867                                 ComponentKey textoncurve_key(&cu->textoncurve->id,
1868                                                              DEG_NODE_TYPE_GEOMETRY);
1869                                 add_relation(textoncurve_key,
1870                                              obdata_geom_eval_key,
1871                                              "Text on Curve");
1872                                 build_object(NULL, cu->textoncurve);
1873                         }
1874                         break;
1875                 }
1876                 case ID_LT:
1877                         break;
1878                 default:
1879                         BLI_assert(!"Should not happen");
1880                         break;
1881         }
1882 }
1883
1884 void DepsgraphRelationBuilder::build_camera(Camera *camera)
1885 {
1886         if (built_map_.checkIsBuiltAndTag(camera)) {
1887                 return;
1888         }
1889         if (camera->dof_ob != NULL) {
1890                 ComponentKey camera_parameters_key(&camera->id, DEG_NODE_TYPE_PARAMETERS);
1891                 ComponentKey dof_ob_key(&camera->dof_ob->id, DEG_NODE_TYPE_TRANSFORM);
1892                 add_relation(dof_ob_key, camera_parameters_key, "Camera DOF");
1893         }
1894 }
1895
1896 /* Lamps */
1897 void DepsgraphRelationBuilder::build_lamp(Lamp *lamp)
1898 {
1899         if (built_map_.checkIsBuiltAndTag(lamp)) {
1900                 return;
1901         }
1902         /* lamp's nodetree */
1903         if (lamp->nodetree != NULL) {
1904                 build_nodetree(lamp->nodetree);
1905                 ComponentKey lamp_parameters_key(&lamp->id, DEG_NODE_TYPE_PARAMETERS);
1906                 ComponentKey nodetree_key(&lamp->nodetree->id, DEG_NODE_TYPE_SHADING);
1907                 add_relation(nodetree_key, lamp_parameters_key, "NTree->Lamp Parameters");
1908                 build_nested_nodetree(&lamp->id, lamp->nodetree);
1909         }
1910 }
1911
1912 void DepsgraphRelationBuilder::build_nodetree(bNodeTree *ntree)
1913 {
1914         if (ntree == NULL) {
1915                 return;
1916         }
1917         if (built_map_.checkIsBuiltAndTag(ntree)) {
1918                 return;
1919         }
1920         build_animdata(&ntree->id);
1921         ComponentKey shading_key(&ntree->id, DEG_NODE_TYPE_SHADING);
1922         /* nodetree's nodes... */
1923         LISTBASE_FOREACH (bNode *, bnode, &ntree->nodes) {
1924                 ID *id = bnode->id;
1925                 if (id == NULL) {
1926                         continue;
1927                 }
1928                 ID_Type id_type = GS(id->name);
1929                 if (id_type == ID_MA) {
1930                         build_material((Material *)bnode->id);
1931                 }
1932                 else if (id_type == ID_TE) {
1933                         build_texture((Tex *)bnode->id);
1934                 }
1935                 else if (id_type == ID_IM) {
1936                         /* nothing for now. */
1937                 }
1938                 else if (id_type == ID_OB) {
1939                         build_object(NULL, (Object *)id);
1940                 }
1941                 else if (id_type == ID_SCE) {
1942                         /* Scenes are used by compositor trees, and handled by render
1943                          * pipeline. No need to build dependencies for them here.
1944                          */
1945                 }
1946                 else if (id_type == ID_TXT) {
1947                         /* Ignore script nodes. */
1948                 }
1949                 else if (bnode->type == NODE_GROUP) {
1950                         bNodeTree *group_ntree = (bNodeTree *)id;
1951                         build_nodetree(group_ntree);
1952                         ComponentKey group_shading_key(&group_ntree->id,
1953                                                        DEG_NODE_TYPE_SHADING);
1954                         add_relation(group_shading_key, shading_key, "Group Node");
1955                 }
1956                 else {
1957                         BLI_assert(!"Unknown ID type used for node");
1958                 }
1959         }
1960
1961         OperationKey shading_update_key(&ntree->id,
1962                                         DEG_NODE_TYPE_SHADING,
1963                                         DEG_OPCODE_MATERIAL_UPDATE);
1964         OperationKey shading_parameters_key(&ntree->id,
1965                                             DEG_NODE_TYPE_SHADING_PARAMETERS,
1966                                             DEG_OPCODE_MATERIAL_UPDATE);
1967         add_relation(shading_parameters_key, shading_update_key, "NTree Shading Parameters");
1968
1969         if (check_id_has_anim_component(&ntree->id)) {
1970                 ComponentKey animation_key(&ntree->id, DEG_NODE_TYPE_ANIMATION);
1971                 add_relation(animation_key, shading_parameters_key, "NTree Shading Parameters");
1972         }
1973 }
1974
1975 /* Recursively build graph for material */
1976 void DepsgraphRelationBuilder::build_material(Material *material)
1977 {
1978         if (built_map_.checkIsBuiltAndTag(material)) {
1979                 return;
1980         }
1981         /* animation */
1982         build_animdata(&material->id);
1983         /* material's nodetree */
1984         if (material->nodetree != NULL) {
1985                 build_nodetree(material->nodetree);
1986                 OperationKey ntree_key(&material->nodetree->id,
1987                                        DEG_NODE_TYPE_SHADING,
1988                                        DEG_OPCODE_MATERIAL_UPDATE);
1989                 OperationKey material_key(&material->id,
1990                                           DEG_NODE_TYPE_SHADING,
1991                                           DEG_OPCODE_MATERIAL_UPDATE);
1992                 add_relation(ntree_key, material_key, "Material's NTree");
1993                 build_nested_nodetree(&material->id, material->nodetree);
1994         }
1995 }
1996
1997 /* Recursively build graph for texture */
1998 void DepsgraphRelationBuilder::build_texture(Tex *texture)
1999 {
2000         if (built_map_.checkIsBuiltAndTag(texture)) {
2001                 return;
2002         }
2003         /* texture itself */
2004         build_animdata(&texture->id);
2005         /* texture's nodetree */
2006         build_nodetree(texture->nodetree);
2007         build_nested_nodetree(&texture->id, texture->nodetree);
2008 }
2009
2010 void DepsgraphRelationBuilder::build_compositor(Scene *scene)
2011 {
2012         /* For now, just a plain wrapper? */
2013         build_nodetree(scene->nodetree);
2014 }
2015
2016 void DepsgraphRelationBuilder::build_gpencil(bGPdata *gpd)
2017 {
2018         if (built_map_.checkIsBuiltAndTag(gpd)) {
2019                 return;
2020         }
2021         /* animation */
2022         build_animdata(&gpd->id);
2023
2024         // TODO: parent object (when that feature is implemented)
2025 }
2026
2027 void DepsgraphRelationBuilder::build_cachefile(CacheFile *cache_file)
2028 {
2029         if (built_map_.checkIsBuiltAndTag(cache_file)) {
2030                 return;
2031         }
2032         /* Animation. */
2033         build_animdata(&cache_file->id);
2034 }
2035
2036 void DepsgraphRelationBuilder::build_mask(Mask *mask)
2037 {
2038         if (built_map_.checkIsBuiltAndTag(mask)) {
2039                 return;
2040         }
2041         ID *mask_id = &mask->id;
2042         /* F-Curve animation. */
2043         build_animdata(mask_id);
2044         /* Own mask animation. */
2045         OperationKey mask_animation_key(mask_id,
2046                                         DEG_NODE_TYPE_ANIMATION,
2047                                         DEG_OPCODE_MASK_ANIMATION);
2048         TimeSourceKey time_src_key;
2049         add_relation(time_src_key, mask_animation_key, "TimeSrc -> Mask Animation");
2050         /* Final mask evaluation. */
2051         ComponentKey parameters_key(mask_id, DEG_NODE_TYPE_PARAMETERS);
2052         add_relation(mask_animation_key, parameters_key, "Mask Animation -> Mask Eval");
2053 }
2054
2055 void DepsgraphRelationBuilder::build_movieclip(MovieClip *clip)
2056 {
2057         if (built_map_.checkIsBuiltAndTag(clip)) {
2058                 return;
2059         }
2060         /* Animation. */
2061         build_animdata(&clip->id);
2062 }
2063
2064 void DepsgraphRelationBuilder::build_lightprobe(LightProbe *probe)
2065 {
2066         if (built_map_.checkIsBuiltAndTag(probe)) {
2067                 return;
2068         }
2069         build_animdata(&probe->id);
2070 }
2071
2072 void DepsgraphRelationBuilder::build_copy_on_write_relations()
2073 {
2074         foreach (IDDepsNode *id_node, graph_->id_nodes) {
2075                 build_copy_on_write_relations(id_node);
2076         }
2077 }
2078
2079 /* Nested datablocks (node trees, shape keys) requires special relation to
2080  * ensure owner's datablock remapping happens after node tree itself is ready.
2081  *
2082  * This is similar to what happens in ntree_hack_remap_pointers().
2083  */
2084 void DepsgraphRelationBuilder::build_nested_datablock(ID *owner, ID *id)
2085 {
2086         OperationKey owner_copy_on_write_key(owner,
2087                                              DEG_NODE_TYPE_COPY_ON_WRITE,
2088                                              DEG_OPCODE_COPY_ON_WRITE);
2089         OperationKey id_copy_on_write_key(id,
2090                                           DEG_NODE_TYPE_COPY_ON_WRITE,
2091                                           DEG_OPCODE_COPY_ON_WRITE);
2092         add_relation(id_copy_on_write_key,
2093                      owner_copy_on_write_key,
2094                      "Eval Order");
2095 }
2096
2097 void DepsgraphRelationBuilder::build_nested_nodetree(ID *owner,
2098                                                      bNodeTree *ntree)
2099 {
2100         if (ntree == NULL) {
2101                 return;
2102         }
2103         build_nested_datablock(owner, &ntree->id);
2104 }
2105
2106 void DepsgraphRelationBuilder::build_nested_shapekey(ID *owner, Key *key)
2107 {
2108         if (key == NULL) {
2109                 return;
2110         }
2111         build_nested_datablock(owner, &key->id);
2112 }
2113
2114 void DepsgraphRelationBuilder::build_copy_on_write_relations(IDDepsNode *id_node)
2115 {
2116         ID *id_orig = id_node->id_orig;
2117
2118         TimeSourceKey time_source_key;
2119         OperationKey copy_on_write_key(id_orig,
2120                                        DEG_NODE_TYPE_COPY_ON_WRITE,
2121                                        DEG_OPCODE_COPY_ON_WRITE);
2122         /* XXX: This is a quick hack to make Alt-A to work. */
2123         // add_relation(time_source_key, copy_on_write_key, "Fluxgate capacitor hack");
2124         /* Resat of code is using rather low level trickery, so need to get some
2125          * explicit pointers.
2126          */
2127         DepsNode *node_cow = find_node(copy_on_write_key);
2128         OperationDepsNode *op_cow = node_cow->get_exit_operation();
2129         /* Plug any other components to this one. */
2130         GHASH_FOREACH_BEGIN(ComponentDepsNode *, comp_node, id_node->components)
2131         {
2132                 if (comp_node->type == DEG_NODE_TYPE_COPY_ON_WRITE) {
2133                         /* Copy-on-write component never depends on itself. */
2134                         continue;
2135                 }
2136                 if (!comp_node->depends_on_cow()) {
2137                         /* Component explicitly requests to not add relation. */
2138                         continue;
2139                 }
2140                 int rel_flag = 0;
2141                 if (comp_node->type == DEG_NODE_TYPE_ANIMATION) {
2142                         rel_flag |= DEPSREL_FLAG_NO_FLUSH;
2143                 }
2144                 /* All entry operations of each component should wait for a proper
2145                  * copy of ID.
2146                  */
2147                 OperationDepsNode *op_entry = comp_node->get_entry_operation();
2148                 if (op_entry != NULL) {
2149                         DepsRelation *rel = graph_->add_new_relation(
2150                                 op_cow, op_entry, "CoW Dependency");
2151                         rel->flag |= rel_flag;
2152                 }
2153                 /* All dangling operations should also be executed after copy-on-write. */
2154                 GHASH_FOREACH_BEGIN(OperationDepsNode *, op_node, comp_node->operations_map)
2155                 {
2156                         if (op_node == op_entry) {
2157                                 continue;
2158                         }
2159                         if (op_node->inlinks.size() == 0) {
2160                                 DepsRelation *rel = graph_->add_new_relation(
2161                                         op_cow, op_node, "CoW Dependency");
2162                                 rel->flag |= rel_flag;
2163                         }
2164                         else {
2165                                 bool has_same_comp_dependency = false;
2166                                 foreach (DepsRelation *rel_current, op_node->inlinks) {
2167                                         if (rel_current->from->type != DEG_NODE_TYPE_OPERATION) {
2168                                                 continue;
2169                                         }
2170                                         OperationDepsNode *op_node_from =
2171                                                 (OperationDepsNode *)rel_current->from;
2172                                         if (op_node_from->owner == op_node->owner) {
2173                                                 has_same_comp_dependency = true;
2174                                                 break;
2175                                         }
2176                                 }
2177                                 if (!has_same_comp_dependency) {
2178                                         DepsRelation *rel = graph_->add_new_relation(
2179                                                 op_cow, op_node, "CoW Dependency");
2180                                         rel->flag |= rel_flag;
2181                                 }
2182                         }
2183                 }
2184                 GHASH_FOREACH_END();
2185                 /* NOTE: We currently ignore implicit relations to an external
2186                  * datablocks for copy-on-write operations. This means, for example,
2187                  * copy-on-write component of Object will not wait for copy-on-write
2188                  * component of it's Mesh. This is because pointers are all known
2189                  * already so remapping will happen all correct. And then If some object
2190                  * evaluation step needs geometry, it will have transitive dependency
2191                  * to Mesh copy-on-write already.
2192                  */
2193         }
2194         GHASH_FOREACH_END();
2195         /* TODO(sergey): This solves crash for now, but causes too many
2196          * updates potentially.
2197          */
2198         if (GS(id_orig->name) == ID_OB) {
2199                 Object *object = (Object *)id_orig;
2200                 ID *object_data_id = (ID *)object->data;
2201                 if (object_data_id != NULL) {
2202                         OperationKey data_copy_on_write_key(object_data_id,
2203                                                             DEG_NODE_TYPE_COPY_ON_WRITE,
2204                                                             DEG_OPCODE_COPY_ON_WRITE);
2205                         add_relation(data_copy_on_write_key, copy_on_write_key, "Eval Order");
2206                 }
2207                 else {
2208                         BLI_assert(object->type == OB_EMPTY);
2209                 }
2210         }
2211 }
2212
2213 /* **** ID traversal callbacks functions **** */
2214
2215 void DepsgraphRelationBuilder::modifier_walk(void *user_data,
2216                                              struct Object * /*object*/,
2217                                              struct ID **idpoin,
2218                                              int /*cb_flag*/)
2219 {
2220         BuilderWalkUserData *data = (BuilderWalkUserData *)user_data;
2221         ID *id = *idpoin;
2222         if (id == NULL) {
2223                 return;
2224         }
2225         switch (GS(id->name)) {
2226                 case ID_OB:
2227                         data->builder->build_object(NULL, (Object *)id);
2228                         break;
2229                 case ID_TE:
2230                         data->builder->build_texture((Tex *)id);
2231                         break;
2232                 default:
2233                         /* pass */
2234                         break;
2235         }
2236 }
2237
2238 void DepsgraphRelationBuilder::constraint_walk(bConstraint * /*con*/,
2239                                                ID **idpoin,
2240                                                bool /*is_reference*/,
2241                                                void *user_data)
2242 {
2243         BuilderWalkUserData *data = (BuilderWalkUserData *)user_data;
2244         if (*idpoin) {
2245                 ID *id = *idpoin;
2246                 if (GS(id->name) == ID_OB) {
2247                         data->builder->build_object(NULL, (Object *)id);
2248                 }
2249         }
2250 }
2251
2252 }  // namespace DEG