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