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