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