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