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