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