0211f5dba68e2052fe42c96d0d9cf37bd5e03018
[blender.git] / source / blender / depsgraph / intern / builder / deg_builder_relations.h
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: Lukas Toenne
22  * Contributor(s): None Yet
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/depsgraph/intern/builder/deg_builder_relations.h
28  *  \ingroup depsgraph
29  */
30
31 #pragma once
32
33 #include <cstdio>
34 #include <cstring>
35
36 #include "intern/depsgraph_types.h"
37
38 #include "DNA_ID.h"
39
40 #include "RNA_access.h"
41 #include "RNA_types.h"
42
43 #include "BLI_utildefines.h"
44 #include "BLI_string.h"
45
46 #include "intern/builder/deg_builder_map.h"
47 #include "intern/nodes/deg_node.h"
48 #include "intern/nodes/deg_node_component.h"
49 #include "intern/nodes/deg_node_operation.h"
50
51 struct Base;
52 struct bGPdata;
53 struct CacheFile;
54 struct ListBase;
55 struct GHash;
56 struct ID;
57 struct FCurve;
58 struct Collection;
59 struct Key;
60 struct LayerCollection;
61 struct LightProbe;
62 struct Main;
63 struct Mask;
64 struct Material;
65 struct MTex;
66 struct ModifierData;
67 struct MovieClip;
68 struct bNodeTree;
69 struct Object;
70 struct bPoseChannel;
71 struct bConstraint;
72 struct ParticleSystem;
73 struct ParticleSettings;
74 struct Scene;
75 struct ViewLayer;
76 struct Tex;
77 struct World;
78 struct EffectorWeights;
79
80 struct PropertyRNA;
81
82 namespace DEG {
83
84 struct Depsgraph;
85 struct DepsNode;
86 struct DepsNodeHandle;
87 struct DepsRelation;
88 struct RootDepsNode;
89 struct IDDepsNode;
90 struct TimeSourceDepsNode;
91 struct ComponentDepsNode;
92 struct OperationDepsNode;
93 struct RootPChanMap;
94
95 struct TimeSourceKey
96 {
97         TimeSourceKey();
98         TimeSourceKey(ID *id);
99
100         string identifier() const;
101
102         ID *id;
103 };
104
105 struct ComponentKey
106 {
107         ComponentKey();
108         ComponentKey(ID *id, eDepsNode_Type type, const char *name = "");
109
110         string identifier() const;
111
112         ID *id;
113         eDepsNode_Type type;
114         const char *name;
115 };
116
117 struct OperationKey
118 {
119         OperationKey();
120         OperationKey(ID *id,
121                      eDepsNode_Type component_type,
122                      const char *name,
123                      int name_tag = -1);
124         OperationKey(ID *id,
125                      eDepsNode_Type component_type,
126                      const char *component_name,
127                      const char *name,
128                      int name_tag);
129
130         OperationKey(ID *id,
131                      eDepsNode_Type component_type,
132                      eDepsOperation_Code opcode);
133         OperationKey(ID *id,
134                      eDepsNode_Type component_type,
135                      const char *component_name,
136                      eDepsOperation_Code opcode);
137
138         OperationKey(ID *id,
139                      eDepsNode_Type component_type,
140                      eDepsOperation_Code opcode,
141                      const char *name,
142                      int name_tag = -1);
143         OperationKey(ID *id,
144                      eDepsNode_Type component_type,
145                      const char *component_name,
146                      eDepsOperation_Code opcode,
147                      const char *name,
148                      int name_tag = -1);
149
150         string identifier() const;
151
152         ID *id;
153         eDepsNode_Type component_type;
154         const char *component_name;
155         eDepsOperation_Code opcode;
156         const char *name;
157         int name_tag;
158 };
159
160 struct RNAPathKey
161 {
162         /* NOTE: see depsgraph_build.cpp for implementation */
163         RNAPathKey(ID *id, const char *path);
164
165         RNAPathKey(ID *id, const PointerRNA &ptr, PropertyRNA *prop);
166
167         string identifier() const;
168
169         ID *id;
170         PointerRNA ptr;
171         PropertyRNA *prop;
172 };
173
174 struct DepsgraphRelationBuilder
175 {
176         DepsgraphRelationBuilder(Main *bmain, Depsgraph *graph);
177
178         void begin_build();
179
180         template <typename KeyFrom, typename KeyTo>
181         DepsRelation *add_relation(const KeyFrom& key_from,
182                                    const KeyTo& key_to,
183                                    const char *description,
184                                    bool check_unique = false);
185
186         template <typename KeyTo>
187         DepsRelation *add_relation(const TimeSourceKey& key_from,
188                                    const KeyTo& key_to,
189                                    const char *description,
190                                    bool check_unique = false);
191
192         template <typename KeyType>
193         DepsRelation *add_node_handle_relation(const KeyType& key_from,
194                                                const DepsNodeHandle *handle,
195                                                const char *description,
196                                                bool check_unique = false);
197
198         void build_id(ID *id);
199         void build_layer_collections(ListBase *lb);
200         void build_view_layer(Scene *scene, ViewLayer *view_layer);
201         void build_collection(Object *object, Collection *collection);
202         void build_object(Base *base, Object *object);
203         void build_object_flags(Base *base, Object *object);
204         void build_object_data(Object *object);
205         void build_object_data_lightprobe(Object *object);
206         void build_object_parent(Object *object);
207         void build_constraints(ID *id,
208                                eDepsNode_Type component_type,
209                                const char *component_subdata,
210                                ListBase *constraints,
211                                RootPChanMap *root_map);
212         void build_animdata(ID *id);
213         void build_animdata_curves(ID *id);
214         void build_animdata_curves_targets(ID *id,
215                                            ComponentKey &adt_key,
216                                            OperationDepsNode *operation_from,
217                                            ListBase *curves);
218         void build_animdata_nlastrip_targets(ID *id,
219                                              ComponentKey &adt_key,
220                                              OperationDepsNode *operation_from,
221                                              ListBase *strips);
222         void build_animdata_drivers(ID *id);
223         void build_driver(ID *id, FCurve *fcurve);
224         void build_driver_data(ID *id, FCurve *fcurve);
225         void build_driver_variables(ID *id, FCurve *fcurve);
226         void build_world(World *world);
227         void build_rigidbody(Scene *scene);
228         void build_particles(Object *object);
229         void build_particle_settings(ParticleSettings *part);
230         void build_particles_visualization_object(Object *object,
231                                                   ParticleSystem *psys,
232                                                   Object *draw_object);
233         void build_cloth(Object *object, ModifierData *md);
234         void build_ik_pose(Object *object,
235                            bPoseChannel *pchan,
236                            bConstraint *con,
237                            RootPChanMap *root_map);
238         void build_splineik_pose(Object *object,
239                                  bPoseChannel *pchan,
240                                  bConstraint *con,
241                                  RootPChanMap *root_map);
242         void build_rig(Object *object);
243         void build_proxy_rig(Object *object);
244         void build_shapekeys(ID *obdata, Key *key);
245         void build_obdata_geom(Object *object);
246         void build_camera(Object *object);
247         void build_lamp(Object *object);
248         void build_nodetree(bNodeTree *ntree);
249         void build_material(Material *ma);
250         void build_texture(Tex *tex);
251         void build_compositor(Scene *scene);
252         void build_gpencil(bGPdata *gpd);
253         void build_cachefile(CacheFile *cache_file);
254         void build_mask(Mask *mask);
255         void build_movieclip(MovieClip *clip);
256         void build_lightprobe(LightProbe *probe);
257
258         void build_nested_datablock(ID *owner, ID *id);
259         void build_nested_nodetree(ID *owner, bNodeTree *ntree);
260         void build_nested_shapekey(ID *owner, Key *key);
261
262         void add_collision_relations(const OperationKey &key,
263                                      Scene *scene,
264                                      Object *object,
265                                      Collection *collection,
266                                      bool dupli,
267                                      const char *name);
268         void add_forcefield_relations(const OperationKey &key,
269                                       Scene *scene,
270                                       Object *object,
271                                       ParticleSystem *psys,
272                                       EffectorWeights *eff,
273                                       bool add_absorption, const char *name);
274
275         void build_copy_on_write_relations();
276         void build_copy_on_write_relations(IDDepsNode *id_node);
277
278         template <typename KeyType>
279         OperationDepsNode *find_operation_node(const KeyType &key);
280
281         Depsgraph *getGraph();
282
283 protected:
284         TimeSourceDepsNode *get_node(const TimeSourceKey &key) const;
285         ComponentDepsNode *get_node(const ComponentKey &key) const;
286         OperationDepsNode *get_node(const OperationKey &key) const;
287         DepsNode *get_node(const RNAPathKey &key) const;
288
289         OperationDepsNode *find_node(const OperationKey &key) const;
290         bool has_node(const OperationKey &key) const;
291
292         DepsRelation *add_time_relation(TimeSourceDepsNode *timesrc,
293                                         DepsNode *node_to,
294                                         const char *description,
295                                         bool check_unique = false);
296         DepsRelation *add_operation_relation(OperationDepsNode *node_from,
297                                              OperationDepsNode *node_to,
298                                              const char *description,
299                                              bool check_unique = false);
300
301         template <typename KeyType>
302         DepsNodeHandle create_node_handle(const KeyType& key,
303                                           const char *default_name = "");
304
305         /* TODO(sergey): All those is_same* functions are to be generalized. */
306
307         /* Check whether two keys correponds to the same bone from same armature.
308          *
309          * This is used by drivers relations builder to avoid possible fake
310          * dependency cycle when one bone property drives another property of the
311          * same bone.
312          */
313         template <typename KeyFrom, typename KeyTo>
314         bool is_same_bone_dependency(const KeyFrom& key_from, const KeyTo& key_to);
315
316         /* Similar to above, but used to check whether driver is using node from
317          * the same node tree as a driver variable.
318          */
319         template <typename KeyFrom, typename KeyTo>
320         bool is_same_nodetree_node_dependency(const KeyFrom& key_from,
321                                               const KeyTo& key_to);
322
323         /* Similar to above, but used to check whether driver is using key from
324          * the same key datablock as a driver variable.
325          */
326         template <typename KeyFrom, typename KeyTo>
327         bool is_same_shapekey_dependency(const KeyFrom& key_from,
328                                          const KeyTo& key_to);
329
330 private:
331         struct BuilderWalkUserData {
332                 DepsgraphRelationBuilder *builder;
333         };
334
335         static void modifier_walk(void *user_data,
336                                   struct Object *object,
337                                   struct ID **idpoin,
338                                   int cb_flag);
339
340         static void constraint_walk(bConstraint *con,
341                                     ID **idpoin,
342                                     bool is_reference,
343                                     void *user_data);
344
345         /* State which never changes, same for the whole builder time. */
346         Main *bmain_;
347         Depsgraph *graph_;
348
349         /* State which demotes currently built entities. */
350         Scene *scene_;
351
352         BuilderMap built_map_;
353 };
354
355 struct DepsNodeHandle
356 {
357         DepsNodeHandle(DepsgraphRelationBuilder *builder,
358                        OperationDepsNode *node,
359                        const char *default_name = "")
360                 : builder(builder),
361                   node(node),
362                   default_name(default_name)
363         {
364                 BLI_assert(node != NULL);
365         }
366
367         DepsgraphRelationBuilder *builder;
368         OperationDepsNode *node;
369         const char *default_name;
370 };
371
372 }  // namespace DEG
373
374
375 #include "intern/builder/deg_builder_relations_impl.h"