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