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