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