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