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 <cstring>  /* required for STREQ later on. */
38
39 #include "MEM_guardedalloc.h"
40
41 extern "C" {
42 #include "BLI_blenlib.h"
43 #include "BLI_utildefines.h"
44
45 #include "DNA_action_types.h"
46 #include "DNA_anim_types.h"
47 #include "DNA_armature_types.h"
48 #include "DNA_camera_types.h"
49 #include "DNA_cachefile_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_mask_types.h"
59 #include "DNA_mesh_types.h"
60 #include "DNA_meta_types.h"
61 #include "DNA_movieclip_types.h"
62 #include "DNA_node_types.h"
63 #include "DNA_object_types.h"
64 #include "DNA_rigidbody_types.h"
65 #include "DNA_scene_types.h"
66 #include "DNA_texture_types.h"
67 #include "DNA_world_types.h"
68 #include "DNA_object_force.h"
69
70 #include "BKE_action.h"
71 #include "BKE_armature.h"
72 #include "BKE_animsys.h"
73 #include "BKE_constraint.h"
74 #include "BKE_curve.h"
75 #include "BKE_effect.h"
76 #include "BKE_collision.h"
77 #include "BKE_fcurve.h"
78 #include "BKE_group.h"
79 #include "BKE_key.h"
80 #include "BKE_library.h"
81 #include "BKE_main.h"
82 #include "BKE_material.h"
83 #include "BKE_mball.h"
84 #include "BKE_modifier.h"
85 #include "BKE_node.h"
86 #include "BKE_object.h"
87 #include "BKE_rigidbody.h"
88 #include "BKE_sound.h"
89 #include "BKE_texture.h"
90 #include "BKE_tracking.h"
91 #include "BKE_world.h"
92
93 #include "DEG_depsgraph.h"
94 #include "DEG_depsgraph_build.h"
95
96 #include "RNA_access.h"
97 #include "RNA_types.h"
98 } /* extern "C" */
99
100 #include "intern/builder/deg_builder.h"
101 #include "intern/builder/deg_builder_pchanmap.h"
102
103 #include "intern/nodes/deg_node.h"
104 #include "intern/nodes/deg_node_component.h"
105 #include "intern/nodes/deg_node_operation.h"
106
107 #include "intern/depsgraph_intern.h"
108 #include "intern/depsgraph_types.h"
109
110 #include "util/deg_util_foreach.h"
111
112 namespace DEG {
113
114 /* ***************** */
115 /* Relations Builder */
116
117 /* TODO(sergey): This is somewhat weak, but we don't want neither false-positive
118  * time dependencies nor special exceptions in the depsgraph evaluation.
119  */
120 static bool python_driver_depends_on_time(ChannelDriver *driver)
121 {
122         if (driver->expression[0] == '\0') {
123                 /* Empty expression depends on nothing. */
124                 return false;
125         }
126         if (strchr(driver->expression, '(') != NULL) {
127                 /* Function calls are considered dependent on a time. */
128                 return true;
129         }
130         if (strstr(driver->expression, "time") != NULL) {
131                 /* Variable `time` depends on time. */
132                 /* TODO(sergey): This is a bit weak, but not sure about better way of
133                  * handling this.
134                  */
135                 return true;
136         }
137         /* Possible indirect time relation s should be handled via variable
138          * targets.
139          */
140         return false;
141 }
142
143 /* **** General purpose functions ****  */
144
145 RNAPathKey::RNAPathKey(ID *id, const char *path) :
146     id(id)
147 {
148         /* create ID pointer for root of path lookup */
149         PointerRNA id_ptr;
150         RNA_id_pointer_create(id, &id_ptr);
151         /* try to resolve path... */
152         int index;
153         if (!RNA_path_resolve_full(&id_ptr, path, &this->ptr, &this->prop, &index)) {
154                 this->ptr = PointerRNA_NULL;
155                 this->prop = NULL;
156         }
157 }
158
159 DepsgraphRelationBuilder::DepsgraphRelationBuilder(Depsgraph *graph) :
160     m_graph(graph)
161 {
162 }
163
164 RootDepsNode *DepsgraphRelationBuilder::find_node(const RootKey &key) const
165 {
166         (void)key;
167         BLI_assert(!"Doesn't seem to be correct");
168         return m_graph->root_node;
169 }
170
171 TimeSourceDepsNode *DepsgraphRelationBuilder::find_node(
172         const TimeSourceKey &key) const
173 {
174         if (key.id) {
175                 /* XXX TODO */
176                 return NULL;
177         }
178         else {
179                 return m_graph->root_node->time_source;
180         }
181 }
182
183 ComponentDepsNode *DepsgraphRelationBuilder::find_node(
184         const ComponentKey &key) const
185 {
186         IDDepsNode *id_node = m_graph->find_id_node(key.id);
187         if (!id_node) {
188                 fprintf(stderr, "find_node component: Could not find ID %s\n",
189                         (key.id != NULL) ? key.id->name : "<null>");
190                 return NULL;
191         }
192
193         ComponentDepsNode *node = id_node->find_component(key.type, key.name);
194         return node;
195 }
196
197 OperationDepsNode *DepsgraphRelationBuilder::find_node(
198         const OperationKey &key) const
199 {
200         IDDepsNode *id_node = m_graph->find_id_node(key.id);
201         if (!id_node) {
202                 fprintf(stderr, "find_node operation: Could not find ID\n");
203                 return NULL;
204         }
205
206         ComponentDepsNode *comp_node = id_node->find_component(key.component_type,
207                                                                key.component_name);
208         if (!comp_node) {
209                 fprintf(stderr, "find_node operation: Could not find component\n");
210                 return NULL;
211         }
212
213         OperationDepsNode *op_node = comp_node->find_operation(key.opcode,
214                                                                key.name,
215                                                                key.name_tag);
216         if (!op_node) {
217                 fprintf(stderr, "find_node_operation: Failed for (%s, '%s')\n",
218                         DEG_OPNAMES[key.opcode], key.name);
219         }
220         return op_node;
221 }
222
223 DepsNode *DepsgraphRelationBuilder::find_node(const RNAPathKey &key) const
224 {
225         return m_graph->find_node_from_pointer(&key.ptr, key.prop);
226 }
227
228 OperationDepsNode *DepsgraphRelationBuilder::has_node(
229         const OperationKey &key) const
230 {
231         IDDepsNode *id_node = m_graph->find_id_node(key.id);
232         if (!id_node) {
233                 return NULL;
234         }
235         ComponentDepsNode *comp_node = id_node->find_component(key.component_type,
236                                                                key.component_name);
237         if (!comp_node) {
238                 return NULL;
239         }
240         return comp_node->has_operation(key.opcode, key.name, key.name_tag);
241 }
242
243 void DepsgraphRelationBuilder::add_time_relation(TimeSourceDepsNode *timesrc,
244                                                  DepsNode *node_to,
245                                                  const char *description)
246 {
247         if (timesrc && node_to) {
248                 m_graph->add_new_relation(timesrc, node_to, DEPSREL_TYPE_TIME, description);
249         }
250         else {
251                 DEG_DEBUG_PRINTF("add_time_relation(%p = %s, %p = %s, %s) Failed\n",
252                                  timesrc,   (timesrc) ? timesrc->identifier().c_str() : "<None>",
253                                  node_to,   (node_to) ? node_to->identifier().c_str() : "<None>",
254                                  description);
255         }
256 }
257
258 void DepsgraphRelationBuilder::add_operation_relation(
259         OperationDepsNode *node_from,
260         OperationDepsNode *node_to,
261         eDepsRelation_Type type,
262         const char *description)
263 {
264         if (node_from && node_to) {
265                 m_graph->add_new_relation(node_from, node_to, type, description);
266         }
267         else {
268                 DEG_DEBUG_PRINTF("add_operation_relation(%p = %s, %p = %s, %d, %s) Failed\n",
269                                  node_from, (node_from) ? node_from->identifier().c_str() : "<None>",
270                                  node_to,   (node_to)   ? node_to->identifier().c_str() : "<None>",
271                                  type, description);
272         }
273 }
274
275 void DepsgraphRelationBuilder::add_collision_relations(const OperationKey &key, Scene *scene, Object *ob, Group *group, int layer, bool dupli, const char *name)
276 {
277         unsigned int numcollobj;
278         Object **collobjs = get_collisionobjects_ext(scene, ob, group, layer, &numcollobj, eModifierType_Collision, dupli);
279
280         for (unsigned int i = 0; i < numcollobj; i++)
281         {
282                 Object *ob1 = collobjs[i];
283
284                 ComponentKey trf_key(&ob1->id, DEPSNODE_TYPE_TRANSFORM);
285                 add_relation(trf_key, key, DEPSREL_TYPE_STANDARD, name);
286
287                 ComponentKey coll_key(&ob1->id, DEPSNODE_TYPE_GEOMETRY);
288                 add_relation(coll_key, key, DEPSREL_TYPE_STANDARD, name);
289         }
290
291         if (collobjs)
292                 MEM_freeN(collobjs);
293 }
294
295 void DepsgraphRelationBuilder::add_forcefield_relations(const OperationKey &key, Scene *scene, Object *ob, EffectorWeights *eff, bool add_absorption, const char *name)
296 {
297         ListBase *effectors = pdInitEffectors(scene, ob, eff, false);
298
299         if (effectors) {
300                 for (EffectorCache *eff = (EffectorCache *)effectors->first; eff; eff = eff->next) {
301                         if (eff->ob != ob) {
302                                 ComponentKey eff_key(&eff->ob->id, DEPSNODE_TYPE_TRANSFORM);
303                                 add_relation(eff_key, key, DEPSREL_TYPE_STANDARD, name);
304                         }
305
306                         if (eff->pd->forcefield == PFIELD_SMOKEFLOW && eff->pd->f_source) {
307                                 ComponentKey trf_key(&eff->pd->f_source->id, DEPSNODE_TYPE_TRANSFORM);
308                                 add_relation(trf_key, key, DEPSREL_TYPE_STANDARD, "Smoke Force Domain");
309
310                                 ComponentKey eff_key(&eff->pd->f_source->id, DEPSNODE_TYPE_GEOMETRY);
311                                 add_relation(eff_key, key, DEPSREL_TYPE_STANDARD, "Smoke Force Domain");
312                         }
313
314                         if (add_absorption && (eff->pd->flag & PFIELD_VISIBILITY)) {
315                                 add_collision_relations(key, scene, ob, NULL, eff->ob->lay, true, "Force Absorption");
316                         }
317                 }
318         }
319
320         pdEndEffectors(&effectors);
321 }
322
323 /* **** Functions to build relations between entities  **** */
324
325 void DepsgraphRelationBuilder::build_group(Main *bmain,
326                                            Scene *scene,
327                                            Object *object,
328                                            Group *group)
329 {
330         ID *group_id = &group->id;
331         bool group_done = (group_id->tag & LIB_TAG_DOIT) != 0;
332         OperationKey object_local_transform_key(&object->id,
333                                                 DEPSNODE_TYPE_TRANSFORM,
334                                                 DEG_OPCODE_TRANSFORM_LOCAL);
335         LINKLIST_FOREACH (GroupObject *, go, &group->gobject) {
336                 if (!group_done) {
337                         build_object(bmain, scene, go->ob);
338                 }
339                 ComponentKey dupli_transform_key(&go->ob->id, DEPSNODE_TYPE_TRANSFORM);
340                 add_relation(dupli_transform_key,
341                              object_local_transform_key,
342                              DEPSREL_TYPE_TRANSFORM,
343                              "Dupligroup");
344         }
345         group_id->tag |= LIB_TAG_DOIT;
346 }
347
348 void DepsgraphRelationBuilder::build_object(Main *bmain, Scene *scene, Object *ob)
349 {
350         if (ob->id.tag & LIB_TAG_DOIT) {
351                 return;
352         }
353         ob->id.tag |= LIB_TAG_DOIT;
354
355         /* Object Transforms */
356         eDepsOperation_Code base_op = (ob->parent) ? DEG_OPCODE_TRANSFORM_PARENT : DEG_OPCODE_TRANSFORM_LOCAL;
357         OperationKey base_op_key(&ob->id, DEPSNODE_TYPE_TRANSFORM, base_op);
358
359         OperationKey local_transform_key(&ob->id, DEPSNODE_TYPE_TRANSFORM, DEG_OPCODE_TRANSFORM_LOCAL);
360         OperationKey parent_transform_key(&ob->id, DEPSNODE_TYPE_TRANSFORM, DEG_OPCODE_TRANSFORM_PARENT);
361         OperationKey final_transform_key(&ob->id, DEPSNODE_TYPE_TRANSFORM, DEG_OPCODE_TRANSFORM_FINAL);
362
363         OperationKey ob_ubereval_key(&ob->id, DEPSNODE_TYPE_TRANSFORM, DEG_OPCODE_OBJECT_UBEREVAL);
364
365         /* parenting */
366         if (ob->parent) {
367                 /* parent relationship */
368                 build_object_parent(ob);
369
370                 /* local -> parent */
371                 add_relation(local_transform_key, parent_transform_key, DEPSREL_TYPE_COMPONENT_ORDER, "[ObLocal -> ObParent]");
372         }
373
374         /* object constraints */
375         if (ob->constraints.first) {
376                 OperationKey constraint_key(&ob->id, DEPSNODE_TYPE_TRANSFORM, DEG_OPCODE_TRANSFORM_CONSTRAINTS);
377
378                 /* constraint relations */
379                 // TODO: provide base op
380                 // XXX: this is broken
381                 build_constraints(scene, &ob->id, DEPSNODE_TYPE_TRANSFORM, "", &ob->constraints, NULL);
382
383                 /* operation order */
384                 add_relation(base_op_key, constraint_key, DEPSREL_TYPE_COMPONENT_ORDER, "[ObBase-> Constraint Stack]");
385                 add_relation(constraint_key, final_transform_key, DEPSREL_TYPE_COMPONENT_ORDER, "[ObConstraints -> Done]");
386
387                 // XXX
388                 add_relation(constraint_key, ob_ubereval_key, DEPSREL_TYPE_COMPONENT_ORDER, "Temp Ubereval");
389                 add_relation(ob_ubereval_key, final_transform_key, DEPSREL_TYPE_COMPONENT_ORDER, "Temp Ubereval");
390         }
391         else {
392                 /* operation order */
393                 add_relation(base_op_key, final_transform_key, DEPSREL_TYPE_COMPONENT_ORDER, "Object Transform");
394
395                 // XXX
396                 add_relation(base_op_key, ob_ubereval_key, DEPSREL_TYPE_COMPONENT_ORDER, "Temp Ubereval");
397                 add_relation(ob_ubereval_key, final_transform_key, DEPSREL_TYPE_COMPONENT_ORDER, "Temp Ubereval");
398         }
399
400
401         /* AnimData */
402         build_animdata(&ob->id);
403
404         // XXX: This should be hooked up by the build_animdata code
405         if (needs_animdata_node(&ob->id)) {
406                 ComponentKey adt_key(&ob->id, DEPSNODE_TYPE_ANIMATION);
407                 add_relation(adt_key, local_transform_key, DEPSREL_TYPE_OPERATION, "Object Animation");
408         }
409
410
411         /* object data */
412         if (ob->data) {
413                 ID *obdata_id = (ID *)ob->data;
414
415                 /* ob data animation */
416                 build_animdata(obdata_id);
417
418                 /* type-specific data... */
419                 switch (ob->type) {
420                         case OB_MESH:     /* Geometry */
421                         case OB_CURVE:
422                         case OB_FONT:
423                         case OB_SURF:
424                         case OB_MBALL:
425                         case OB_LATTICE:
426                         {
427                                 build_obdata_geom(bmain, scene, ob);
428                                 break;
429                         }
430
431                         case OB_ARMATURE: /* Pose */
432                                 if (ID_IS_LINKED_DATABLOCK(ob) && ob->proxy_from != NULL) {
433                                         build_proxy_rig(ob);
434                                 }
435                                 else {
436                                         build_rig(scene, ob);
437                                 }
438                                 break;
439
440                         case OB_LAMP:   /* Lamp */
441                                 build_lamp(ob);
442                                 break;
443
444                         case OB_CAMERA: /* Camera */
445                                 build_camera(ob);
446                                 break;
447                 }
448
449                 Key *key = BKE_key_from_object(ob);
450                 if (key != NULL) {
451                         ComponentKey geometry_key((ID *)ob->data, DEPSNODE_TYPE_GEOMETRY);
452                         ComponentKey key_key(&key->id, DEPSNODE_TYPE_GEOMETRY);
453                         add_relation(key_key, geometry_key, DEPSREL_TYPE_GEOMETRY_EVAL, "Shapekeys");
454                 }
455         }
456
457         /* grease pencil */
458         if (ob->gpd) {
459                 build_gpencil(&ob->id, ob->gpd);
460         }
461 }
462
463 void DepsgraphRelationBuilder::build_object_parent(Object *ob)
464 {
465         /* XXX: for now, need to use the component key (not just direct to the parent op), or else the matrix doesn't get reset */
466         // XXX: @sergey - it would be good if we got that backwards flushing working when tagging for updates
467         //OperationKey ob_key(&ob->id, DEPSNODE_TYPE_TRANSFORM, DEG_OPCODE_TRANSFORM_PARENT);
468         ComponentKey ob_key(&ob->id, DEPSNODE_TYPE_TRANSFORM);
469
470         /* type-specific links */
471         switch (ob->partype) {
472                 case PARSKEL:  /* Armature Deform (Virtual Modifier) */
473                 {
474                         ComponentKey parent_key(&ob->parent->id, DEPSNODE_TYPE_TRANSFORM);
475                         add_relation(parent_key, ob_key, DEPSREL_TYPE_STANDARD, "Armature Deform Parent");
476                         break;
477                 }
478
479                 case PARVERT1: /* Vertex Parent */
480                 case PARVERT3:
481                 {
482                         ComponentKey parent_key(&ob->parent->id, DEPSNODE_TYPE_GEOMETRY);
483                         add_relation(parent_key, ob_key, DEPSREL_TYPE_GEOMETRY_EVAL, "Vertex Parent");
484
485                         /* XXX not sure what this is for or how you could be done properly - lukas */
486                         OperationDepsNode *parent_node = find_operation_node(parent_key);
487                         if (parent_node != NULL) {
488                                 parent_node->customdata_mask |= CD_MASK_ORIGINDEX;
489                         }
490
491                         ComponentKey transform_key(&ob->parent->id, DEPSNODE_TYPE_TRANSFORM);
492                         add_relation(transform_key, ob_key, DEPSREL_TYPE_TRANSFORM, "Vertex Parent TFM");
493                         break;
494                 }
495
496                 case PARBONE: /* Bone Parent */
497                 {
498                         ComponentKey parent_key(&ob->parent->id, DEPSNODE_TYPE_BONE, ob->parsubstr);
499                         add_relation(parent_key, ob_key, DEPSREL_TYPE_TRANSFORM, "Bone Parent");
500                         break;
501                 }
502
503                 default:
504                 {
505                         if (ob->parent->type == OB_LATTICE) {
506                                 /* Lattice Deform Parent - Virtual Modifier */
507                                 // XXX: no virtual modifiers should be left!
508                                 ComponentKey parent_key(&ob->parent->id, DEPSNODE_TYPE_TRANSFORM);
509                                 ComponentKey geom_key(&ob->parent->id, DEPSNODE_TYPE_GEOMETRY);
510
511                                 add_relation(parent_key, ob_key, DEPSREL_TYPE_STANDARD, "Lattice Deform Parent");
512                                 add_relation(geom_key, ob_key, DEPSREL_TYPE_STANDARD, "Lattice Deform Parent Geom");
513                         }
514                         else if (ob->parent->type == OB_CURVE) {
515                                 Curve *cu = (Curve *)ob->parent->data;
516
517                                 if (cu->flag & CU_PATH) {
518                                         /* Follow Path */
519                                         ComponentKey parent_key(&ob->parent->id, DEPSNODE_TYPE_GEOMETRY);
520                                         add_relation(parent_key, ob_key, DEPSREL_TYPE_TRANSFORM, "Curve Follow Parent");
521
522                                         ComponentKey transform_key(&ob->parent->id, DEPSNODE_TYPE_TRANSFORM);
523                                         add_relation(transform_key, ob_key, DEPSREL_TYPE_TRANSFORM, "Curve Follow TFM");
524                                 }
525                                 else {
526                                         /* Standard Parent */
527                                         ComponentKey parent_key(&ob->parent->id, DEPSNODE_TYPE_TRANSFORM);
528                                         add_relation(parent_key, ob_key, DEPSREL_TYPE_TRANSFORM, "Curve Parent");
529                                 }
530                         }
531                         else {
532                                 /* Standard Parent */
533                                 ComponentKey parent_key(&ob->parent->id, DEPSNODE_TYPE_TRANSFORM);
534                                 add_relation(parent_key, ob_key, DEPSREL_TYPE_TRANSFORM, "Parent");
535                         }
536                         break;
537                 }
538         }
539
540         /* exception case: parent is duplivert */
541         if ((ob->type == OB_MBALL) && (ob->parent->transflag & OB_DUPLIVERTS)) {
542                 //dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA | DAG_RL_OB_OB, "Duplivert");
543         }
544 }
545
546 void DepsgraphRelationBuilder::build_constraints(Scene *scene, ID *id, eDepsNode_Type component_type, const char *component_subdata,
547                                                  ListBase *constraints, RootPChanMap *root_map)
548 {
549         OperationKey constraint_op_key(id, component_type, component_subdata,
550                                        (component_type == DEPSNODE_TYPE_BONE) ? DEG_OPCODE_BONE_CONSTRAINTS : DEG_OPCODE_TRANSFORM_CONSTRAINTS);
551
552         /* add dependencies for each constraint in turn */
553         for (bConstraint *con = (bConstraint *)constraints->first; con; con = con->next) {
554                 const bConstraintTypeInfo *cti = BKE_constraint_typeinfo_get(con);
555
556                 /* invalid constraint type... */
557                 if (cti == NULL)
558                         continue;
559
560                 /* special case for camera tracking -- it doesn't use targets to define relations */
561                 // TODO: we can now represent dependencies in a much richer manner, so review how this is done...
562                 if (ELEM(cti->type, CONSTRAINT_TYPE_FOLLOWTRACK, CONSTRAINT_TYPE_CAMERASOLVER, CONSTRAINT_TYPE_OBJECTSOLVER)) {
563                         bool depends_on_camera = false;
564
565                         if (cti->type == CONSTRAINT_TYPE_FOLLOWTRACK) {
566                                 bFollowTrackConstraint *data = (bFollowTrackConstraint *)con->data;
567
568                                 if (((data->clip) || (data->flag & FOLLOWTRACK_ACTIVECLIP)) && data->track[0])
569                                         depends_on_camera = true;
570
571                                 if (data->depth_ob) {
572                                         // DAG_RL_DATA_OB | DAG_RL_OB_OB
573                                         ComponentKey depth_key(&data->depth_ob->id, DEPSNODE_TYPE_TRANSFORM);
574                                         add_relation(depth_key, constraint_op_key, DEPSREL_TYPE_TRANSFORM, cti->name);
575                                 }
576                         }
577                         else if (cti->type == CONSTRAINT_TYPE_OBJECTSOLVER) {
578                                 depends_on_camera = true;
579                         }
580
581                         if (depends_on_camera && scene->camera) {
582                                 // DAG_RL_DATA_OB | DAG_RL_OB_OB
583                                 ComponentKey camera_key(&scene->camera->id, DEPSNODE_TYPE_TRANSFORM);
584                                 add_relation(camera_key, constraint_op_key, DEPSREL_TYPE_TRANSFORM, cti->name);
585                         }
586
587                         /* TODO(sergey): This is more a TimeSource -> MovieClip -> Constraint dependency chain. */
588                         TimeSourceKey time_src_key;
589                         add_relation(time_src_key, constraint_op_key, DEPSREL_TYPE_TIME, "[TimeSrc -> Animation]");
590                 }
591                 else if (cti->type == CONSTRAINT_TYPE_TRANSFORM_CACHE) {
592                         /* TODO(kevin): This is more a TimeSource -> CacheFile -> Constraint dependency chain. */
593                         TimeSourceKey time_src_key;
594                         add_relation(time_src_key, constraint_op_key, DEPSREL_TYPE_TIME, "[TimeSrc -> Animation]");
595
596                         bTransformCacheConstraint *data = (bTransformCacheConstraint *)con->data;
597
598                         if (data->cache_file) {
599                                 ComponentKey cache_key(&data->cache_file->id, DEPSNODE_TYPE_CACHE);
600                                 add_relation(cache_key, constraint_op_key, DEPSREL_TYPE_CACHE, cti->name);
601                         }
602                 }
603                 else if (cti->get_constraint_targets) {
604                         ListBase targets = {NULL, NULL};
605                         cti->get_constraint_targets(con, &targets);
606
607                         LINKLIST_FOREACH (bConstraintTarget *, ct, &targets) {
608                                 if (ct->tar == NULL) {
609                                         continue;
610                                 }
611
612                                 if (ELEM(con->type, CONSTRAINT_TYPE_KINEMATIC, CONSTRAINT_TYPE_SPLINEIK)) {
613                                         /* ignore IK constraints - these are handled separately (on pose level) */
614                                 }
615                                 else if (ELEM(con->type, CONSTRAINT_TYPE_FOLLOWPATH, CONSTRAINT_TYPE_CLAMPTO)) {
616                                         /* these constraints require path geometry data... */
617                                         ComponentKey target_key(&ct->tar->id, DEPSNODE_TYPE_GEOMETRY);
618                                         add_relation(target_key, constraint_op_key, DEPSREL_TYPE_GEOMETRY_EVAL, cti->name); // XXX: type = geom_transform
619                                         // TODO: path dependency
620                                 }
621                                 else if ((ct->tar->type == OB_ARMATURE) && (ct->subtarget[0])) {
622                                         /* bone */
623                                         if (&ct->tar->id == id) {
624                                                 /* same armature  */
625                                                 eDepsOperation_Code target_key_opcode;
626
627                                                 /* Using "done" here breaks in-chain deps, while using "ready" here breaks most production rigs instead...
628                                                  * So, we do a compromise here, and only do this when an IK chain conflict may occur
629                                                  */
630                                                 if (root_map->has_common_root(component_subdata, ct->subtarget)) {
631                                                         target_key_opcode = DEG_OPCODE_BONE_READY;
632                                                 }
633                                                 else {
634                                                         target_key_opcode = DEG_OPCODE_BONE_DONE;
635                                                 }
636
637                                                 OperationKey target_key(&ct->tar->id, DEPSNODE_TYPE_BONE, ct->subtarget, target_key_opcode);
638                                                 add_relation(target_key, constraint_op_key, DEPSREL_TYPE_TRANSFORM, cti->name);
639                                         }
640                                         else {
641                                                 /* different armature - we can safely use the result of that */
642                                                 OperationKey target_key(&ct->tar->id, DEPSNODE_TYPE_BONE, ct->subtarget, DEG_OPCODE_BONE_DONE);
643                                                 add_relation(target_key, constraint_op_key, DEPSREL_TYPE_TRANSFORM, cti->name);
644                                         }
645                                 }
646                                 else if (ELEM(ct->tar->type, OB_MESH, OB_LATTICE) && (ct->subtarget[0])) {
647                                         /* vertex group */
648                                         /* NOTE: for now, we don't need to represent vertex groups separately... */
649                                         ComponentKey target_key(&ct->tar->id, DEPSNODE_TYPE_GEOMETRY);
650                                         add_relation(target_key, constraint_op_key, DEPSREL_TYPE_GEOMETRY_EVAL, cti->name);
651
652                                         if (ct->tar->type == OB_MESH) {
653                                                 OperationDepsNode *node2 = find_operation_node(target_key);
654                                                 if (node2 != NULL) {
655                                                         node2->customdata_mask |= CD_MASK_MDEFORMVERT;
656                                                 }
657                                         }
658                                 }
659                                 else if (con->type == CONSTRAINT_TYPE_SHRINKWRAP) {
660                                         /* Constraints which requires the target object surface. */
661                                         ComponentKey target_key(&ct->tar->id, DEPSNODE_TYPE_GEOMETRY);
662                                         add_relation(target_key, constraint_op_key, DEPSREL_TYPE_TRANSFORM, cti->name);
663
664                                         /* NOTE: obdata eval now doesn't necessarily depend on the object's transform... */
665                                         ComponentKey target_transform_key(&ct->tar->id, DEPSNODE_TYPE_TRANSFORM);
666                                         add_relation(target_transform_key, constraint_op_key, DEPSREL_TYPE_TRANSFORM, cti->name);
667                                 }
668                                 else {
669                                         /* standard object relation */
670                                         // TODO: loc vs rot vs scale?
671                                         if (&ct->tar->id == id) {
672                                                 /* Constraint targetting own object:
673                                                  * - This case is fine IFF we're dealing with a bone constraint pointing to
674                                                  *   its own armature. In that case, it's just transform -> bone.
675                                                  * - If however it is a real self targetting case, just make it depend on the
676                                                  *   previous constraint (or the pre-constraint state)...
677                                                  */
678                                                 if ((ct->tar->type == OB_ARMATURE) && (component_type == DEPSNODE_TYPE_BONE)) {
679                                                         OperationKey target_key(&ct->tar->id, DEPSNODE_TYPE_TRANSFORM, DEG_OPCODE_TRANSFORM_FINAL);
680                                                         add_relation(target_key, constraint_op_key, DEPSREL_TYPE_TRANSFORM, cti->name);
681                                                 }
682                                                 else {
683                                                         OperationKey target_key(&ct->tar->id, DEPSNODE_TYPE_TRANSFORM, DEG_OPCODE_TRANSFORM_LOCAL);
684                                                         add_relation(target_key, constraint_op_key, DEPSREL_TYPE_TRANSFORM, cti->name);
685                                                 }
686                                         }
687                                         else {
688                                                 /* normal object dependency */
689                                                 OperationKey target_key(&ct->tar->id, DEPSNODE_TYPE_TRANSFORM, DEG_OPCODE_TRANSFORM_FINAL);
690                                                 add_relation(target_key, constraint_op_key, DEPSREL_TYPE_TRANSFORM, cti->name);
691                                         }
692                                 }
693
694                                 /* Constraints which needs world's matrix for transform.
695                                  * TODO(sergey): More constraints here?
696                                  */
697                                 if (ELEM(con->type,
698                                          CONSTRAINT_TYPE_ROTLIKE,
699                                          CONSTRAINT_TYPE_SIZELIKE,
700                                          CONSTRAINT_TYPE_LOCLIKE,
701                                          CONSTRAINT_TYPE_TRANSLIKE))
702                                 {
703                                         /* TODO(sergey): Add used space check. */
704                                         ComponentKey target_transform_key(&ct->tar->id, DEPSNODE_TYPE_TRANSFORM);
705                                         add_relation(target_transform_key, constraint_op_key, DEPSREL_TYPE_TRANSFORM, cti->name);
706                                 }
707
708                         }
709
710                         if (cti->flush_constraint_targets)
711                                 cti->flush_constraint_targets(con, &targets, 1);
712                 }
713         }
714 }
715
716 void DepsgraphRelationBuilder::build_animdata(ID *id)
717 {
718         AnimData *adt = BKE_animdata_from_id(id);
719
720         if (adt == NULL)
721                 return;
722
723         ComponentKey adt_key(id, DEPSNODE_TYPE_ANIMATION);
724
725         /* animation */
726         if (adt->action || adt->nla_tracks.first) {
727                 /* wire up dependency to time source */
728                 TimeSourceKey time_src_key;
729                 add_relation(time_src_key, adt_key, DEPSREL_TYPE_TIME, "[TimeSrc -> Animation]");
730
731                 // XXX: Hook up specific update callbacks for special properties which may need it...
732
733                 // XXX: animdata "hierarchy" - top-level overrides need to go after lower-down
734         }
735
736         /* drivers */
737         LINKLIST_FOREACH (FCurve *, fcu, &adt->drivers) {
738                 OperationKey driver_key(id,
739                                         DEPSNODE_TYPE_PARAMETERS,
740                                         DEG_OPCODE_DRIVER,
741                                         fcu->rna_path,
742                                         fcu->array_index);
743
744                 /* create the driver's relations to targets */
745                 build_driver(id, fcu);
746
747                 /* Special case for array drivers: we can not multithread them because
748                  * of the way how they work internally: animation system will write the
749                  * whole array back to RNA even when changing individual array value.
750                  *
751                  * Some tricky things here:
752                  * - array_index is -1 for single channel drivers, meaning we only have
753                  *   to do some magic when array_index is not -1.
754                  * - We do relation from next array index to a previous one, so we don't
755                  *   have to deal with array index 0.
756                  *
757                  * TODO(sergey): Avoid liner lookup somehow.
758                  */
759                 if (fcu->array_index > 0) {
760                         FCurve *fcu_prev = NULL;
761                         LINKLIST_FOREACH (FCurve *, fcu_candidate, &adt->drivers) {
762                                 /* Writing to different RNA paths is  */
763                                 if (!STREQ(fcu_candidate->rna_path, fcu->rna_path)) {
764                                         continue;
765                                 }
766                                 /* We only do relation from previous fcurve to previous one. */
767                                 if (fcu_candidate->array_index >= fcu->array_index) {
768                                         continue;
769                                 }
770                                 /* Choose fcurve with highest possible array index. */
771                                 if (fcu_prev == NULL ||
772                                     fcu_candidate->array_index > fcu_prev->array_index)
773                                 {
774                                         fcu_prev = fcu_candidate;
775                                 }
776                         }
777                         if (fcu_prev != NULL) {
778                                 OperationKey prev_driver_key(id,
779                                                              DEPSNODE_TYPE_PARAMETERS,
780                                                              DEG_OPCODE_DRIVER,
781                                                              fcu_prev->rna_path,
782                                                              fcu_prev->array_index);
783                                 OperationKey driver_key(id,
784                                                         DEPSNODE_TYPE_PARAMETERS,
785                                                         DEG_OPCODE_DRIVER,
786                                                         fcu->rna_path,
787                                                         fcu->array_index);
788                                 add_relation(prev_driver_key,
789                                              driver_key,
790                                              DEPSREL_TYPE_OPERATION,
791                                              "[Driver Order]");
792                         }
793                 }
794
795                 /* prevent driver from occurring before own animation... */
796                 if (adt->action || adt->nla_tracks.first) {
797                         add_relation(adt_key, driver_key, DEPSREL_TYPE_OPERATION,
798                                      "[AnimData Before Drivers]");
799                 }
800         }
801 }
802
803 void DepsgraphRelationBuilder::build_driver(ID *id, FCurve *fcu)
804 {
805         ChannelDriver *driver = fcu->driver;
806         OperationKey driver_key(id,
807                                 DEPSNODE_TYPE_PARAMETERS,
808                                 DEG_OPCODE_DRIVER,
809                                 fcu->rna_path,
810                                 fcu->array_index);
811         bPoseChannel *pchan = NULL;
812
813         /* create dependency between driver and data affected by it */
814         /* - direct property relationship... */
815         //RNAPathKey affected_key(id, fcu->rna_path);
816         //add_relation(driver_key, affected_key, DEPSREL_TYPE_DRIVER, "[Driver -> Data] DepsRel");
817
818         /* driver -> data components (for interleaved evaluation - bones/constraints/modifiers) */
819         // XXX: this probably should probably be moved out into a separate function
820         if (strstr(fcu->rna_path, "pose.bones[") != NULL) {
821                 /* interleaved drivers during bone eval */
822                 // TODO: ideally, if this is for a constraint, it goes to said constraint
823                 Object *ob = (Object *)id;
824                 char *bone_name;
825
826                 bone_name = BLI_str_quoted_substrN(fcu->rna_path, "pose.bones[");
827                 pchan = BKE_pose_channel_find_name(ob->pose, bone_name);
828
829                 if (bone_name) {
830                         MEM_freeN(bone_name);
831                         bone_name = NULL;
832                 }
833
834                 if (pchan) {
835                         OperationKey bone_key(id, DEPSNODE_TYPE_BONE, pchan->name, DEG_OPCODE_BONE_LOCAL);
836                         add_relation(driver_key, bone_key, DEPSREL_TYPE_DRIVER, "[Driver -> Bone]");
837                 }
838                 else {
839                         fprintf(stderr,
840                                 "Couldn't find bone name for driver path - '%s'\n",
841                                 fcu->rna_path);
842                 }
843         }
844         else if (GS(id->name) == ID_AR && strstr(fcu->rna_path, "bones[")) {
845                 /* drivers on armature-level bone settings (i.e. bbone stuff),
846                  * which will affect the evaluation of corresponding pose bones
847                  */
848                 IDDepsNode *arm_node = m_graph->find_id_node(id);
849                 char *bone_name = BLI_str_quoted_substrN(fcu->rna_path, "bones[");
850
851                 if (arm_node && bone_name) {
852                         /* find objects which use this, and make their eval callbacks depend on this */
853                         foreach (DepsRelation *rel, arm_node->outlinks) {
854                                 IDDepsNode *to_node = (IDDepsNode *)rel->to;
855
856                                 /* we only care about objects with pose data which use this... */
857                                 if (GS(to_node->id->name) == ID_OB) {
858                                         Object *ob = (Object *)to_node->id;
859                                         bPoseChannel *pchan = BKE_pose_channel_find_name(ob->pose, bone_name); // NOTE: ob->pose may be NULL
860
861                                         if (pchan) {
862                                                 OperationKey bone_key(&ob->id, DEPSNODE_TYPE_BONE, pchan->name, DEG_OPCODE_BONE_LOCAL);
863                                                 add_relation(driver_key, bone_key, DEPSREL_TYPE_DRIVER, "[Arm Bone -> Driver -> Bone]");
864                                         }
865                                 }
866                         }
867
868                         /* free temp data */
869                         MEM_freeN(bone_name);
870                         bone_name = NULL;
871                 }
872                 else {
873                         fprintf(stderr,
874                                 "Couldn't find armature bone name for driver path - '%s'\n",
875                                 fcu->rna_path);
876                 }
877         }
878         else if (GS(id->name) == ID_OB && strstr(fcu->rna_path, "modifiers[")) {
879                 /* modifier driver - connect directly to the modifier */
880                 char *modifier_name = BLI_str_quoted_substrN(fcu->rna_path, "modifiers[");
881                 if (modifier_name) {
882                         OperationKey modifier_key(id,
883                                                   DEPSNODE_TYPE_GEOMETRY,
884                                                   DEG_OPCODE_GEOMETRY_MODIFIER,
885                                                   modifier_name);
886                         if (has_node(modifier_key)) {
887                                 add_relation(driver_key, modifier_key, DEPSREL_TYPE_DRIVER, "[Driver -> Modifier]");
888                         }
889                         else {
890                                 printf("Unexisting driver RNA path: %s\n", fcu->rna_path);
891                         }
892
893                         MEM_freeN(modifier_name);
894                 }
895         }
896         else if (GS(id->name) == ID_KE && strstr(fcu->rna_path, "key_blocks[")) {
897                 /* shape key driver - hook into the base geometry operation */
898                 // XXX: double check where this points
899                 Key *shape_key = (Key *)id;
900
901                 ComponentKey geometry_key(shape_key->from, DEPSNODE_TYPE_GEOMETRY);
902                 add_relation(driver_key, geometry_key, DEPSREL_TYPE_DRIVER, "[Driver -> ShapeKey Geom]");
903         }
904         else if (strstr(fcu->rna_path, "key_blocks[")) {
905                 ComponentKey geometry_key(id, DEPSNODE_TYPE_GEOMETRY);
906                 add_relation(driver_key, geometry_key, DEPSREL_TYPE_DRIVER, "[Driver -> ShapeKey Geom]");
907         }
908         else {
909                 if (GS(id->name) == ID_OB) {
910                         /* assume that driver affects a transform... */
911                         OperationKey local_transform_key(id, DEPSNODE_TYPE_TRANSFORM, DEG_OPCODE_TRANSFORM_LOCAL);
912                         add_relation(driver_key, local_transform_key, DEPSREL_TYPE_OPERATION, "[Driver -> Transform]");
913                 }
914                 else if (GS(id->name) == ID_KE) {
915                         ComponentKey geometry_key(id, DEPSNODE_TYPE_GEOMETRY);
916                         add_relation(driver_key, geometry_key, DEPSREL_TYPE_GEOMETRY_EVAL, "[Driver -> Shapekey Geometry]");
917                 }
918         }
919
920         /* ensure that affected prop's update callbacks will be triggered once done */
921         // TODO: implement this once the functionality to add these links exists in RNA
922         // XXX: the data itself could also set this, if it were to be truly initialised later?
923
924         /* loop over variables to get the target relationships */
925         LINKLIST_FOREACH (DriverVar *, dvar, &driver->variables) {
926                 /* only used targets */
927                 DRIVER_TARGETS_USED_LOOPER(dvar)
928                 {
929                         if (dtar->id == NULL)
930                                 continue;
931
932                         /* special handling for directly-named bones */
933                         if ((dtar->flag & DTAR_FLAG_STRUCT_REF) && (dtar->pchan_name[0])) {
934                                 Object *ob = (Object *)dtar->id;
935                                 bPoseChannel *target_pchan = BKE_pose_channel_find_name(ob->pose, dtar->pchan_name);
936                                 if (target_pchan != NULL) {
937                                         /* get node associated with bone */
938                                         // XXX: watch the space!
939                                         /* Some cases can't use final bone transform, for example:
940                                          * - Driving the bone with itself (addressed here)
941                                          * - Relations inside an IK chain (TODO?)
942                                          */
943                                         if (dtar->id == id &&
944                                             pchan != NULL &&
945                                             STREQ(pchan->name, target_pchan->name))
946                                         {
947                                                 continue;
948                                         }
949                                         OperationKey target_key(dtar->id, DEPSNODE_TYPE_BONE, target_pchan->name, DEG_OPCODE_BONE_DONE);
950                                         add_relation(target_key, driver_key, DEPSREL_TYPE_DRIVER_TARGET, "[Bone Target -> Driver]");
951                                 }
952                         }
953                         else if (dtar->flag & DTAR_FLAG_STRUCT_REF) {
954                                 /* get node associated with the object's transforms */
955                                 OperationKey target_key(dtar->id, DEPSNODE_TYPE_TRANSFORM, DEG_OPCODE_TRANSFORM_FINAL);
956                                 add_relation(target_key, driver_key, DEPSREL_TYPE_DRIVER_TARGET, "[Target -> Driver]");
957                         }
958                         else if (dtar->rna_path && strstr(dtar->rna_path, "pose.bones[")) {
959                                 /* workaround for ensuring that local bone transforms don't end up
960                                  * having to wait for pose eval to finish (to prevent cycles)
961                                  */
962                                 Object *ob = (Object *)dtar->id;
963                                 char *bone_name = BLI_str_quoted_substrN(dtar->rna_path, "pose.bones[");
964                                 bPoseChannel *target_pchan = BKE_pose_channel_find_name(ob->pose, bone_name);
965                                 if (bone_name) {
966                                         MEM_freeN(bone_name);
967                                         bone_name = NULL;
968                                 }
969                                 if (target_pchan) {
970                                         if (dtar->id == id &&
971                                             pchan != NULL &&
972                                             STREQ(pchan->name, target_pchan->name))
973                                         {
974                                                 continue;
975                                         }
976                                         OperationKey bone_key(dtar->id, DEPSNODE_TYPE_BONE, target_pchan->name, DEG_OPCODE_BONE_LOCAL);
977                                         add_relation(bone_key, driver_key, DEPSREL_TYPE_DRIVER, "[RNA Bone -> Driver]");
978                                 }
979                         }
980                         else {
981                                 if (dtar->id == id) {
982                                         /* Ignore input dependency if we're driving properties of the same ID,
983                                          * otherwise we'll be ending up in a cyclic dependency here.
984                                          */
985                                         continue;
986                                 }
987                                 /* resolve path to get node */
988                                 RNAPathKey target_key(dtar->id, dtar->rna_path ? dtar->rna_path : "");
989                                 add_relation(target_key, driver_key, DEPSREL_TYPE_DRIVER_TARGET, "[RNA Target -> Driver]");
990                         }
991                 }
992                 DRIVER_TARGETS_LOOPER_END
993         }
994
995         /* It's quite tricky to detect if the driver actually depends on time or not,
996          * so for now we'll be quite conservative here about optimization and consider
997          * all python drivers to be depending on time.
998          */
999         if ((driver->type == DRIVER_TYPE_PYTHON) &&
1000             python_driver_depends_on_time(driver))
1001         {
1002                 TimeSourceKey time_src_key;
1003                 add_relation(time_src_key, driver_key, DEPSREL_TYPE_TIME, "[TimeSrc -> Driver]");
1004         }
1005 }
1006
1007 void DepsgraphRelationBuilder::build_world(World *world)
1008 {
1009         ID *world_id = &world->id;
1010         if (world_id->tag & LIB_TAG_DOIT) {
1011                 return;
1012         }
1013         world_id->tag |= LIB_TAG_DOIT;
1014
1015         build_animdata(world_id);
1016
1017         /* TODO: other settings? */
1018
1019         /* textures */
1020         build_texture_stack(world_id, world->mtex);
1021
1022         /* world's nodetree */
1023         build_nodetree(world_id, world->nodetree);
1024 }
1025
1026 void DepsgraphRelationBuilder::build_rigidbody(Scene *scene)
1027 {
1028         RigidBodyWorld *rbw = scene->rigidbody_world;
1029
1030         OperationKey init_key(&scene->id, DEPSNODE_TYPE_TRANSFORM, DEG_OPCODE_RIGIDBODY_REBUILD);
1031         OperationKey sim_key(&scene->id, DEPSNODE_TYPE_TRANSFORM, DEG_OPCODE_RIGIDBODY_SIM);
1032
1033         /* rel between the two sim-nodes */
1034         add_relation(init_key, sim_key, DEPSREL_TYPE_OPERATION, "Rigidbody [Init -> SimStep]");
1035
1036         /* set up dependencies between these operations and other builtin nodes --------------- */
1037
1038         /* time dependency */
1039         TimeSourceKey time_src_key;
1040         add_relation(time_src_key, init_key, DEPSREL_TYPE_TIME, "TimeSrc -> Rigidbody Reset/Rebuild (Optional)");
1041         add_relation(time_src_key, sim_key, DEPSREL_TYPE_TIME, "TimeSrc -> Rigidbody Sim Step");
1042
1043         /* objects - simulation participants */
1044         if (rbw->group) {
1045                 LINKLIST_FOREACH (GroupObject *, go, &rbw->group->gobject) {
1046                         Object *ob = go->ob;
1047                         if (ob == NULL || ob->type != OB_MESH) {
1048                                 continue;
1049                         }
1050
1051                         /* hook up evaluation order...
1052                          * 1) flushing rigidbody results follows base transforms being applied
1053                          * 2) rigidbody flushing can only be performed after simulation has been run
1054                          *
1055                          * 3) simulation needs to know base transforms to figure out what to do
1056                          *    XXX: there's probably a difference between passive and active
1057                          *         - passive don't change, so may need to know full transform...
1058                          */
1059                         OperationKey rbo_key(&ob->id, DEPSNODE_TYPE_TRANSFORM, DEG_OPCODE_TRANSFORM_RIGIDBODY);
1060
1061                         eDepsOperation_Code trans_opcode = ob->parent ? DEG_OPCODE_TRANSFORM_PARENT : DEG_OPCODE_TRANSFORM_LOCAL;
1062                         OperationKey trans_op(&ob->id, DEPSNODE_TYPE_TRANSFORM, trans_opcode);
1063
1064                         add_relation(trans_op, rbo_key, DEPSREL_TYPE_OPERATION, "Base Ob Transform -> RBO Sync");
1065                         add_relation(sim_key, rbo_key, DEPSREL_TYPE_COMPONENT_ORDER, "Rigidbody Sim Eval -> RBO Sync");
1066
1067                         /* if constraints exist, those depend on the result of the rigidbody sim
1068                          * - This allows constraints to modify the result of the sim (i.e. clamping)
1069                          *   while still allowing the sim to depend on some changes to the objects.
1070                          *   Also, since constraints are hooked up to the final nodes, this link
1071                          *   means that we can also fit in there too...
1072                          * - Later, it might be good to include a constraint in the stack allowing us
1073                          *   to control whether rigidbody eval gets interleaved into the constraint stack
1074                          */
1075                         if (ob->constraints.first) {
1076                                 OperationKey constraint_key(&ob->id, DEPSNODE_TYPE_TRANSFORM, DEG_OPCODE_TRANSFORM_CONSTRAINTS);
1077                                 add_relation(rbo_key, constraint_key, DEPSREL_TYPE_COMPONENT_ORDER, "RBO Sync -> Ob Constraints");
1078                         }
1079                         else {
1080                                 /* final object transform depends on rigidbody */
1081                                 OperationKey done_key(&ob->id, DEPSNODE_TYPE_TRANSFORM, DEG_OPCODE_TRANSFORM_FINAL);
1082                                 add_relation(rbo_key, done_key, DEPSREL_TYPE_COMPONENT_ORDER, "RBO Sync -> Done");
1083
1084                                 // XXX: ubereval will be removed eventually, but we still need it in the meantime
1085                                 OperationKey uber_key(&ob->id, DEPSNODE_TYPE_TRANSFORM, DEG_OPCODE_OBJECT_UBEREVAL);
1086                                 add_relation(rbo_key, uber_key, DEPSREL_TYPE_COMPONENT_ORDER, "RBO Sync -> Uber (Temp)");
1087                         }
1088
1089
1090                         /* needed to get correct base values */
1091                         add_relation(trans_op, sim_key, DEPSREL_TYPE_OPERATION, "Base Ob Transform -> Rigidbody Sim Eval");
1092                 }
1093         }
1094
1095         /* constraints */
1096         if (rbw->constraints) {
1097                 LINKLIST_FOREACH (GroupObject *, go, &rbw->constraints->gobject) {
1098                         Object *ob = go->ob;
1099                         if (ob == NULL || !ob->rigidbody_constraint) {
1100                                 continue;
1101                         }
1102
1103                         RigidBodyCon *rbc = ob->rigidbody_constraint;
1104
1105                         /* final result of the constraint object's transform controls how the
1106                          * constraint affects the physics sim for these objects
1107                          */
1108                         ComponentKey trans_key(&ob->id, DEPSNODE_TYPE_TRANSFORM);
1109                         OperationKey ob1_key(&rbc->ob1->id, DEPSNODE_TYPE_TRANSFORM, DEG_OPCODE_TRANSFORM_RIGIDBODY);
1110                         OperationKey ob2_key(&rbc->ob2->id, DEPSNODE_TYPE_TRANSFORM, DEG_OPCODE_TRANSFORM_RIGIDBODY);
1111
1112                         /* - constrained-objects sync depends on the constraint-holder */
1113                         add_relation(trans_key, ob1_key, DEPSREL_TYPE_TRANSFORM, "RigidBodyConstraint -> RBC.Object_1");
1114                         add_relation(trans_key, ob2_key, DEPSREL_TYPE_TRANSFORM, "RigidBodyConstraint -> RBC.Object_2");
1115
1116                         /* - ensure that sim depends on this constraint's transform */
1117                         add_relation(trans_key, sim_key, DEPSREL_TYPE_TRANSFORM, "RigidBodyConstraint Transform -> RB Simulation");
1118                 }
1119         }
1120 }
1121
1122 /* Shapekeys */
1123 void DepsgraphRelationBuilder::build_shapekeys(ID *obdata, Key *key)
1124 {
1125         ComponentKey obdata_key(obdata, DEPSNODE_TYPE_GEOMETRY);
1126
1127         /* attach animdata to geometry */
1128         build_animdata(&key->id);
1129
1130         if (key->adt) {
1131                 // TODO: this should really be handled in build_animdata, since many of these cases will need it
1132                 if (key->adt->action || key->adt->nla_tracks.first) {
1133                         ComponentKey adt_key(&key->id, DEPSNODE_TYPE_ANIMATION);
1134                         add_relation(adt_key, obdata_key, DEPSREL_TYPE_OPERATION, "Animation");
1135                 }
1136
1137                 /* NOTE: individual shapekey drivers are handled above already */
1138         }
1139
1140         /* attach to geometry */
1141         // XXX: aren't shapekeys now done as a pseudo-modifier on object?
1142         //ComponentKey key_key(&key->id, DEPSNODE_TYPE_GEOMETRY); // FIXME: this doesn't exist
1143         //add_relation(key_key, obdata_key, DEPSREL_TYPE_GEOMETRY_EVAL, "Shapekeys");
1144 }
1145
1146 /**
1147  * ObData Geometry Evaluation
1148  * ==========================
1149  *
1150  * The evaluation of geometry on objects is as follows:
1151  * - The actual evaluated of the derived geometry (e.g. DerivedMesh, DispList, etc.)
1152  *   occurs in the Geometry component of the object which references this. This includes
1153  *   modifiers, and the temporary "ubereval" for geometry.
1154  * - Therefore, each user of a piece of shared geometry data ends up evaluating its own
1155  *   version of the stuff, complete with whatever modifiers it may use.
1156  *
1157  * - The datablocks for the geometry data - "obdata" (e.g. ID_ME, ID_CU, ID_LT, etc.) are used for
1158  *     1) calculating the bounding boxes of the geometry data,
1159  *     2) aggregating inward links from other objects (e.g. for text on curve, etc.)
1160  *        and also for the links coming from the shapekey datablocks
1161  * - Animation/Drivers affecting the parameters of the geometry are made to trigger
1162  *   updates on the obdata geometry component, which then trigger downstream
1163  *   re-evaluation of the individual instances of this geometry.
1164  */
1165 // TODO: Materials and lighting should probably get their own component, instead of being lumped under geometry?
1166 void DepsgraphRelationBuilder::build_obdata_geom(Main *bmain, Scene *scene, Object *ob)
1167 {
1168         ID *obdata = (ID *)ob->data;
1169
1170         /* Init operation of object-level geometry evaluation. */
1171         OperationKey geom_init_key(&ob->id, DEPSNODE_TYPE_GEOMETRY, DEG_OPCODE_PLACEHOLDER, "Eval Init");
1172
1173         /* get nodes for result of obdata's evaluation, and geometry evaluation on object */
1174         ComponentKey obdata_geom_key(obdata, DEPSNODE_TYPE_GEOMETRY);
1175         ComponentKey geom_key(&ob->id, DEPSNODE_TYPE_GEOMETRY);
1176
1177         /* link components to each other */
1178         add_relation(obdata_geom_key, geom_key, DEPSREL_TYPE_DATABLOCK, "Object Geometry Base Data");
1179
1180         /* Modifiers */
1181         if (ob->modifiers.first) {
1182                 OperationKey prev_mod_key;
1183
1184                 LINKLIST_FOREACH (ModifierData *, md, &ob->modifiers) {
1185                         const ModifierTypeInfo *mti = modifierType_getInfo((ModifierType)md->type);
1186                         OperationKey mod_key(&ob->id, DEPSNODE_TYPE_GEOMETRY, DEG_OPCODE_GEOMETRY_MODIFIER, md->name);
1187
1188                         if (md->prev) {
1189                                 /* Stack relation: modifier depends on previous modifier in the stack */
1190                                 add_relation(prev_mod_key, mod_key, DEPSREL_TYPE_GEOMETRY_EVAL, "Modifier Stack");
1191                         }
1192                         else {
1193                                 /* Stack relation: first modifier depends on the geometry. */
1194                                 add_relation(geom_init_key, mod_key, DEPSREL_TYPE_GEOMETRY_EVAL, "Modifier Stack");
1195                         }
1196
1197                         if (mti->updateDepsgraph) {
1198                                 DepsNodeHandle handle = create_node_handle(mod_key);
1199                                 mti->updateDepsgraph(
1200                                         md,
1201                                         bmain,
1202                                         scene,
1203                                         ob,
1204                                         reinterpret_cast< ::DepsNodeHandle* >(&handle));
1205                         }
1206
1207                         if (BKE_object_modifier_use_time(ob, md)) {
1208                                 TimeSourceKey time_src_key;
1209                                 add_relation(time_src_key, mod_key, DEPSREL_TYPE_TIME, "Time Source");
1210
1211                                 /* Hacky fix for T45633 (Animated modifiers aren't updated)
1212                                  *
1213                                  * This check works because BKE_object_modifier_use_time() tests
1214                                  * for either the modifier needing time, or that it is animated.
1215                                  */
1216                                 /* XXX: Remove this hack when these links are added as part of build_animdata() instead */
1217                                 if (modifier_dependsOnTime(md) == false && needs_animdata_node(&ob->id)) {
1218                                         ComponentKey animation_key(&ob->id, DEPSNODE_TYPE_ANIMATION);
1219                                         add_relation(animation_key, mod_key, DEPSREL_TYPE_OPERATION, "Modifier Animation");
1220                                 }
1221                         }
1222
1223                         prev_mod_key = mod_key;
1224                 }
1225         }
1226
1227         /* materials */
1228         if (ob->totcol) {
1229                 int a;
1230
1231                 for (a = 1; a <= ob->totcol; a++) {
1232                         Material *ma = give_current_material(ob, a);
1233
1234                         if (ma)
1235                                 build_material(&ob->id, ma);
1236                 }
1237         }
1238
1239         /* geometry collision */
1240         if (ELEM(ob->type, OB_MESH, OB_CURVE, OB_LATTICE)) {
1241                 // add geometry collider relations
1242         }
1243
1244         /* Make sure uber update is the last in the dependencies.
1245          *
1246          * TODO(sergey): Get rid of this node.
1247          */
1248         if (ob->type != OB_ARMATURE) {
1249                 /* Armatures does no longer require uber node. */
1250                 OperationKey obdata_ubereval_key(&ob->id, DEPSNODE_TYPE_GEOMETRY, DEG_OPCODE_GEOMETRY_UBEREVAL);
1251                 if (ob->modifiers.last) {
1252                         ModifierData *md = (ModifierData *)ob->modifiers.last;
1253                         OperationKey mod_key(&ob->id, DEPSNODE_TYPE_GEOMETRY, DEG_OPCODE_GEOMETRY_MODIFIER, md->name);
1254                         add_relation(mod_key, obdata_ubereval_key, DEPSREL_TYPE_OPERATION, "Object Geometry UberEval");
1255                 }
1256                 else {
1257                         add_relation(geom_init_key, obdata_ubereval_key, DEPSREL_TYPE_OPERATION, "Object Geometry UberEval");
1258                 }
1259         }
1260
1261         if (obdata->tag & LIB_TAG_DOIT) {
1262                 return;
1263         }
1264         obdata->tag |= LIB_TAG_DOIT;
1265
1266         /* Link object data evaluation node to exit operation. */
1267         OperationKey obdata_geom_eval_key(obdata, DEPSNODE_TYPE_GEOMETRY, DEG_OPCODE_PLACEHOLDER, "Geometry Eval");
1268         OperationKey obdata_geom_done_key(obdata, DEPSNODE_TYPE_GEOMETRY, DEG_OPCODE_PLACEHOLDER, "Eval Done");
1269         add_relation(obdata_geom_eval_key, obdata_geom_done_key, DEPSREL_TYPE_DATABLOCK, "ObData Geom Eval Done");
1270
1271         /* type-specific node/links */
1272         switch (ob->type) {
1273                 case OB_MESH:
1274                         break;
1275
1276                 case OB_MBALL:
1277                 {
1278                         Object *mom = BKE_mball_basis_find(scene, ob);
1279
1280                         /* motherball - mom depends on children! */
1281                         if (mom != ob) {
1282                                 /* non-motherball -> cannot be directly evaluated! */
1283                                 ComponentKey mom_key(&mom->id, DEPSNODE_TYPE_GEOMETRY);
1284                                 ComponentKey transform_key(&ob->id, DEPSNODE_TYPE_TRANSFORM);
1285                                 add_relation(geom_key, mom_key, DEPSREL_TYPE_GEOMETRY_EVAL, "Metaball Motherball");
1286                                 add_relation(transform_key, mom_key, DEPSREL_TYPE_GEOMETRY_EVAL, "Metaball Motherball");
1287                         }
1288                         break;
1289                 }
1290
1291                 case OB_CURVE:
1292                 case OB_FONT:
1293                 {
1294                         Curve *cu = (Curve *)obdata;
1295
1296                         /* curve's dependencies */
1297                         // XXX: these needs geom data, but where is geom stored?
1298                         if (cu->bevobj) {
1299                                 ComponentKey bevob_key(&cu->bevobj->id, DEPSNODE_TYPE_GEOMETRY);
1300                                 build_object(bmain, scene, cu->bevobj);
1301                                 add_relation(bevob_key, geom_key, DEPSREL_TYPE_GEOMETRY_EVAL, "Curve Bevel");
1302                         }
1303                         if (cu->taperobj) {
1304                                 ComponentKey taperob_key(&cu->taperobj->id, DEPSNODE_TYPE_GEOMETRY);
1305                                 build_object(bmain, scene, cu->taperobj);
1306                                 add_relation(taperob_key, geom_key, DEPSREL_TYPE_GEOMETRY_EVAL, "Curve Taper");
1307                         }
1308                         if (ob->type == OB_FONT) {
1309                                 if (cu->textoncurve) {
1310                                         ComponentKey textoncurve_key(&cu->textoncurve->id, DEPSNODE_TYPE_GEOMETRY);
1311                                         build_object(bmain, scene, cu->textoncurve);
1312                                         add_relation(textoncurve_key, geom_key, DEPSREL_TYPE_GEOMETRY_EVAL, "Text on Curve");
1313                                 }
1314                         }
1315                         break;
1316                 }
1317
1318                 case OB_SURF: /* Nurbs Surface */
1319                 {
1320                         break;
1321                 }
1322
1323                 case OB_LATTICE: /* Lattice */
1324                 {
1325                         break;
1326                 }
1327         }
1328
1329         /* ShapeKeys */
1330         Key *key = BKE_key_from_object(ob);
1331         if (key) {
1332                 build_shapekeys(obdata, key);
1333         }
1334
1335         if (needs_animdata_node(obdata)) {
1336                 ComponentKey animation_key(obdata, DEPSNODE_TYPE_ANIMATION);
1337                 ComponentKey parameters_key(obdata, DEPSNODE_TYPE_PARAMETERS);
1338                 add_relation(animation_key, parameters_key,
1339                              DEPSREL_TYPE_COMPONENT_ORDER, "Geom Parameters");
1340                 /* Evaluation usually depends on animation.
1341                  * TODO(sergey): Need to re-hook it after granular update is implemented..
1342                  */
1343                 add_relation(animation_key, obdata_geom_eval_key, DEPSREL_TYPE_GEOMETRY_EVAL, "Animation");
1344         }
1345 }
1346
1347 /* Cameras */
1348 // TODO: Link scene-camera links in somehow...
1349 void DepsgraphRelationBuilder::build_camera(Object *ob)
1350 {
1351         Camera *cam = (Camera *)ob->data;
1352         ID *camera_id = &cam->id;
1353         if (camera_id->tag & LIB_TAG_DOIT) {
1354                 return;
1355         }
1356         camera_id->tag |= LIB_TAG_DOIT;
1357
1358         ComponentKey parameters_key(camera_id, DEPSNODE_TYPE_PARAMETERS);
1359
1360         if (needs_animdata_node(camera_id)) {
1361                 ComponentKey animation_key(camera_id, DEPSNODE_TYPE_ANIMATION);
1362                 add_relation(animation_key, parameters_key,
1363                              DEPSREL_TYPE_COMPONENT_ORDER, "Camera Parameters");
1364         }
1365
1366         /* DOF */
1367         if (cam->dof_ob) {
1368                 ComponentKey ob_param_key(&ob->id, DEPSNODE_TYPE_PARAMETERS);
1369                 ComponentKey dof_ob_key(&cam->dof_ob->id, DEPSNODE_TYPE_TRANSFORM);
1370                 add_relation(dof_ob_key, ob_param_key, DEPSREL_TYPE_TRANSFORM, "Camera DOF");
1371         }
1372 }
1373
1374 /* Lamps */
1375 void DepsgraphRelationBuilder::build_lamp(Object *ob)
1376 {
1377         Lamp *la = (Lamp *)ob->data;
1378         ID *lamp_id = &la->id;
1379         if (lamp_id->tag & LIB_TAG_DOIT) {
1380                 return;
1381         }
1382         lamp_id->tag |= LIB_TAG_DOIT;
1383
1384         ComponentKey parameters_key(lamp_id, DEPSNODE_TYPE_PARAMETERS);
1385
1386         if (needs_animdata_node(lamp_id)) {
1387                 ComponentKey animation_key(lamp_id, DEPSNODE_TYPE_ANIMATION);
1388                 add_relation(animation_key, parameters_key,
1389                              DEPSREL_TYPE_COMPONENT_ORDER, "Lamp Parameters");
1390         }
1391
1392         /* lamp's nodetree */
1393         if (la->nodetree) {
1394                 build_nodetree(lamp_id, la->nodetree);
1395                 ComponentKey nodetree_key(&la->nodetree->id, DEPSNODE_TYPE_PARAMETERS);
1396                 add_relation(nodetree_key, parameters_key,
1397                              DEPSREL_TYPE_COMPONENT_ORDER, "NTree->Lamp Parameters");
1398         }
1399
1400         /* textures */
1401         build_texture_stack(lamp_id, la->mtex);
1402 }
1403
1404 void DepsgraphRelationBuilder::build_nodetree(ID *owner, bNodeTree *ntree)
1405 {
1406         if (!ntree)
1407                 return;
1408
1409         ID *ntree_id = &ntree->id;
1410
1411         build_animdata(ntree_id);
1412
1413         OperationKey parameters_key(ntree_id,
1414                                     DEPSNODE_TYPE_PARAMETERS,
1415                                     DEG_OPCODE_PLACEHOLDER,
1416                                     "Parameters Eval");
1417
1418         /* nodetree's nodes... */
1419         LINKLIST_FOREACH (bNode *, bnode, &ntree->nodes) {
1420                 if (bnode->id) {
1421                         if (GS(bnode->id->name) == ID_MA) {
1422                                 build_material(owner, (Material *)bnode->id);
1423                         }
1424                         else if (bnode->type == ID_TE) {
1425                                 build_texture(owner, (Tex *)bnode->id);
1426                         }
1427                         else if (bnode->type == NODE_GROUP) {
1428                                 bNodeTree *group_ntree = (bNodeTree *)bnode->id;
1429                                 if ((group_ntree->id.tag & LIB_TAG_DOIT) == 0) {
1430                                         build_nodetree(owner, group_ntree);
1431                                         group_ntree->id.tag |= LIB_TAG_DOIT;
1432                                 }
1433                                 OperationKey group_parameters_key(&group_ntree->id,
1434                                                                   DEPSNODE_TYPE_PARAMETERS,
1435                                                                   DEG_OPCODE_PLACEHOLDER,
1436                                                                   "Parameters Eval");
1437                                 add_relation(group_parameters_key, parameters_key,
1438                                              DEPSREL_TYPE_COMPONENT_ORDER, "Group Node");
1439                         }
1440                 }
1441         }
1442
1443         if (needs_animdata_node(ntree_id)) {
1444                 ComponentKey animation_key(ntree_id, DEPSNODE_TYPE_ANIMATION);
1445                 add_relation(animation_key, parameters_key,
1446                              DEPSREL_TYPE_COMPONENT_ORDER, "NTree Parameters");
1447         }
1448
1449         // TODO: link from nodetree to owner_component?
1450 }
1451
1452 /* Recursively build graph for material */
1453 void DepsgraphRelationBuilder::build_material(ID *owner, Material *ma)
1454 {
1455         ID *ma_id = &ma->id;
1456         if (ma_id->tag & LIB_TAG_DOIT) {
1457                 return;
1458         }
1459         ma_id->tag |= LIB_TAG_DOIT;
1460
1461         /* animation */
1462         build_animdata(ma_id);
1463
1464         /* textures */
1465         build_texture_stack(owner, ma->mtex);
1466
1467         /* material's nodetree */
1468         build_nodetree(owner, ma->nodetree);
1469 }
1470
1471 /* Recursively build graph for texture */
1472 void DepsgraphRelationBuilder::build_texture(ID *owner, Tex *tex)
1473 {
1474         ID *tex_id = &tex->id;
1475         if (tex_id->tag & LIB_TAG_DOIT) {
1476                 return;
1477         }
1478         tex_id->tag |= LIB_TAG_DOIT;
1479
1480         /* texture itself */
1481         build_animdata(tex_id);
1482
1483         /* texture's nodetree */
1484         build_nodetree(owner, tex->nodetree);
1485 }
1486
1487 /* Texture-stack attached to some shading datablock */
1488 void DepsgraphRelationBuilder::build_texture_stack(ID *owner, MTex **texture_stack)
1489 {
1490         int i;
1491
1492         /* for now assume that all texture-stacks have same number of max items */
1493         for (i = 0; i < MAX_MTEX; i++) {
1494                 MTex *mtex = texture_stack[i];
1495                 if (mtex && mtex->tex)
1496                         build_texture(owner, mtex->tex);
1497         }
1498 }
1499
1500 void DepsgraphRelationBuilder::build_compositor(Scene *scene)
1501 {
1502         /* For now, just a plain wrapper? */
1503         build_nodetree(&scene->id, scene->nodetree);
1504 }
1505
1506 void DepsgraphRelationBuilder::build_gpencil(ID *UNUSED(owner), bGPdata *gpd)
1507 {
1508         /* animation */
1509         build_animdata(&gpd->id);
1510
1511         // TODO: parent object (when that feature is implemented)
1512 }
1513
1514 bool DepsgraphRelationBuilder::needs_animdata_node(ID *id)
1515 {
1516         AnimData *adt = BKE_animdata_from_id(id);
1517         if (adt != NULL) {
1518                 return (adt->action != NULL) || (adt->nla_tracks.first != NULL);
1519         }
1520         return false;
1521 }
1522
1523 void DepsgraphRelationBuilder::build_cachefile(CacheFile *cache_file) {
1524         /* Animation. */
1525         build_animdata(&cache_file->id);
1526 }
1527
1528 void DepsgraphRelationBuilder::build_mask(Mask *mask)
1529 {
1530         /* Animation. */
1531         build_animdata(&mask->id);
1532 }
1533
1534 void DepsgraphRelationBuilder::build_movieclip(MovieClip *clip)
1535 {
1536         /* Animation. */
1537         build_animdata(&clip->id);
1538 }
1539
1540 }  // namespace DEG