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