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