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