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