Merge branch 'blender-v2.81-release'
[blender.git] / source / blender / blenkernel / BKE_collection.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_COLLECTION_H__
18 #define __BKE_COLLECTION_H__
19
20 /** \file
21  * \ingroup bke
22  */
23
24 #include "BLI_compiler_compat.h"
25 #include "BLI_ghash.h"
26 #include "BLI_iterator.h"
27 #include "DNA_listBase.h"
28
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32
33 /* Structs */
34
35 struct BLI_Iterator;
36 struct Base;
37 struct Collection;
38 struct Main;
39 struct Object;
40 struct Scene;
41 struct ViewLayer;
42
43 typedef struct CollectionParent {
44   struct CollectionParent *next, *prev;
45   struct Collection *collection;
46 } CollectionParent;
47
48 /* Collections */
49
50 struct Collection *BKE_collection_add(struct Main *bmain,
51                                       struct Collection *parent,
52                                       const char *name);
53 void BKE_collection_free(struct Collection *collection);
54 bool BKE_collection_delete(struct Main *bmain, struct Collection *collection, bool hierarchy);
55
56 struct Collection *BKE_collection_copy(struct Main *bmain,
57                                        struct Collection *parent,
58                                        struct Collection *collection);
59 void BKE_collection_copy_data(struct Main *bmain,
60                               struct Collection *collection_dst,
61                               const struct Collection *collection_src,
62                               const int flag);
63 void BKE_collection_make_local(struct Main *bmain,
64                                struct Collection *collection,
65                                const bool lib_local);
66
67 struct Collection *BKE_collection_duplicate(struct Main *bmain,
68                                             struct Collection *parent,
69                                             struct Collection *collection,
70                                             const bool do_hierarchy,
71                                             const bool do_objects,
72                                             const bool do_obdata);
73
74 /* Master Collection for Scene */
75
76 struct Collection *BKE_collection_master_add(void);
77 struct Scene *BKE_collection_master_scene_search(const struct Main *bmain,
78                                                  const struct Collection *master_collection);
79
80 /* Collection Objects */
81
82 bool BKE_collection_has_object(struct Collection *collection, const struct Object *ob);
83 bool BKE_collection_has_object_recursive(struct Collection *collection, struct Object *ob);
84 struct Collection *BKE_collection_object_find(struct Main *bmain,
85                                               struct Scene *scene,
86                                               struct Collection *collection,
87                                               struct Object *ob);
88 bool BKE_collection_is_empty(struct Collection *collection);
89
90 bool BKE_collection_object_add(struct Main *bmain,
91                                struct Collection *collection,
92                                struct Object *ob);
93 void BKE_collection_object_add_from(struct Main *bmain,
94                                     struct Scene *scene,
95                                     struct Object *ob_src,
96                                     struct Object *ob_dst);
97 bool BKE_collection_object_remove(struct Main *bmain,
98                                   struct Collection *collection,
99                                   struct Object *object,
100                                   const bool free_us);
101 void BKE_collection_object_move(struct Main *bmain,
102                                 struct Scene *scene,
103                                 struct Collection *collection_dst,
104                                 struct Collection *collection_src,
105                                 struct Object *ob);
106
107 bool BKE_scene_collections_object_remove(struct Main *bmain,
108                                          struct Scene *scene,
109                                          struct Object *object,
110                                          const bool free_us);
111 void BKE_collections_object_remove_nulls(struct Main *bmain);
112 void BKE_collections_child_remove_nulls(struct Main *bmain, struct Collection *old_collection);
113
114 /* Dependencies. */
115
116 bool BKE_collection_is_in_scene(struct Collection *collection);
117 void BKE_collections_after_lib_link(struct Main *bmain);
118 bool BKE_collection_object_cyclic_check(struct Main *bmain,
119                                         struct Object *object,
120                                         struct Collection *collection);
121
122 /* Object list cache. */
123
124 struct ListBase BKE_collection_object_cache_get(struct Collection *collection);
125 void BKE_collection_object_cache_free(struct Collection *collection);
126
127 struct Base *BKE_collection_or_layer_objects(const struct ViewLayer *view_layer,
128                                              struct Collection *collection);
129
130 /* Editing. */
131
132 struct Collection *BKE_collection_from_index(struct Scene *scene, const int index);
133 void BKE_collection_new_name_get(struct Collection *collection_parent, char *rname);
134 const char *BKE_collection_ui_name_get(struct Collection *collection);
135 bool BKE_collection_objects_select(struct ViewLayer *view_layer,
136                                    struct Collection *collection,
137                                    bool deselect);
138
139 /* Collection children */
140
141 bool BKE_collection_child_add(struct Main *bmain,
142                               struct Collection *parent,
143                               struct Collection *child);
144
145 bool BKE_collection_child_remove(struct Main *bmain,
146                                  struct Collection *parent,
147                                  struct Collection *child);
148
149 bool BKE_collection_move(struct Main *bmain,
150                          struct Collection *to_parent,
151                          struct Collection *from_parent,
152                          struct Collection *relative,
153                          bool relative_after,
154                          struct Collection *collection);
155
156 bool BKE_collection_find_cycle(struct Collection *new_ancestor, struct Collection *collection);
157
158 bool BKE_collection_has_collection(struct Collection *parent, struct Collection *collection);
159
160 void BKE_collection_parent_relations_rebuild(struct Collection *collection);
161 void BKE_main_collections_parent_relations_rebuild(struct Main *bmain);
162
163 /* Iteration callbacks. */
164
165 typedef void (*BKE_scene_objects_Cb)(struct Object *ob, void *data);
166 typedef void (*BKE_scene_collections_Cb)(struct Collection *ob, void *data);
167
168 /* Iteration over objects in collection. */
169
170 #define FOREACH_COLLECTION_VISIBLE_OBJECT_RECURSIVE_BEGIN(_collection, _object, _mode) \
171   { \
172     int _base_flag = (_mode == DAG_EVAL_VIEWPORT) ? BASE_ENABLED_VIEWPORT : BASE_ENABLED_RENDER; \
173     int _object_restrict_flag = (_mode == DAG_EVAL_VIEWPORT) ? OB_RESTRICT_VIEWPORT : \
174                                                                OB_RESTRICT_RENDER; \
175     int _base_id = 0; \
176     for (Base *_base = (Base *)BKE_collection_object_cache_get(_collection).first; _base; \
177          _base = _base->next, _base_id++) { \
178       Object *_object = _base->object; \
179       if ((_base->flag & _base_flag) && (_object->restrictflag & _object_restrict_flag) == 0) {
180
181 #define FOREACH_COLLECTION_VISIBLE_OBJECT_RECURSIVE_END \
182   } \
183   } \
184   } \
185   ((void)0)
186
187 #define FOREACH_COLLECTION_OBJECT_RECURSIVE_BEGIN(_collection, _object) \
188   for (Base *_base = (Base *)BKE_collection_object_cache_get(_collection).first; _base; \
189        _base = _base->next) { \
190     Object *_object = _base->object; \
191     BLI_assert(_object != NULL);
192
193 #define FOREACH_COLLECTION_OBJECT_RECURSIVE_END \
194   } \
195   ((void)0)
196
197 /* Iteration over collections in scene. */
198
199 void BKE_scene_collections_iterator_begin(struct BLI_Iterator *iter, void *data_in);
200 void BKE_scene_collections_iterator_next(struct BLI_Iterator *iter);
201 void BKE_scene_collections_iterator_end(struct BLI_Iterator *iter);
202
203 void BKE_scene_objects_iterator_begin(struct BLI_Iterator *iter, void *data_in);
204 void BKE_scene_objects_iterator_next(struct BLI_Iterator *iter);
205 void BKE_scene_objects_iterator_end(struct BLI_Iterator *iter);
206
207 #define FOREACH_SCENE_COLLECTION_BEGIN(scene, _instance) \
208   ITER_BEGIN (BKE_scene_collections_iterator_begin, \
209               BKE_scene_collections_iterator_next, \
210               BKE_scene_collections_iterator_end, \
211               scene, \
212               Collection *, \
213               _instance)
214
215 #define FOREACH_SCENE_COLLECTION_END ITER_END
216
217 #define FOREACH_COLLECTION_BEGIN(_bmain, _scene, Type, _instance) \
218   { \
219     Type _instance; \
220     Collection *_instance_next; \
221     bool is_scene_collection = (_scene) != NULL; \
222 \
223     if (_scene) { \
224       _instance_next = _scene->master_collection; \
225     } \
226     else { \
227       _instance_next = (_bmain)->collections.first; \
228     } \
229 \
230     while ((_instance = _instance_next)) { \
231       if (is_scene_collection) { \
232         _instance_next = (_bmain)->collections.first; \
233         is_scene_collection = false; \
234       } \
235       else { \
236         _instance_next = _instance->id.next; \
237       }
238
239 #define FOREACH_COLLECTION_END \
240   } \
241   } \
242   ((void)0)
243
244 #define FOREACH_SCENE_OBJECT_BEGIN(scene, _instance) \
245   ITER_BEGIN (BKE_scene_objects_iterator_begin, \
246               BKE_scene_objects_iterator_next, \
247               BKE_scene_objects_iterator_end, \
248               scene, \
249               Object *, \
250               _instance)
251
252 #define FOREACH_SCENE_OBJECT_END ITER_END
253
254 #ifdef __cplusplus
255 }
256 #endif
257
258 #endif /* __BKE_COLLECTION_H__ */