Fix T64842: crash rendering files with bevel curves
[blender.git] / source / blender / blenkernel / BKE_layer.h
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16
17 #ifndef __BKE_LAYER_H__
18 #define __BKE_LAYER_H__
19
20 /** \file
21  * \ingroup bke
22  */
23
24 #include "BKE_collection.h"
25
26 #include "DNA_listBase.h"
27 #include "DNA_scene_types.h"
28
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32
33 #define TODO_LAYER_OVERRIDE  /* CollectionOverride */
34 #define TODO_LAYER_OPERATORS /* collection mamanger and property panel operators */
35 #define TODO_LAYER           /* generic todo */
36
37 struct Base;
38 struct Collection;
39 struct Depsgraph;
40 struct ID;
41 struct IDProperty;
42 struct LayerCollection;
43 struct ListBase;
44 struct Main;
45 struct Object;
46 struct RenderEngine;
47 struct Scene;
48 struct View3D;
49 struct ViewLayer;
50 struct WorkSpace;
51
52 struct ViewLayer *BKE_view_layer_default_view(const struct Scene *scene);
53 struct ViewLayer *BKE_view_layer_default_render(const struct Scene *scene);
54 struct ViewLayer *BKE_view_layer_find(const struct Scene *scene, const char *layer_name);
55 struct ViewLayer *BKE_view_layer_add(struct Scene *scene, const char *name);
56
57 /* DEPRECATED */
58 struct ViewLayer *BKE_view_layer_context_active_PLACEHOLDER(const struct Scene *scene);
59
60 void BKE_view_layer_free(struct ViewLayer *view_layer);
61 void BKE_view_layer_free_ex(struct ViewLayer *view_layer, const bool do_id_user);
62
63 void BKE_view_layer_selected_objects_tag(struct ViewLayer *view_layer, const int tag);
64
65 struct Object *BKE_view_layer_camera_find(struct ViewLayer *view_layer);
66 struct ViewLayer *BKE_view_layer_find_from_collection(const struct Scene *scene,
67                                                       struct LayerCollection *lc);
68 struct Base *BKE_view_layer_base_find(struct ViewLayer *view_layer, struct Object *ob);
69 void BKE_view_layer_base_deselect_all(struct ViewLayer *view_layer);
70
71 void BKE_view_layer_base_select_and_set_active(struct ViewLayer *view_layer, struct Base *selbase);
72
73 void BKE_view_layer_copy_data(struct Scene *scene_dst,
74                               const struct Scene *scene_src,
75                               struct ViewLayer *view_layer_dst,
76                               const struct ViewLayer *view_layer_src,
77                               const int flag);
78
79 void BKE_view_layer_rename(struct Main *bmain,
80                            struct Scene *scene,
81                            struct ViewLayer *view_layer,
82                            const char *name);
83
84 struct LayerCollection *BKE_layer_collection_get_active(struct ViewLayer *view_layer);
85 bool BKE_layer_collection_activate(struct ViewLayer *view_layer, struct LayerCollection *lc);
86 struct LayerCollection *BKE_layer_collection_activate_parent(struct ViewLayer *view_layer,
87                                                              struct LayerCollection *lc);
88
89 int BKE_layer_collection_count(struct ViewLayer *view_layer);
90
91 struct LayerCollection *BKE_layer_collection_from_index(struct ViewLayer *view_layer,
92                                                         const int index);
93 int BKE_layer_collection_findindex(struct ViewLayer *view_layer, const struct LayerCollection *lc);
94
95 void BKE_main_collection_sync(const struct Main *bmain);
96 void BKE_scene_collection_sync(const struct Scene *scene);
97 void BKE_layer_collection_sync(const struct Scene *scene, struct ViewLayer *view_layer);
98
99 void BKE_main_collection_sync_remap(const struct Main *bmain);
100
101 struct LayerCollection *BKE_layer_collection_first_from_scene_collection(
102     struct ViewLayer *view_layer, const struct Collection *collection);
103 bool BKE_view_layer_has_collection(struct ViewLayer *view_layer,
104                                    const struct Collection *collection);
105 bool BKE_scene_has_object(struct Scene *scene, struct Object *ob);
106
107 /* selection and hiding */
108
109 bool BKE_layer_collection_objects_select(struct ViewLayer *view_layer,
110                                          struct LayerCollection *lc,
111                                          bool deselect);
112 bool BKE_layer_collection_has_selected_objects(struct ViewLayer *view_layer,
113                                                struct LayerCollection *lc);
114 bool BKE_layer_collection_has_layer_collection(struct LayerCollection *lc_parent,
115                                                struct LayerCollection *lc_child);
116
117 void BKE_base_set_visible(struct Scene *scene,
118                           struct ViewLayer *view_layer,
119                           struct Base *base,
120                           bool extend);
121 bool BKE_layer_collection_isolate(struct Scene *scene,
122                                   struct ViewLayer *view_layer,
123                                   struct LayerCollection *lc,
124                                   bool extend);
125 bool BKE_layer_collection_set_visible(struct ViewLayer *view_layer,
126                                       struct LayerCollection *lc,
127                                       const bool visible,
128                                       const bool hierarchy);
129
130 /* evaluation */
131
132 void BKE_base_eval_flags(struct Base *base);
133
134 void BKE_layer_eval_view_layer_indexed(struct Depsgraph *depsgraph,
135                                        struct Scene *scene,
136                                        int view_layer_index);
137
138 /* iterators */
139
140 typedef struct ObjectsVisibleIteratorData {
141   struct ViewLayer *view_layer;
142   struct View3D *v3d;
143 } ObjectsVisibleIteratorData;
144
145 void BKE_view_layer_selected_objects_iterator_begin(BLI_Iterator *iter, void *data_in);
146 void BKE_view_layer_selected_objects_iterator_next(BLI_Iterator *iter);
147 void BKE_view_layer_selected_objects_iterator_end(BLI_Iterator *iter);
148
149 void BKE_view_layer_visible_objects_iterator_begin(BLI_Iterator *iter, void *data_in);
150 void BKE_view_layer_visible_objects_iterator_next(BLI_Iterator *iter);
151 void BKE_view_layer_visible_objects_iterator_end(BLI_Iterator *iter);
152
153 void BKE_view_layer_selected_editable_objects_iterator_begin(BLI_Iterator *iter, void *data_in);
154 void BKE_view_layer_selected_editable_objects_iterator_next(BLI_Iterator *iter);
155 void BKE_view_layer_selected_editable_objects_iterator_end(BLI_Iterator *iter);
156
157 struct ObjectsInModeIteratorData {
158   int object_mode;
159   int object_type;
160   struct ViewLayer *view_layer;
161   struct View3D *v3d;
162   struct Base *base_active;
163 };
164
165 void BKE_view_layer_bases_in_mode_iterator_begin(BLI_Iterator *iter, void *data_in);
166 void BKE_view_layer_bases_in_mode_iterator_next(BLI_Iterator *iter);
167 void BKE_view_layer_bases_in_mode_iterator_end(BLI_Iterator *iter);
168
169 void BKE_view_layer_selected_bases_iterator_begin(BLI_Iterator *iter, void *data_in);
170 void BKE_view_layer_selected_bases_iterator_next(BLI_Iterator *iter);
171 void BKE_view_layer_selected_bases_iterator_end(BLI_Iterator *iter);
172
173 void BKE_view_layer_visible_bases_iterator_begin(BLI_Iterator *iter, void *data_in);
174 void BKE_view_layer_visible_bases_iterator_next(BLI_Iterator *iter);
175 void BKE_view_layer_visible_bases_iterator_end(BLI_Iterator *iter);
176
177 #define FOREACH_SELECTED_OBJECT_BEGIN(_view_layer, _v3d, _instance) \
178   { \
179     struct ObjectsVisibleIteratorData data_ = { \
180         .view_layer = _view_layer, \
181         .v3d = _v3d, \
182     }; \
183     ITER_BEGIN (BKE_view_layer_selected_objects_iterator_begin, \
184                 BKE_view_layer_selected_objects_iterator_next, \
185                 BKE_view_layer_selected_objects_iterator_end, \
186                 &data_, \
187                 Object *, \
188                 _instance)
189
190 #define FOREACH_SELECTED_OBJECT_END \
191   ITER_END; \
192   } \
193   ((void)0)
194
195 #define FOREACH_SELECTED_EDITABLE_OBJECT_BEGIN(_view_layer, _v3d, _instance) \
196   { \
197     struct ObjectsVisibleIteratorData data_ = { \
198         .view_layer = _view_layer, \
199         .v3d = _v3d, \
200     }; \
201     ITER_BEGIN (BKE_view_layer_selected_editable_objects_iterator_begin, \
202                 BKE_view_layer_selected_editable_objects_iterator_next, \
203                 BKE_view_layer_selected_editable_objects_iterator_end, \
204                 &data_, \
205                 Object *, \
206                 _instance)
207
208 #define FOREACH_SELECTED_EDITABLE_OBJECT_END \
209   ITER_END; \
210   } \
211   ((void)0)
212
213 #define FOREACH_VISIBLE_OBJECT_BEGIN(_view_layer, _v3d, _instance) \
214   { \
215     struct ObjectsVisibleIteratorData data_ = { \
216         .view_layer = _view_layer, \
217         .v3d = _v3d, \
218     }; \
219     ITER_BEGIN (BKE_view_layer_visible_objects_iterator_begin, \
220                 BKE_view_layer_visible_objects_iterator_next, \
221                 BKE_view_layer_visible_objects_iterator_end, \
222                 &data_, \
223                 Object *, \
224                 _instance)
225
226 #define FOREACH_VISIBLE_OBJECT_END \
227   ITER_END; \
228   } \
229   ((void)0)
230
231 #define FOREACH_BASE_IN_MODE_BEGIN(_view_layer, _v3d, _object_type, _object_mode, _instance) \
232   { \
233     struct ObjectsInModeIteratorData data_ = { \
234         .object_mode = _object_mode, \
235         .object_type = _object_type, \
236         .view_layer = _view_layer, \
237         .v3d = _v3d, \
238         .base_active = _view_layer->basact, \
239     }; \
240     ITER_BEGIN (BKE_view_layer_bases_in_mode_iterator_begin, \
241                 BKE_view_layer_bases_in_mode_iterator_next, \
242                 BKE_view_layer_bases_in_mode_iterator_end, \
243                 &data_, \
244                 Base *, \
245                 _instance)
246
247 #define FOREACH_BASE_IN_MODE_END \
248   ITER_END; \
249   } \
250   ((void)0)
251
252 #define FOREACH_BASE_IN_EDIT_MODE_BEGIN(_view_layer, _v3d, _instance) \
253   FOREACH_BASE_IN_MODE_BEGIN (_view_layer, _v3d, -1, OB_MODE_EDIT, _instance)
254
255 #define FOREACH_BASE_IN_EDIT_MODE_END FOREACH_BASE_IN_MODE_END
256
257 #define FOREACH_OBJECT_IN_MODE_BEGIN(_view_layer, _v3d, _object_type, _object_mode, _instance) \
258   FOREACH_BASE_IN_MODE_BEGIN (_view_layer, _v3d, _object_type, _object_mode, _base) { \
259     Object *_instance = _base->object;
260
261 #define FOREACH_OBJECT_IN_MODE_END \
262   } \
263   FOREACH_BASE_IN_MODE_END
264
265 #define FOREACH_OBJECT_IN_EDIT_MODE_BEGIN(_view_layer, _v3d, _instance) \
266   FOREACH_BASE_IN_EDIT_MODE_BEGIN (_view_layer, _v3d, _base) { \
267     Object *_instance = _base->object;
268
269 #define FOREACH_OBJECT_IN_EDIT_MODE_END \
270   } \
271   FOREACH_BASE_IN_EDIT_MODE_END
272
273 #define FOREACH_SELECTED_BASE_BEGIN(view_layer, _instance) \
274   ITER_BEGIN (BKE_view_layer_selected_bases_iterator_begin, \
275               BKE_view_layer_selected_bases_iterator_next, \
276               BKE_view_layer_selected_bases_iterator_end, \
277               view_layer, \
278               Base *, \
279               _instance)
280
281 #define FOREACH_SELECTED_BASE_END ITER_END
282
283 #define FOREACH_VISIBLE_BASE_BEGIN(_view_layer, _v3d, _instance) \
284   { \
285     struct ObjectsVisibleIteratorData data_ = { \
286         .view_layer = _view_layer, \
287         .v3d = _v3d, \
288     }; \
289     ITER_BEGIN (BKE_view_layer_visible_bases_iterator_begin, \
290                 BKE_view_layer_visible_bases_iterator_next, \
291                 BKE_view_layer_visible_bases_iterator_end, \
292                 &data_, \
293                 Base *, \
294                 _instance)
295
296 #define FOREACH_VISIBLE_BASE_END \
297   ITER_END; \
298   } \
299   ((void)0)
300
301 #define FOREACH_OBJECT_BEGIN(view_layer, _instance) \
302   { \
303     Object *_instance; \
304     Base *_base; \
305     for (_base = (view_layer)->object_bases.first; _base; _base = _base->next) { \
306       _instance = _base->object;
307
308 #define FOREACH_OBJECT_END \
309   } \
310   } \
311   ((void)0)
312
313 #define FOREACH_OBJECT_FLAG_BEGIN(scene, _view_layer, _v3d, flag, _instance) \
314   { \
315     IteratorBeginCb func_begin; \
316     IteratorCb func_next, func_end; \
317     void *data_in; \
318     struct ObjectsVisibleIteratorData data_ = { \
319         .view_layer = _view_layer, \
320         .v3d = _v3d, \
321     }; \
322 \
323     if (flag == SELECT) { \
324       func_begin = &BKE_view_layer_selected_objects_iterator_begin; \
325       func_next = &BKE_view_layer_selected_objects_iterator_next; \
326       func_end = &BKE_view_layer_selected_objects_iterator_end; \
327       data_in = &data_; \
328     } \
329     else { \
330       func_begin = BKE_scene_objects_iterator_begin; \
331       func_next = BKE_scene_objects_iterator_next; \
332       func_end = BKE_scene_objects_iterator_end; \
333       data_in = (scene); \
334     } \
335     ITER_BEGIN (func_begin, func_next, func_end, data_in, Object *, _instance)
336
337 #define FOREACH_OBJECT_FLAG_END \
338   ITER_END; \
339   } \
340   ((void)0)
341
342 /* layer_utils.c */
343
344 struct ObjectsInModeParams {
345   int object_mode;
346   uint no_dup_data : 1;
347
348   bool (*filter_fn)(struct Object *ob, void *user_data);
349   void *filter_userdata;
350 };
351
352 Base **BKE_view_layer_array_from_bases_in_mode_params(struct ViewLayer *view_layer,
353                                                       struct View3D *v3d,
354                                                       uint *r_len,
355                                                       const struct ObjectsInModeParams *params);
356
357 struct Object **BKE_view_layer_array_from_objects_in_mode_params(
358     struct ViewLayer *view_layer,
359     struct View3D *v3d,
360     uint *len,
361     const struct ObjectsInModeParams *params);
362
363 #define BKE_view_layer_array_from_objects_in_mode(view_layer, v3d, r_len, ...) \
364   BKE_view_layer_array_from_objects_in_mode_params( \
365       view_layer, v3d, r_len, &(const struct ObjectsInModeParams)__VA_ARGS__)
366
367 #define BKE_view_layer_array_from_bases_in_mode(view_layer, v3d, r_len, ...) \
368   BKE_view_layer_array_from_bases_in_mode_params( \
369       view_layer, v3d, r_len, &(const struct ObjectsInModeParams)__VA_ARGS__)
370
371 bool BKE_view_layer_filter_edit_mesh_has_uvs(struct Object *ob, void *user_data);
372 bool BKE_view_layer_filter_edit_mesh_has_edges(struct Object *ob, void *user_data);
373
374 /* Utility macros that wrap common args (add more as needed). */
375
376 #define BKE_view_layer_array_from_objects_in_edit_mode(view_layer, v3d, r_len) \
377   BKE_view_layer_array_from_objects_in_mode(view_layer, v3d, r_len, {.object_mode = OB_MODE_EDIT})
378
379 #define BKE_view_layer_array_from_bases_in_edit_mode(view_layer, v3d, r_len) \
380   BKE_view_layer_array_from_bases_in_mode(view_layer, v3d, r_len, {.object_mode = OB_MODE_EDIT})
381
382 #define BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, v3d, r_len) \
383   BKE_view_layer_array_from_objects_in_mode( \
384       view_layer, v3d, r_len, {.object_mode = OB_MODE_EDIT, .no_dup_data = true})
385
386 #define BKE_view_layer_array_from_bases_in_edit_mode_unique_data(view_layer, v3d, r_len) \
387   BKE_view_layer_array_from_bases_in_mode( \
388       view_layer, v3d, r_len, {.object_mode = OB_MODE_EDIT, .no_dup_data = true})
389
390 #define BKE_view_layer_array_from_objects_in_edit_mode_unique_data_with_uvs( \
391     view_layer, v3d, r_len) \
392   BKE_view_layer_array_from_objects_in_mode( \
393       view_layer, \
394       v3d, \
395       r_len, \
396       {.object_mode = OB_MODE_EDIT, \
397        .no_dup_data = true, \
398        .filter_fn = BKE_view_layer_filter_edit_mesh_has_uvs})
399
400 #define BKE_view_layer_array_from_objects_in_mode_unique_data(view_layer, v3d, r_len, mode) \
401   BKE_view_layer_array_from_objects_in_mode( \
402       view_layer, v3d, r_len, {.object_mode = mode, .no_dup_data = true})
403
404 #ifdef __cplusplus
405 }
406 #endif
407
408 #endif /* __BKE_LAYER_H__ */