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