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 - bones/constraints/modifiers) */
941         // XXX: this probably should probably be moved out into a separate function
942         if (strstr(rna_path, "pose.bones[") != NULL) {
943                 /* interleaved drivers during bone eval */
944                 // TODO: ideally, if this is for a constraint, it goes to said constraint
945                 Object *ob = (Object *)id;
946                 char *bone_name = BLI_str_quoted_substrN(rna_path, "pose.bones[");
947                 pchan = BKE_pose_channel_find_name(ob->pose, bone_name);
948                 if (bone_name != NULL) {
949                         MEM_freeN(bone_name);
950                         bone_name = NULL;
951                 }
952                 if (pchan != NULL) {
953                         OperationKey bone_key(id,
954                                               DEG_NODE_TYPE_BONE,
955                                               pchan->name,
956                                               DEG_OPCODE_BONE_LOCAL);
957                         add_relation(driver_key, bone_key, "[Driver -> Bone]");
958                 }
959                 else {
960                         fprintf(stderr,
961                                 "Couldn't find bone name for driver path - '%s'\n",
962                                 rna_path);
963                 }
964         }
965         else if (id_type == ID_AR && strstr(rna_path, "bones[")) {
966                 /* Drivers on armature-level bone settings (i.e. bbone stuff),
967                  * which will affect the evaluation of corresponding pose bones.
968                  */
969                 IDDepsNode *arm_node = m_graph->find_id_node(id);
970                 char *bone_name = BLI_str_quoted_substrN(rna_path, "bones[");
971                 if (arm_node != NULL && bone_name != NULL) {
972                         /* Find objects which use this, and make their eval callbacks
973                          * depend on this.
974                          */
975                         foreach (DepsRelation *rel, arm_node->outlinks) {
976                                 IDDepsNode *to_node = (IDDepsNode *)rel->to;
977                                 /* We only care about objects with pose data which use this. */
978                                 if (GS(to_node->id_orig->name) == ID_OB) {
979                                         Object *ob = (Object *)to_node->id_orig;
980                                         // NOTE: ob->pose may be NULL
981                                         bPoseChannel *pchan = BKE_pose_channel_find_name(ob->pose,
982                                                                                          bone_name);
983                                         if (pchan != NULL) {
984                                                 OperationKey bone_key(&ob->id,
985                                                                       DEG_NODE_TYPE_BONE,
986                                                                       pchan->name,
987                                                                       DEG_OPCODE_BONE_LOCAL);
988                                                 add_relation(driver_key,
989                                                              bone_key,
990                                                              "[Arm Bone -> Driver -> Bone]");
991                                         }
992                                 }
993                         }
994                         /* Free temp data/ */
995                         MEM_freeN(bone_name);
996                         bone_name = NULL;
997                 }
998                 else {
999                         fprintf(stderr,
1000                                 "Couldn't find armature bone name for driver path - '%s'\n",
1001                                 rna_path);
1002                 }
1003         }
1004         else if (id_type == ID_OB && strstr(rna_path, "modifiers[")) {
1005                 OperationKey modifier_key(id,
1006                                           DEG_NODE_TYPE_GEOMETRY,
1007                                           DEG_OPCODE_GEOMETRY_UBEREVAL);
1008                 if (has_node(modifier_key)) {
1009                         add_relation(driver_key, modifier_key, "[Driver -> Modifier]");
1010                 }
1011                 else {
1012                         printf("Unexisting driver RNA path: %s\n", rna_path);
1013                 }
1014         }
1015         else if (id_type == ID_KE && strstr(rna_path, "key_blocks[")) {
1016                 /* Shape key driver - hook into the base geometry operation. */
1017                 // XXX: double check where this points
1018                 Key *shape_key = (Key *)id;
1019                 ComponentKey geometry_key(shape_key->from, DEG_NODE_TYPE_GEOMETRY);
1020                 add_relation(driver_key, geometry_key, "[Driver -> ShapeKey Geom]");
1021         }
1022         else if (strstr(rna_path, "key_blocks[")) {
1023                 ComponentKey geometry_key(id, DEG_NODE_TYPE_GEOMETRY);
1024                 add_relation(driver_key, geometry_key, "[Driver -> ShapeKey Geom]");
1025         }
1026         else {
1027                 switch (id_type) {
1028                         case ID_OB:
1029                         {
1030                                 /* Assume that driver affects a transform. */
1031                                 OperationKey local_transform_key(id,
1032                                                                  DEG_NODE_TYPE_TRANSFORM,
1033                                                                  DEG_OPCODE_TRANSFORM_LOCAL);
1034                                 add_relation(driver_key,
1035                                              local_transform_key,
1036                                              "[Driver -> Transform]");
1037                                 break;
1038                         }
1039                         case ID_KE:
1040                         {
1041                                 ComponentKey geometry_key(id, DEG_NODE_TYPE_GEOMETRY);
1042                                 add_relation(driver_key,
1043                                              geometry_key,
1044                                              "[Driver -> Shapekey Geometry]");
1045                                 break;
1046                         }
1047                         case ID_NT:
1048                         {
1049                                 OperationKey ntree_key(id,
1050                                                        DEG_NODE_TYPE_PARAMETERS,
1051                                                        DEG_OPCODE_PARAMETERS_EVAL);
1052                                 add_relation(driver_key,
1053                                              ntree_key,
1054                                              "[Driver -> NTree Shading Update]");
1055                                 break;
1056                         }
1057                 }
1058         }
1059
1060         /* ensure that affected prop's update callbacks will be triggered once done */
1061         // TODO: implement this once the functionality to add these links exists in RNA
1062         // XXX: the data itself could also set this, if it were to be truly initialised later?
1063
1064         /* loop over variables to get the target relationships */
1065         LINKLIST_FOREACH (DriverVar *, dvar, &driver->variables) {
1066                 /* only used targets */
1067                 DRIVER_TARGETS_USED_LOOPER(dvar)
1068                 {
1069                         if (dtar->id == NULL)
1070                                 continue;
1071
1072                         /* special handling for directly-named bones */
1073                         if ((dtar->flag & DTAR_FLAG_STRUCT_REF) && (dtar->pchan_name[0])) {
1074                                 Object *ob = (Object *)dtar->id;
1075                                 bPoseChannel *target_pchan = BKE_pose_channel_find_name(ob->pose, dtar->pchan_name);
1076                                 if (target_pchan != NULL) {
1077                                         /* get node associated with bone */
1078                                         // XXX: watch the space!
1079                                         /* Some cases can't use final bone transform, for example:
1080                                          * - Driving the bone with itself (addressed here)
1081                                          * - Relations inside an IK chain (TODO?)
1082                                          */
1083                                         if (dtar->id == id &&
1084                                             pchan != NULL &&
1085                                             STREQ(pchan->name, target_pchan->name))
1086                                         {
1087                                                 continue;
1088                                         }
1089                                         OperationKey target_key(dtar->id, DEG_NODE_TYPE_BONE, target_pchan->name, DEG_OPCODE_BONE_DONE);
1090                                         add_relation(target_key, driver_key, "[Bone Target -> Driver]");
1091                                 }
1092                         }
1093                         else if (dtar->flag & DTAR_FLAG_STRUCT_REF) {
1094                                 /* get node associated with the object's transforms */
1095                                 OperationKey target_key(dtar->id, DEG_NODE_TYPE_TRANSFORM, DEG_OPCODE_TRANSFORM_FINAL);
1096                                 add_relation(target_key, driver_key, "[Target -> Driver]");
1097                         }
1098                         else if (dtar->rna_path && strstr(dtar->rna_path, "pose.bones[")) {
1099                                 /* workaround for ensuring that local bone transforms don't end up
1100                                  * having to wait for pose eval to finish (to prevent cycles)
1101                                  */
1102                                 Object *ob = (Object *)dtar->id;
1103                                 char *bone_name = BLI_str_quoted_substrN(dtar->rna_path, "pose.bones[");
1104                                 bPoseChannel *target_pchan = BKE_pose_channel_find_name(ob->pose, bone_name);
1105                                 if (bone_name) {
1106                                         MEM_freeN(bone_name);
1107                                         bone_name = NULL;
1108                                 }
1109                                 if (target_pchan) {
1110                                         if (dtar->id == id &&
1111                                             pchan != NULL &&
1112                                             STREQ(pchan->name, target_pchan->name))
1113                                         {
1114                                                 continue;
1115                                         }
1116                                         OperationKey bone_key(dtar->id, DEG_NODE_TYPE_BONE, target_pchan->name, DEG_OPCODE_BONE_LOCAL);
1117                                         add_relation(bone_key, driver_key, "[RNA Bone -> Driver]");
1118                                 }
1119                         }
1120                         else {
1121                                 if (dtar->id == id) {
1122                                         /* Ignore input dependency if we're driving properties of the same ID,
1123                                          * otherwise we'll be ending up in a cyclic dependency here.
1124                                          */
1125                                         continue;
1126                                 }
1127                                 /* resolve path to get node */
1128                                 RNAPathKey target_key(dtar->id, dtar->rna_path ? dtar->rna_path : "");
1129                                 add_relation(target_key, driver_key, "[RNA Target -> Driver]");
1130                         }
1131                 }
1132                 DRIVER_TARGETS_LOOPER_END
1133         }
1134
1135         /* It's quite tricky to detect if the driver actually depends on time or not,
1136          * so for now we'll be quite conservative here about optimization and consider
1137          * all python drivers to be depending on time.
1138          */
1139         if ((driver->type == DRIVER_TYPE_PYTHON) &&
1140             python_driver_depends_on_time(driver))
1141         {
1142                 TimeSourceKey time_src_key;
1143                 add_relation(time_src_key, driver_key, "[TimeSrc -> Driver]");
1144         }
1145 }
1146
1147 void DepsgraphRelationBuilder::build_world(World *world)
1148 {
1149         ID *world_id = &world->id;
1150         if (world_id->tag & LIB_TAG_DOIT) {
1151                 return;
1152         }
1153         world_id->tag |= LIB_TAG_DOIT;
1154
1155         build_animdata(world_id);
1156
1157         /* TODO: other settings? */
1158
1159         /* textures */
1160         build_texture_stack(world->mtex);
1161
1162         /* world's nodetree */
1163         if (world->nodetree != NULL) {
1164                 build_nodetree(world->nodetree);
1165                 ComponentKey ntree_key(&world->nodetree->id, DEG_NODE_TYPE_SHADING);
1166                 ComponentKey world_key(world_id, DEG_NODE_TYPE_SHADING);
1167                 add_relation(ntree_key, world_key, "NTree->World Shading Update");
1168         }
1169 }
1170
1171 void DepsgraphRelationBuilder::build_rigidbody(Scene *scene)
1172 {
1173         RigidBodyWorld *rbw = scene->rigidbody_world;
1174
1175         OperationKey init_key(&scene->id, DEG_NODE_TYPE_TRANSFORM, DEG_OPCODE_RIGIDBODY_REBUILD);
1176         OperationKey sim_key(&scene->id, DEG_NODE_TYPE_TRANSFORM, DEG_OPCODE_RIGIDBODY_SIM);
1177
1178         /* rel between the two sim-nodes */
1179         add_relation(init_key, sim_key, "Rigidbody [Init -> SimStep]");
1180
1181         /* set up dependencies between these operations and other builtin nodes --------------- */
1182
1183         /* time dependency */
1184         TimeSourceKey time_src_key;
1185         add_relation(time_src_key, init_key, "TimeSrc -> Rigidbody Reset/Rebuild (Optional)");
1186
1187         /* objects - simulation participants */
1188         if (rbw->group) {
1189                 LINKLIST_FOREACH (GroupObject *, go, &rbw->group->gobject) {
1190                         Object *ob = go->ob;
1191                         if (ob == NULL || ob->type != OB_MESH) {
1192                                 continue;
1193                         }
1194
1195                         /* hook up evaluation order...
1196                          * 1) flushing rigidbody results follows base transforms being applied
1197                          * 2) rigidbody flushing can only be performed after simulation has been run
1198                          *
1199                          * 3) simulation needs to know base transforms to figure out what to do
1200                          *    XXX: there's probably a difference between passive and active
1201                          *         - passive don't change, so may need to know full transform...
1202                          */
1203                         OperationKey rbo_key(&ob->id, DEG_NODE_TYPE_TRANSFORM, DEG_OPCODE_RIGIDBODY_TRANSFORM_COPY);
1204
1205                         eDepsOperation_Code trans_opcode = ob->parent ? DEG_OPCODE_TRANSFORM_PARENT : DEG_OPCODE_TRANSFORM_LOCAL;
1206                         OperationKey trans_op(&ob->id, DEG_NODE_TYPE_TRANSFORM, trans_opcode);
1207
1208                         add_relation(sim_key, rbo_key, "Rigidbody Sim Eval -> RBO Sync");
1209
1210                         /* if constraints exist, those depend on the result of the rigidbody sim
1211                          * - This allows constraints to modify the result of the sim (i.e. clamping)
1212                          *   while still allowing the sim to depend on some changes to the objects.
1213                          *   Also, since constraints are hooked up to the final nodes, this link
1214                          *   means that we can also fit in there too...
1215                          * - Later, it might be good to include a constraint in the stack allowing us
1216                          *   to control whether rigidbody eval gets interleaved into the constraint stack
1217                          */
1218                         if (ob->constraints.first) {
1219                                 OperationKey constraint_key(&ob->id,
1220                                                             DEG_NODE_TYPE_TRANSFORM,
1221                                                             DEG_OPCODE_TRANSFORM_CONSTRAINTS);
1222                                 add_relation(rbo_key, constraint_key, "RBO Sync -> Ob Constraints");
1223                         }
1224                         else {
1225                                 /* Final object transform depends on rigidbody.
1226                                  *
1227                                  * NOTE: Currently we consider final here an ubereval node.
1228                                  * If it is gone we'll need to reconsider relation here.
1229                                  */
1230                                 OperationKey uber_key(&ob->id,
1231                                                       DEG_NODE_TYPE_TRANSFORM,
1232                                                       DEG_OPCODE_TRANSFORM_OBJECT_UBEREVAL);
1233                                 add_relation(rbo_key, uber_key, "RBO Sync -> Uber (Temp)");
1234                         }
1235
1236                         /* Needed to get correct base values. */
1237                         add_relation(trans_op, sim_key, "Base Ob Transform -> Rigidbody Sim Eval");
1238                 }
1239         }
1240
1241         /* constraints */
1242         if (rbw->constraints) {
1243                 LINKLIST_FOREACH (GroupObject *, go, &rbw->constraints->gobject) {
1244                         Object *ob = go->ob;
1245                         if (ob == NULL || !ob->rigidbody_constraint) {
1246                                 continue;
1247                         }
1248
1249                         RigidBodyCon *rbc = ob->rigidbody_constraint;
1250
1251                         /* final result of the constraint object's transform controls how the
1252                          * constraint affects the physics sim for these objects
1253                          */
1254                         ComponentKey trans_key(&ob->id, DEG_NODE_TYPE_TRANSFORM);
1255                         OperationKey ob1_key(&rbc->ob1->id, DEG_NODE_TYPE_TRANSFORM, DEG_OPCODE_RIGIDBODY_TRANSFORM_COPY);
1256                         OperationKey ob2_key(&rbc->ob2->id, DEG_NODE_TYPE_TRANSFORM, DEG_OPCODE_RIGIDBODY_TRANSFORM_COPY);
1257
1258                         /* - constrained-objects sync depends on the constraint-holder */
1259                         add_relation(trans_key, ob1_key, "RigidBodyConstraint -> RBC.Object_1");
1260                         add_relation(trans_key, ob2_key, "RigidBodyConstraint -> RBC.Object_2");
1261
1262                         /* - ensure that sim depends on this constraint's transform */
1263                         add_relation(trans_key, sim_key, "RigidBodyConstraint Transform -> RB Simulation");
1264                 }
1265         }
1266 }
1267
1268 void DepsgraphRelationBuilder::build_particles(Scene *scene, Object *ob)
1269 {
1270         TimeSourceKey time_src_key;
1271         OperationKey obdata_ubereval_key(&ob->id,
1272                                          DEG_NODE_TYPE_GEOMETRY,
1273                                          DEG_OPCODE_GEOMETRY_UBEREVAL);
1274         OperationKey eval_init_key(&ob->id,
1275                                    DEG_NODE_TYPE_EVAL_PARTICLES,
1276                                    DEG_OPCODE_PARTICLE_SYSTEM_EVAL_INIT);
1277         /* TODO(sergey): Are all particle systems depends on time?
1278          * Hair without dynamics i.e.
1279          */
1280         add_relation(time_src_key, eval_init_key, "TimeSrc -> PSys");
1281
1282         /* particle systems */
1283         LINKLIST_FOREACH (ParticleSystem *, psys, &ob->particlesystem) {
1284                 ParticleSettings *part = psys->part;
1285
1286                 /* Build particle settings relations.
1287                  *
1288                  * NOTE: The call itself ensures settings are only build once.
1289                  */
1290                 build_particle_settings(part);
1291
1292                 /* This particle system. */
1293                 OperationKey psys_key(&ob->id,
1294                                       DEG_NODE_TYPE_EVAL_PARTICLES,
1295                                       DEG_OPCODE_PARTICLE_SYSTEM_EVAL,
1296                                       psys->name);
1297
1298                 /* Update particle system when settings changes. */
1299                 OperationKey particle_settings_key(&part->id,
1300                                                    DEG_NODE_TYPE_PARAMETERS,
1301                                                    DEG_OPCODE_PARTICLE_SETTINGS_EVAL);
1302                 OperationKey particle_settings_recalc_clear_key(
1303                         &part->id,
1304                         DEG_NODE_TYPE_PARAMETERS,
1305                         DEG_OPCODE_PARTICLE_SETTINGS_RECALC_CLEAR);
1306                 OperationKey psys_settings_key(&ob->id,
1307                                                DEG_NODE_TYPE_EVAL_PARTICLES,
1308                                                DEG_OPCODE_PARTICLE_SETTINGS_EVAL,
1309                                                psys->name);
1310                 add_relation(particle_settings_key, psys_settings_key, "Particle Settings Change");
1311                 add_relation(psys_settings_key, psys_key, "Particle Settings Update");
1312                 add_relation(psys_key,
1313                              particle_settings_recalc_clear_key,
1314                              "Particle Settings Recalc Clear");
1315
1316                 /* XXX: if particle system is later re-enabled, we must do full rebuild? */
1317                 if (!psys_check_enabled(ob, psys, G.is_rendering))
1318                         continue;
1319
1320                 add_relation(eval_init_key, psys_key, "Init -> PSys");
1321
1322                 /* TODO(sergey): Currently particle update is just a placeholder,
1323                  * hook it to the ubereval node so particle system is getting updated
1324                  * on playback.
1325                  */
1326                 add_relation(psys_key, obdata_ubereval_key, "PSys -> UberEval");
1327
1328                 /* collisions */
1329                 if (part->type != PART_HAIR) {
1330                         add_collision_relations(psys_key,
1331                                                 scene,
1332                                                 ob,
1333                                                 part->collision_group,
1334                                                 true,
1335                                                 "Particle Collision");
1336                 }
1337                 else if ((psys->flag & PSYS_HAIR_DYNAMICS) &&
1338                          psys->clmd != NULL &&
1339                          psys->clmd->coll_parms != NULL) {
1340                         add_collision_relations(psys_key,
1341                                                 scene,
1342                                                 ob,
1343                                                 psys->clmd->coll_parms->group,
1344                                                 true,
1345                                                 "Hair Collision");
1346                 }
1347
1348                 /* effectors */
1349                 add_forcefield_relations(psys_key,
1350                                          scene,
1351                                          ob,
1352                                          psys,
1353                                          part->effector_weights,
1354                                          part->type == PART_HAIR,
1355                                          "Particle Field");
1356
1357                 /* boids */
1358                 if (part->boids) {
1359                         LINKLIST_FOREACH (BoidState *, state, &part->boids->states) {
1360                                 LINKLIST_FOREACH (BoidRule *, rule, &state->rules) {
1361                                         Object *ruleob = NULL;
1362                                         if (rule->type == eBoidRuleType_Avoid)
1363                                                 ruleob = ((BoidRuleGoalAvoid *)rule)->ob;
1364                                         else if (rule->type == eBoidRuleType_FollowLeader)
1365                                                 ruleob = ((BoidRuleFollowLeader *)rule)->ob;
1366
1367                                         if (ruleob) {
1368                                                 ComponentKey ruleob_key(&ruleob->id, DEG_NODE_TYPE_TRANSFORM);
1369                                                 add_relation(ruleob_key, psys_key, "Boid Rule");
1370                                         }
1371                                 }
1372                         }
1373                 }
1374
1375                 if (part->ren_as == PART_DRAW_OB && part->dup_ob) {
1376                         ComponentKey dup_ob_key(&part->dup_ob->id, DEG_NODE_TYPE_TRANSFORM);
1377                         add_relation(dup_ob_key, psys_key, "Particle Object Visualization");
1378                 }
1379         }
1380
1381         /* Particle depends on the object transform, so that channel is to be ready
1382          * first.
1383          *
1384          * TODO(sergey): This relation should be altered once real granular update
1385          * is implemented.
1386          */
1387         ComponentKey transform_key(&ob->id, DEG_NODE_TYPE_TRANSFORM);
1388         add_relation(transform_key, obdata_ubereval_key, "Partcile Eval");
1389
1390         /* TODO(sergey): Do we need a point cache operations here? */
1391 }
1392
1393 void DepsgraphRelationBuilder::build_particle_settings(ParticleSettings *part)
1394 {
1395         ID *part_id = &part->id;
1396         if (part_id->tag & LIB_TAG_DOIT) {
1397                 return;
1398         }
1399         part_id->tag |= LIB_TAG_DOIT;
1400
1401         /* Animation data relations. */
1402         build_animdata(&part->id);
1403
1404         OperationKey eval_key(part_id,
1405                               DEG_NODE_TYPE_PARAMETERS,
1406                               DEG_OPCODE_PARTICLE_SETTINGS_EVAL);
1407         OperationKey recalc_clear_key(part_id,
1408                                      DEG_NODE_TYPE_PARAMETERS,
1409                                      DEG_OPCODE_PARTICLE_SETTINGS_RECALC_CLEAR);
1410         add_relation(eval_key, recalc_clear_key, "Particle Settings Clear Recalc");
1411 }
1412
1413 void DepsgraphRelationBuilder::build_cloth(Scene * /*scene*/,
1414                                            Object *object,
1415                                            ModifierData * /*md*/)
1416 {
1417         OperationKey cache_key(&object->id,
1418                                DEG_NODE_TYPE_CACHE,
1419                                DEG_OPCODE_PLACEHOLDER,
1420                                "Cloth Modifier");
1421         /* Cache component affects on modifier. */
1422         OperationKey modifier_key(&object->id,
1423                                   DEG_NODE_TYPE_GEOMETRY,
1424                                   DEG_OPCODE_GEOMETRY_UBEREVAL);
1425         add_relation(cache_key, modifier_key, "Cloth Cache -> Cloth");
1426 }
1427
1428 /* Shapekeys */
1429 void DepsgraphRelationBuilder::build_shapekeys(ID *obdata, Key *key)
1430 {
1431         ComponentKey obdata_key(obdata, DEG_NODE_TYPE_GEOMETRY);
1432
1433         /* attach animdata to geometry */
1434         build_animdata(&key->id);
1435
1436         if (key->adt) {
1437                 // TODO: this should really be handled in build_animdata, since many of these cases will need it
1438                 if (key->adt->action || key->adt->nla_tracks.first) {
1439                         ComponentKey adt_key(&key->id, DEG_NODE_TYPE_ANIMATION);
1440                         add_relation(adt_key, obdata_key, "Animation");
1441                 }
1442
1443                 /* NOTE: individual shapekey drivers are handled above already */
1444         }
1445
1446         /* attach to geometry */
1447         // XXX: aren't shapekeys now done as a pseudo-modifier on object?
1448         //ComponentKey key_key(&key->id, DEG_NODE_TYPE_GEOMETRY); // FIXME: this doesn't exist
1449         //add_relation(key_key, obdata_key, "Shapekeys");
1450 }
1451
1452 /**
1453  * ObData Geometry Evaluation
1454  * ==========================
1455  *
1456  * The evaluation of geometry on objects is as follows:
1457  * - The actual evaluated of the derived geometry (e.g. DerivedMesh, DispList, etc.)
1458  *   occurs in the Geometry component of the object which references this. This includes
1459  *   modifiers, and the temporary "ubereval" for geometry.
1460  * - Therefore, each user of a piece of shared geometry data ends up evaluating its own
1461  *   version of the stuff, complete with whatever modifiers it may use.
1462  *
1463  * - The datablocks for the geometry data - "obdata" (e.g. ID_ME, ID_CU, ID_LT, etc.) are used for
1464  *     1) calculating the bounding boxes of the geometry data,
1465  *     2) aggregating inward links from other objects (e.g. for text on curve, etc.)
1466  *        and also for the links coming from the shapekey datablocks
1467  * - Animation/Drivers affecting the parameters of the geometry are made to trigger
1468  *   updates on the obdata geometry component, which then trigger downstream
1469  *   re-evaluation of the individual instances of this geometry.
1470  */
1471 // TODO: Materials and lighting should probably get their own component, instead of being lumped under geometry?
1472 void DepsgraphRelationBuilder::build_obdata_geom(Main *bmain, Scene *scene, Object *ob)
1473 {
1474         ID *obdata = (ID *)ob->data;
1475
1476         /* Init operation of object-level geometry evaluation. */
1477         OperationKey geom_init_key(&ob->id, DEG_NODE_TYPE_GEOMETRY, DEG_OPCODE_PLACEHOLDER, "Eval Init");
1478
1479         /* get nodes for result of obdata's evaluation, and geometry evaluation on object */
1480         ComponentKey obdata_geom_key(obdata, DEG_NODE_TYPE_GEOMETRY);
1481         ComponentKey geom_key(&ob->id, DEG_NODE_TYPE_GEOMETRY);
1482
1483         /* link components to each other */
1484         add_relation(obdata_geom_key, geom_key, "Object Geometry Base Data");
1485
1486         OperationKey obdata_ubereval_key(&ob->id,
1487                                          DEG_NODE_TYPE_GEOMETRY,
1488                                          DEG_OPCODE_GEOMETRY_UBEREVAL);
1489
1490         /* Special case: modifiers and DerivedMesh creation queries scene for various
1491          * things like data mask to be used. We add relation here to ensure object is
1492          * never evaluated prior to Scene's CoW is ready.
1493          */
1494         OperationKey scene_key(&scene->id,
1495                                DEG_NODE_TYPE_PARAMETERS,
1496                                DEG_OPCODE_PLACEHOLDER,
1497                                "Scene Eval");
1498         add_relation(scene_key, obdata_ubereval_key, "CoW Relation");
1499
1500         /* Modifiers */
1501         if (ob->modifiers.first != NULL) {
1502                 LINKLIST_FOREACH (ModifierData *, md, &ob->modifiers) {
1503                         const ModifierTypeInfo *mti = modifierType_getInfo((ModifierType)md->type);
1504
1505                         if (mti->updateDepsgraph) {
1506                                 DepsNodeHandle handle = create_node_handle(obdata_ubereval_key);
1507                                 mti->updateDepsgraph(
1508                                         md,
1509                                         bmain,
1510                                         scene,
1511                                         ob,
1512                                         reinterpret_cast< ::DepsNodeHandle* >(&handle));
1513                         }
1514
1515                         if (BKE_object_modifier_use_time(ob, md)) {
1516                                 TimeSourceKey time_src_key;
1517                                 add_relation(time_src_key, obdata_ubereval_key, "Time Source");
1518
1519                                 /* Hacky fix for T45633 (Animated modifiers aren't updated)
1520                                  *
1521                                  * This check works because BKE_object_modifier_use_time() tests
1522                                  * for either the modifier needing time, or that it is animated.
1523                                  */
1524                                 /* XXX: Remove this hack when these links are added as part of build_animdata() instead */
1525                                 if (modifier_dependsOnTime(md) == false && needs_animdata_node(&ob->id)) {
1526                                         ComponentKey animation_key(&ob->id, DEG_NODE_TYPE_ANIMATION);
1527                                         add_relation(animation_key, obdata_ubereval_key, "Modifier Animation");
1528                                 }
1529                         }
1530
1531                         if (md->type == eModifierType_Cloth) {
1532                                 build_cloth(scene, ob, md);
1533                         }
1534                 }
1535         }
1536
1537         /* materials */
1538         if (ob->totcol) {
1539                 for (int a = 1; a <= ob->totcol; a++) {
1540                         Material *ma = give_current_material(ob, a);
1541                         if (ma != NULL) {
1542                                 build_material(ma);
1543
1544                                 if (ob->type == OB_MESH) {
1545                                         OperationKey material_key(&ma->id,
1546                                                                   DEG_NODE_TYPE_SHADING,
1547                                                                   DEG_OPCODE_MATERIAL_UPDATE);
1548                                         OperationKey shading_key(&ob->id, DEG_NODE_TYPE_SHADING, DEG_OPCODE_SHADING);
1549                                         add_relation(material_key, shading_key, "Material Update");
1550                                 }
1551                         }
1552                 }
1553         }
1554
1555         /* geometry collision */
1556         if (ELEM(ob->type, OB_MESH, OB_CURVE, OB_LATTICE)) {
1557                 // add geometry collider relations
1558         }
1559
1560         /* Make sure uber update is the last in the dependencies.
1561          *
1562          * TODO(sergey): Get rid of this node.
1563          */
1564         if (ob->type != OB_ARMATURE) {
1565                 /* Armatures does no longer require uber node. */
1566                 OperationKey obdata_ubereval_key(&ob->id, DEG_NODE_TYPE_GEOMETRY, DEG_OPCODE_GEOMETRY_UBEREVAL);
1567                 add_relation(geom_init_key, obdata_ubereval_key, "Object Geometry UberEval");
1568         }
1569
1570         if (obdata->tag & LIB_TAG_DOIT) {
1571                 return;
1572         }
1573         obdata->tag |= LIB_TAG_DOIT;
1574
1575         /* Link object data evaluation node to exit operation. */
1576         OperationKey obdata_geom_eval_key(obdata, DEG_NODE_TYPE_GEOMETRY, DEG_OPCODE_PLACEHOLDER, "Geometry Eval");
1577         OperationKey obdata_geom_done_key(obdata, DEG_NODE_TYPE_GEOMETRY, DEG_OPCODE_PLACEHOLDER, "Eval Done");
1578         add_relation(obdata_geom_eval_key, obdata_geom_done_key, "ObData Geom Eval Done");
1579
1580         /* type-specific node/links */
1581         switch (ob->type) {
1582                 case OB_MESH:
1583                         /* NOTE: This is compatibility code to support particle systems
1584                          *
1585                          * for viewport being properly rendered in final render mode.
1586                          * This relation is similar to what dag_object_time_update_flags()
1587                          * was doing for mesh objects with particle system/
1588                          *
1589                          * Ideally we need to get rid of this relation.
1590                          */
1591                         if (ob->particlesystem.first != NULL) {
1592                                 TimeSourceKey time_key;
1593                                 OperationKey obdata_ubereval_key(&ob->id, DEG_NODE_TYPE_GEOMETRY, DEG_OPCODE_GEOMETRY_UBEREVAL);
1594                                 add_relation(time_key, obdata_ubereval_key, "Legacy particle time");
1595                         }
1596                         break;
1597
1598                 case OB_MBALL:
1599                 {
1600                         Object *mom = BKE_mball_basis_find(scene, ob);
1601
1602                         /* motherball - mom depends on children! */
1603                         if (mom != ob) {
1604                                 /* non-motherball -> cannot be directly evaluated! */
1605                                 ComponentKey mom_key(&mom->id, DEG_NODE_TYPE_GEOMETRY);
1606                                 ComponentKey transform_key(&ob->id, DEG_NODE_TYPE_TRANSFORM);
1607                                 add_relation(geom_key, mom_key, "Metaball Motherball");
1608                                 add_relation(transform_key, mom_key, "Metaball Motherball");
1609                         }
1610                         break;
1611                 }
1612
1613                 case OB_CURVE:
1614                 case OB_FONT:
1615                 {
1616                         Curve *cu = (Curve *)obdata;
1617
1618                         /* curve's dependencies */
1619                         // XXX: these needs geom data, but where is geom stored?
1620                         if (cu->bevobj) {
1621                                 ComponentKey bevob_key(&cu->bevobj->id, DEG_NODE_TYPE_GEOMETRY);
1622                                 build_object(bmain, scene, cu->bevobj);
1623                                 add_relation(bevob_key, geom_key, "Curve Bevel");
1624                         }
1625                         if (cu->taperobj) {
1626                                 ComponentKey taperob_key(&cu->taperobj->id, DEG_NODE_TYPE_GEOMETRY);
1627                                 build_object(bmain, scene, cu->taperobj);
1628                                 add_relation(taperob_key, geom_key, "Curve Taper");
1629                         }
1630                         if (ob->type == OB_FONT) {
1631                                 if (cu->textoncurve) {
1632                                         ComponentKey textoncurve_key(&cu->textoncurve->id, DEG_NODE_TYPE_GEOMETRY);
1633                                         build_object(bmain, scene, cu->textoncurve);
1634                                         add_relation(textoncurve_key, geom_key, "Text on Curve");
1635                                 }
1636                         }
1637                         break;
1638                 }
1639
1640                 case OB_SURF: /* Nurbs Surface */
1641                 {
1642                         break;
1643                 }
1644
1645                 case OB_LATTICE: /* Lattice */
1646                 {
1647                         break;
1648                 }
1649         }
1650
1651         /* ShapeKeys */
1652         Key *key = BKE_key_from_object(ob);
1653         if (key) {
1654                 build_shapekeys(obdata, key);
1655         }
1656
1657         if (needs_animdata_node(obdata)) {
1658                 ComponentKey animation_key(obdata, DEG_NODE_TYPE_ANIMATION);
1659                 ComponentKey parameters_key(obdata, DEG_NODE_TYPE_PARAMETERS);
1660                 add_relation(animation_key, parameters_key, "Geom Parameters");
1661                 /* Evaluation usually depends on animation.
1662                  * TODO(sergey): Need to re-hook it after granular update is implemented..
1663                  */
1664                 add_relation(animation_key, obdata_geom_eval_key, "Animation");
1665         }
1666 }
1667
1668 /* Cameras */
1669 // TODO: Link scene-camera links in somehow...
1670 void DepsgraphRelationBuilder::build_camera(Object *ob)
1671 {
1672         Camera *cam = (Camera *)ob->data;
1673         ID *camera_id = &cam->id;
1674         if (camera_id->tag & LIB_TAG_DOIT) {
1675                 return;
1676         }
1677         camera_id->tag |= LIB_TAG_DOIT;
1678
1679         ComponentKey parameters_key(camera_id, DEG_NODE_TYPE_PARAMETERS);
1680
1681         if (needs_animdata_node(camera_id)) {
1682                 ComponentKey animation_key(camera_id, DEG_NODE_TYPE_ANIMATION);
1683                 add_relation(animation_key, parameters_key, "Camera Parameters");
1684         }
1685
1686         /* DOF */
1687         if (cam->dof_ob) {
1688                 ComponentKey ob_param_key(&ob->id, DEG_NODE_TYPE_PARAMETERS);
1689                 ComponentKey dof_ob_key(&cam->dof_ob->id, DEG_NODE_TYPE_TRANSFORM);
1690                 add_relation(dof_ob_key, ob_param_key, "Camera DOF");
1691         }
1692 }
1693
1694 /* Lamps */
1695 void DepsgraphRelationBuilder::build_lamp(Object *ob)
1696 {
1697         Lamp *la = (Lamp *)ob->data;
1698         ID *lamp_id = &la->id;
1699         if (lamp_id->tag & LIB_TAG_DOIT) {
1700                 return;
1701         }
1702         lamp_id->tag |= LIB_TAG_DOIT;
1703
1704         ComponentKey parameters_key(lamp_id, DEG_NODE_TYPE_PARAMETERS);
1705
1706         if (needs_animdata_node(lamp_id)) {
1707                 ComponentKey animation_key(lamp_id, DEG_NODE_TYPE_ANIMATION);
1708                 add_relation(animation_key, parameters_key, "Lamp Parameters");
1709         }
1710
1711         /* lamp's nodetree */
1712         if (la->nodetree) {
1713                 build_nodetree(la->nodetree);
1714                 ComponentKey nodetree_key(&la->nodetree->id, DEG_NODE_TYPE_PARAMETERS);
1715                 add_relation(nodetree_key, parameters_key, "NTree->Lamp Parameters");
1716         }
1717
1718         /* textures */
1719         build_texture_stack(la->mtex);
1720
1721 #ifdef WITH_COPY_ON_WRITE
1722         /* Make sure copy on write of lamp data is always properly updated for
1723          * visible lamps.
1724          */
1725         OperationKey ob_copy_on_write_key(&ob->id,
1726                                           DEG_NODE_TYPE_COPY_ON_WRITE,
1727                                           DEG_OPCODE_COPY_ON_WRITE);
1728         OperationKey lamp_copy_on_write_key(lamp_id,
1729                                             DEG_NODE_TYPE_COPY_ON_WRITE,
1730                                             DEG_OPCODE_COPY_ON_WRITE);
1731         add_relation(lamp_copy_on_write_key,
1732                      ob_copy_on_write_key,
1733                      "Evaluation Order");
1734 #endif
1735 }
1736
1737 void DepsgraphRelationBuilder::build_nodetree(bNodeTree *ntree)
1738 {
1739         if (!ntree)
1740                 return;
1741
1742         ID *ntree_id = &ntree->id;
1743
1744         build_animdata(ntree_id);
1745
1746         OperationKey parameters_key(ntree_id,
1747                                     DEG_NODE_TYPE_PARAMETERS,
1748                                     DEG_OPCODE_PARAMETERS_EVAL);
1749
1750         /* nodetree's nodes... */
1751         LINKLIST_FOREACH (bNode *, bnode, &ntree->nodes) {
1752                 if (bnode->id) {
1753                         if (GS(bnode->id->name) == ID_MA) {
1754                                 build_material((Material *)bnode->id);
1755                         }
1756                         else if (bnode->type == ID_TE) {
1757                                 build_texture((Tex *)bnode->id);
1758                         }
1759                         else if (bnode->type == NODE_GROUP) {
1760                                 bNodeTree *group_ntree = (bNodeTree *)bnode->id;
1761                                 if ((group_ntree->id.tag & LIB_TAG_DOIT) == 0) {
1762                                         build_nodetree(group_ntree);
1763                                         group_ntree->id.tag |= LIB_TAG_DOIT;
1764                                 }
1765                                 OperationKey group_parameters_key(&group_ntree->id,
1766                                                                   DEG_NODE_TYPE_PARAMETERS,
1767                                                                   DEG_OPCODE_PARAMETERS_EVAL);
1768                                 add_relation(group_parameters_key, parameters_key, "Group Node");
1769                         }
1770                 }
1771         }
1772
1773         if (needs_animdata_node(ntree_id)) {
1774                 ComponentKey animation_key(ntree_id, DEG_NODE_TYPE_ANIMATION);
1775                 add_relation(animation_key, parameters_key, "NTree Parameters");
1776         }
1777
1778         OperationKey shading_update_key(ntree_id,
1779                                         DEG_NODE_TYPE_SHADING,
1780                                         DEG_OPCODE_MATERIAL_UPDATE);
1781         add_relation(parameters_key, shading_update_key, "NTree Parameters");
1782 }
1783
1784 /* Recursively build graph for material */
1785 void DepsgraphRelationBuilder::build_material(Material *ma)
1786 {
1787         ID *ma_id = &ma->id;
1788         if (ma_id->tag & LIB_TAG_DOIT) {
1789                 return;
1790         }
1791         ma_id->tag |= LIB_TAG_DOIT;
1792
1793         /* animation */
1794         build_animdata(ma_id);
1795
1796         /* textures */
1797         build_texture_stack(ma->mtex);
1798
1799         /* material's nodetree */
1800         if (ma->nodetree != NULL) {
1801                 build_nodetree(ma->nodetree);
1802                 OperationKey ntree_key(&ma->nodetree->id,
1803                                        DEG_NODE_TYPE_SHADING,
1804                                        DEG_OPCODE_MATERIAL_UPDATE);
1805                 OperationKey material_key(&ma->id,
1806                                           DEG_NODE_TYPE_SHADING,
1807                                           DEG_OPCODE_MATERIAL_UPDATE);
1808                 add_relation(ntree_key, material_key, "Material's NTree");
1809         }
1810 }
1811
1812 /* Recursively build graph for texture */
1813 void DepsgraphRelationBuilder::build_texture(Tex *tex)
1814 {
1815         ID *tex_id = &tex->id;
1816         if (tex_id->tag & LIB_TAG_DOIT) {
1817                 return;
1818         }
1819         tex_id->tag |= LIB_TAG_DOIT;
1820
1821         /* texture itself */
1822         build_animdata(tex_id);
1823
1824         /* texture's nodetree */
1825         build_nodetree(tex->nodetree);
1826 }
1827
1828 /* Texture-stack attached to some shading datablock */
1829 void DepsgraphRelationBuilder::build_texture_stack(MTex **texture_stack)
1830 {
1831         int i;
1832
1833         /* for now assume that all texture-stacks have same number of max items */
1834         for (i = 0; i < MAX_MTEX; i++) {
1835                 MTex *mtex = texture_stack[i];
1836                 if (mtex && mtex->tex)
1837                         build_texture(mtex->tex);
1838         }
1839 }
1840
1841 void DepsgraphRelationBuilder::build_compositor(Scene *scene)
1842 {
1843         /* For now, just a plain wrapper? */
1844         build_nodetree(scene->nodetree);
1845 }
1846
1847 void DepsgraphRelationBuilder::build_gpencil(bGPdata *gpd)
1848 {
1849         /* animation */
1850         build_animdata(&gpd->id);
1851
1852         // TODO: parent object (when that feature is implemented)
1853 }
1854
1855 bool DepsgraphRelationBuilder::needs_animdata_node(ID *id)
1856 {
1857         AnimData *adt = BKE_animdata_from_id(id);
1858         if (adt != NULL) {
1859                 return (adt->action != NULL) || (adt->nla_tracks.first != NULL);
1860         }
1861         return false;
1862 }
1863
1864 void DepsgraphRelationBuilder::build_cachefile(CacheFile *cache_file) {
1865         /* Animation. */
1866         build_animdata(&cache_file->id);
1867 }
1868
1869 void DepsgraphRelationBuilder::build_mask(Mask *mask)
1870 {
1871         /* Animation. */
1872         build_animdata(&mask->id);
1873 }
1874
1875 void DepsgraphRelationBuilder::build_movieclip(MovieClip *clip)
1876 {
1877         /* Animation. */
1878         build_animdata(&clip->id);
1879 }
1880
1881 void DepsgraphRelationBuilder::build_lightprobe(Object *object)
1882 {
1883         LightProbe *probe = (LightProbe *)object->data;
1884         ID *probe_id = &probe->id;
1885         if (probe_id->tag & LIB_TAG_DOIT) {
1886                 return;
1887         }
1888         probe_id->tag |= LIB_TAG_DOIT;
1889         build_animdata(&probe->id);
1890
1891         OperationKey probe_key(probe_id,
1892                                DEG_NODE_TYPE_PARAMETERS,
1893                                DEG_OPCODE_PLACEHOLDER,
1894                                "LightProbe Eval");
1895         OperationKey object_key(&object->id,
1896                                 DEG_NODE_TYPE_PARAMETERS,
1897                                 DEG_OPCODE_PLACEHOLDER,
1898                                 "LightProbe Eval");
1899         add_relation(probe_key, object_key, "LightProbe Update");
1900 }
1901
1902 void DepsgraphRelationBuilder::build_copy_on_write_relations()
1903 {
1904         GHASH_FOREACH_BEGIN(IDDepsNode *, id_node, m_graph->id_hash)
1905         {
1906                 build_copy_on_write_relations(id_node);
1907         }
1908         GHASH_FOREACH_END();
1909 }
1910
1911 void DepsgraphRelationBuilder::build_copy_on_write_relations(IDDepsNode *id_node)
1912 {
1913         ID *id_orig = id_node->id_orig;
1914
1915         TimeSourceKey time_source_key;
1916         OperationKey copy_on_write_key(id_orig,
1917                                        DEG_NODE_TYPE_COPY_ON_WRITE,
1918                                        DEG_OPCODE_COPY_ON_WRITE);
1919         /* XXX: This is a quick hack to make Alt-A to work. */
1920         // add_relation(time_source_key, copy_on_write_key, "Fluxgate capacitor hack");
1921         /* Resat of code is using rather low level trickery, so need to get some
1922          * explicit pointers.
1923          */
1924         DepsNode *node_cow = find_node(copy_on_write_key);
1925         OperationDepsNode *op_cow = node_cow->get_exit_operation();
1926         /* Plug any other components to this one. */
1927         GHASH_FOREACH_BEGIN(ComponentDepsNode *, comp_node, id_node->components)
1928         {
1929                 if (comp_node->type == DEG_NODE_TYPE_COPY_ON_WRITE) {
1930                         /* Copy-on-write component never depends on itself. */
1931                         continue;
1932                 }
1933                 /* All entry operations of each component should wait for a proper
1934                  * copy of ID.
1935                  */
1936                 OperationDepsNode *op_entry = comp_node->get_entry_operation();
1937                 if (op_entry != NULL) {
1938                         m_graph->add_new_relation(op_cow, op_entry, "CoW Dependency");
1939                 }
1940                 /* All dangling operations should also be executed after copy-on-write. */
1941                 GHASH_FOREACH_BEGIN(OperationDepsNode *, op_node, comp_node->operations_map)
1942                 {
1943                         if (op_node->inlinks.size() == 0) {
1944                                 m_graph->add_new_relation(op_cow, op_node, "CoW Dependency");
1945                         }
1946                 }
1947                 GHASH_FOREACH_END();
1948                 /* NOTE: We currently ignore implicit relations to an external
1949                  * datablocks for copy-on-write operations. This means, for example,
1950                  * copy-on-write component of Object will not wait for copy-on-write
1951                  * component of it's Mesh. This is because pointers are all known
1952                  * already so remapping will happen all correct. And then If some object
1953                  * evaluation step needs geometry, it will have transitive dependency
1954                  * to Mesh copy-on-write already.
1955                  */
1956         }
1957         GHASH_FOREACH_END();
1958 }
1959
1960 }  // namespace DEG