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