Merge branch 'master' into blender2.8
[blender.git] / source / blender / depsgraph / intern / builder / deg_builder_nodes.cc
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2013 Blender Foundation.
19  * All rights reserved.
20  *
21  * Original Author: Joshua Leung
22  * Contributor(s): Based on original depsgraph.c code - Blender Foundation (2005-2013)
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/depsgraph/intern/builder/deg_builder_nodes.cc
28  *  \ingroup depsgraph
29  *
30  * Methods for constructing depsgraph's nodes
31  */
32
33 #include "intern/builder/deg_builder_nodes.h"
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #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 that this is a proxy for */
298                 // XXX: the way that proxies work needs to be completely reviewed!
299                 if (ob->proxy) {
300                         ob->proxy->proxy_from = ob;
301                 }
302
303                 /* object itself */
304                 build_object(scene, base, ob);
305
306                 /* Object dupligroup. */
307                 if (ob->dup_group) {
308                         build_group(scene, base, ob->dup_group);
309                 }
310         }
311
312         /* rigidbody */
313         if (scene->rigidbody_world) {
314                 build_rigidbody(scene);
315         }
316
317         /* scene's animation and drivers */
318         if (scene->adt) {
319                 build_animdata(&scene->id);
320         }
321
322         /* world */
323         if (scene->world) {
324                 build_world(scene->world);
325         }
326
327         /* compo nodes */
328         if (scene->nodetree) {
329                 build_compositor(scene);
330         }
331
332         /* sequencer */
333         // XXX...
334
335         /* grease pencil */
336         if (scene->gpd) {
337                 build_gpencil(scene->gpd);
338         }
339 }
340
341 void DepsgraphNodeBuilder::build_group(Scene *scene,
342                                        Base *base,
343                                        Group *group)
344 {
345         ID *group_id = &group->id;
346         if (group_id->tag & LIB_TAG_DOIT) {
347                 return;
348         }
349         group_id->tag |= LIB_TAG_DOIT;
350
351         for (GroupObject *go = (GroupObject *)group->gobject.first;
352              go != NULL;
353              go = go->next)
354         {
355                 build_object(scene, base, go->ob);
356         }
357 }
358
359 SubgraphDepsNode *DepsgraphNodeBuilder::build_subgraph(Group *group)
360 {
361         /* sanity checks */
362         if (!group)
363                 return NULL;
364
365         /* create new subgraph's data */
366         Depsgraph *subgraph = reinterpret_cast<Depsgraph *>(DEG_graph_new());
367
368         DepsgraphNodeBuilder subgraph_builder(m_bmain, subgraph);
369
370         /* add group objects */
371         for (GroupObject *go = (GroupObject *)group->gobject.first;
372              go != NULL;
373              go = go->next)
374         {
375                 /*Object *ob = go->ob;*/
376
377                 /* Each "group object" is effectively a separate instance of the underlying
378                  * object data. When the group is evaluated, the transform results and/or
379                  * some other attributes end up getting overridden by the group
380                  */
381         }
382
383         /* create a node for representing subgraph */
384         SubgraphDepsNode *subgraph_node = m_graph->add_subgraph_node(&group->id);
385         subgraph_node->graph = subgraph;
386
387         /* make a copy of the data this node will need? */
388         // XXX: do we do this now, or later?
389         // TODO: need API function which queries graph's ID's hash, and duplicates those blocks thoroughly with all outside links removed...
390
391         return subgraph_node;
392 }
393
394 void DepsgraphNodeBuilder::build_object(Scene *scene, Base *base, Object *ob)
395 {
396         if (ob->id.tag & LIB_TAG_DOIT) {
397                 IDDepsNode *id_node = m_graph->find_id_node(&ob->id);
398                 id_node->layers = base->lay;
399                 return;
400         }
401
402         IDDepsNode *id_node = add_id_node(&ob->id);
403         id_node->layers = base->lay;
404         ob->customdata_mask = 0;
405
406         /* standard components */
407         build_object_transform(scene, ob);
408
409         /* object data */
410         if (ob->data) {
411                 /* type-specific data... */
412                 switch (ob->type) {
413                         case OB_MESH:     /* Geometry */
414                         case OB_CURVE:
415                         case OB_FONT:
416                         case OB_SURF:
417                         case OB_MBALL:
418                         case OB_LATTICE:
419                         {
420                                 /* TODO(sergey): This way using this object's
421                                  * properties as driver target works fine.
422                                  *
423                                  * Does this depend on other nodes?
424                                  */
425                                 add_operation_node(&ob->id, DEPSNODE_TYPE_PARAMETERS, DEPSOP_TYPE_POST, NULL,
426                                                    DEG_OPCODE_PLACEHOLDER, "Parameters Eval");
427
428                                 build_obdata_geom(scene, ob);
429                                 /* TODO(sergey): Only for until we support granular
430                                  * update of curves.
431                                  */
432                                 if (ob->type == OB_FONT) {
433                                         Curve *curve = (Curve *)ob->data;
434                                         if (curve->textoncurve) {
435                                                 id_node->eval_flags |= DAG_EVAL_NEED_CURVE_PATH;
436                                         }
437                                 }
438                                 break;
439                         }
440
441                         case OB_ARMATURE: /* Pose */
442                                 if (ob->id.lib != NULL && ob->proxy_from != NULL) {
443                                         build_proxy_rig(ob);
444                                 }
445                                 else {
446                                         build_rig(scene, ob);
447                                 }
448                                 break;
449
450                         case OB_LAMP:   /* Lamp */
451                                 build_lamp(ob);
452                                 break;
453
454                         case OB_CAMERA: /* Camera */
455                                 build_camera(ob);
456                                 break;
457
458                         default:
459                         {
460                                 ID *obdata = (ID *)ob->data;
461                                 if ((obdata->tag & LIB_TAG_DOIT) == 0) {
462                                         build_animdata(obdata);
463                                 }
464                                 break;
465                         }
466                 }
467         }
468
469         /* Build animation data,
470          *
471          * Do it now because it's possible object data will affect
472          * on object's level animation, for example in case of rebuilding
473          * pose for proxy.
474          */
475         build_animdata(&ob->id);
476
477         /* grease pencil */
478         if (ob->gpd) {
479                 build_gpencil(ob->gpd);
480         }
481
482         if (ob->proxy != NULL) {
483                 add_operation_node(&ob->id, DEPSNODE_TYPE_PROXY, DEPSOP_TYPE_POST,
484                                    function_bind(BKE_object_eval_proxy_backlink, _1, ob),
485                                    DEG_OPCODE_PLACEHOLDER, "Parameters Eval");
486         }
487 }
488
489 void DepsgraphNodeBuilder::build_object_transform(Scene *scene, Object *ob)
490 {
491         /* local transforms (from transform channels - loc/rot/scale + deltas) */
492         add_operation_node(&ob->id, DEPSNODE_TYPE_TRANSFORM,
493                            DEPSOP_TYPE_INIT, function_bind(BKE_object_eval_local_transform, _1, scene, ob),
494                            DEG_OPCODE_TRANSFORM_LOCAL);
495
496         /* object parent */
497         if (ob->parent) {
498                 add_operation_node(&ob->id, DEPSNODE_TYPE_TRANSFORM,
499                                    DEPSOP_TYPE_EXEC, function_bind(BKE_object_eval_parent, _1, scene, ob),
500                                    DEG_OPCODE_TRANSFORM_PARENT);
501         }
502
503         /* object constraints */
504         if (ob->constraints.first) {
505                 build_object_constraints(scene, ob);
506         }
507
508         /* Temporary uber-update node, which does everything.
509          * It is for the being we're porting old dependencies into the new system.
510          * We'll get rid of this node as soon as all the granular update functions
511          * are filled in.
512          *
513          * TODO(sergey): Get rid of this node.
514          */
515         add_operation_node(&ob->id, DEPSNODE_TYPE_TRANSFORM,
516                            DEPSOP_TYPE_EXEC, function_bind(BKE_object_eval_uber_transform, _1, scene, ob),
517                            DEG_OPCODE_OBJECT_UBEREVAL);
518
519         /* object transform is done */
520         add_operation_node(&ob->id, DEPSNODE_TYPE_TRANSFORM,
521                            DEPSOP_TYPE_POST, function_bind(BKE_object_eval_done, _1, ob),
522                            DEG_OPCODE_TRANSFORM_FINAL);
523 }
524
525 /**
526  * Constraints Graph Notes
527  *
528  * For constraints, we currently only add a operation node to the Transform
529  * or Bone components (depending on whichever type of owner we have).
530  * This represents the entire constraints stack, which is for now just
531  * executed as a single monolithic block. At least initially, this should
532  * be sufficient for ensuring that the porting/refactoring process remains
533  * manageable.
534  *
535  * However, when the time comes for developing "node-based" constraints,
536  * we'll need to split this up into pre/post nodes for "constraint stack
537  * evaluation" + operation nodes for each constraint (i.e. the contents
538  * of the loop body used in the current "solve_constraints()" operation).
539  *
540  * -- Aligorith, August 2013
541  */
542 void DepsgraphNodeBuilder::build_object_constraints(Scene *scene, Object *ob)
543 {
544         /* create node for constraint stack */
545         add_operation_node(&ob->id, DEPSNODE_TYPE_TRANSFORM,
546                            DEPSOP_TYPE_EXEC, function_bind(BKE_object_eval_constraints, _1, scene, ob),
547                            DEG_OPCODE_TRANSFORM_CONSTRAINTS);
548 }
549
550 void DepsgraphNodeBuilder::build_pose_constraints(Object *ob, bPoseChannel *pchan)
551 {
552         /* create node for constraint stack */
553         add_operation_node(&ob->id, DEPSNODE_TYPE_BONE, pchan->name,
554                            DEPSOP_TYPE_EXEC, function_bind(BKE_pose_constraints_evaluate, _1, ob, pchan),
555                            DEG_OPCODE_BONE_CONSTRAINTS);
556 }
557
558 /**
559  * Build graph nodes for AnimData block
560  * \param id: ID-Block which hosts the AnimData
561  */
562 void DepsgraphNodeBuilder::build_animdata(ID *id)
563 {
564         AnimData *adt = BKE_animdata_from_id(id);
565
566         if (adt == NULL)
567                 return;
568
569         /* animation */
570         if (adt->action || adt->nla_tracks.first || adt->drivers.first) {
571                 // XXX: Hook up specific update callbacks for special properties which may need it...
572
573                 /* actions and NLA - as a single unit for now, as it gets complicated to schedule otherwise */
574                 if ((adt->action) || (adt->nla_tracks.first)) {
575                         /* create the node */
576                         add_operation_node(id, DEPSNODE_TYPE_ANIMATION,
577                                            DEPSOP_TYPE_EXEC, function_bind(BKE_animsys_eval_animdata, _1, id),
578                                            DEG_OPCODE_ANIMATION, id->name);
579
580                         // TODO: for each channel affected, we might also want to add some support for running RNA update callbacks on them
581                         // (which will be needed for proper handling of drivers later)
582                 }
583
584                 /* drivers */
585                 for (FCurve *fcu = (FCurve *)adt->drivers.first; fcu; fcu = fcu->next) {
586                         /* create driver */
587                         build_driver(id, fcu);
588                 }
589         }
590 }
591
592 /**
593  * Build graph node(s) for Driver
594  * \param id: ID-Block that driver is attached to
595  * \param fcu: Driver-FCurve
596  */
597 OperationDepsNode *DepsgraphNodeBuilder::build_driver(ID *id, FCurve *fcu)
598 {
599         ChannelDriver *driver = fcu->driver;
600
601         /* Create data node for this driver */
602         /* TODO(sergey): Avoid creating same operation multiple times,
603          * in the future we need to avoid lookup of the operation as well
604          * and use some tagging magic instead.
605          */
606         OperationDepsNode *driver_op = find_operation_node(id,
607                                                            DEPSNODE_TYPE_PARAMETERS,
608                                                            DEG_OPCODE_DRIVER,
609                                                            deg_fcurve_id_name(fcu));
610
611         if (driver_op == NULL) {
612                 driver_op = add_operation_node(id, DEPSNODE_TYPE_PARAMETERS,
613                                                DEPSOP_TYPE_EXEC, function_bind(BKE_animsys_eval_driver, _1, id, fcu),
614                                                DEG_OPCODE_DRIVER, deg_fcurve_id_name(fcu));
615         }
616
617         /* tag "scripted expression" drivers as needing Python (due to GIL issues, etc.) */
618         if (driver->type == DRIVER_TYPE_PYTHON) {
619                 driver_op->flag |= DEPSOP_FLAG_USES_PYTHON;
620         }
621
622         /* return driver node created */
623         return driver_op;
624 }
625
626 /* Recursively build graph for world */
627 void DepsgraphNodeBuilder::build_world(World *world)
628 {
629         ID *world_id = &world->id;
630         if (world_id->tag & LIB_TAG_DOIT) {
631                 return;
632         }
633
634         /* world itself */
635         IDDepsNode *world_node = add_id_node(world_id); /* world shading/params? */
636
637         build_animdata(world_id);
638
639         /* TODO: other settings? */
640
641         /* textures */
642         build_texture_stack(world_node, world->mtex);
643
644         /* world's nodetree */
645         if (world->nodetree) {
646                 build_nodetree(world_node, world->nodetree);
647         }
648 }
649
650 /* Rigidbody Simulation - Scene Level */
651 void DepsgraphNodeBuilder::build_rigidbody(Scene *scene)
652 {
653         RigidBodyWorld *rbw = scene->rigidbody_world;
654
655         /**
656          * Rigidbody Simulation Nodes
657          * ==========================
658          *
659          * There are 3 nodes related to Rigidbody Simulation:
660          * 1) "Initialize/Rebuild World" - this is called sparingly, only when the simulation
661          *    needs to be rebuilt (mainly after file reload, or moving back to start frame)
662          * 2) "Do Simulation" - perform a simulation step - interleaved between the evaluation
663          *    steps for clusters of objects (i.e. between those affected and/or not affected by
664          *    the sim for instance)
665          *
666          * 3) "Pull Results" - grab the specific transforms applied for a specific object -
667          *    performed as part of object's transform-stack building
668          */
669
670         /* create nodes ------------------------------------------------------------------------ */
671         /* XXX: is this the right component, or do we want to use another one instead? */
672
673         /* init/rebuild operation */
674         /*OperationDepsNode *init_node =*/ add_operation_node(&scene->id, DEPSNODE_TYPE_TRANSFORM,
675                                                               DEPSOP_TYPE_REBUILD, function_bind(BKE_rigidbody_rebuild_sim, _1, scene),
676                                                               DEG_OPCODE_RIGIDBODY_REBUILD);
677
678         /* do-sim operation */
679         // XXX: what happens if we need to split into several groups?
680         OperationDepsNode *sim_node     = add_operation_node(&scene->id, DEPSNODE_TYPE_TRANSFORM,
681                                                              DEPSOP_TYPE_SIM, function_bind(BKE_rigidbody_eval_simulation, _1, scene),
682                                                              DEG_OPCODE_RIGIDBODY_SIM);
683
684         /* XXX: For now, the sim node is the only one that really matters here. If any other
685          * sims get added later, we may have to remove these hacks...
686          */
687         sim_node->owner->entry_operation = sim_node;
688         sim_node->owner->exit_operation  = sim_node;
689
690
691         /* objects - simulation participants */
692         if (rbw->group) {
693                 for (GroupObject *go = (GroupObject *)rbw->group->gobject.first; go; go = go->next) {
694                         Object *ob = go->ob;
695
696                         if (!ob || (ob->type != OB_MESH))
697                                 continue;
698
699                         /* 2) create operation for flushing results */
700                         /* object's transform component - where the rigidbody operation lives */
701                         add_operation_node(&ob->id, DEPSNODE_TYPE_TRANSFORM,
702                                            DEPSOP_TYPE_EXEC, function_bind(BKE_rigidbody_object_sync_transforms, _1, scene, ob),
703                                            DEG_OPCODE_TRANSFORM_RIGIDBODY);
704                 }
705         }
706 }
707
708 /* IK Solver Eval Steps */
709 void DepsgraphNodeBuilder::build_ik_pose(Scene *scene, Object *ob, bPoseChannel *pchan, bConstraint *con)
710 {
711         bKinematicConstraint *data = (bKinematicConstraint *)con->data;
712
713         /* Find the chain's root. */
714         bPoseChannel *rootchan = BKE_armature_ik_solver_find_root(pchan, data);
715
716         if (has_operation_node(&ob->id, DEPSNODE_TYPE_EVAL_POSE, rootchan->name,
717                                DEG_OPCODE_POSE_IK_SOLVER))
718         {
719                 return;
720         }
721
722         /* Operation node for evaluating/running IK Solver. */
723         add_operation_node(&ob->id, DEPSNODE_TYPE_EVAL_POSE, rootchan->name,
724                            DEPSOP_TYPE_SIM, function_bind(BKE_pose_iktree_evaluate, _1, scene, ob, rootchan),
725                            DEG_OPCODE_POSE_IK_SOLVER);
726 }
727
728 /* Spline IK Eval Steps */
729 void DepsgraphNodeBuilder::build_splineik_pose(Scene *scene, Object *ob, bPoseChannel *pchan, bConstraint *con)
730 {
731         bSplineIKConstraint *data = (bSplineIKConstraint *)con->data;
732
733         /* Find the chain's root. */
734         bPoseChannel *rootchan = BKE_armature_splineik_solver_find_root(pchan, data);
735
736         /* Operation node for evaluating/running Spline IK Solver.
737          * Store the "root bone" of this chain in the solver, so it knows where to start.
738          */
739         add_operation_node(&ob->id, DEPSNODE_TYPE_EVAL_POSE, rootchan->name,
740                            DEPSOP_TYPE_SIM, function_bind(BKE_pose_splineik_evaluate, _1, scene, ob, rootchan),
741                            DEG_OPCODE_POSE_SPLINE_IK_SOLVER);
742 }
743
744 /* Pose/Armature Bones Graph */
745 void DepsgraphNodeBuilder::build_rig(Scene *scene, Object *ob)
746 {
747         bArmature *arm = (bArmature *)ob->data;
748
749         /* animation and/or drivers linking posebones to base-armature used to define them
750          * NOTE: AnimData here is really used to control animated deform properties,
751          *       which ideally should be able to be unique across different instances.
752          *       Eventually, we need some type of proxy/isolation mechanism in-between here
753          *       to ensure that we can use same rig multiple times in same scene...
754          */
755         build_animdata(&arm->id);
756
757         /* Rebuild pose if not up to date. */
758         if (ob->pose == NULL || (ob->pose->flag & POSE_RECALC)) {
759                 BKE_pose_rebuild(ob, arm);
760                 /* XXX: Without this animation gets lost in certain circumstances
761                  * after loading file. Need to investigate further since it does
762                  * not happen with simple scenes..
763                  */
764                 if (ob->adt) {
765                         ob->adt->recalc |= ADT_RECALC_ANIM;
766                 }
767         }
768
769         /* speed optimization for animation lookups */
770         if (ob->pose) {
771                 BKE_pose_channels_hash_make(ob->pose);
772                 if (ob->pose->flag & POSE_CONSTRAINTS_NEED_UPDATE_FLAGS) {
773                         BKE_pose_update_constraint_flags(ob->pose);
774                 }
775         }
776
777         /* Make sure pose is up-to-date with armature updates. */
778         add_operation_node(&arm->id,
779                            DEPSNODE_TYPE_PARAMETERS,
780                            DEPSOP_TYPE_EXEC,
781                            NULL,
782                            DEG_OPCODE_PLACEHOLDER,
783                            "Armature Eval");
784
785         /**
786          * Pose Rig Graph
787          * ==============
788          *
789          * Pose Component:
790          * - Mainly used for referencing Bone components.
791          * - This is where the evaluation operations for init/exec/cleanup
792          *   (ik) solvers live, and are later hooked up (so that they can be
793          *   interleaved during runtime) with bone-operations they depend on/affect.
794          * - init_pose_eval() and cleanup_pose_eval() are absolute first and last
795          *   steps of pose eval process. ALL bone operations must be performed
796          *   between these two...
797          *
798          * Bone Component:
799          * - Used for representing each bone within the rig
800          * - Acts to encapsulate the evaluation operations (base matrix + parenting,
801          *   and constraint stack) so that they can be easily found.
802          * - Everything else which depends on bone-results hook up to the component only
803          *   so that we can redirect those to point at either the the post-IK/
804          *   post-constraint/post-matrix steps, as needed.
805          */
806
807         /* pose eval context */
808         add_operation_node(&ob->id, DEPSNODE_TYPE_EVAL_POSE,
809                            DEPSOP_TYPE_INIT, function_bind(BKE_pose_eval_init, _1, scene, ob, ob->pose), DEG_OPCODE_POSE_INIT);
810
811         add_operation_node(&ob->id, DEPSNODE_TYPE_EVAL_POSE,
812                            DEPSOP_TYPE_POST, function_bind(BKE_pose_eval_flush, _1, scene, ob, ob->pose), DEG_OPCODE_POSE_DONE);
813
814         /* bones */
815         for (bPoseChannel *pchan = (bPoseChannel *)ob->pose->chanbase.first; pchan; pchan = pchan->next) {
816                 /* node for bone eval */
817                 add_operation_node(&ob->id, DEPSNODE_TYPE_BONE, pchan->name,
818                                    DEPSOP_TYPE_INIT, NULL, // XXX: BKE_pose_eval_bone_local
819                                    DEG_OPCODE_BONE_LOCAL);
820
821                 add_operation_node(&ob->id, DEPSNODE_TYPE_BONE, pchan->name,
822                                    DEPSOP_TYPE_EXEC, function_bind(BKE_pose_eval_bone, _1, scene, ob, pchan), // XXX: BKE_pose_eval_bone_pose
823                                    DEG_OPCODE_BONE_POSE_PARENT);
824
825                 add_operation_node(&ob->id, DEPSNODE_TYPE_BONE, pchan->name,
826                                    DEPSOP_TYPE_OUT, NULL, /* NOTE: dedicated noop for easier relationship construction */
827                                    DEG_OPCODE_BONE_READY);
828
829                 add_operation_node(&ob->id, DEPSNODE_TYPE_BONE, pchan->name,
830                                    DEPSOP_TYPE_POST, function_bind(BKE_pose_bone_done, _1, pchan),
831                                    DEG_OPCODE_BONE_DONE);
832
833                 /* constraints */
834                 if (pchan->constraints.first != NULL) {
835                         build_pose_constraints(ob, pchan);
836                 }
837
838                 /**
839                  * IK Solvers...
840                  *
841                  * - These require separate processing steps are pose-level
842                  *   to be executed between chains of bones (i.e. once the
843                  *   base transforms of a bunch of bones is done)
844                  *
845                  * Unsolved Issues:
846                  * - Care is needed to ensure that multi-headed trees work out the same as in ik-tree building
847                  * - Animated chain-lengths are a problem...
848                  */
849                 for (bConstraint *con = (bConstraint *)pchan->constraints.first; con; con = con->next) {
850                         switch (con->type) {
851                                 case CONSTRAINT_TYPE_KINEMATIC:
852                                         build_ik_pose(scene, ob, pchan, con);
853                                         break;
854
855                                 case CONSTRAINT_TYPE_SPLINEIK:
856                                         build_splineik_pose(scene, ob, pchan, con);
857                                         break;
858
859                                 default:
860                                         break;
861                         }
862                 }
863         }
864 }
865
866 void DepsgraphNodeBuilder::build_proxy_rig(Object *ob)
867 {
868         ID *obdata = (ID *)ob->data;
869         build_animdata(obdata);
870
871         BLI_assert(ob->pose != NULL);
872
873         /* speed optimization for animation lookups */
874         BKE_pose_channels_hash_make(ob->pose);
875         if (ob->pose->flag & POSE_CONSTRAINTS_NEED_UPDATE_FLAGS) {
876                 BKE_pose_update_constraint_flags(ob->pose);
877         }
878
879         add_operation_node(&ob->id,
880                            DEPSNODE_TYPE_EVAL_POSE,
881                            DEPSOP_TYPE_INIT,
882                            function_bind(BKE_pose_eval_proxy_copy, _1, ob),
883                            DEG_OPCODE_POSE_INIT);
884
885         for (bPoseChannel *pchan = (bPoseChannel *)ob->pose->chanbase.first;
886              pchan != NULL;
887              pchan = pchan->next)
888         {
889                 add_operation_node(&ob->id, DEPSNODE_TYPE_BONE, pchan->name,
890                                    DEPSOP_TYPE_INIT, NULL,
891                                    DEG_OPCODE_BONE_LOCAL);
892
893                 add_operation_node(&ob->id, DEPSNODE_TYPE_BONE, pchan->name,
894                                    DEPSOP_TYPE_EXEC, NULL,
895                                    DEG_OPCODE_BONE_READY);
896
897                 add_operation_node(&ob->id, DEPSNODE_TYPE_BONE, pchan->name,
898                                    DEPSOP_TYPE_POST, NULL,
899                                    DEG_OPCODE_BONE_DONE);
900         }
901
902         add_operation_node(&ob->id,
903                            DEPSNODE_TYPE_EVAL_POSE,
904                            DEPSOP_TYPE_POST,
905                            NULL,
906                            DEG_OPCODE_POSE_DONE);
907 }
908
909 /* Shapekeys */
910 void DepsgraphNodeBuilder::build_shapekeys(Key *key)
911 {
912         build_animdata(&key->id);
913
914         add_operation_node(&key->id, DEPSNODE_TYPE_GEOMETRY, DEPSOP_TYPE_EXEC, NULL,
915                            DEG_OPCODE_PLACEHOLDER, "Shapekey Eval");
916 }
917
918 /* ObData Geometry Evaluation */
919 // XXX: what happens if the datablock is shared!
920 void DepsgraphNodeBuilder::build_obdata_geom(Scene *scene, Object *ob)
921 {
922         ID *obdata = (ID *)ob->data;
923
924         /* Temporary uber-update node, which does everything.
925          * It is for the being we're porting old dependencies into the new system.
926          * We'll get rid of this node as soon as all the granular update functions
927          * are filled in.
928          *
929          * TODO(sergey): Get rid of this node.
930          */
931         add_operation_node(&ob->id, DEPSNODE_TYPE_GEOMETRY,
932                            DEPSOP_TYPE_POST, function_bind(BKE_object_eval_uber_data, _1, scene, ob),
933                            DEG_OPCODE_GEOMETRY_UBEREVAL);
934
935         add_operation_node(&ob->id, DEPSNODE_TYPE_GEOMETRY,
936                            DEPSOP_TYPE_INIT, NULL,
937                            DEG_OPCODE_PLACEHOLDER, "Eval Init");
938
939         // TODO: "Done" operation
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         /* ShapeKeys */
977         Key *key = BKE_key_from_object(ob);
978         if (key) {
979                 build_shapekeys(key);
980         }
981
982         build_animdata(obdata);
983
984         /* nodes for result of obdata's evaluation, and geometry evaluation on object */
985         switch (ob->type) {
986                 case OB_MESH:
987                 {
988                         //Mesh *me = (Mesh *)ob->data;
989
990                         /* evaluation operations */
991                         add_operation_node(obdata, DEPSNODE_TYPE_GEOMETRY,
992                                            DEPSOP_TYPE_INIT, function_bind(BKE_mesh_eval_geometry, _1, (Mesh *)obdata),
993                                            DEG_OPCODE_PLACEHOLDER, "Geometry Eval");
994                         break;
995                 }
996
997                 case OB_MBALL:
998                 {
999                         Object *mom = BKE_mball_basis_find(scene, ob);
1000
1001                         /* motherball - mom depends on children! */
1002                         if (mom == ob) {
1003                                 /* metaball evaluation operations */
1004                                 /* NOTE: only the motherball gets evaluated! */
1005                                 add_operation_node(obdata, DEPSNODE_TYPE_GEOMETRY,
1006                                                    DEPSOP_TYPE_INIT, function_bind(BKE_mball_eval_geometry, _1, (MetaBall *)obdata),
1007                                                    DEG_OPCODE_PLACEHOLDER, "Geometry Eval");
1008                         }
1009                         break;
1010                 }
1011
1012                 case OB_CURVE:
1013                 case OB_FONT:
1014                 {
1015                         /* curve evaluation operations */
1016                         /* - calculate curve geometry (including path) */
1017                         add_operation_node(obdata, DEPSNODE_TYPE_GEOMETRY,
1018                                            DEPSOP_TYPE_INIT, function_bind(BKE_curve_eval_geometry, _1, (Curve *)obdata),
1019                                            DEG_OPCODE_PLACEHOLDER, "Geometry Eval");
1020
1021                         /* - calculate curve path - this is used by constraints, etc. */
1022                         add_operation_node(obdata, DEPSNODE_TYPE_GEOMETRY,
1023                                            DEPSOP_TYPE_EXEC, function_bind(BKE_curve_eval_path, _1, (Curve *)obdata),
1024                                            DEG_OPCODE_GEOMETRY_PATH, "Path");
1025                         break;
1026                 }
1027
1028                 case OB_SURF: /* Nurbs Surface */
1029                 {
1030                         /* nurbs evaluation operations */
1031                         add_operation_node(obdata, DEPSNODE_TYPE_GEOMETRY,
1032                                            DEPSOP_TYPE_INIT, function_bind(BKE_curve_eval_geometry, _1, (Curve *)obdata),
1033                                            DEG_OPCODE_PLACEHOLDER, "Geometry Eval");
1034                         break;
1035                 }
1036
1037                 case OB_LATTICE: /* Lattice */
1038                 {
1039                         /* lattice evaluation operations */
1040                         add_operation_node(obdata, DEPSNODE_TYPE_GEOMETRY,
1041                                            DEPSOP_TYPE_INIT, function_bind(BKE_lattice_eval_geometry, _1, (Lattice *)obdata),
1042                                            DEG_OPCODE_PLACEHOLDER, "Geometry Eval");
1043                         break;
1044                 }
1045         }
1046
1047         add_operation_node(obdata, DEPSNODE_TYPE_GEOMETRY,
1048                            DEPSOP_TYPE_POST, NULL,
1049                            DEG_OPCODE_PLACEHOLDER, "Eval Done");
1050
1051         /* Parameters for driver sources. */
1052         add_operation_node(obdata, DEPSNODE_TYPE_PARAMETERS, DEPSOP_TYPE_EXEC, NULL,
1053                            DEG_OPCODE_PLACEHOLDER, "Parameters Eval");
1054 }
1055
1056 /* Cameras */
1057 void DepsgraphNodeBuilder::build_camera(Object *ob)
1058 {
1059         /* TODO: Link scene-camera links in somehow... */
1060         Camera *cam = (Camera *)ob->data;
1061         ID *camera_id = &cam->id;
1062         if (camera_id->tag & LIB_TAG_DOIT) {
1063                 return;
1064         }
1065
1066         build_animdata(&cam->id);
1067
1068         add_operation_node(camera_id, DEPSNODE_TYPE_PARAMETERS, DEPSOP_TYPE_EXEC, NULL,
1069                            DEG_OPCODE_PLACEHOLDER, "Parameters Eval");
1070
1071         if (cam->dof_ob != NULL) {
1072                 /* TODO(sergey): For now parametrs are on object level. */
1073                 add_operation_node(&ob->id, DEPSNODE_TYPE_PARAMETERS,
1074                                    DEPSOP_TYPE_EXEC, NULL,
1075                                    DEG_OPCODE_PLACEHOLDER,
1076                                    "Camera DOF");
1077         }
1078 }
1079
1080 /* Lamps */
1081 void DepsgraphNodeBuilder::build_lamp(Object *ob)
1082 {
1083         Lamp *la = (Lamp *)ob->data;
1084         ID *lamp_id = &la->id;
1085         if (lamp_id->tag & LIB_TAG_DOIT) {
1086                 return;
1087         }
1088
1089         build_animdata(&la->id);
1090
1091         /* node for obdata */
1092         ComponentDepsNode *param_node = add_component_node(lamp_id, DEPSNODE_TYPE_PARAMETERS);
1093
1094         /* TODO(sergey): Is it really how we're supposed to work with drivers? */
1095         add_operation_node(lamp_id, DEPSNODE_TYPE_PARAMETERS, DEPSOP_TYPE_EXEC, NULL,
1096                            DEG_OPCODE_PLACEHOLDER, "Parameters Eval");
1097
1098         /* lamp's nodetree */
1099         if (la->nodetree) {
1100                 build_nodetree(param_node, la->nodetree);
1101         }
1102
1103         /* textures */
1104         build_texture_stack(param_node, la->mtex);
1105 }
1106
1107 void DepsgraphNodeBuilder::build_nodetree(DepsNode *owner_node, bNodeTree *ntree)
1108 {
1109         if (!ntree)
1110                 return;
1111
1112         /* nodetree itself */
1113         ID *ntree_id = &ntree->id;
1114
1115         build_animdata(ntree_id);
1116
1117         /* Parameters for drivers. */
1118         add_operation_node(ntree_id, DEPSNODE_TYPE_PARAMETERS, DEPSOP_TYPE_POST, NULL,
1119                            DEG_OPCODE_PLACEHOLDER, "Parameters Eval");
1120
1121         /* nodetree's nodes... */
1122         for (bNode *bnode = (bNode *)ntree->nodes.first; bnode; bnode = bnode->next) {
1123                 if (bnode->id) {
1124                         if (GS(bnode->id->name) == ID_MA) {
1125                                 build_material(owner_node, (Material *)bnode->id);
1126                         }
1127                         else if (bnode->type == ID_TE) {
1128                                 build_texture(owner_node, (Tex *)bnode->id);
1129                         }
1130                         else if (bnode->type == NODE_GROUP) {
1131                                 bNodeTree *group_ntree = (bNodeTree *)bnode->id;
1132                                 if ((group_ntree->id.tag & LIB_TAG_DOIT) == 0) {
1133                                         build_nodetree(owner_node, group_ntree);
1134                                 }
1135                         }
1136                 }
1137         }
1138
1139         // TODO: link from nodetree to owner_component?
1140 }
1141
1142 /* Recursively build graph for material */
1143 void DepsgraphNodeBuilder::build_material(DepsNode *owner_node, Material *ma)
1144 {
1145         ID *ma_id = &ma->id;
1146         if (ma_id->tag & LIB_TAG_DOIT) {
1147                 return;
1148         }
1149
1150         /* material itself */
1151         add_id_node(ma_id);
1152
1153         add_operation_node(ma_id, DEPSNODE_TYPE_SHADING,
1154                            DEPSOP_TYPE_EXEC, NULL,
1155                            DEG_OPCODE_PLACEHOLDER, "Material Update");
1156
1157         /* material animation */
1158         build_animdata(ma_id);
1159
1160         /* textures */
1161         build_texture_stack(owner_node, ma->mtex);
1162
1163         /* material's nodetree */
1164         build_nodetree(owner_node, ma->nodetree);
1165 }
1166
1167 /* Texture-stack attached to some shading datablock */
1168 void DepsgraphNodeBuilder::build_texture_stack(DepsNode *owner_node, MTex **texture_stack)
1169 {
1170         int i;
1171
1172         /* for now assume that all texture-stacks have same number of max items */
1173         for (i = 0; i < MAX_MTEX; i++) {
1174                 MTex *mtex = texture_stack[i];
1175                 if (mtex && mtex->tex)
1176                         build_texture(owner_node, mtex->tex);
1177         }
1178 }
1179
1180 /* Recursively build graph for texture */
1181 void DepsgraphNodeBuilder::build_texture(DepsNode *owner_node, Tex *tex)
1182 {
1183         ID *tex_id = &tex->id;
1184         if (tex_id->tag & LIB_TAG_DOIT) {
1185                 return;
1186         }
1187         tex_id->tag |= LIB_TAG_DOIT;
1188         /* texture itself */
1189         build_animdata(tex_id);
1190         /* texture's nodetree */
1191         build_nodetree(owner_node, tex->nodetree);
1192 }
1193
1194 void DepsgraphNodeBuilder::build_compositor(Scene *scene)
1195 {
1196         /* For now, just a plain wrapper? */
1197         // TODO: create compositing component?
1198         // XXX: component type undefined!
1199         //graph->get_node(&scene->id, NULL, DEPSNODE_TYPE_COMPOSITING, NULL);
1200
1201         /* for now, nodetrees are just parameters; compositing occurs in internals of renderer... */
1202         ComponentDepsNode *owner_node = add_component_node(&scene->id, DEPSNODE_TYPE_PARAMETERS);
1203         build_nodetree(owner_node, scene->nodetree);
1204 }
1205
1206 void DepsgraphNodeBuilder::build_gpencil(bGPdata *gpd)
1207 {
1208         ID *gpd_id = &gpd->id;
1209
1210         /* gpencil itself */
1211         // XXX: what about multiple users of same datablock? This should only get added once
1212         add_id_node(gpd_id);
1213
1214         /* The main reason Grease Pencil is included here is because the animation (and drivers)
1215          * need to be hosted somewhere...
1216          */
1217         build_animdata(gpd_id);
1218 }
1219
1220 }  // namespace DEG