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