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
18  * \ingroup bke
19  */
20 
21 #include <string.h>
22 
23 #include "BLI_array.h"
24 
25 #include "BKE_collection.h"
26 #include "BKE_editmesh.h"
27 #include "BKE_layer.h"
28 
29 #include "DNA_ID.h"
30 #include "DNA_layer_types.h"
31 #include "DNA_mesh_types.h"
32 #include "DNA_object_types.h"
33 #include "DNA_scene_types.h"
34 
35 #include "MEM_guardedalloc.h"
36 
37 /* -------------------------------------------------------------------- */
38 /** \name Selected Object Array
39  * \{ */
40 
BKE_view_layer_array_selected_objects_params(struct ViewLayer * view_layer,const struct View3D * v3d,uint * r_len,const struct ObjectsInViewLayerParams * params)41 Object **BKE_view_layer_array_selected_objects_params(
42     struct ViewLayer *view_layer,
43     const struct View3D *v3d,
44     uint *r_len,
45     const struct ObjectsInViewLayerParams *params)
46 {
47   if (params->no_dup_data) {
48     FOREACH_SELECTED_OBJECT_BEGIN (view_layer, v3d, ob_iter) {
49       ID *id = ob_iter->data;
50       if (id) {
51         id->tag |= LIB_TAG_DOIT;
52       }
53     }
54     FOREACH_SELECTED_OBJECT_END;
55   }
56 
57   Object **object_array = NULL;
58   BLI_array_declare(object_array);
59 
60   FOREACH_SELECTED_OBJECT_BEGIN (view_layer, v3d, ob_iter) {
61     if (params->filter_fn) {
62       if (!params->filter_fn(ob_iter, params->filter_userdata)) {
63         continue;
64       }
65     }
66 
67     if (params->no_dup_data) {
68       ID *id = ob_iter->data;
69       if (id) {
70         if (id->tag & LIB_TAG_DOIT) {
71           id->tag &= ~LIB_TAG_DOIT;
72         }
73         else {
74           continue;
75         }
76       }
77     }
78 
79     BLI_array_append(object_array, ob_iter);
80   }
81   FOREACH_SELECTED_OBJECT_END;
82 
83   object_array = MEM_reallocN(object_array, sizeof(*object_array) * BLI_array_len(object_array));
84   /* We always need a valid allocation (prevent crash on free). */
85   if (object_array == NULL) {
86     object_array = MEM_mallocN(0, __func__);
87   }
88   *r_len = BLI_array_len(object_array);
89   return object_array;
90 }
91 
92 /** \} */
93 
94 /* -------------------------------------------------------------------- */
95 /** \name Objects in Mode Array
96  * \{ */
97 
BKE_view_layer_array_from_bases_in_mode_params(ViewLayer * view_layer,const View3D * v3d,uint * r_len,const struct ObjectsInModeParams * params)98 Base **BKE_view_layer_array_from_bases_in_mode_params(ViewLayer *view_layer,
99                                                       const View3D *v3d,
100                                                       uint *r_len,
101                                                       const struct ObjectsInModeParams *params)
102 {
103   if (params->no_dup_data) {
104     FOREACH_BASE_IN_MODE_BEGIN (view_layer, v3d, -1, params->object_mode, base_iter) {
105       ID *id = base_iter->object->data;
106       if (id) {
107         id->tag |= LIB_TAG_DOIT;
108       }
109     }
110     FOREACH_BASE_IN_MODE_END;
111   }
112 
113   Base **base_array = NULL;
114   BLI_array_declare(base_array);
115 
116   FOREACH_BASE_IN_MODE_BEGIN (view_layer, v3d, -1, params->object_mode, base_iter) {
117     if (params->filter_fn) {
118       if (!params->filter_fn(base_iter->object, params->filter_userdata)) {
119         continue;
120       }
121     }
122     if (params->no_dup_data) {
123       ID *id = base_iter->object->data;
124       if (id) {
125         if (id->tag & LIB_TAG_DOIT) {
126           id->tag &= ~LIB_TAG_DOIT;
127         }
128         else {
129           continue;
130         }
131       }
132     }
133     BLI_array_append(base_array, base_iter);
134   }
135   FOREACH_BASE_IN_MODE_END;
136 
137   base_array = MEM_reallocN(base_array, sizeof(*base_array) * BLI_array_len(base_array));
138   /* We always need a valid allocation (prevent crash on free). */
139   if (base_array == NULL) {
140     base_array = MEM_mallocN(0, __func__);
141   }
142   *r_len = BLI_array_len(base_array);
143   return base_array;
144 }
145 
BKE_view_layer_array_from_objects_in_mode_params(ViewLayer * view_layer,const View3D * v3d,uint * r_len,const struct ObjectsInModeParams * params)146 Object **BKE_view_layer_array_from_objects_in_mode_params(ViewLayer *view_layer,
147                                                           const View3D *v3d,
148                                                           uint *r_len,
149                                                           const struct ObjectsInModeParams *params)
150 {
151   Base **base_array = BKE_view_layer_array_from_bases_in_mode_params(
152       view_layer, v3d, r_len, params);
153   if (base_array != NULL) {
154     for (uint i = 0; i < *r_len; i++) {
155       ((Object **)base_array)[i] = base_array[i]->object;
156     }
157   }
158   return (Object **)base_array;
159 }
160 
161 /** \} */
162 
163 /* -------------------------------------------------------------------- */
164 /** \name Filter Functions
165  * \{ */
166 
BKE_view_layer_filter_edit_mesh_has_uvs(Object * ob,void * UNUSED (user_data))167 bool BKE_view_layer_filter_edit_mesh_has_uvs(Object *ob, void *UNUSED(user_data))
168 {
169   if (ob->type == OB_MESH) {
170     Mesh *me = ob->data;
171     BMEditMesh *em = me->edit_mesh;
172     if (em != NULL) {
173       if (CustomData_get_offset(&em->bm->ldata, CD_MLOOPUV) != -1) {
174         return true;
175       }
176     }
177   }
178   return false;
179 }
180 
BKE_view_layer_filter_edit_mesh_has_edges(Object * ob,void * UNUSED (user_data))181 bool BKE_view_layer_filter_edit_mesh_has_edges(Object *ob, void *UNUSED(user_data))
182 {
183   if (ob->type == OB_MESH) {
184     Mesh *me = ob->data;
185     BMEditMesh *em = me->edit_mesh;
186     if (em != NULL) {
187       if (em->bm->totedge != 0) {
188         return true;
189       }
190     }
191   }
192   return false;
193 }
194 
195 /** \} */
196