Merge branch 'master' into blender2.8
[blender.git] / source / blender / blenkernel / intern / group.c
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  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/blenkernel/intern/group.c
29  *  \ingroup bke
30  */
31
32
33 #include <stdio.h>
34 #include <string.h>
35 #include <math.h>
36
37 #include "MEM_guardedalloc.h"
38
39 #include "DNA_group_types.h"
40 #include "DNA_material_types.h"
41 #include "DNA_object_types.h"
42 #include "DNA_scene_types.h"
43 #include "DNA_particle_types.h"
44
45 #include "BLI_blenlib.h"
46 #include "BLI_utildefines.h"
47
48 #include "BKE_collection.h"
49 #include "BKE_global.h"
50 #include "BKE_group.h"
51 #include "BKE_icons.h"
52 #include "BKE_layer.h"
53 #include "BKE_library.h"
54 #include "BKE_main.h"
55 #include "BKE_object.h"
56 #include "BKE_scene.h"
57
58 #include "DEG_depsgraph.h"
59
60 /** Free (or release) any data used by this group (does not free the group itself). */
61 void BKE_group_free(Group *group)
62 {
63         /* No animdata here. */
64         BKE_previewimg_free(&group->preview);
65
66         if (group->view_layer != NULL) {
67                 BKE_view_layer_free(group->view_layer);
68                 group->view_layer = NULL;
69         }
70
71         if (group->collection != NULL) {
72                 BKE_collection_master_free(&group->id, false);
73                 MEM_freeN(group->collection);
74                 group->collection = NULL;
75         }
76 }
77
78 /**
79  * Run when adding new groups or during doversion.
80  */
81 void BKE_group_init(Group *group)
82 {
83         group->collection = MEM_callocN(sizeof(SceneCollection), __func__);
84         BLI_strncpy(group->collection->name, "Master Collection", sizeof(group->collection->name));
85         group->view_layer = BKE_view_layer_group_add(group);
86
87         /* Unlink the master collection. */
88         BKE_collection_unlink(group->view_layer, group->view_layer->layer_collections.first);
89
90         /* Create and link a new default collection. */
91         SceneCollection *defaut_collection = BKE_collection_add(&group->id, NULL, COLLECTION_TYPE_GROUP_INTERNAL, NULL);
92         BKE_collection_link(group->view_layer, defaut_collection);
93 }
94
95 Group *BKE_group_add(Main *bmain, const char *name)
96 {
97         Group *group;
98         
99         group = BKE_libblock_alloc(bmain, ID_GR, name, 0);
100         id_us_min(&group->id);
101         id_us_ensure_real(&group->id);
102         group->layer = (1 << 20) - 1;
103
104         group->preview = NULL;
105         BKE_group_init(group);
106         return group;
107 }
108
109 /**
110  * Only copy internal data of Group ID from source to already allocated/initialized destination.
111  * You probably nerver want to use that directly, use id_copy or BKE_id_copy_ex for typical needs.
112  *
113  * WARNING! This function will not handle ID user count!
114  *
115  * \param flag  Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more).
116  */
117 void BKE_group_copy_data(Main *UNUSED(bmain), Group *group_dst, const Group *group_src, const int flag)
118 {
119         /* We never handle usercount here for own data. */
120         const int flag_subdata = flag | LIB_ID_CREATE_NO_USER_REFCOUNT;
121
122         /* Do not copy group's preview (same behavior as for objects). */
123         if ((flag & LIB_ID_COPY_NO_PREVIEW) == 0 && false) {  /* XXX TODO temp hack */
124                 BKE_previewimg_id_copy(&group_dst->id, &group_src->id);
125         }
126         else {
127                 group_dst->preview = NULL;
128         }
129
130         group_dst->collection = MEM_dupallocN(group_src->collection);
131         SceneCollection *master_collection_src = BKE_collection_master(&group_src->id);
132         SceneCollection *master_collection_dst = BKE_collection_master(&group_dst->id);
133
134         /* Recursively creates a new SceneCollection tree. */
135         BKE_collection_copy_data(master_collection_dst, master_collection_src,
136                                  flag_subdata);
137
138         group_dst->view_layer = MEM_dupallocN(group_src->view_layer);
139         BKE_view_layer_copy_data(group_dst->view_layer, group_src->view_layer,
140                                  master_collection_dst, master_collection_src,
141                                  flag_subdata);
142 }
143
144 Group *BKE_group_copy(Main *bmain, const Group *group)
145 {
146         Group *group_copy;
147         BKE_id_copy_ex(bmain, &group->id, (ID **)&group_copy, 0, false);
148         return group_copy;
149 }
150
151 void BKE_group_make_local(Main *bmain, Group *group, const bool lib_local)
152 {
153         BKE_id_make_local_generic(bmain, &group->id, true, lib_local);
154 }
155
156 /* external */
157 static bool group_object_add_internal(Group *group, Object *ob)
158 {
159         if (group == NULL || ob == NULL) {
160                 return false;
161         }
162
163         /* For now always add to master collection of the group. */
164         SceneCollection *scene_collection = GROUP_MASTER_COLLECTION(group);
165
166         /* If the object has been added already it returns false. */
167         if (BKE_collection_object_add(&group->id, scene_collection, ob) == false) {
168                 return false;
169         }
170
171         id_us_ensure_real(&ob->id);
172         return true;
173 }
174
175 bool BKE_group_object_add(Group *group, Object *object)
176 {
177         if (group_object_add_internal(group, object)) {
178                 if ((object->flag & OB_FROMGROUP) == 0) {
179                         object->flag |= OB_FROMGROUP;
180                 }
181                 return true;
182         }
183         else {
184                 return false;
185         }
186 }
187
188 /* also used for (ob == NULL) */
189 static bool group_object_unlink_internal(Group *group, Object *ob)
190 {
191         if (group == NULL) {
192                 return false;
193         }
194
195         if (BKE_collections_object_remove(NULL, &group->id, ob, false)) {
196                 return true;
197         }
198
199         return false;
200 }
201
202 static bool group_object_cyclic_check_internal(Object *object, Group *group)
203 {
204         if (object->dup_group) {
205                 Group *dup_group = object->dup_group;
206                 if ((dup_group->id.tag & LIB_TAG_DOIT) == 0) {
207                         /* Cycle already exists in groups, let's prevent further crappyness */
208                         return true;
209                 }
210                 /* flag the object to identify cyclic dependencies in further dupli groups */
211                 dup_group->id.tag &= ~LIB_TAG_DOIT;
212
213                 if (dup_group == group)
214                         return true;
215                 else {
216                         FOREACH_GROUP_OBJECT_BEGIN(dup_group, group_object)
217                         {
218                                 if (group_object_cyclic_check_internal(group_object, dup_group)) {
219                                         return true;
220                                 }
221                         }
222                         FOREACH_GROUP_OBJECT_END;
223                 }
224
225                 /* un-flag the object, it's allowed to have the same group multiple times in parallel */
226                 dup_group->id.tag |= LIB_TAG_DOIT;
227         }
228
229         return false;
230 }
231
232 bool BKE_group_object_cyclic_check(Main *bmain, Object *object, Group *group)
233 {
234         /* first flag all groups */
235         BKE_main_id_tag_listbase(&bmain->group, LIB_TAG_DOIT, true);
236
237         return group_object_cyclic_check_internal(object, group);
238 }
239
240 bool BKE_group_object_unlink(Group *group, Object *object)
241 {
242         if (group_object_unlink_internal(group, object)) {
243                 /* object can be NULL */
244                 if (object && BKE_group_object_find(NULL, object) == NULL) {
245                         object->flag &= ~OB_FROMGROUP;
246                 }
247                 return true;
248         }
249         else {
250                 return false;
251         }
252 }
253
254 bool BKE_group_object_exists(Group *group, Object *ob)
255 {
256         if (group == NULL || ob == NULL) {
257                 return false;
258         }
259         else {
260                 return (BLI_findptr(&group->view_layer->object_bases, ob, offsetof(Base, object)));
261         }
262 }
263
264 Group *BKE_group_object_find(Group *group, Object *ob)
265 {
266         if (group)
267                 group = group->id.next;
268         else
269                 group = G.main->group.first;
270         
271         while (group) {
272                 if (BKE_group_object_exists(group, ob))
273                         return group;
274                 group = group->id.next;
275         }
276         return NULL;
277 }
278
279 bool BKE_group_is_animated(Group *group, Object *UNUSED(parent))
280 {
281         FOREACH_GROUP_OBJECT_BEGIN(group, object)
282         {
283                 if (object->proxy) {
284                         return true;
285                 }
286         }
287         FOREACH_GROUP_OBJECT_END;
288         return false;
289 }
290
291 #if 0 // add back when timeoffset & animsys work again
292 /* only replaces object strips or action when parent nla instructs it */
293 /* keep checking nla.c though, in case internal structure of strip changes */
294 static void group_replaces_nla(Object *parent, Object *target, char mode)
295 {
296         static ListBase nlastrips = {NULL, NULL};
297         static bAction *action = NULL;
298         static bool done = false;
299         bActionStrip *strip, *nstrip;
300         
301         if (mode == 's') {
302                 
303                 for (strip = parent->nlastrips.first; strip; strip = strip->next) {
304                         if (strip->object == target) {
305                                 if (done == 0) {
306                                         /* clear nla & action from object */
307                                         nlastrips = target->nlastrips;
308                                         BLI_listbase_clear(&target->nlastrips);
309                                         action = target->action;
310                                         target->action = NULL;
311                                         target->nlaflag |= OB_NLA_OVERRIDE;
312                                         done = true;
313                                 }
314                                 nstrip = MEM_dupallocN(strip);
315                                 BLI_addtail(&target->nlastrips, nstrip);
316                         }
317                 }
318         }
319         else if (mode == 'e') {
320                 if (done) {
321                         BLI_freelistN(&target->nlastrips);
322                         target->nlastrips = nlastrips;
323                         target->action = action;
324                         
325                         BLI_listbase_clear(&nlastrips);  /* not needed, but yah... :) */
326                         action = NULL;
327                         done = false;
328                 }
329         }
330 }
331 #endif
332
333 /* puts all group members in local timing system, after this call
334  * you can draw everything, leaves tags in objects to signal it needs further updating */
335
336 /* note: does not work for derivedmesh and render... it recreates all again in convertblender.c */
337 void BKE_group_handle_recalc_and_update(const struct EvaluationContext *eval_ctx, Scene *scene, Object *UNUSED(parent), Group *group)
338 {
339 #if 0 /* warning, isn't clearing the recalc flag on the object which causes it to run all the time,
340            * not just on frame change.
341            * This isn't working because the animation data is only re-evaluated on frame change so commenting for now
342            * but when its enabled at some point it will need to be changed so as not to update so much - campbell */
343
344         /* if animated group... */
345         if (parent->nlastrips.first) {
346                 int cfrao;
347                 
348                 /* switch to local time */
349                 cfrao = scene->r.cfra;
350                 
351                 /* we need a DAG per group... */
352                 for (go = group->gobject.first; go; go = go->next) {
353                         if (go->ob && go->recalc) {
354                                 go->ob->recalc = go->recalc;
355                                 
356                                 group_replaces_nla(parent, go->ob, 's');
357                                 BKE_object_handle_update(eval_ctx, scene, go->ob);
358                                 group_replaces_nla(parent, go->ob, 'e');
359                                 
360                                 /* leave recalc tags in case group members are in normal scene */
361                                 go->ob->recalc = go->recalc;
362                         }
363                 }
364                 
365                 /* restore */
366                 scene->r.cfra = cfrao;
367         }
368         else
369 #endif
370         {
371                 /* only do existing tags, as set by regular depsgraph */
372                 FOREACH_GROUP_OBJECT_BEGIN(group, object)
373                 {
374                         if (object->id.recalc & ID_RECALC_ALL) {
375                                 BKE_object_handle_update(eval_ctx, scene, object);
376                         }
377                 }
378                 FOREACH_GROUP_OBJECT_END;
379         }
380 }
381
382 /* ******** Dependency graph evaluation ******** */
383
384 static void group_eval_layer_collections(
385         const struct EvaluationContext *eval_ctx,
386         Group *group,
387         ListBase *layer_collections,
388         LayerCollection *parent_layer_collection)
389 {
390         LISTBASE_FOREACH (LayerCollection *, layer_collection, layer_collections) {
391                 /* Evaluate layer collection itself. */
392                 BKE_layer_eval_layer_collection(eval_ctx,
393                                                 layer_collection,
394                                                 parent_layer_collection);
395                 /* Evaluate nested collections. */
396                 group_eval_layer_collections(eval_ctx,
397                                              group,
398                                              &layer_collection->layer_collections,
399                                              layer_collection);
400         }
401 }
402
403 void BKE_group_eval_view_layers(const struct EvaluationContext *eval_ctx,
404                                 Group *group)
405 {
406         DEG_debug_print_eval(__func__, group->id.name, group);
407         BKE_layer_eval_layer_collection_pre(eval_ctx, &group->id, group->view_layer);
408         group_eval_layer_collections(eval_ctx,
409                                      group,
410                                      &group->view_layer->layer_collections,
411                                      NULL);
412         BKE_layer_eval_layer_collection_post(eval_ctx, group->view_layer);
413 }