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