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