Merge branch 'blender2.7'
[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, struct LayerCollection *lc);
67 struct Base *BKE_view_layer_base_find(struct ViewLayer *view_layer, struct Object *ob);
68 void BKE_view_layer_base_deselect_all(struct ViewLayer *view_layer);
69
70 void BKE_view_layer_base_select(struct Base *selbase);
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(
74         struct Scene *scene_dst, const struct Scene *scene_src,
75         struct ViewLayer *view_layer_dst, const struct ViewLayer *view_layer_src,
76         const int flag);
77
78 void BKE_view_layer_rename(struct Main *bmain, struct Scene *scene, struct ViewLayer *view_layer, const char *name);
79
80 struct LayerCollection *BKE_layer_collection_get_active(struct ViewLayer *view_layer);
81 bool BKE_layer_collection_activate(struct ViewLayer *view_layer, struct LayerCollection *lc);
82 struct LayerCollection *BKE_layer_collection_activate_parent(struct ViewLayer *view_layer, struct LayerCollection *lc);
83
84 int BKE_layer_collection_count(struct ViewLayer *view_layer);
85
86 struct LayerCollection *BKE_layer_collection_from_index(struct ViewLayer *view_layer, const int index);
87 int BKE_layer_collection_findindex(struct ViewLayer *view_layer, const struct LayerCollection *lc);
88
89 void BKE_main_collection_sync(const struct Main *bmain);
90 void BKE_scene_collection_sync(const struct Scene *scene);
91 void BKE_layer_collection_sync(const struct Scene *scene, struct ViewLayer *view_layer);
92
93 void BKE_main_collection_sync_remap(const struct Main *bmain);
94
95 struct LayerCollection *BKE_layer_collection_first_from_scene_collection(
96         struct ViewLayer *view_layer, const struct Collection *collection);
97 bool BKE_view_layer_has_collection(
98         struct ViewLayer *view_layer, const struct Collection *collection);
99 bool BKE_scene_has_object(
100         struct Scene *scene, struct Object *ob);
101
102 /* selection and hiding */
103
104 bool BKE_layer_collection_objects_select(
105         struct ViewLayer *view_layer, struct LayerCollection *lc, bool deselect);
106 bool BKE_layer_collection_has_selected_objects(
107         struct ViewLayer *view_layer, struct LayerCollection *lc);
108 bool BKE_layer_collection_has_layer_collection(
109         struct LayerCollection *lc_parent, struct LayerCollection *lc_child);
110
111 void BKE_base_set_visible(struct Scene *scene, struct ViewLayer *view_layer, struct Base *base, bool extend);
112 bool BKE_layer_collection_isolate(struct Scene *scene, struct ViewLayer *view_layer, struct LayerCollection *lc, bool extend);
113 bool BKE_layer_collection_set_visible(struct ViewLayer *view_layer, struct LayerCollection *lc, const bool visible, const bool hierarchy);
114
115 /* evaluation */
116
117 void BKE_base_eval_flags(struct Base *base);
118
119 void BKE_layer_eval_view_layer_indexed(
120         struct Depsgraph *depsgraph,
121         struct Scene *scene,
122         int view_layer_index);
123
124 /* iterators */
125
126 typedef struct ObjectsVisibleIteratorData {
127         struct ViewLayer *view_layer;
128         struct View3D *v3d;
129 } ObjectsVisibleIteratorData;
130
131 void BKE_view_layer_selected_objects_iterator_begin(BLI_Iterator *iter, void *data_in);
132 void BKE_view_layer_selected_objects_iterator_next(BLI_Iterator *iter);
133 void BKE_view_layer_selected_objects_iterator_end(BLI_Iterator *iter);
134
135 void BKE_view_layer_visible_objects_iterator_begin(BLI_Iterator *iter, void *data_in);
136 void BKE_view_layer_visible_objects_iterator_next(BLI_Iterator *iter);
137 void BKE_view_layer_visible_objects_iterator_end(BLI_Iterator *iter);
138
139 void BKE_view_layer_selected_editable_objects_iterator_begin(BLI_Iterator *iter, void *data_in);
140 void BKE_view_layer_selected_editable_objects_iterator_next(BLI_Iterator *iter);
141 void BKE_view_layer_selected_editable_objects_iterator_end(BLI_Iterator *iter);
142
143 struct ObjectsInModeIteratorData {
144         int object_mode;
145         int object_type;
146         struct ViewLayer *view_layer;
147         struct View3D *v3d;
148         struct Base *base_active;
149 };
150
151 void BKE_view_layer_bases_in_mode_iterator_begin(BLI_Iterator *iter, void *data_in);
152 void BKE_view_layer_bases_in_mode_iterator_next(BLI_Iterator *iter);
153 void BKE_view_layer_bases_in_mode_iterator_end(BLI_Iterator *iter);
154
155 void BKE_view_layer_selected_bases_iterator_begin(BLI_Iterator *iter, void *data_in);
156 void BKE_view_layer_selected_bases_iterator_next(BLI_Iterator *iter);
157 void BKE_view_layer_selected_bases_iterator_end(BLI_Iterator *iter);
158
159 void BKE_view_layer_visible_bases_iterator_begin(BLI_Iterator *iter, void *data_in);
160 void BKE_view_layer_visible_bases_iterator_next(BLI_Iterator *iter);
161 void BKE_view_layer_visible_bases_iterator_end(BLI_Iterator *iter);
162
163 #define FOREACH_SELECTED_OBJECT_BEGIN(_view_layer, _v3d, _instance)           \
164 { \
165         struct ObjectsVisibleIteratorData data_ = {                               \
166                 .view_layer = _view_layer,                                            \
167                 .v3d = _v3d,                                                          \
168         };                                                                        \
169         ITER_BEGIN(BKE_view_layer_selected_objects_iterator_begin,                \
170                    BKE_view_layer_selected_objects_iterator_next,                 \
171                    BKE_view_layer_selected_objects_iterator_end,                  \
172                    &data_, Object *, _instance)
173
174 #define FOREACH_SELECTED_OBJECT_END                                           \
175         ITER_END;                                                                 \
176 } ((void)0)
177
178 #define FOREACH_SELECTED_EDITABLE_OBJECT_BEGIN(_view_layer, _v3d, _instance)  \
179 { \
180         struct ObjectsVisibleIteratorData data_ = {                               \
181                 .view_layer = _view_layer,                                            \
182                 .v3d = _v3d,                                                          \
183         };                                                                        \
184         ITER_BEGIN(BKE_view_layer_selected_editable_objects_iterator_begin,       \
185                    BKE_view_layer_selected_editable_objects_iterator_next,        \
186                    BKE_view_layer_selected_editable_objects_iterator_end,         \
187                    &data_, Object *, _instance)
188
189 #define FOREACH_SELECTED_EDITABLE_OBJECT_END                                  \
190         ITER_END;                                                                 \
191 } ((void)0)
192
193 #define FOREACH_VISIBLE_OBJECT_BEGIN(_view_layer, _v3d, _instance)            \
194 { \
195         struct ObjectsVisibleIteratorData data_ = {                               \
196                 .view_layer = _view_layer,                                            \
197                 .v3d = _v3d,                                                          \
198         };                                                                        \
199         ITER_BEGIN(BKE_view_layer_visible_objects_iterator_begin,                 \
200                    BKE_view_layer_visible_objects_iterator_next,                  \
201                    BKE_view_layer_visible_objects_iterator_end,                   \
202                    &data_, Object *, _instance)
203
204 #define FOREACH_VISIBLE_OBJECT_END                                            \
205         ITER_END;                                                                 \
206 } ((void)0)
207
208 #define FOREACH_BASE_IN_MODE_BEGIN(_view_layer, _v3d, _object_type, _object_mode, _instance) \
209 { \
210         struct ObjectsInModeIteratorData data_ = {                                \
211                 .object_mode = _object_mode,                                          \
212                 .object_type = _object_type,                                          \
213                 .view_layer = _view_layer,                                            \
214                 .v3d = _v3d,                                                          \
215                 .base_active = _view_layer->basact,                                   \
216         };                                                                        \
217         ITER_BEGIN(BKE_view_layer_bases_in_mode_iterator_begin,                   \
218                    BKE_view_layer_bases_in_mode_iterator_next,                    \
219                    BKE_view_layer_bases_in_mode_iterator_end,                     \
220                    &data_, Base *, _instance)
221
222 #define FOREACH_BASE_IN_MODE_END                                              \
223         ITER_END;                                                                 \
224 } ((void)0)
225
226 #define FOREACH_BASE_IN_EDIT_MODE_BEGIN(_view_layer, _v3d, _instance)         \
227         FOREACH_BASE_IN_MODE_BEGIN(_view_layer, _v3d, -1, OB_MODE_EDIT, _instance)
228
229 #define FOREACH_BASE_IN_EDIT_MODE_END                                         \
230         FOREACH_BASE_IN_MODE_END
231
232 #define FOREACH_OBJECT_IN_MODE_BEGIN(_view_layer, _v3d, _object_type, _object_mode, _instance) \
233         FOREACH_BASE_IN_MODE_BEGIN(_view_layer, _v3d, _object_type, _object_mode, _base) {      \
234                 Object *_instance = _base->object;
235
236 #define FOREACH_OBJECT_IN_MODE_END                                            \
237         } FOREACH_BASE_IN_MODE_END
238
239 #define FOREACH_OBJECT_IN_EDIT_MODE_BEGIN(_view_layer, _v3d, _instance)       \
240         FOREACH_BASE_IN_EDIT_MODE_BEGIN(_view_layer, _v3d, _base) {               \
241                 Object *_instance = _base->object;
242
243 #define FOREACH_OBJECT_IN_EDIT_MODE_END                                       \
244         } FOREACH_BASE_IN_EDIT_MODE_END
245
246 #define FOREACH_SELECTED_BASE_BEGIN(view_layer, _instance)                    \
247         ITER_BEGIN(BKE_view_layer_selected_bases_iterator_begin,                  \
248                    BKE_view_layer_selected_bases_iterator_next,                   \
249                    BKE_view_layer_selected_bases_iterator_end,                    \
250                    view_layer, Base *, _instance)
251
252 #define FOREACH_SELECTED_BASE_END                                             \
253         ITER_END
254
255 #define FOREACH_VISIBLE_BASE_BEGIN(_view_layer, _v3d, _instance)              \
256 { \
257         struct ObjectsVisibleIteratorData data_ = {                               \
258                 .view_layer = _view_layer,                                            \
259                 .v3d = _v3d,                                                          \
260         };                                                                        \
261         ITER_BEGIN(BKE_view_layer_visible_bases_iterator_begin,                   \
262                    BKE_view_layer_visible_bases_iterator_next,                    \
263                    BKE_view_layer_visible_bases_iterator_end,                     \
264                    &data_, Base *, _instance)
265
266 #define FOREACH_VISIBLE_BASE_END                                              \
267         ITER_END;                                                                 \
268 } ((void)0)
269
270
271 #define FOREACH_OBJECT_BEGIN(view_layer, _instance)                           \
272 {                                                                             \
273         Object *_instance;                                                        \
274         Base *_base;                                                              \
275         for (_base = (view_layer)->object_bases.first; _base; _base = _base->next) { \
276                 _instance = _base->object;
277
278 #define FOREACH_OBJECT_END                                                    \
279     }                                                                         \
280 } ((void)0)
281
282 #define FOREACH_OBJECT_FLAG_BEGIN(scene, _view_layer, _v3d, flag, _instance)  \
283 {                                                                             \
284         IteratorBeginCb func_begin;                                               \
285         IteratorCb func_next, func_end;                                           \
286         void *data_in;                                                            \
287         struct ObjectsVisibleIteratorData data_ = {                               \
288                 .view_layer = _view_layer,                                            \
289                 .v3d = _v3d,                                                          \
290         };                                                                        \
291                                                                                   \
292         if (flag == SELECT) {                                                     \
293             func_begin = &BKE_view_layer_selected_objects_iterator_begin;         \
294             func_next = &BKE_view_layer_selected_objects_iterator_next;           \
295             func_end = &BKE_view_layer_selected_objects_iterator_end;             \
296             data_in = &data_;                                                     \
297         }                                                                         \
298         else {                                                                    \
299             func_begin = BKE_scene_objects_iterator_begin;                        \
300             func_next = BKE_scene_objects_iterator_next;                          \
301             func_end = BKE_scene_objects_iterator_end;                            \
302             data_in = (scene);                                                    \
303         }                                                                         \
304         ITER_BEGIN(func_begin, func_next, func_end, data_in, Object *, _instance)
305
306
307 #define FOREACH_OBJECT_FLAG_END                                               \
308         ITER_END;                                                                 \
309 } ((void)0)
310
311 /* layer_utils.c */
312
313 struct ObjectsInModeParams {
314         int object_mode;
315         uint no_dup_data : 1;
316
317         bool (*filter_fn)(struct Object *ob, void *user_data);
318         void  *filter_userdata;
319 };
320
321 Base **BKE_view_layer_array_from_bases_in_mode_params(
322         struct ViewLayer *view_layer, struct View3D *v3d, uint *r_len,
323         const struct ObjectsInModeParams *params);
324
325 struct Object **BKE_view_layer_array_from_objects_in_mode_params(
326         struct ViewLayer *view_layer, struct View3D *v3d, uint *len,
327         const struct ObjectsInModeParams *params);
328
329 #define BKE_view_layer_array_from_objects_in_mode(view_layer, v3d, r_len, ...) \
330         BKE_view_layer_array_from_objects_in_mode_params( \
331                 view_layer, v3d, r_len, \
332                 &(const struct ObjectsInModeParams)__VA_ARGS__)
333
334 #define BKE_view_layer_array_from_bases_in_mode(view_layer, v3d, r_len, ...) \
335         BKE_view_layer_array_from_bases_in_mode_params( \
336                 view_layer, v3d, r_len, \
337                 &(const struct ObjectsInModeParams)__VA_ARGS__)
338
339 bool BKE_view_layer_filter_edit_mesh_has_uvs(struct Object *ob, void *user_data);
340 bool BKE_view_layer_filter_edit_mesh_has_edges(struct Object *ob, void *user_data);
341
342 /* Utility macros that wrap common args (add more as needed). */
343
344 #define BKE_view_layer_array_from_objects_in_edit_mode(view_layer, v3d, r_len) \
345         BKE_view_layer_array_from_objects_in_mode( \
346         view_layer, v3d, r_len, { \
347                 .object_mode = OB_MODE_EDIT});
348
349 #define BKE_view_layer_array_from_bases_in_edit_mode(view_layer, v3d, r_len) \
350         BKE_view_layer_array_from_bases_in_mode( \
351         view_layer, v3d, r_len, { \
352                 .object_mode = OB_MODE_EDIT});
353
354 #define BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, v3d, r_len) \
355         BKE_view_layer_array_from_objects_in_mode( \
356         view_layer, v3d, r_len, { \
357                 .object_mode = OB_MODE_EDIT, \
358                 .no_dup_data = true});
359
360 #define BKE_view_layer_array_from_bases_in_edit_mode_unique_data(view_layer, v3d, r_len) \
361         BKE_view_layer_array_from_bases_in_mode( \
362         view_layer, v3d, r_len, { \
363                 .object_mode = OB_MODE_EDIT, \
364                 .no_dup_data = true});
365
366 #define BKE_view_layer_array_from_objects_in_edit_mode_unique_data_with_uvs(view_layer, v3d, r_len) \
367         BKE_view_layer_array_from_objects_in_mode( \
368         view_layer, v3d, r_len, { \
369                 .object_mode = OB_MODE_EDIT, \
370                 .no_dup_data = true, \
371                 .filter_fn = BKE_view_layer_filter_edit_mesh_has_uvs});
372
373 #define BKE_view_layer_array_from_objects_in_mode_unique_data(view_layer, v3d, r_len, mode) \
374         BKE_view_layer_array_from_objects_in_mode( \
375         view_layer, v3d, r_len, { \
376                 .object_mode = mode, \
377                 .no_dup_data = true});
378
379 #ifdef __cplusplus
380 }
381 #endif
382
383 #endif /* __BKE_LAYER_H__ */