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