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