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