Blender  V2.93
layer_utils.c
Go to the documentation of this file.
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 
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 /* -------------------------------------------------------------------- */
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  }
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  }
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 
94 /* -------------------------------------------------------------------- */
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  }
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  }
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 
147  const View3D *v3d,
148  uint *r_len,
149  const struct ObjectsInModeParams *params)
150 {
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 
163 /* -------------------------------------------------------------------- */
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 
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 
203  const struct View3D *v3d)
204 {
205  Object *ob_active = OBACT(view_layer);
206  Object *ob_result = NULL;
207  FOREACH_SELECTED_OBJECT_BEGIN (view_layer, v3d, ob_iter) {
208  if (ob_iter == ob_active) {
209  continue;
210  }
211 
212  if (ob_result == NULL) {
213  ob_result = ob_iter;
214  }
215  else {
216  ob_result = NULL;
217  break;
218  }
219  }
221  return ob_result;
222 }
int CustomData_get_offset(const struct CustomData *data, int type)
#define FOREACH_BASE_IN_MODE_END
Definition: BKE_layer.h:270
#define FOREACH_SELECTED_OBJECT_BEGIN(_view_layer, _v3d, _instance)
Definition: BKE_layer.h:200
#define FOREACH_BASE_IN_MODE_BEGIN(_view_layer, _v3d, _object_type, _object_mode, _instance)
Definition: BKE_layer.h:254
#define FOREACH_SELECTED_OBJECT_END
Definition: BKE_layer.h:213
A (mainly) macro array library.
#define BLI_array_append(arr, item)
Definition: BLI_array.h:104
#define BLI_array_declare(arr)
Definition: BLI_array.h:62
#define BLI_array_len(arr)
Definition: BLI_array.h:74
unsigned int uint
Definition: BLI_sys_types.h:83
#define UNUSED(x)
ID and Library types, which are fundamental for sdna.
@ LIB_TAG_DOIT
Definition: DNA_ID.h:554
@ CD_MLOOPUV
Object is a sort of wrapper for general info.
@ OB_MESH
#define OBACT(_view_layer)
Read Guarded memory(de)allocation.
#define MEM_reallocN(vmemh, len)
void * user_data
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
Object ** BKE_view_layer_array_selected_objects_params(struct ViewLayer *view_layer, const struct View3D *v3d, uint *r_len, const struct ObjectsInViewLayerParams *params)
Definition: layer_utils.c:41
Object ** BKE_view_layer_array_from_objects_in_mode_params(ViewLayer *view_layer, const View3D *v3d, uint *r_len, const struct ObjectsInModeParams *params)
Definition: layer_utils.c:146
Object * BKE_view_layer_non_active_selected_object(struct ViewLayer *view_layer, const struct View3D *v3d)
Definition: layer_utils.c:202
bool BKE_view_layer_filter_edit_mesh_has_edges(Object *ob, void *UNUSED(user_data))
Definition: layer_utils.c:181
bool BKE_view_layer_filter_edit_mesh_has_uvs(Object *ob, void *UNUSED(user_data))
Definition: layer_utils.c:167
Base ** BKE_view_layer_array_from_bases_in_mode_params(ViewLayer *view_layer, const View3D *v3d, uint *r_len, const struct ObjectsInModeParams *params)
Definition: layer_utils.c:98
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:47
struct BMesh * bm
Definition: BKE_editmesh.h:52
int totedge
Definition: bmesh_class.h:297
CustomData ldata
Definition: bmesh_class.h:337
struct Object * object
Definition: DNA_ID.h:273
int tag
Definition: DNA_ID.h:292
struct BMEditMesh * edit_mesh
void * data