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