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