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