Merge branch 'master' into blender2.8
[blender.git] / source / blender / blenkernel / intern / layer_utils.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  * ***** END GPL LICENSE BLOCK *****
19  */
20
21 /** \file blender/blenkernel/intern/layer_utils.c
22  *  \ingroup bke
23  */
24
25 #include <string.h>
26
27 #include "BLI_array.h"
28 #include "BLI_listbase.h"
29
30 #include "BKE_collection.h"
31 #include "BKE_editmesh.h"
32 #include "BKE_layer.h"
33
34 #include "DNA_ID.h"
35 #include "DNA_layer_types.h"
36 #include "DNA_object_types.h"
37 #include "DNA_mesh_types.h"
38 #include "DNA_scene_types.h"
39
40 #include "MEM_guardedalloc.h"
41
42 Base **BKE_view_layer_array_from_bases_in_mode_params(
43         ViewLayer *view_layer, View3D *v3d, uint *r_len,
44         const struct ObjectsInModeParams *params)
45 {
46         if (params->no_dup_data) {
47                 FOREACH_BASE_IN_MODE_BEGIN(view_layer, v3d, -1, params->object_mode, base_iter) {
48                         ID *id = base_iter->object->data;
49                         if (id) {
50                                 id->tag |= LIB_TAG_DOIT;
51                         }
52                 } FOREACH_BASE_IN_MODE_END;
53         }
54
55         Base **base_array = NULL;
56         BLI_array_declare(base_array);
57
58         FOREACH_BASE_IN_MODE_BEGIN(view_layer, v3d, -1, params->object_mode, base_iter) {
59                 if (params->filter_fn) {
60                         if (!params->filter_fn(base_iter->object, params->filter_userdata)) {
61                                 continue;
62                         }
63                 }
64                 if (params->no_dup_data) {
65                         ID *id = base_iter->object->data;
66                         if (id) {
67                                 if (id->tag & LIB_TAG_DOIT) {
68                                         id->tag &= ~LIB_TAG_DOIT;
69                                 }
70                                 else {
71                                         continue;
72                                 }
73                         }
74                 }
75                 BLI_array_append(base_array, base_iter);
76         } FOREACH_BASE_IN_MODE_END;
77
78         base_array = MEM_reallocN(base_array, sizeof(*base_array) * BLI_array_len(base_array));
79         /* We always need a valid allocation (prevent crash on free). */
80         if (base_array == NULL) {
81                 base_array = MEM_mallocN(0, __func__);
82         }
83         *r_len = BLI_array_len(base_array);
84         return base_array;
85 }
86
87 Object **BKE_view_layer_array_from_objects_in_mode_params(
88         ViewLayer *view_layer, View3D *v3d, uint *r_len,
89         const struct ObjectsInModeParams *params)
90 {
91         Base **base_array = BKE_view_layer_array_from_bases_in_mode_params(
92                 view_layer, v3d, r_len, params);
93         if (base_array != NULL) {
94                 for (uint i = 0; i < *r_len; i++) {
95                         ((Object **)base_array)[i] = base_array[i]->object;
96                 }
97         }
98         return (Object **)base_array;
99 }
100
101 bool BKE_view_layer_filter_edit_mesh_has_uvs(Object *ob, void *UNUSED(user_data))
102 {
103         if (ob->type == OB_MESH) {
104                 Mesh *me = ob->data;
105                 BMEditMesh *em = me->edit_btmesh;
106                 if (em != NULL) {
107                         if (CustomData_get_offset(&em->bm->ldata, CD_MLOOPUV) != -1) {
108                                 return true;
109                         }
110                 }
111         }
112         return false;
113 }
114
115 bool BKE_view_layer_filter_edit_mesh_has_edges(Object *ob, void *UNUSED(user_data))
116 {
117         if (ob->type == OB_MESH) {
118                 Mesh *me = ob->data;
119                 BMEditMesh *em = me->edit_btmesh;
120                 if (em != NULL) {
121                         if (em->bm->totedge != 0) {
122                                 return true;
123                         }
124                 }
125         }
126         return false;
127 }