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