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