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