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