75fb4962bef3aec15bb80d9b9de4f83d3dd6aa26
[blender.git] / source / blender / blenkernel / BKE_layer.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  * Contributor(s): Blender Foundation, Dalai Felinto
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 #ifndef __BKE_LAYER_H__
24 #define __BKE_LAYER_H__
25
26 /** \file blender/blenkernel/BKE_layer.h
27  *  \ingroup bke
28  */
29
30 #include "BKE_collection.h"
31
32 #include "DNA_scene_types.h"
33
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37
38 #define TODO_LAYER_OVERRIDE /* CollectionOverride */
39 #define TODO_LAYER_OPERATORS /* collection mamanger and property panel operators */
40 #define TODO_LAYER /* generic todo */
41
42 #define ROOT_PROP "root"
43
44 struct Base;
45 struct EvaluationContext;
46 struct Group;
47 struct ID;
48 struct IDProperty;
49 struct LayerCollection;
50 struct ListBase;
51 struct Main;
52 struct Object;
53 struct RenderEngine;
54 struct Scene;
55 struct SceneCollection;
56 struct ViewLayer;
57 struct WorkSpace;
58
59 void BKE_layer_exit(void);
60
61 struct ViewLayer *BKE_view_layer_from_scene_get(const struct Scene *scene);
62 struct ViewLayer *BKE_view_layer_from_workspace_get(const struct Scene *scene, const struct WorkSpace *workspace);
63 struct ViewLayer *BKE_view_layer_add(struct Scene *scene, const char *name);
64 struct ViewLayer *BKE_view_layer_group_add(struct Group *group);
65
66 /* DEPRECATED */
67 struct ViewLayer *BKE_view_layer_context_active_PLACEHOLDER(const struct Scene *scene);
68
69 void BKE_view_layer_free(struct ViewLayer *view_layer);
70 void BKE_view_layer_free_ex(struct ViewLayer *view_layer, const bool do_id_user);
71
72 void BKE_view_layer_selected_objects_tag(struct ViewLayer *view_layer, const int tag);
73
74 struct Object *BKE_view_layer_camera_find(struct ViewLayer *view_layer);
75 struct ViewLayer *BKE_view_layer_first_from_id(const struct ID *owner_id);
76 struct ViewLayer *BKE_view_layer_find_from_collection(const struct ID *owner_id, struct LayerCollection *lc);
77 struct Base *BKE_view_layer_base_find(struct ViewLayer *view_layer, struct Object *ob);
78 void BKE_view_layer_base_deselect_all(struct ViewLayer *view_layer);
79 void BKE_view_layer_base_select(struct ViewLayer *view_layer, struct Base *selbase);
80
81 void BKE_layer_collection_sync_flags(
82         struct ID *owner_id,
83         struct SceneCollection *scene_collection_dst,
84         struct SceneCollection *scene_collection_src);
85
86 void BKE_view_layer_copy_data(
87         struct ViewLayer *view_layer_dst, struct ViewLayer *view_layer_src,
88         struct SceneCollection *mc_dst, struct SceneCollection *mc_src,
89         const int flag);
90
91 struct LayerCollection *BKE_layer_collection_duplicate(struct ID *owner_id, struct LayerCollection *layer_collection);
92
93 void BKE_layer_collection_free(struct ViewLayer *view_layer, struct LayerCollection *lc);
94
95 struct LayerCollection *BKE_layer_collection_get_active(struct ViewLayer *view_layer);
96 struct LayerCollection *BKE_layer_collection_get_active_ensure(struct Scene *scene, struct ViewLayer *view_layer);
97
98 int BKE_layer_collection_count(struct ViewLayer *view_layer);
99
100 struct LayerCollection *BKE_layer_collection_from_index(struct ViewLayer *view_layer, const int index);
101 int BKE_layer_collection_findindex(struct ViewLayer *view_layer, const struct LayerCollection *lc);
102
103 bool BKE_layer_collection_move_above(const struct ID *owner_id, struct LayerCollection *lc_dst, struct LayerCollection *lc_src);
104 bool BKE_layer_collection_move_below(const struct ID *owner_id, struct LayerCollection *lc_dst, struct LayerCollection *lc_src);
105 bool BKE_layer_collection_move_into(const struct ID *owner_id, struct LayerCollection *lc_dst, struct LayerCollection *lc_src);
106
107 void BKE_layer_collection_resync(const struct ID *owner_id, const struct SceneCollection *sc);
108
109 struct LayerCollection *BKE_collection_link(struct ViewLayer *view_layer, struct SceneCollection *sc);
110
111 void BKE_collection_unlink(struct ViewLayer *view_layer, struct LayerCollection *lc);
112
113 void BKE_collection_enable(struct ViewLayer *view_layer, struct LayerCollection *lc);
114
115 struct LayerCollection *BKE_layer_collection_first_from_scene_collection(struct ViewLayer *view_layer, const struct SceneCollection *scene_collection);
116 bool BKE_view_layer_has_collection(struct ViewLayer *view_layer, const struct SceneCollection *sc);
117 bool BKE_scene_has_object(struct Scene *scene, struct Object *ob);
118
119 void BKE_layer_collection_objects_select(struct LayerCollection *layer_collection);
120
121 /* syncing */
122
123 void BKE_layer_sync_new_scene_collection(struct ID *owner_id, const struct SceneCollection *sc_parent, struct SceneCollection *sc);
124 void BKE_layer_sync_object_link(const struct ID *owner_id, struct SceneCollection *sc, struct Object *ob);
125 void BKE_layer_sync_object_unlink(const struct ID *owner_id, struct SceneCollection *sc, struct Object *ob);
126
127 /* override */
128
129 void BKE_override_view_layer_datablock_add(struct ViewLayer *view_layer, int id_type, const char *data_path, const struct ID *owner_id);
130 void BKE_override_view_layer_int_add(struct ViewLayer *view_layer, int id_type, const char *data_path, const int value);
131
132 void BKE_override_layer_collection_boolean_add(struct LayerCollection *layer_collection, int id_type, const char *data_path, const bool value);
133
134 /* engine settings */
135 typedef void (*EngineSettingsCB)(struct RenderEngine *engine, struct IDProperty *props);
136
137 struct IDProperty *BKE_layer_collection_engine_evaluated_get(struct Object *ob, const int type, const char *engine_name);
138 struct IDProperty *BKE_layer_collection_engine_collection_get(struct LayerCollection *lc, const int type, const char *engine_name);
139 struct IDProperty *BKE_layer_collection_engine_scene_get(struct Scene *scene, const int type, const char *engine_name);
140 void BKE_layer_collection_engine_settings_callback_register(struct Main *bmain, const char *engine_name, EngineSettingsCB func);
141 void BKE_layer_collection_engine_settings_callback_free(void);
142 void BKE_layer_collection_engine_settings_create(struct IDProperty *root);
143 void BKE_layer_collection_engine_settings_validate_scene(struct Scene *scene);
144 void BKE_layer_collection_engine_settings_validate_collection(struct LayerCollection *lc);
145
146 struct IDProperty *BKE_view_layer_engine_evaluated_get(struct ViewLayer *view_layer, const int type, const char *engine_name);
147 struct IDProperty *BKE_view_layer_engine_layer_get(struct ViewLayer *view_layer, const int type, const char *engine_name);
148 struct IDProperty *BKE_view_layer_engine_scene_get(struct Scene *scene, const int type, const char *engine_name);
149 void BKE_view_layer_engine_settings_callback_register(struct Main *bmain, const char *engine_name, EngineSettingsCB func);
150 void BKE_view_layer_engine_settings_callback_free(void);
151 void BKE_view_layer_engine_settings_validate_scene(struct Scene *scene);
152 void BKE_view_layer_engine_settings_validate_layer(struct ViewLayer *view_layer);
153 void BKE_view_layer_engine_settings_create(struct IDProperty *root);
154
155 void BKE_collection_engine_property_add_float(struct IDProperty *props, const char *name, float value);
156 void BKE_collection_engine_property_add_float_array(
157         struct IDProperty *props, const char *name, const float *values, const int array_length);
158 void BKE_collection_engine_property_add_int(struct IDProperty *props, const char *name, int value);
159 void BKE_collection_engine_property_add_bool(struct IDProperty *props, const char *name, bool value);
160
161 int BKE_collection_engine_property_value_get_int(struct IDProperty *props, const char *name);
162 float BKE_collection_engine_property_value_get_float(struct IDProperty *props, const char *name);
163 const float *BKE_collection_engine_property_value_get_float_array(struct IDProperty *props, const char *name);
164 bool BKE_collection_engine_property_value_get_bool(struct IDProperty *props, const char *name);
165 void BKE_collection_engine_property_value_set_int(struct IDProperty *props, const char *name, int value);
166 void BKE_collection_engine_property_value_set_float(struct IDProperty *props, const char *name, float value);
167 void BKE_collection_engine_property_value_set_float_array(struct IDProperty *props, const char *name, const float *values);
168 void BKE_collection_engine_property_value_set_bool(struct IDProperty *props, const char *name, bool value);
169
170 /* evaluation */
171
172 void BKE_layer_eval_view_layer(const struct EvaluationContext *eval_ctx,
173                                struct ID *owner_id,
174                                struct ViewLayer *view_layer);
175
176 void BKE_layer_eval_view_layer_indexed(const struct EvaluationContext *eval_ctx,
177                                        struct ID *owner_id,
178                                        int view_layer_index);
179
180 /* iterators */
181
182 void BKE_selected_objects_iterator_begin(BLI_Iterator *iter, void *data_in);
183 void BKE_selected_objects_iterator_next(BLI_Iterator *iter);
184 void BKE_selected_objects_iterator_end(BLI_Iterator *iter);
185
186 void BKE_visible_objects_iterator_begin(BLI_Iterator *iter, void *data_in);
187 void BKE_visible_objects_iterator_next(BLI_Iterator *iter);
188 void BKE_visible_objects_iterator_end(BLI_Iterator *iter);
189
190 struct ObjectsInModeIteratorData {
191         int object_mode;
192         struct ViewLayer *view_layer;
193         struct Base *base_active;
194 };
195
196 void BKE_renderable_objects_iterator_begin(BLI_Iterator *iter, void *data_in);
197 void BKE_renderable_objects_iterator_next(BLI_Iterator *iter);
198 void BKE_renderable_objects_iterator_end(BLI_Iterator *iter);
199
200 void BKE_view_layer_objects_in_mode_iterator_begin(BLI_Iterator *iter, void *data_in);
201 void BKE_view_layer_objects_in_mode_iterator_next(BLI_Iterator *iter);
202 void BKE_view_layer_objects_in_mode_iterator_end(BLI_Iterator *iter);
203
204 void BKE_selected_bases_iterator_begin(BLI_Iterator *iter, void *data_in);
205 void BKE_selected_bases_iterator_next(BLI_Iterator *iter);
206 void BKE_selected_bases_iterator_end(BLI_Iterator *iter);
207
208 void BKE_visible_bases_iterator_begin(BLI_Iterator *iter, void *data_in);
209 void BKE_visible_bases_iterator_next(BLI_Iterator *iter);
210 void BKE_visible_bases_iterator_end(BLI_Iterator *iter);
211
212 #define FOREACH_SELECTED_OBJECT_BEGIN(view_layer, _instance)                  \
213         ITER_BEGIN(BKE_selected_objects_iterator_begin,                           \
214                    BKE_selected_objects_iterator_next,                            \
215                    BKE_selected_objects_iterator_end,                             \
216                    view_layer, Object *, _instance)
217
218 #define FOREACH_SELECTED_OBJECT_END                                           \
219         ITER_END
220
221 #define FOREACH_VISIBLE_OBJECT_BEGIN(view_layer, _instance)                   \
222         ITER_BEGIN(BKE_visible_objects_iterator_begin,                            \
223                    BKE_visible_objects_iterator_next,                             \
224                    BKE_visible_objects_iterator_end,                              \
225                    view_layer, Object *, _instance)
226
227 #define FOREACH_VISIBLE_OBJECT_END                                            \
228         ITER_END
229
230
231 #define FOREACH_BASE_IN_MODE_BEGIN(_view_layer, _object_mode, _instance)     \
232 { \
233         struct ObjectsInModeIteratorData data_ = {                                \
234                 .object_mode = _object_mode,                                          \
235                 .view_layer = _view_layer,                                            \
236                 .base_active = _view_layer->basact,                                   \
237         };                                                                        \
238         ITER_BEGIN(BKE_view_layer_objects_in_mode_iterator_begin,                 \
239                    BKE_view_layer_objects_in_mode_iterator_next,                  \
240                    BKE_view_layer_objects_in_mode_iterator_end,                   \
241                    &data_, Base *, _instance)
242
243 #define FOREACH_BASE_IN_MODE_END                                              \
244         ITER_END;                                                                 \
245 } ((void)0)
246
247 #define FOREACH_BASE_IN_EDIT_MODE_BEGIN(_view_layer, _instance)               \
248         FOREACH_BASE_IN_MODE_BEGIN(_view_layer, OB_MODE_EDIT, _instance)
249
250 #define FOREACH_BASE_IN_EDIT_MODE_END                                         \
251         FOREACH_BASE_IN_MODE_END
252
253 #define FOREACH_OBJECT_IN_MODE_BEGIN(_view_layer, _object_mode, _instance)    \
254         FOREACH_BASE_IN_MODE_BEGIN(_view_layer, _object_mode, _base) {            \
255                 Object *_instance = _base->object;
256
257 #define FOREACH_OBJECT_IN_MODE_END                                            \
258         } FOREACH_BASE_IN_MODE_END
259
260 #define FOREACH_OBJECT_IN_EDIT_MODE_BEGIN(_view_layer, _instance)             \
261         FOREACH_BASE_IN_EDIT_MODE_BEGIN(_view_layer, _base) {                     \
262                 Object *_instance = _base->object;
263
264 #define FOREACH_OBJECT_IN_EDIT_MODE_END                                        \
265         } FOREACH_BASE_IN_EDIT_MODE_END
266
267 #define FOREACH_SELECTED_BASE_BEGIN(view_layer, _instance)                     \
268         ITER_BEGIN(BKE_selected_bases_iterator_begin,                              \
269                    BKE_selected_bases_iterator_next,                               \
270                    BKE_selected_bases_iterator_end,                                \
271                    view_layer, Base *, _instance)
272
273 #define FOREACH_SELECTED_BASE_END                                              \
274         ITER_END
275
276 #define FOREACH_VISIBLE_BASE_BEGIN(view_layer, _instance)                     \
277         ITER_BEGIN(BKE_visible_bases_iterator_begin,                              \
278                    BKE_visible_bases_iterator_next,                               \
279                    BKE_visible_bases_iterator_end,                                \
280                    view_layer, Base *, _instance)
281
282 #define FOREACH_VISIBLE_BASE_END                                              \
283         ITER_END
284
285
286 #define FOREACH_OBJECT_BEGIN(view_layer, _instance)                           \
287 {                                                                             \
288         Object *_instance;                                                        \
289         Base *_base;                                                              \
290         for (_base = (view_layer)->object_bases.first; _base; _base = _base->next) { \
291                 _instance = _base->object;
292
293 #define FOREACH_OBJECT_END                                                    \
294     }                                                                         \
295 } ((void)0)
296
297 #define FOREACH_OBJECT_FLAG_BEGIN(scene, view_layer, flag, _instance)         \
298 {                                                                             \
299         IteratorBeginCb func_begin;                                               \
300         IteratorCb func_next, func_end;                                           \
301         void *data_in;                                                            \
302                                                                                   \
303         if (flag == SELECT) {                                                     \
304             func_begin = &BKE_selected_objects_iterator_begin;                    \
305             func_next = &BKE_selected_objects_iterator_next;                      \
306             func_end = &BKE_selected_objects_iterator_end;                        \
307             data_in = (view_layer);                                               \
308         }                                                                         \
309         else {                                                                    \
310             func_begin = BKE_scene_objects_iterator_begin;                        \
311             func_next = BKE_scene_objects_iterator_next;                          \
312             func_end = BKE_scene_objects_iterator_end;                            \
313             data_in = (scene);                                                    \
314         }                                                                         \
315         ITER_BEGIN(func_begin, func_next, func_end, data_in, Object *, _instance)
316
317
318 #define FOREACH_OBJECT_FLAG_END                                               \
319         ITER_END;                                                                 \
320 } ((void)0)
321
322 struct ObjectsRenderableIteratorData {
323         struct Scene *scene;
324         struct Base base_temp;
325         struct Scene scene_temp;
326
327         struct {
328                 struct ViewLayer *view_layer;
329                 struct Base *base;
330                 struct Scene *set;
331         } iter;
332 };
333
334 #define FOREACH_OBJECT_RENDERABLE_BEGIN(scene_, _instance)                    \
335 {                                                                             \
336         struct ObjectsRenderableIteratorData data_ = {                                   \
337             .scene = (scene_),                                                    \
338         };                                                                        \
339         ITER_BEGIN(BKE_renderable_objects_iterator_begin,                         \
340                    BKE_renderable_objects_iterator_next,                          \
341                    BKE_renderable_objects_iterator_end,                           \
342                    &data_, Object *, _instance)
343
344
345 #define FOREACH_OBJECT_RENDERABLE_END                                         \
346         ITER_END;                                                                 \
347 } ((void)0)
348
349
350 /* layer_utils.c */
351
352 struct ObjectsInModeParams {
353         int object_mode;
354         uint no_dup_data : 1;
355
356         bool (*filter_fn)(struct Object *ob, void *user_data);
357         void  *filter_userdata;
358 };
359
360 Base **BKE_view_layer_array_from_bases_in_mode_params(
361         struct ViewLayer *view_layer, uint *r_len,
362         const struct ObjectsInModeParams *params);
363
364 struct Object **BKE_view_layer_array_from_objects_in_mode_params(
365         struct ViewLayer *view_layer, uint *len,
366         const struct ObjectsInModeParams *params);
367
368 #define BKE_view_layer_array_from_objects_in_mode(view_layer, r_len, ...) \
369         BKE_view_layer_array_from_objects_in_mode_params( \
370                 view_layer, r_len, \
371                 &(const struct ObjectsInModeParams)__VA_ARGS__)
372
373 #define BKE_view_layer_array_from_bases_in_mode(view_layer, r_len, ...) \
374         BKE_view_layer_array_from_bases_in_mode_params( \
375                 view_layer, r_len, \
376                 &(const struct ObjectsInModeParams)__VA_ARGS__)
377
378 bool BKE_view_layer_filter_edit_mesh_has_uvs(struct Object *ob, void *user_data);
379 bool BKE_view_layer_filter_edit_mesh_has_edges(struct Object *ob, void *user_data);
380
381 /* Utility macros that wrap common args (add more as needed). */
382
383 #define BKE_view_layer_array_from_objects_in_edit_mode(view_layer, r_len) \
384         BKE_view_layer_array_from_objects_in_mode( \
385         view_layer, r_len, { \
386                 .object_mode = OB_MODE_EDIT});
387
388 #define BKE_view_layer_array_from_bases_in_edit_mode(view_layer, r_len) \
389         BKE_view_layer_array_from_bases_in_mode( \
390         view_layer, r_len, { \
391                 .object_mode = OB_MODE_EDIT});
392
393 #define BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, r_len) \
394         BKE_view_layer_array_from_objects_in_mode( \
395         view_layer, r_len, { \
396                 .object_mode = OB_MODE_EDIT, \
397                 .no_dup_data = true});
398
399 #define BKE_view_layer_array_from_bases_in_edit_mode_unique_data(view_layer, r_len) \
400         BKE_view_layer_array_from_bases_in_mode( \
401         view_layer, r_len, { \
402                 .object_mode = OB_MODE_EDIT, \
403                 .no_dup_data = true});
404
405 #define BKE_view_layer_array_from_objects_in_edit_mode_unique_data_with_uvs(view_layer, r_len) \
406         BKE_view_layer_array_from_objects_in_mode( \
407         view_layer, r_len, { \
408                 .object_mode = OB_MODE_EDIT, \
409                 .no_dup_data = true, \
410                 .filter_fn = BKE_view_layer_filter_edit_mesh_has_uvs});
411
412
413 #ifdef __cplusplus
414 }
415 #endif
416
417 #endif /* __BKE_LAYER_H__ */