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