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