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