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