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