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