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