Merge branch 'master' into blender2.8
[blender.git] / source / blender / depsgraph / intern / builder / deg_builder_nodes.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_nodes.cc
28  *  \ingroup depsgraph
29  *
30  * Methods for constructing depsgraph's nodes
31  */
32
33 #include "intern/builder/deg_builder_nodes.h"
34
35 #include <stdio.h>
36 #include <stdlib.h>
37
38 #include "MEM_guardedalloc.h"
39
40 extern "C" {
41 #include "BLI_blenlib.h"
42 #include "BLI_string.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_cachefile_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_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_particle_types.h"
64 #include "DNA_object_types.h"
65 #include "DNA_rigidbody_types.h"
66 #include "DNA_scene_types.h"
67 #include "DNA_texture_types.h"
68 #include "DNA_world_types.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_fcurve.h"
77 #include "BKE_idcode.h"
78 #include "BKE_group.h"
79 #include "BKE_key.h"
80 #include "BKE_lattice.h"
81 #include "BKE_library.h"
82 #include "BKE_main.h"
83 #include "BKE_material.h"
84 #include "BKE_mesh.h"
85 #include "BKE_mball.h"
86 #include "BKE_modifier.h"
87 #include "BKE_node.h"
88 #include "BKE_object.h"
89 #include "BKE_particle.h"
90 #include "BKE_rigidbody.h"
91 #include "BKE_sound.h"
92 #include "BKE_texture.h"
93 #include "BKE_tracking.h"
94 #include "BKE_world.h"
95
96 #include "DEG_depsgraph.h"
97 #include "DEG_depsgraph_build.h"
98
99 #include "RNA_access.h"
100 #include "RNA_types.h"
101 } /* extern "C" */
102
103 #include "intern/builder/deg_builder.h"
104 #include "intern/nodes/deg_node.h"
105 #include "intern/nodes/deg_node_component.h"
106 #include "intern/nodes/deg_node_operation.h"
107 #include "intern/depsgraph_types.h"
108 #include "intern/depsgraph_intern.h"
109 #include "util/deg_util_foreach.h"
110
111 namespace DEG {
112
113 namespace {
114
115 struct BuilderWalkUserData {
116         DepsgraphNodeBuilder *builder;
117         Scene *scene;
118 };
119
120 static void modifier_walk(void *user_data,
121                           struct Object * /*ob*/,
122                           struct Object **obpoin,
123                           int /*cb_flag*/)
124 {
125         BuilderWalkUserData *data = (BuilderWalkUserData *)user_data;
126         if (*obpoin) {
127                 data->builder->build_object(data->scene, *obpoin);
128         }
129 }
130
131 void constraint_walk(bConstraint * /*con*/,
132                      ID **idpoin,
133                      bool /*is_reference*/,
134                      void *user_data)
135 {
136         BuilderWalkUserData *data = (BuilderWalkUserData *)user_data;
137         if (*idpoin) {
138                 ID *id = *idpoin;
139                 if (GS(id->name) == ID_OB) {
140                         data->builder->build_object(data->scene, (Object *)id);
141                 }
142         }
143 }
144
145 }  /* namespace */
146
147 /* ************ */
148 /* Node Builder */
149
150 /* **** General purpose functions **** */
151
152 DepsgraphNodeBuilder::DepsgraphNodeBuilder(Main *bmain, Depsgraph *graph) :
153     m_bmain(bmain),
154     m_graph(graph)
155 {
156 }
157
158 DepsgraphNodeBuilder::~DepsgraphNodeBuilder()
159 {
160 }
161
162 RootDepsNode *DepsgraphNodeBuilder::add_root_node()
163 {
164         return m_graph->add_root_node();
165 }
166
167 IDDepsNode *DepsgraphNodeBuilder::add_id_node(ID *id)
168 {
169         return m_graph->add_id_node(id, id->name);
170 }
171
172 TimeSourceDepsNode *DepsgraphNodeBuilder::add_time_source(ID *id)
173 {
174         /* determine which node to attach timesource to */
175         if (id) {
176 #if 0 /* XXX TODO */
177                 /* get ID node */
178                 IDDepsNode id_node = m_graph->find_id_node(id);
179
180                 /* depends on what this is... */
181                 switch (GS(id->name)) {
182                         case ID_SCE: /* Scene - Usually sequencer strip causing time remapping... */
183                         {
184                                 // TODO...
185                         }
186                         break;
187
188                         case ID_GR: /* Group */
189                         {
190                                 // TODO...
191                         }
192                         break;
193
194                         // XXX: time source...
195
196                         default:     /* Unhandled */
197                                 printf("%s(): Unhandled ID - %s \n", __func__, id->name);
198                                 break;
199                 }
200 #endif
201         }
202         else {
203                 /* root-node */
204                 RootDepsNode *root_node = m_graph->root_node;
205                 if (root_node) {
206                         return root_node->add_time_source("Time Source");
207                 }
208         }
209
210         return NULL;
211 }
212
213 ComponentDepsNode *DepsgraphNodeBuilder::add_component_node(
214         ID *id,
215         eDepsNode_Type comp_type,
216         const char *comp_name)
217 {
218         IDDepsNode *id_node = add_id_node(id);
219         ComponentDepsNode *comp_node = id_node->add_component(comp_type, comp_name);
220         comp_node->owner = id_node;
221         return comp_node;
222 }
223
224 OperationDepsNode *DepsgraphNodeBuilder::add_operation_node(
225         ComponentDepsNode *comp_node,
226         eDepsOperation_Type optype,
227         DepsEvalOperationCb op,
228         eDepsOperation_Code opcode,
229         const char *name,
230         int name_tag)
231 {
232         OperationDepsNode *op_node = comp_node->has_operation(opcode,
233                                                               name,
234                                                               name_tag);
235         if (op_node == NULL) {
236                 op_node = comp_node->add_operation(optype, op, opcode, name, name_tag);
237                 m_graph->operations.push_back(op_node);
238         }
239         else {
240                 fprintf(stderr,
241                         "add_operation: Operation already exists - %s has %s at %p\n",
242                         comp_node->identifier().c_str(),
243                         op_node->identifier().c_str(),
244                         op_node);
245                 BLI_assert(!"Should not happen!");
246         }
247         return op_node;
248 }
249
250 OperationDepsNode *DepsgraphNodeBuilder::add_operation_node(
251         ID *id,
252         eDepsNode_Type comp_type,
253         const char *comp_name,
254         eDepsOperation_Type optype,
255         DepsEvalOperationCb op,
256         eDepsOperation_Code opcode,
257         const char *name,
258         int name_tag)
259 {
260         ComponentDepsNode *comp_node = add_component_node(id, comp_type, comp_name);
261         return add_operation_node(comp_node, optype, op, opcode, name, name_tag);
262 }
263
264 OperationDepsNode *DepsgraphNodeBuilder::add_operation_node(
265         ID *id,
266         eDepsNode_Type comp_type,
267         eDepsOperation_Type optype,
268         DepsEvalOperationCb op,
269         eDepsOperation_Code opcode,
270         const char *name,
271         int name_tag)
272 {
273         return add_operation_node(id,
274                                   comp_type,
275                                   "",
276                                   optype,
277                                   op,
278                                   opcode,
279                                   name,
280                                   name_tag);
281 }
282
283 bool DepsgraphNodeBuilder::has_operation_node(ID *id,
284                                               eDepsNode_Type comp_type,
285                                               const char *comp_name,
286                                               eDepsOperation_Code opcode,
287                                               const char *name,
288                                               int name_tag)
289 {
290         return find_operation_node(id,
291                                    comp_type,
292                                    comp_name,
293                                    opcode,
294                                    name,
295                                    name_tag) != NULL;
296 }
297
298 OperationDepsNode *DepsgraphNodeBuilder::find_operation_node(
299         ID *id,
300         eDepsNode_Type comp_type,
301         const char *comp_name,
302         eDepsOperation_Code opcode,
303         const char *name,
304         int name_tag)
305 {
306         ComponentDepsNode *comp_node = add_component_node(id, comp_type, comp_name);
307         return comp_node->has_operation(opcode, name, name_tag);
308 }
309
310 OperationDepsNode *DepsgraphNodeBuilder::find_operation_node(
311         ID *id,
312         eDepsNode_Type comp_type,
313         eDepsOperation_Code opcode,
314         const char *name,
315         int name_tag)
316 {
317         return find_operation_node(id, comp_type, "", opcode, name, name_tag);
318 }
319
320 /* **** Build functions for entity nodes **** */
321
322 void DepsgraphNodeBuilder::begin_build(Main *bmain) {
323         /* LIB_TAG_DOIT is used to indicate whether node for given ID was already
324          * created or not. This flag is being set in add_id_node(), so functions
325          * shouldn't bother with setting it, they only might query this flag when
326          * needed.
327          */
328         BKE_main_id_tag_all(bmain, LIB_TAG_DOIT, false);
329         /* XXX nested node trees are not included in tag-clearing above,
330          * so we need to do this manually.
331          */
332         FOREACH_NODETREE(bmain, nodetree, id) {
333                 if (id != (ID *)nodetree) {
334                         nodetree->id.tag &= ~LIB_TAG_DOIT;
335                 }
336         } FOREACH_NODETREE_END
337 }
338
339 void DepsgraphNodeBuilder::build_group(Scene *scene, Group *group)
340 {
341         ID *group_id = &group->id;
342         if (group_id->tag & LIB_TAG_DOIT) {
343                 return;
344         }
345         group_id->tag |= LIB_TAG_DOIT;
346
347         LINKLIST_FOREACH (GroupObject *, go, &group->gobject) {
348                 build_object(scene, go->ob);
349         }
350 }
351
352 SubgraphDepsNode *DepsgraphNodeBuilder::build_subgraph(Group *group)
353 {
354         /* sanity checks */
355         if (!group)
356                 return NULL;
357
358         /* create new subgraph's data */
359         Depsgraph *subgraph = reinterpret_cast<Depsgraph *>(DEG_graph_new());
360
361         DepsgraphNodeBuilder subgraph_builder(m_bmain, subgraph);
362
363         /* add group objects */
364         LINKLIST_FOREACH (GroupObject *, go, &group->gobject) {
365                 /*Object *ob = go->ob;*/
366
367                 /* Each "group object" is effectively a separate instance of the
368                  * underlying object data. When the group is evaluated, the transform
369                  * results and/or some other attributes end up getting overridden by
370                  * the group.
371                  */
372         }
373
374         /* Create a node for representing subgraph. */
375         SubgraphDepsNode *subgraph_node = m_graph->add_subgraph_node(&group->id);
376         subgraph_node->graph = subgraph;
377
378         /* Make a copy of the data this node will need? */
379         /* XXX: do we do this now, or later? */
380         /* TODO: need API function which queries graph's ID's hash, and duplicates
381          * those blocks thoroughly with all outside links removed.
382          */
383
384         return subgraph_node;
385 }
386
387 void DepsgraphNodeBuilder::build_object(Scene *scene, Object *ob)
388 {
389         /* Skip rest of components if the ID node was already there. */
390         if (ob->id.tag & LIB_TAG_DOIT) {
391                 return;
392         }
393         ob->id.tag |= LIB_TAG_DOIT;
394
395         /* Create ID node for obejct and begin init. */
396         IDDepsNode *id_node = add_id_node(&ob->id);
397         ob->customdata_mask = 0;
398
399         /* Standard components. */
400         build_object_transform(scene, ob);
401
402         if (ob->parent != NULL) {
403                 build_object(scene, ob->parent);
404         }
405         if (ob->modifiers.first != NULL) {
406                 BuilderWalkUserData data;
407                 data.builder = this;
408                 data.scene = scene;
409                 modifiers_foreachObjectLink(ob, modifier_walk, &data);
410         }
411         if (ob->constraints.first != NULL) {
412                 BuilderWalkUserData data;
413                 data.builder = this;
414                 data.scene = scene;
415                 modifiers_foreachObjectLink(ob, modifier_walk, &data);
416                 BKE_constraints_id_loop(&ob->constraints, constraint_walk, &data);
417         }
418
419         /* Object data. */
420         if (ob->data != NULL) {
421                 /* type-specific data... */
422                 switch (ob->type) {
423                         case OB_MESH:     /* Geometry */
424                         case OB_CURVE:
425                         case OB_FONT:
426                         case OB_SURF:
427                         case OB_MBALL:
428                         case OB_LATTICE:
429                                 build_obdata_geom(scene, ob);
430                                 /* TODO(sergey): Only for until we support granular
431                                  * update of curves.
432                                  */
433                                 if (ob->type == OB_FONT) {
434                                         Curve *curve = (Curve *)ob->data;
435                                         if (curve->textoncurve) {
436                                                 id_node->eval_flags |= DAG_EVAL_NEED_CURVE_PATH;
437                                         }
438                                 }
439                                 break;
440
441                         case OB_ARMATURE: /* Pose */
442                                 if (ID_IS_LINKED_DATABLOCK(ob) && 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                         default:
459                         {
460                                 ID *obdata = (ID *)ob->data;
461                                 if ((obdata->tag & LIB_TAG_DOIT) == 0) {
462                                         build_animdata(obdata);
463                                 }
464                                 break;
465                         }
466                 }
467         }
468
469         /* Build animation data,
470          *
471          * Do it now because it's possible object data will affect
472          * on object's level animation, for example in case of rebuilding
473          * pose for proxy.
474          */
475         build_animdata(&ob->id);
476
477         /* particle systems */
478         if (ob->particlesystem.first != NULL) {
479                 build_particles(scene, ob);
480         }
481
482         /* Grease pencil. */
483         if (ob->gpd != NULL) {
484                 build_gpencil(ob->gpd);
485         }
486
487         /* Object that this is a proxy for. */
488         if (ob->proxy) {
489                 ob->proxy->proxy_from = ob;
490                 build_object(scene, ob->proxy);
491         }
492
493         /* Object dupligroup. */
494         if (ob->dup_group != NULL) {
495                 build_group(scene, ob->dup_group);
496         }
497 }
498
499 void DepsgraphNodeBuilder::build_object_transform(Scene *scene, Object *ob)
500 {
501         /* local transforms (from transform channels - loc/rot/scale + deltas) */
502         add_operation_node(&ob->id, DEPSNODE_TYPE_TRANSFORM,
503                            DEPSOP_TYPE_INIT, function_bind(BKE_object_eval_local_transform, _1, scene, ob),
504                            DEG_OPCODE_TRANSFORM_LOCAL);
505
506         /* object parent */
507         if (ob->parent) {
508                 add_operation_node(&ob->id, DEPSNODE_TYPE_TRANSFORM,
509                                    DEPSOP_TYPE_EXEC, function_bind(BKE_object_eval_parent, _1, scene, ob),
510                                    DEG_OPCODE_TRANSFORM_PARENT);
511         }
512
513         /* object constraints */
514         if (ob->constraints.first) {
515                 build_object_constraints(scene, ob);
516         }
517
518         /* Temporary uber-update node, which does everything.
519          * It is for the being we're porting old dependencies into the new system.
520          * We'll get rid of this node as soon as all the granular update functions
521          * are filled in.
522          *
523          * TODO(sergey): Get rid of this node.
524          */
525         add_operation_node(&ob->id, DEPSNODE_TYPE_TRANSFORM,
526                            DEPSOP_TYPE_EXEC, function_bind(BKE_object_eval_uber_transform, _1, scene, ob),
527                            DEG_OPCODE_OBJECT_UBEREVAL);
528
529         /* object transform is done */
530         add_operation_node(&ob->id, DEPSNODE_TYPE_TRANSFORM,
531                            DEPSOP_TYPE_POST, function_bind(BKE_object_eval_done, _1, ob),
532                            DEG_OPCODE_TRANSFORM_FINAL);
533 }
534
535 /**
536  * Constraints Graph Notes
537  *
538  * For constraints, we currently only add a operation node to the Transform
539  * or Bone components (depending on whichever type of owner we have).
540  * This represents the entire constraints stack, which is for now just
541  * executed as a single monolithic block. At least initially, this should
542  * be sufficient for ensuring that the porting/refactoring process remains
543  * manageable.
544  *
545  * However, when the time comes for developing "node-based" constraints,
546  * we'll need to split this up into pre/post nodes for "constraint stack
547  * evaluation" + operation nodes for each constraint (i.e. the contents
548  * of the loop body used in the current "solve_constraints()" operation).
549  *
550  * -- Aligorith, August 2013
551  */
552 void DepsgraphNodeBuilder::build_object_constraints(Scene *scene, Object *ob)
553 {
554         /* create node for constraint stack */
555         add_operation_node(&ob->id, DEPSNODE_TYPE_TRANSFORM,
556                            DEPSOP_TYPE_EXEC, function_bind(BKE_object_eval_constraints, _1, scene, ob),
557                            DEG_OPCODE_TRANSFORM_CONSTRAINTS);
558 }
559
560 /**
561  * Build graph nodes for AnimData block
562  * \param id: ID-Block which hosts the AnimData
563  */
564 void DepsgraphNodeBuilder::build_animdata(ID *id)
565 {
566         AnimData *adt = BKE_animdata_from_id(id);
567
568         if (adt == NULL)
569                 return;
570
571         /* animation */
572         if (adt->action || adt->nla_tracks.first || adt->drivers.first) {
573                 // XXX: Hook up specific update callbacks for special properties which may need it...
574
575                 /* actions and NLA - as a single unit for now, as it gets complicated to schedule otherwise */
576                 if ((adt->action) || (adt->nla_tracks.first)) {
577                         /* create the node */
578                         add_operation_node(id, DEPSNODE_TYPE_ANIMATION,
579                                            DEPSOP_TYPE_EXEC, function_bind(BKE_animsys_eval_animdata, _1, id),
580                                            DEG_OPCODE_ANIMATION, id->name);
581
582                         // TODO: for each channel affected, we might also want to add some support for running RNA update callbacks on them
583                         // (which will be needed for proper handling of drivers later)
584                 }
585
586                 /* drivers */
587                 LINKLIST_FOREACH (FCurve *, fcu, &adt->drivers) {
588                         /* create driver */
589                         build_driver(id, fcu);
590                 }
591         }
592 }
593
594 /**
595  * Build graph node(s) for Driver
596  * \param id: ID-Block that driver is attached to
597  * \param fcu: Driver-FCurve
598  */
599 OperationDepsNode *DepsgraphNodeBuilder::build_driver(ID *id, FCurve *fcu)
600 {
601         ChannelDriver *driver = fcu->driver;
602
603         /* Create data node for this driver */
604         /* TODO(sergey): Avoid creating same operation multiple times,
605          * in the future we need to avoid lookup of the operation as well
606          * and use some tagging magic instead.
607          */
608         OperationDepsNode *driver_op = find_operation_node(id,
609                                                            DEPSNODE_TYPE_PARAMETERS,
610                                                            DEG_OPCODE_DRIVER,
611                                                            fcu->rna_path,
612                                                            fcu->array_index);
613
614         if (driver_op == NULL) {
615                 driver_op = add_operation_node(id,
616                                                DEPSNODE_TYPE_PARAMETERS,
617                                                DEPSOP_TYPE_EXEC,
618                                                function_bind(BKE_animsys_eval_driver, _1, id, fcu),
619                                                DEG_OPCODE_DRIVER,
620                                                fcu->rna_path,
621                                                fcu->array_index);
622         }
623
624         /* tag "scripted expression" drivers as needing Python (due to GIL issues, etc.) */
625         if (driver->type == DRIVER_TYPE_PYTHON) {
626                 driver_op->flag |= DEPSOP_FLAG_USES_PYTHON;
627         }
628
629         /* return driver node created */
630         return driver_op;
631 }
632
633 /* Recursively build graph for world */
634 void DepsgraphNodeBuilder::build_world(World *world)
635 {
636         ID *world_id = &world->id;
637         if (world_id->tag & LIB_TAG_DOIT) {
638                 return;
639         }
640
641         /* world itself */
642         add_id_node(world_id); /* world shading/params? */
643
644         build_animdata(world_id);
645
646         /* TODO: other settings? */
647
648         /* textures */
649         build_texture_stack(world->mtex);
650
651         /* world's nodetree */
652         if (world->nodetree) {
653                 build_nodetree(world->nodetree);
654         }
655 }
656
657 /* Rigidbody Simulation - Scene Level */
658 void DepsgraphNodeBuilder::build_rigidbody(Scene *scene)
659 {
660         RigidBodyWorld *rbw = scene->rigidbody_world;
661
662         /**
663          * Rigidbody Simulation Nodes
664          * ==========================
665          *
666          * There are 3 nodes related to Rigidbody Simulation:
667          * 1) "Initialize/Rebuild World" - this is called sparingly, only when the simulation
668          *    needs to be rebuilt (mainly after file reload, or moving back to start frame)
669          * 2) "Do Simulation" - perform a simulation step - interleaved between the evaluation
670          *    steps for clusters of objects (i.e. between those affected and/or not affected by
671          *    the sim for instance)
672          *
673          * 3) "Pull Results" - grab the specific transforms applied for a specific object -
674          *    performed as part of object's transform-stack building
675          */
676
677         /* create nodes ------------------------------------------------------------------------ */
678         /* XXX: is this the right component, or do we want to use another one instead? */
679
680         /* init/rebuild operation */
681         /*OperationDepsNode *init_node =*/ add_operation_node(&scene->id, DEPSNODE_TYPE_TRANSFORM,
682                                                               DEPSOP_TYPE_REBUILD, function_bind(BKE_rigidbody_rebuild_sim, _1, scene),
683                                                               DEG_OPCODE_RIGIDBODY_REBUILD);
684
685         /* do-sim operation */
686         // XXX: what happens if we need to split into several groups?
687         OperationDepsNode *sim_node     = add_operation_node(&scene->id, DEPSNODE_TYPE_TRANSFORM,
688                                                              DEPSOP_TYPE_SIM, function_bind(BKE_rigidbody_eval_simulation, _1, scene),
689                                                              DEG_OPCODE_RIGIDBODY_SIM);
690
691         /* XXX: For now, the sim node is the only one that really matters here. If any other
692          * sims get added later, we may have to remove these hacks...
693          */
694         sim_node->owner->entry_operation = sim_node;
695         sim_node->owner->exit_operation  = sim_node;
696
697
698         /* objects - simulation participants */
699         if (rbw->group) {
700                 LINKLIST_FOREACH (GroupObject *, go, &rbw->group->gobject) {
701                         Object *ob = go->ob;
702
703                         if (!ob || (ob->type != OB_MESH))
704                                 continue;
705
706                         /* 2) create operation for flushing results */
707                         /* object's transform component - where the rigidbody operation lives */
708                         add_operation_node(&ob->id, DEPSNODE_TYPE_TRANSFORM,
709                                            DEPSOP_TYPE_EXEC, function_bind(BKE_rigidbody_object_sync_transforms, _1, scene, ob),
710                                            DEG_OPCODE_TRANSFORM_RIGIDBODY);
711                 }
712         }
713 }
714
715 void DepsgraphNodeBuilder::build_particles(Scene *scene, Object *ob)
716 {
717         /**
718          * Particle Systems Nodes
719          * ======================
720          *
721          * There are two types of nodes associated with representing
722          * particle systems:
723          *  1) Component (EVAL_PARTICLES) - This is the particle-system
724          *     evaluation context for an object. It acts as the container
725          *     for all the nodes associated with a particular set of particle
726          *     systems.
727          *  2) Particle System Eval Operation - This operation node acts as a
728          *     blackbox evaluation step for one particle system referenced by
729          *     the particle systems stack. All dependencies link to this operation.
730          */
731
732         /* component for all particle systems */
733         ComponentDepsNode *psys_comp =
734                 add_component_node(&ob->id, DEPSNODE_TYPE_EVAL_PARTICLES);
735
736         /* particle systems */
737         LINKLIST_FOREACH (ParticleSystem *, psys, &ob->particlesystem) {
738                 ParticleSettings *part = psys->part;
739
740                 /* particle settings */
741                 // XXX: what if this is used more than once!
742                 build_animdata(&part->id);
743
744                 /* this particle system */
745                 // TODO: for now, this will just be a placeholder "ubereval" node
746                 add_operation_node(psys_comp,
747                                    DEPSOP_TYPE_EXEC,
748                                    function_bind(BKE_particle_system_eval,
749                                                  _1,
750                                                  scene,
751                                                  ob,
752                                                  psys),
753                                    DEG_OPCODE_PSYS_EVAL,
754                                    psys->name);
755         }
756
757         /* pointcache */
758         // TODO...
759 }
760
761 void DepsgraphNodeBuilder::build_cloth(Scene *scene, Object *object)
762 {
763         ComponentDepsNode *cache_comp = add_component_node(&object->id,
764                                                            DEPSNODE_TYPE_CACHE);
765         add_operation_node(cache_comp,
766                            DEPSOP_TYPE_EXEC,
767                            function_bind(BKE_object_eval_cloth,
768                                          _1,
769                                          scene,
770                                          object),
771                            DEG_OPCODE_PLACEHOLDER,
772                            "Cloth Modifier");
773 }
774
775 /* Shapekeys */
776 void DepsgraphNodeBuilder::build_shapekeys(Key *key)
777 {
778         build_animdata(&key->id);
779
780         add_operation_node(&key->id, DEPSNODE_TYPE_GEOMETRY, DEPSOP_TYPE_EXEC, NULL,
781                            DEG_OPCODE_PLACEHOLDER, "Shapekey Eval");
782 }
783
784 /* ObData Geometry Evaluation */
785 // XXX: what happens if the datablock is shared!
786 void DepsgraphNodeBuilder::build_obdata_geom(Scene *scene, Object *ob)
787 {
788         ID *obdata = (ID *)ob->data;
789
790         /* TODO(sergey): This way using this object's properties as driver target
791          * works fine.
792          *
793          * Does this depend on other nodes?
794          */
795         add_operation_node(&ob->id,
796                            DEPSNODE_TYPE_PARAMETERS,
797                            DEPSOP_TYPE_POST,
798                            NULL,
799                            DEG_OPCODE_PLACEHOLDER,
800                            "Parameters Eval");
801
802         /* Temporary uber-update node, which does everything.
803          * It is for the being we're porting old dependencies into the new system.
804          * We'll get rid of this node as soon as all the granular update functions
805          * are filled in.
806          *
807          * TODO(sergey): Get rid of this node.
808          */
809         add_operation_node(&ob->id,
810                            DEPSNODE_TYPE_GEOMETRY,
811                            DEPSOP_TYPE_POST,
812                            function_bind(BKE_object_eval_uber_data, _1, scene, ob),
813                            DEG_OPCODE_GEOMETRY_UBEREVAL);
814
815         add_operation_node(&ob->id,
816                            DEPSNODE_TYPE_GEOMETRY,
817                            DEPSOP_TYPE_INIT,
818                            NULL,
819                            DEG_OPCODE_PLACEHOLDER,
820                            "Eval Init");
821
822         // TODO: "Done" operation
823
824         /* Modifiers */
825         LINKLIST_FOREACH (ModifierData *, md, &ob->modifiers) {
826                 add_operation_node(&ob->id,
827                                    DEPSNODE_TYPE_GEOMETRY,
828                                    DEPSOP_TYPE_EXEC,
829                                    function_bind(BKE_object_eval_modifier,
830                                                  _1,
831                                                  scene,
832                                                  ob,
833                                                  md),
834                                    DEG_OPCODE_GEOMETRY_MODIFIER,
835                                    md->name);
836                 if (md->type == eModifierType_Cloth) {
837                         build_cloth(scene, ob);
838                 }
839         }
840
841         /* materials */
842         for (int a = 1; a <= ob->totcol; a++) {
843                 Material *ma = give_current_material(ob, a);
844                 if (ma != NULL) {
845                         build_material(ma);
846                 }
847         }
848
849         /* geometry collision */
850         if (ELEM(ob->type, OB_MESH, OB_CURVE, OB_LATTICE)) {
851                 // add geometry collider relations
852         }
853
854         if (obdata->tag & LIB_TAG_DOIT) {
855                 return;
856         }
857
858         /* ShapeKeys */
859         Key *key = BKE_key_from_object(ob);
860         if (key) {
861                 build_shapekeys(key);
862         }
863
864         build_animdata(obdata);
865
866         /* Nodes for result of obdata's evaluation, and geometry
867          * evaluation on object.
868          */
869         switch (ob->type) {
870                 case OB_MESH:
871                 {
872                         //Mesh *me = (Mesh *)ob->data;
873
874                         /* evaluation operations */
875                         add_operation_node(obdata,
876                                            DEPSNODE_TYPE_GEOMETRY,
877                                            DEPSOP_TYPE_INIT,
878                                            function_bind(BKE_mesh_eval_geometry,
879                                                          _1,
880                                                          (Mesh *)obdata),
881                                            DEG_OPCODE_PLACEHOLDER,
882                                            "Geometry Eval");
883                         break;
884                 }
885
886                 case OB_MBALL:
887                 {
888                         Object *mom = BKE_mball_basis_find(scene, ob);
889
890                         /* Motherball - mom depends on children! */
891                         if (mom == ob) {
892                                 /* metaball evaluation operations */
893                                 /* NOTE: only the motherball gets evaluated! */
894                                 add_operation_node(obdata,
895                                                    DEPSNODE_TYPE_GEOMETRY,
896                                                    DEPSOP_TYPE_INIT,
897                                                    function_bind(BKE_mball_eval_geometry,
898                                                                  _1,
899                                                                  (MetaBall *)obdata),
900                                                    DEG_OPCODE_PLACEHOLDER,
901                                                    "Geometry Eval");
902                         }
903                         break;
904                 }
905
906                 case OB_CURVE:
907                 case OB_SURF:
908                 case OB_FONT:
909                 {
910                         /* Curve/nurms evaluation operations. */
911                         /* - calculate curve geometry (including path) */
912                         add_operation_node(obdata,
913                                            DEPSNODE_TYPE_GEOMETRY,
914                                            DEPSOP_TYPE_INIT,
915                                            function_bind(BKE_curve_eval_geometry,
916                                                          _1,
917                                                          (Curve *)obdata),
918                                            DEG_OPCODE_PLACEHOLDER,
919                                            "Geometry Eval");
920
921                         /* Calculate curve path - this is used by constraints, etc. */
922                         if (ELEM(ob->type, OB_CURVE, OB_FONT)) {
923                                 add_operation_node(obdata,
924                                                    DEPSNODE_TYPE_GEOMETRY,
925                                                    DEPSOP_TYPE_EXEC,
926                                                    function_bind(BKE_curve_eval_path,
927                                                                  _1,
928                                                                  (Curve *)obdata),
929                                                    DEG_OPCODE_GEOMETRY_PATH,
930                                                    "Path");
931                         }
932
933                         /* Make sure objects used for bevel.taper are in the graph.
934                          * NOTE: This objects might be not linked to the scene.
935                          */
936                         Curve *cu = (Curve *)obdata;
937                         if (cu->bevobj != NULL) {
938                                 build_object(scene, cu->bevobj);
939                         }
940                         if (cu->taperobj != NULL) {
941                                 build_object(scene, cu->taperobj);
942                         }
943                         if (ob->type == OB_FONT && cu->textoncurve != NULL) {
944                                 build_object(scene, cu->textoncurve);
945                         }
946                         break;
947                 }
948
949                 case OB_LATTICE:
950                 {
951                         /* Lattice evaluation operations. */
952                         add_operation_node(obdata,
953                                            DEPSNODE_TYPE_GEOMETRY,
954                                            DEPSOP_TYPE_INIT,
955                                            function_bind(BKE_lattice_eval_geometry,
956                                                          _1,
957                                                          (Lattice *)obdata),
958                                            DEG_OPCODE_PLACEHOLDER,
959                                            "Geometry Eval");
960                         break;
961                 }
962         }
963
964         add_operation_node(obdata, DEPSNODE_TYPE_GEOMETRY,
965                            DEPSOP_TYPE_POST, NULL,
966                            DEG_OPCODE_PLACEHOLDER, "Eval Done");
967
968         /* Parameters for driver sources. */
969         add_operation_node(obdata, DEPSNODE_TYPE_PARAMETERS, DEPSOP_TYPE_EXEC, NULL,
970                            DEG_OPCODE_PLACEHOLDER, "Parameters Eval");
971 }
972
973 /* Cameras */
974 void DepsgraphNodeBuilder::build_camera(Object *ob)
975 {
976         /* TODO: Link scene-camera links in somehow... */
977         Camera *cam = (Camera *)ob->data;
978         ID *camera_id = &cam->id;
979         if (camera_id->tag & LIB_TAG_DOIT) {
980                 return;
981         }
982
983         build_animdata(&cam->id);
984
985         add_operation_node(camera_id, DEPSNODE_TYPE_PARAMETERS, DEPSOP_TYPE_EXEC, NULL,
986                            DEG_OPCODE_PLACEHOLDER, "Parameters Eval");
987
988         if (cam->dof_ob != NULL) {
989                 /* TODO(sergey): For now parametrs are on object level. */
990                 add_operation_node(&ob->id, DEPSNODE_TYPE_PARAMETERS,
991                                    DEPSOP_TYPE_EXEC, NULL,
992                                    DEG_OPCODE_PLACEHOLDER,
993                                    "Camera DOF");
994         }
995 }
996
997 /* Lamps */
998 void DepsgraphNodeBuilder::build_lamp(Object *ob)
999 {
1000         Lamp *la = (Lamp *)ob->data;
1001         ID *lamp_id = &la->id;
1002         if (lamp_id->tag & LIB_TAG_DOIT) {
1003                 return;
1004         }
1005
1006         build_animdata(&la->id);
1007
1008         /* node for obdata */
1009         add_component_node(lamp_id, DEPSNODE_TYPE_PARAMETERS);
1010
1011         /* TODO(sergey): Is it really how we're supposed to work with drivers? */
1012         add_operation_node(lamp_id, DEPSNODE_TYPE_PARAMETERS, DEPSOP_TYPE_EXEC, NULL,
1013                            DEG_OPCODE_PLACEHOLDER, "Parameters Eval");
1014
1015         /* lamp's nodetree */
1016         if (la->nodetree) {
1017                 build_nodetree(la->nodetree);
1018         }
1019
1020         /* textures */
1021         build_texture_stack(la->mtex);
1022 }
1023
1024 void DepsgraphNodeBuilder::build_nodetree(bNodeTree *ntree)
1025 {
1026         if (!ntree)
1027                 return;
1028
1029         /* nodetree itself */
1030         ID *ntree_id = &ntree->id;
1031
1032         build_animdata(ntree_id);
1033
1034         /* Parameters for drivers. */
1035         add_operation_node(ntree_id, DEPSNODE_TYPE_PARAMETERS, DEPSOP_TYPE_POST, NULL,
1036                            DEG_OPCODE_PLACEHOLDER, "Parameters Eval");
1037
1038         /* nodetree's nodes... */
1039         LINKLIST_FOREACH (bNode *, bnode, &ntree->nodes) {
1040                 ID *id = bnode->id;
1041                 if (id != NULL) {
1042                         short id_type = GS(id->name);
1043                         if (id_type == ID_MA) {
1044                                 build_material((Material *)id);
1045                         }
1046                         else if (id_type == ID_TE) {
1047                                 build_texture((Tex *)id);
1048                         }
1049                         else if (id_type == ID_IM) {
1050                                 build_image((Image *)id);
1051                         }
1052                         else if (bnode->type == NODE_GROUP) {
1053                                 bNodeTree *group_ntree = (bNodeTree *)id;
1054                                 if ((group_ntree->id.tag & LIB_TAG_DOIT) == 0) {
1055                                         build_nodetree(group_ntree);
1056                                 }
1057                         }
1058                 }
1059         }
1060
1061         // TODO: link from nodetree to owner_component?
1062 }
1063
1064 /* Recursively build graph for material */
1065 void DepsgraphNodeBuilder::build_material(Material *ma)
1066 {
1067         ID *ma_id = &ma->id;
1068         if (ma_id->tag & LIB_TAG_DOIT) {
1069                 return;
1070         }
1071
1072         /* material itself */
1073         add_id_node(ma_id);
1074
1075         add_operation_node(ma_id, DEPSNODE_TYPE_SHADING,
1076                            DEPSOP_TYPE_EXEC, NULL,
1077                            DEG_OPCODE_PLACEHOLDER, "Material Update");
1078
1079         /* material animation */
1080         build_animdata(ma_id);
1081
1082         /* textures */
1083         build_texture_stack(ma->mtex);
1084
1085         /* material's nodetree */
1086         build_nodetree(ma->nodetree);
1087 }
1088
1089 /* Texture-stack attached to some shading datablock */
1090 void DepsgraphNodeBuilder::build_texture_stack(MTex **texture_stack)
1091 {
1092         int i;
1093
1094         /* for now assume that all texture-stacks have same number of max items */
1095         for (i = 0; i < MAX_MTEX; i++) {
1096                 MTex *mtex = texture_stack[i];
1097                 if (mtex && mtex->tex)
1098                         build_texture(mtex->tex);
1099         }
1100 }
1101
1102 /* Recursively build graph for texture */
1103 void DepsgraphNodeBuilder::build_texture(Tex *tex)
1104 {
1105         ID *tex_id = &tex->id;
1106         if (tex_id->tag & LIB_TAG_DOIT) {
1107                 return;
1108         }
1109         tex_id->tag |= LIB_TAG_DOIT;
1110         /* Texture itself. */
1111         build_animdata(tex_id);
1112         /* Texture's nodetree. */
1113         build_nodetree(tex->nodetree);
1114         /* Special cases for different IDs which texture uses. */
1115         if (tex->type == TEX_IMAGE) {
1116                 if (tex->ima != NULL) {
1117                         build_image(tex->ima);
1118                 }
1119         }
1120 }
1121
1122 void DepsgraphNodeBuilder::build_image(Image *image) {
1123         ID *image_id = &image->id;
1124         if (image_id->tag & LIB_TAG_DOIT) {
1125                 return;
1126         }
1127         image_id->tag |= LIB_TAG_DOIT;
1128         /* Image ID node itself. */
1129         add_id_node(image_id);
1130         /* Placeholder so we can add relations and tag ID node for update. */
1131         add_operation_node(image_id,
1132                            DEPSNODE_TYPE_PARAMETERS,
1133                            DEPSOP_TYPE_EXEC,
1134                            NULL,
1135                            DEG_OPCODE_PLACEHOLDER,
1136                            "Image Eval");
1137 }
1138
1139 void DepsgraphNodeBuilder::build_compositor(Scene *scene)
1140 {
1141         /* For now, just a plain wrapper? */
1142         // TODO: create compositing component?
1143         // XXX: component type undefined!
1144         //graph->get_node(&scene->id, NULL, DEPSNODE_TYPE_COMPOSITING, NULL);
1145
1146         /* for now, nodetrees are just parameters; compositing occurs in internals of renderer... */
1147         add_component_node(&scene->id, DEPSNODE_TYPE_PARAMETERS);
1148         build_nodetree(scene->nodetree);
1149 }
1150
1151 void DepsgraphNodeBuilder::build_gpencil(bGPdata *gpd)
1152 {
1153         ID *gpd_id = &gpd->id;
1154
1155         /* gpencil itself */
1156         // XXX: what about multiple users of same datablock? This should only get added once
1157         add_id_node(gpd_id);
1158
1159         /* The main reason Grease Pencil is included here is because the animation (and drivers)
1160          * need to be hosted somewhere...
1161          */
1162         build_animdata(gpd_id);
1163 }
1164
1165 void DepsgraphNodeBuilder::build_cachefile(CacheFile *cache_file)
1166 {
1167         ID *cache_file_id = &cache_file->id;
1168
1169         add_component_node(cache_file_id, DEPSNODE_TYPE_CACHE);
1170         add_operation_node(cache_file_id, DEPSNODE_TYPE_CACHE,
1171                            DEPSOP_TYPE_EXEC, NULL,
1172                            DEG_OPCODE_PLACEHOLDER, "Cache File Update");
1173
1174         add_id_node(cache_file_id);
1175         build_animdata(cache_file_id);
1176 }
1177
1178 void DepsgraphNodeBuilder::build_mask(Mask *mask)
1179 {
1180         ID *mask_id = &mask->id;
1181         add_id_node(mask_id);
1182         build_animdata(mask_id);
1183 }
1184
1185 void DepsgraphNodeBuilder::build_movieclip(MovieClip *clip) {
1186         ID *clip_id = &clip->id;
1187         add_id_node(clip_id);
1188         build_animdata(clip_id);
1189 }
1190
1191 }  // namespace DEG