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