2e4d2a7bf2cb5d52a71a71714f0fc5743e40f816
[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 Depsgraph;
39 struct ID;
40 struct Main;
41 struct Object;
42 struct Scene;
43 struct ViewLayer;
44
45 typedef struct CollectionParent {
46         struct CollectionParent *next, *prev;
47         struct Collection *collection;
48 } CollectionParent;
49
50 /* Collections */
51
52 struct Collection *BKE_collection_add(struct Main *bmain, struct Collection *parent, 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, struct Collection *parent, struct Collection *collection);
57 struct Collection *BKE_collection_copy_master(struct Main *bmain, struct Collection *collection, const int flag);
58 void               BKE_collection_copy_data(struct Main *bmain, struct Collection *collection_dst, const struct Collection *collection_src, const int flag);
59 void               BKE_collection_copy_full(struct Main *bmain, struct Collection *collection);
60 void               BKE_collection_make_local(struct Main *bmain, struct Collection *collection, const bool lib_local);
61
62 /* Master Collection for Scene */
63
64 struct Collection *BKE_collection_master(const struct Scene *scene);
65 struct Collection *BKE_collection_master_add(void);
66
67 /* Collection Objects */
68
69 bool               BKE_collection_has_object(struct Collection *collection, struct Object *ob);
70 bool               BKE_collection_has_object_recursive(struct Collection *collection, struct Object *ob);
71 struct Collection *BKE_collection_object_find(struct Main *bmain, struct Collection *collection, struct Object *ob);
72 bool               BKE_collection_is_empty(struct Collection *collection);
73
74 bool BKE_collection_object_add(struct Main *bmain, struct Collection *collection, struct Object *ob);
75 void BKE_collection_object_add_from(struct Main *bmain, struct Scene *scene, struct Object *ob_src, struct Object *ob_dst);
76 bool BKE_collection_object_remove(struct Main *bmain, struct Collection *collection, struct Object *object, const bool free_us);
77 void BKE_collection_object_move(struct Main *bmain, struct Scene *scene, struct Collection *collection_dst, struct Collection *collection_src, struct Object *ob);
78
79 bool BKE_scene_collections_object_remove(struct Main *bmain, struct Scene *scene, struct Object *object, const bool free_us);
80 void BKE_collections_object_remove_nulls(struct Main *bmain);
81 void BKE_collections_child_remove_nulls(struct Main *bmain, struct Collection *old_collection);
82
83 /* Dependencies. */
84
85 bool BKE_collection_is_in_scene(struct Collection *collection);
86 void BKE_collections_after_lib_link(struct Main *bmain);
87 bool BKE_collection_object_cyclic_check(struct Main *bmain, struct Object *object, struct Collection *collection);
88
89 /* Object list cache. */
90
91 struct ListBase BKE_collection_object_cache_get(struct Collection *collection);
92 void BKE_collection_object_cache_free(struct Collection *collection);
93
94 struct Base *BKE_collection_or_layer_objects(const struct ViewLayer *view_layer, struct Collection *collection);
95
96 /* Editing. */
97
98 struct Collection *BKE_collection_from_index(struct Scene *scene, const int index);
99 void BKE_collection_new_name_get(struct Collection *collection_parent, char *rname);
100 const char *BKE_collection_ui_name_get(struct Collection *collection);
101 bool BKE_collection_objects_select(struct ViewLayer *view_layer, struct Collection *collection, bool deselect);
102
103 /* Collection children */
104
105 bool BKE_collection_child_add(struct Main *bmain,
106                               struct Collection *parent,
107                               struct Collection *child);
108
109 bool BKE_collection_child_remove(struct Main *bmain,
110                                  struct Collection *parent,
111                                  struct Collection *child);
112
113 bool BKE_collection_move(struct Main *bmain,
114                          struct Collection *to_parent,
115                          struct Collection *from_parent,
116                          struct Collection *relative,
117                          bool relative_after,
118                          struct Collection *collection);
119
120 bool BKE_collection_find_cycle(struct Collection *new_ancestor,
121                                struct Collection *collection);
122
123
124 /* Iteration callbacks. */
125
126 typedef void (*BKE_scene_objects_Cb)(struct Object *ob, void *data);
127 typedef void (*BKE_scene_collections_Cb)(struct Collection *ob, void *data);
128
129 /* Iteratorion over objects in collection. */
130
131 #define FOREACH_COLLECTION_VISIBLE_OBJECT_RECURSIVE_BEGIN(_collection, _object, _mode) \
132     {                                                                                  \
133                 int _base_flag = (_mode == DAG_EVAL_VIEWPORT) ?                                \
134                         BASE_ENABLED_VIEWPORT : BASE_ENABLED_RENDER;                               \
135                 int _base_id = 0;                                                              \
136                 for (Base *_base = (Base*)BKE_collection_object_cache_get(_collection).first;  \
137                          _base;                                                                    \
138                          _base = _base->next, _base_id++)                                          \
139                 {                                                                              \
140                         if (_base->flag & _base_flag) {                                            \
141                                 Object *_object = _base->object;                                       \
142
143 #define FOREACH_COLLECTION_VISIBLE_OBJECT_RECURSIVE_END                               \
144                         }                                                                         \
145                 }                                                                             \
146         }
147
148 #define FOREACH_COLLECTION_OBJECT_RECURSIVE_BEGIN(_collection, _object)           \
149         for (Base *_base = (Base*)BKE_collection_object_cache_get(_collection).first; \
150              _base;                                                                   \
151              _base = _base->next)                                                     \
152         {                                                                             \
153                 Object *_object = _base->object;                                          \
154                 BLI_assert(_object != NULL);
155
156 #define FOREACH_COLLECTION_OBJECT_RECURSIVE_END                                   \
157         } ((void)0)
158
159 /* Iteration over collections in scene. */
160
161 void BKE_scene_collections_iterator_begin(struct BLI_Iterator *iter, void *data_in);
162 void BKE_scene_collections_iterator_next(struct BLI_Iterator *iter);
163 void BKE_scene_collections_iterator_end(struct BLI_Iterator *iter);
164
165 void BKE_scene_objects_iterator_begin(struct BLI_Iterator *iter, void *data_in);
166 void BKE_scene_objects_iterator_next(struct BLI_Iterator *iter);
167 void BKE_scene_objects_iterator_end(struct BLI_Iterator *iter);
168
169 #define FOREACH_SCENE_COLLECTION_BEGIN(scene, _instance)                      \
170         ITER_BEGIN(BKE_scene_collections_iterator_begin,                          \
171                    BKE_scene_collections_iterator_next,                           \
172                    BKE_scene_collections_iterator_end,                            \
173                    scene, Collection *, _instance)
174
175 #define FOREACH_SCENE_COLLECTION_END                                          \
176         ITER_END
177
178 #define FOREACH_SCENE_OBJECT_BEGIN(scene, _instance)                          \
179         ITER_BEGIN(BKE_scene_objects_iterator_begin,                              \
180                    BKE_scene_objects_iterator_next,                               \
181                    BKE_scene_objects_iterator_end,                                \
182                    scene, Object *, _instance)
183
184 #define FOREACH_SCENE_OBJECT_END                                              \
185         ITER_END
186
187 #ifdef __cplusplus
188 }
189 #endif
190
191 #endif /* __BKE_COLLECTION_H__ */