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