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