Blender V5.3
bmesh_py_types_customdata.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2012 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
11
12#include <Python.h>
13
14#include <algorithm>
15
16#include "BLI_math_vector_c.hh"
17#include "BLI_utildefines.hh"
18
19#include "bmesh.hh"
20
21#include "bmesh_py_types.hh"
24
28
29#include "BKE_customdata.hh"
30
31#include "DNA_meshdata_types.h"
32
33namespace blender {
34
41
42static const char *bpy_bm_layer_skin_radius_name = "skin_modifier_radius";
43static const char *bpy_bm_layer_skin_root_name = "skin_modifier_root";
44static const char *bpy_bm_layer_skin_loose_name = "skin_modifier_loose";
45
46static bool bpy_bm_layer_type_is_skin(const int type)
47{
48 return type == BPY_BM_LAYER_TYPE_SKIN;
49}
50
52{
53 switch (htype) {
54 case BM_VERT:
55 return &bm->vdata;
56 case BM_EDGE:
57 return &bm->edata;
58 case BM_FACE:
59 return &bm->pdata;
60 case BM_LOOP:
61 return &bm->ldata;
62 }
63
65 return nullptr;
66}
67
69{
71 const eCustomDataType real_type = bpy_bm_layer_type_is_skin(self->type) ?
73 eCustomDataType(self->type);
74 const int index_absolute = CustomData_get_layer_index_n(data, real_type, self->index);
75 if (index_absolute != -1) {
76 return &data->layers[index_absolute];
77 }
78
79 PyErr_SetString(PyExc_RuntimeError, "layer has become invalid");
80 return nullptr;
81}
82
83/* py-type definitions
84 * ******************* */
85
86/* getseters
87 * ========= */
88
89/* used for many different types. */
90
92 /* Wrap. */
93 bpy_bmlayeraccess_collection__float_doc,
94 "Generic float custom-data layer.\n"
95 "\n"
96 ":type: :class:`bmesh.types.BMLayerCollection`\\ [float]\n");
98 /* Wrap. */
99 bpy_bmlayeraccess_collection__int_doc,
100 "Generic int custom-data layer.\n"
101 "\n"
102 ":type: :class:`bmesh.types.BMLayerCollection`\\ [int]\n");
104 /* Wrap. */
105 bpy_bmlayeraccess_collection__bool_doc,
106 "Generic boolean custom-data layer.\n"
107 "\n"
108 ":type: :class:`bmesh.types.BMLayerCollection`\\ [bool]\n");
110 /* Wrap. */
111 bpy_bmlayeraccess_collection__float_vector_doc,
112 "Generic 3D vector with float precision custom-data layer.\n"
113 "\n"
114 ":type: :class:`bmesh.types.BMLayerCollection`\\ [:class:`mathutils.Vector`]\n");
116 /* Wrap. */
117 bpy_bmlayeraccess_collection__float_color_doc,
118 "Generic RGBA color with float precision custom-data layer.\n"
119 "\n"
120 ":type: :class:`bmesh.types.BMLayerCollection`\\ [:class:`mathutils.Vector`]\n");
122 /* Wrap. */
123 bpy_bmlayeraccess_collection__color_doc,
124 "Generic RGBA color with 8-bit precision custom-data layer.\n"
125 "\n"
126 ":type: :class:`bmesh.types.BMLayerCollection`\\ [:class:`mathutils.Vector`]\n");
128 /* Wrap. */
129 bpy_bmlayeraccess_collection__string_doc,
130 "Generic string custom-data layer (exposed as bytes, 255 max length).\n"
131 "\n"
132 ":type: :class:`bmesh.types.BMLayerCollection`\\ [bytes]\n");
134 /* Wrap. */
135 bpy_bmlayeraccess_collection__deform_doc,
136 "Vertex deform weight :class:`bmesh.types.BMDeformVert` (TODO).\n"
137 "\n"
138 ":type: "
139 /* TYPE DOESN'T EXIST YET */
140 ":class:`bmesh.types.BMLayerCollection`\\ [:class:`bmesh.types.BMDeformVert`]");
142 /* Wrap. */
143 bpy_bmlayeraccess_collection__shape_doc,
144 "Vertex shape-key absolute location (as a 3D Vector).\n"
145 "\n"
146 ":type: :class:`bmesh.types.BMLayerCollection`\\ [:class:`mathutils.Vector`]\n");
148 /* Wrap. */
149 bpy_bmlayeraccess_collection__uv_doc,
150 "Accessor for :class:`bmesh.types.BMLoopUV` UV (as a 2D Vector).\n"
151 "\n"
152 ":type: :class:`bmesh.types.BMLayerCollection`\\ [:class:`bmesh.types.BMLoopUV`]\n");
154 /* Wrap. */
155 bpy_bmlayeraccess_collection__skin_doc,
156 "Accessor for skin layer.\n"
157 "\n"
158 ":type: :class:`bmesh.types.BMLayerCollection`\\ [:class:`bmesh.types.BMVertSkin`]\n");
159
160static PyObject *bpy_bmlayeraccess_collection_get(BPy_BMLayerAccess *self, void *flag)
161{
162 const int type = int(POINTER_AS_INT(flag));
163
164 BPY_BM_CHECK_OBJ(self);
165
166 return BPy_BMLayerCollection_CreatePyObject(self->bm, self->htype, type);
167}
168
170 /* Wrap. */
171 bpy_bmlayercollection_active_doc,
172 "The active layer of this type (read-only).\n"
173 "\n"
174 ":type: :class:`bmesh.types.BMLayerItem` | None\n");
175static PyObject *bpy_bmlayercollection_active_get(BPy_BMLayerItem *self, void * /*flag*/)
176{
178 int index;
179
180 BPY_BM_CHECK_OBJ(self);
181
182 data = bpy_bm_customdata_get(self->bm, self->htype);
183 if (bpy_bm_layer_type_is_skin(self->type)) {
185 }
186 else {
187 index = CustomData_get_active_layer(data, eCustomDataType(self->type)); /* type relative */
188 }
189
190 if (index != -1) {
191 return BPy_BMLayerItem_CreatePyObject(self->bm, self->htype, self->type, index);
192 }
193
194 Py_RETURN_NONE;
195}
196
198 /* Wrap. */
199 bpy_bmlayercollection_is_singleton_doc,
200 "True if there can exist only one layer of this type (read-only).\n"
201 "\n"
202 ":type: bool\n");
203static PyObject *bpy_bmlayercollection_is_singleton_get(BPy_BMLayerItem *self, void * /*flag*/)
204{
205 BPY_BM_CHECK_OBJ(self);
206
207 if (bpy_bm_layer_type_is_skin(self->type)) {
208 Py_RETURN_TRUE;
209 }
210
211 return PyBool_FromLong(CustomData_layertype_is_singleton(eCustomDataType(self->type)));
212}
213
215 /* Wrap. */
216 bpy_bmlayercollection_name_doc,
217 "The layer's unique name (read-only).\n"
218 "\n"
219 ":type: str\n");
220static PyObject *bpy_bmlayeritem_name_get(BPy_BMLayerItem *self, void * /*flag*/)
221{
223
224 BPY_BM_CHECK_OBJ(self);
225
227 if (layer) {
228 return PyUnicode_FromString(layer->name);
229 }
230
231 return nullptr;
232}
233
234static PyGetSetDef bpy_bmlayeraccess_vert_getseters[] = {
235 {"deform",
236 reinterpret_cast<getter>(bpy_bmlayeraccess_collection_get),
237 static_cast<setter>(nullptr),
238 bpy_bmlayeraccess_collection__deform_doc,
239 reinterpret_cast<void *>(CD_MDEFORMVERT)},
240
241 {"float",
242 reinterpret_cast<getter>(bpy_bmlayeraccess_collection_get),
243 static_cast<setter>(nullptr),
244 bpy_bmlayeraccess_collection__float_doc,
245 reinterpret_cast<void *>(CD_PROP_FLOAT)},
246 {"bool",
247 reinterpret_cast<getter>(bpy_bmlayeraccess_collection_get),
248 static_cast<setter>(nullptr),
249 bpy_bmlayeraccess_collection__bool_doc,
250 reinterpret_cast<void *>(CD_PROP_BOOL)},
251 {"int",
252 reinterpret_cast<getter>(bpy_bmlayeraccess_collection_get),
253 static_cast<setter>(nullptr),
254 bpy_bmlayeraccess_collection__int_doc,
255 reinterpret_cast<void *>(CD_PROP_INT32)},
256 {"float_vector",
257 reinterpret_cast<getter>(bpy_bmlayeraccess_collection_get),
258 static_cast<setter>(nullptr),
259 bpy_bmlayeraccess_collection__float_vector_doc,
260 reinterpret_cast<void *>(CD_PROP_FLOAT3)},
261 {"float_color",
262 reinterpret_cast<getter>(bpy_bmlayeraccess_collection_get),
263 static_cast<setter>(nullptr),
264 bpy_bmlayeraccess_collection__float_color_doc,
265 reinterpret_cast<void *>(CD_PROP_COLOR)},
266 {"color",
267 reinterpret_cast<getter>(bpy_bmlayeraccess_collection_get),
268 static_cast<setter>(nullptr),
269 bpy_bmlayeraccess_collection__color_doc,
270 reinterpret_cast<void *>(CD_PROP_BYTE_COLOR)},
271 {"string",
272 reinterpret_cast<getter>(bpy_bmlayeraccess_collection_get),
273 static_cast<setter>(nullptr),
274 bpy_bmlayeraccess_collection__string_doc,
275 reinterpret_cast<void *>(CD_PROP_STRING)},
276
277 {"shape",
278 reinterpret_cast<getter>(bpy_bmlayeraccess_collection_get),
279 static_cast<setter>(nullptr),
280 bpy_bmlayeraccess_collection__shape_doc,
281 reinterpret_cast<void *>(CD_SHAPEKEY)},
282 {"skin",
283 reinterpret_cast<getter>(bpy_bmlayeraccess_collection_get),
284 static_cast<setter>(nullptr),
285 bpy_bmlayeraccess_collection__skin_doc,
286 reinterpret_cast<void *>(BPY_BM_LAYER_TYPE_SKIN)},
287 {nullptr, nullptr, nullptr, nullptr, nullptr} /* Sentinel */
288};
289
290static PyGetSetDef bpy_bmlayeraccess_edge_getseters[] = {
291 {"float",
292 reinterpret_cast<getter>(bpy_bmlayeraccess_collection_get),
293 static_cast<setter>(nullptr),
294 bpy_bmlayeraccess_collection__float_doc,
295 reinterpret_cast<void *>(CD_PROP_FLOAT)},
296 {"int",
297 reinterpret_cast<getter>(bpy_bmlayeraccess_collection_get),
298 static_cast<setter>(nullptr),
299 bpy_bmlayeraccess_collection__int_doc,
300 reinterpret_cast<void *>(CD_PROP_INT32)},
301 {"bool",
302 reinterpret_cast<getter>(bpy_bmlayeraccess_collection_get),
303 static_cast<setter>(nullptr),
304 bpy_bmlayeraccess_collection__bool_doc,
305 reinterpret_cast<void *>(CD_PROP_BOOL)},
306 {"float_vector",
307 reinterpret_cast<getter>(bpy_bmlayeraccess_collection_get),
308 static_cast<setter>(nullptr),
309 bpy_bmlayeraccess_collection__float_vector_doc,
310 reinterpret_cast<void *>(CD_PROP_FLOAT3)},
311 {"float_color",
312 reinterpret_cast<getter>(bpy_bmlayeraccess_collection_get),
313 static_cast<setter>(nullptr),
314 bpy_bmlayeraccess_collection__float_color_doc,
315 reinterpret_cast<void *>(CD_PROP_COLOR)},
316 {"color",
317 reinterpret_cast<getter>(bpy_bmlayeraccess_collection_get),
318 static_cast<setter>(nullptr),
319 bpy_bmlayeraccess_collection__color_doc,
320 reinterpret_cast<void *>(CD_PROP_BYTE_COLOR)},
321 {"string",
322 reinterpret_cast<getter>(bpy_bmlayeraccess_collection_get),
323 static_cast<setter>(nullptr),
324 bpy_bmlayeraccess_collection__string_doc,
325 reinterpret_cast<void *>(CD_PROP_STRING)},
326 {nullptr, nullptr, nullptr, nullptr, nullptr} /* Sentinel */
327};
328
329static PyGetSetDef bpy_bmlayeraccess_face_getseters[] = {
330 {"float",
331 reinterpret_cast<getter>(bpy_bmlayeraccess_collection_get),
332 static_cast<setter>(nullptr),
333 bpy_bmlayeraccess_collection__float_doc,
334 reinterpret_cast<void *>(CD_PROP_FLOAT)},
335 {"int",
336 reinterpret_cast<getter>(bpy_bmlayeraccess_collection_get),
337 static_cast<setter>(nullptr),
338 bpy_bmlayeraccess_collection__int_doc,
339 reinterpret_cast<void *>(CD_PROP_INT32)},
340 {"bool",
341 reinterpret_cast<getter>(bpy_bmlayeraccess_collection_get),
342 static_cast<setter>(nullptr),
343 bpy_bmlayeraccess_collection__bool_doc,
344 reinterpret_cast<void *>(CD_PROP_BOOL)},
345 {"float_vector",
346 reinterpret_cast<getter>(bpy_bmlayeraccess_collection_get),
347 static_cast<setter>(nullptr),
348 bpy_bmlayeraccess_collection__float_vector_doc,
349 reinterpret_cast<void *>(CD_PROP_FLOAT3)},
350 {"float_color",
351 reinterpret_cast<getter>(bpy_bmlayeraccess_collection_get),
352 static_cast<setter>(nullptr),
353 bpy_bmlayeraccess_collection__float_color_doc,
354 reinterpret_cast<void *>(CD_PROP_COLOR)},
355 {"color",
356 reinterpret_cast<getter>(bpy_bmlayeraccess_collection_get),
357 static_cast<setter>(nullptr),
358 bpy_bmlayeraccess_collection__color_doc,
359 reinterpret_cast<void *>(CD_PROP_BYTE_COLOR)},
360 {"string",
361 reinterpret_cast<getter>(bpy_bmlayeraccess_collection_get),
362 static_cast<setter>(nullptr),
363 bpy_bmlayeraccess_collection__string_doc,
364 reinterpret_cast<void *>(CD_PROP_STRING)},
365
366 {nullptr, nullptr, nullptr, nullptr, nullptr} /* Sentinel */
367};
368
369static PyGetSetDef bpy_bmlayeraccess_loop_getseters[] = {
370 {"float",
371 reinterpret_cast<getter>(bpy_bmlayeraccess_collection_get),
372 static_cast<setter>(nullptr),
373 bpy_bmlayeraccess_collection__float_doc,
374 reinterpret_cast<void *>(CD_PROP_FLOAT)},
375 {"int",
376 reinterpret_cast<getter>(bpy_bmlayeraccess_collection_get),
377 static_cast<setter>(nullptr),
378 bpy_bmlayeraccess_collection__int_doc,
379 reinterpret_cast<void *>(CD_PROP_INT32)},
380 {"bool",
381 reinterpret_cast<getter>(bpy_bmlayeraccess_collection_get),
382 static_cast<setter>(nullptr),
383 bpy_bmlayeraccess_collection__bool_doc,
384 reinterpret_cast<void *>(CD_PROP_BOOL)},
385 {"float_vector",
386 reinterpret_cast<getter>(bpy_bmlayeraccess_collection_get),
387 static_cast<setter>(nullptr),
388 bpy_bmlayeraccess_collection__float_vector_doc,
389 reinterpret_cast<void *>(CD_PROP_FLOAT3)},
390 {"float_color",
391 reinterpret_cast<getter>(bpy_bmlayeraccess_collection_get),
392 static_cast<setter>(nullptr),
393 bpy_bmlayeraccess_collection__float_color_doc,
394 reinterpret_cast<void *>(CD_PROP_COLOR)},
395 {"string",
396 reinterpret_cast<getter>(bpy_bmlayeraccess_collection_get),
397 static_cast<setter>(nullptr),
398 bpy_bmlayeraccess_collection__string_doc,
399 reinterpret_cast<void *>(CD_PROP_STRING)},
400 {"uv",
401 reinterpret_cast<getter>(bpy_bmlayeraccess_collection_get),
402 static_cast<setter>(nullptr),
403 bpy_bmlayeraccess_collection__uv_doc,
404 reinterpret_cast<void *>(CD_PROP_FLOAT2)},
405 {"color",
406 reinterpret_cast<getter>(bpy_bmlayeraccess_collection_get),
407 static_cast<setter>(nullptr),
408 bpy_bmlayeraccess_collection__color_doc,
409 reinterpret_cast<void *>(CD_PROP_BYTE_COLOR)},
410
411 {nullptr, nullptr, nullptr, nullptr, nullptr} /* Sentinel */
412};
413
414static PyGetSetDef bpy_bmlayercollection_getseters[] = {
415 /* BMESH_TODO, make writeable */
416 {"active",
417 reinterpret_cast<getter>(bpy_bmlayercollection_active_get),
418 static_cast<setter>(nullptr),
419 bpy_bmlayercollection_active_doc,
420 nullptr},
421 {"is_singleton",
422 reinterpret_cast<getter>(bpy_bmlayercollection_is_singleton_get),
423 static_cast<setter>(nullptr),
424 bpy_bmlayercollection_is_singleton_doc,
425 nullptr},
426
427 {nullptr, nullptr, nullptr, nullptr, nullptr} /* Sentinel */
428};
429
430static PyGetSetDef bpy_bmlayeritem_getseters[] = {
431 /* BMESH_TODO, make writeable */
432 {"name",
433 reinterpret_cast<getter>(bpy_bmlayeritem_name_get),
434 static_cast<setter>(nullptr),
435 bpy_bmlayercollection_name_doc,
436 nullptr},
437
438 {nullptr, nullptr, nullptr, nullptr, nullptr} /* Sentinel */
439};
440
441/* Methods
442 * ======= */
443
444/* BMLayerCollection
445 * ----------------- */
446
448 /* Wrap. */
449 bpy_bmlayeritem_copy_from_doc,
450 ".. method:: copy_from(other)\n"
451 "\n"
452 " Copy data from another layer.\n"
453 "\n"
454 " :param other: Another layer to copy from.\n"
455 " :type other: :class:`bmesh.types.BMLayerItem`\n");
457{
458 const char *error_prefix = "layer.copy_from(...)";
460
461 if (!BPy_BMLayerItem_Check(value)) {
462 PyErr_Format(PyExc_TypeError,
463 "%s: expected BMLayerItem, not '%.200s'",
464 error_prefix,
465 Py_TYPE(value)->tp_name);
466 return nullptr;
467 }
468
469 BPY_BM_CHECK_OBJ(self);
470 BPY_BM_CHECK_SOURCE_OBJ(self->bm, error_prefix, value);
471
472 if ((self->htype != value->htype) || (self->type != value->type)) {
473 PyErr_Format(PyExc_ValueError, "%s: layer type mismatch", error_prefix);
474 return nullptr;
475 }
476
477 if (self->index == value->index) {
478 Py_RETURN_NONE;
479 }
480
481 data = bpy_bm_customdata_get(self->bm, self->htype);
482
483 if ((bpy_bmlayeritem_get(self) == nullptr) || (bpy_bmlayeritem_get(value) == nullptr)) {
484 return nullptr;
485 }
486
487 const eCustomDataType real_type = bpy_bm_layer_type_is_skin(self->type) ?
489 eCustomDataType(self->type);
490 BM_data_layer_copy(self->bm, data, real_type, value->index, self->index);
491
492 Py_RETURN_NONE;
493}
494
495/* similar to new(), but no name arg. */
497 /* Wrap. */
498 bpy_bmlayercollection_verify_doc,
499 ".. method:: verify()\n"
500 "\n"
501 " Create a new layer or return an existing active layer\n"
502 "\n"
503 " :return: The newly created layer, or the existing active layer.\n"
504 " :rtype: :class:`bmesh.types.BMLayerItem`\n");
506{
507 int index;
509
510 BPY_BM_CHECK_OBJ(self);
511
512 data = bpy_bm_customdata_get(self->bm, self->htype);
513
514 if (bpy_bm_layer_type_is_skin(self->type)) {
516 if (index == -1) {
517 /* Because adding CustomData layers to a bmesh will invalidate any existing pointers
518 * in Python objects we can't lazily add the associated boolean layers later on. */
523 }
524 BLI_assert(index != -1);
525 return BPy_BMLayerItem_CreatePyObject(self->bm, self->htype, self->type, index);
526 }
527
528 index = CustomData_get_active_layer(data, eCustomDataType(self->type)); /* type relative */
529
530 if (index == -1) {
531 BM_data_layer_add(self->bm, data, self->type);
532 index = 0;
533 }
534 if (self->type == CD_PROP_FLOAT2 && self->htype == BM_LOOP) {
535 /* Because adding CustomData layers to a bmesh will invalidate any existing pointers
536 * in Py objects we can't lazily add the associated bool layers. So add them all right
537 * now. */
539 }
540
541 BLI_assert(index >= 0);
542
543 return BPy_BMLayerItem_CreatePyObject(self->bm, self->htype, self->type, index);
544}
545
547 /* Wrap. */
548 bpy_bmlayercollection_new_doc,
549 ".. method:: new(name=\"\")\n"
550 "\n"
551 " Create a new layer\n"
552 "\n"
553 " :param name: Optional name argument (will be made unique).\n"
554 " :type name: str\n"
555 " :return: The newly created layer.\n"
556 " :rtype: :class:`bmesh.types.BMLayerItem`\n");
557static PyObject *bpy_bmlayercollection_new(BPy_BMLayerCollection *self, PyObject *args)
558{
559 const char *name = nullptr;
560 int index;
562
563 BPY_BM_CHECK_OBJ(self);
564
565 if (!PyArg_ParseTuple(args,
566 "|" /* Optional arguments. */
567 "s" /* `name` */
568 ":new",
569 &name))
570 {
571 return nullptr;
572 }
573
574 data = bpy_bm_customdata_get(self->bm, self->htype);
575
576 if (bpy_bm_layer_type_is_skin(self->type)) {
578 PyErr_SetString(PyExc_ValueError, "layers.new(): is a singleton, use verify() instead");
579 return nullptr;
580 }
581 /* The name is reserved; user-supplied names are not supported for this composite type. */
586 BLI_assert(index != -1);
587 return BPy_BMLayerItem_CreatePyObject(self->bm, self->htype, self->type, index);
588 }
589
592 {
593 PyErr_SetString(PyExc_ValueError, "layers.new(): is a singleton, use verify() instead");
594 return nullptr;
595 }
596
597 if (name) {
598 BM_data_layer_add_named(self->bm, data, self->type, name);
599 }
600 else {
601 BM_data_layer_add(self->bm, data, self->type);
602 }
603
604 if (self->type == CD_PROP_FLOAT2 && self->htype == BM_LOOP) {
605 /* Because adding CustomData layers to a bmesh will invalidate any existing pointers
606 * in Py objects we can't lazily add the associated bool layers. So add them all right
607 * now. */
609 }
610
612 BLI_assert(index >= 0);
613
614 return BPy_BMLayerItem_CreatePyObject(self->bm, self->htype, self->type, index);
615}
616
618 /* Wrap. */
619 bpy_bmlayercollection_remove_doc,
620 ".. method:: remove(layer)\n"
621 "\n"
622 " Remove a layer\n"
623 "\n"
624 " :param layer: The layer to remove.\n"
625 " :type layer: :class:`bmesh.types.BMLayerItem`\n");
627{
629
630 BPY_BM_CHECK_OBJ(self);
631
632 if (!BPy_BMLayerItem_Check(value)) {
633 PyErr_Format(PyExc_TypeError,
634 "layers.remove(x): expected BMLayerItem, not '%.200s'",
635 Py_TYPE(value)->tp_name);
636 return nullptr;
637 }
638
639 BPY_BM_CHECK_OBJ(value);
640
641 if ((self->bm != value->bm) || (self->type != value->type) || (self->htype != value->htype)) {
642 PyErr_SetString(PyExc_ValueError, "layers.remove(x): x not in layers");
643 return nullptr;
644 }
645
646 data = bpy_bm_customdata_get(self->bm, self->htype);
647 if (bpy_bm_layer_type_is_skin(self->type)) {
651 }
652 else {
653 BM_data_layer_free_n(self->bm, data, self->type, value->index);
654 }
655
656 Py_RETURN_NONE;
657}
658
660 /* Wrap. */
661 bpy_bmlayercollection_keys_doc,
662 ".. method:: keys()\n"
663 "\n"
664 " Return the identifiers of collection members\n"
665 " (matching Python's dict.keys() functionality).\n"
666 "\n"
667 " :return: the identifiers for each member of this collection.\n"
668 " :rtype: list[str]\n");
670{
671 PyObject *ret;
672 PyObject *item;
673 int index;
675 int tot, i;
676
677 BPY_BM_CHECK_OBJ(self);
678
679 data = bpy_bm_customdata_get(self->bm, self->htype);
680
681 if (bpy_bm_layer_type_is_skin(self->type)) {
683 return PyList_New(0);
684 }
685 ret = PyList_New(1);
686 PyList_SET_ITEM(ret, 0, PyUnicode_FromString(bpy_bm_layer_skin_radius_name));
687 return ret;
688 }
689
690 /* Absolute, but no need to make relative. */
692
693 tot = (index != -1) ? CustomData_number_of_layers(data, eCustomDataType(self->type)) : 0;
694
695 ret = PyList_New(tot);
696
697 for (i = 0; tot-- > 0; index++) {
698 item = PyUnicode_FromString(data->layers[index].name);
699 PyList_SET_ITEM(ret, i++, item);
700 }
701
702 return ret;
703}
704
706 /* Wrap. */
707 bpy_bmlayercollection_items_doc,
708 ".. method:: items()\n"
709 "\n"
710 " Return the (key, value) pairs of collection members\n"
711 " (matching Python's dict.items() functionality).\n"
712 "\n"
713 " :return: (key, value) pairs for each member of this collection.\n"
714 " :rtype: list[tuple[str, :class:`bmesh.types.BMLayerItem`]]\n");
716{
717 PyObject *ret;
718 PyObject *item;
719 int index;
721 int tot, i;
722
723 BPY_BM_CHECK_OBJ(self);
724
725 data = bpy_bm_customdata_get(self->bm, self->htype);
726
727 if (bpy_bm_layer_type_is_skin(self->type)) {
729 if (index == -1) {
730 return PyList_New(0);
731 }
732 ret = PyList_New(1);
733 item = PyTuple_New(2);
735 PyUnicode_FromString(bpy_bm_layer_skin_radius_name),
736 BPy_BMLayerItem_CreatePyObject(self->bm, self->htype, self->type, index));
737 PyList_SET_ITEM(ret, 0, item);
738 return ret;
739 }
740
742 tot = (index != -1) ? CustomData_number_of_layers(data, eCustomDataType(self->type)) : 0;
743
744 ret = PyList_New(tot);
745
746 for (i = 0; tot-- > 0; index++) {
747 item = PyTuple_New(2);
749 PyUnicode_FromString(data->layers[index].name),
750 BPy_BMLayerItem_CreatePyObject(self->bm, self->htype, self->type, i));
751 PyList_SET_ITEM(ret, i++, item);
752 }
753
754 return ret;
755}
756
758 /* Wrap. */
759 bpy_bmlayercollection_values_doc,
760 ".. method:: values()\n"
761 "\n"
762 " Return the values of collection\n"
763 " (matching Python's dict.values() functionality).\n"
764 "\n"
765 " :return: the members of this collection.\n"
766 " :rtype: list[:class:`bmesh.types.BMLayerItem`]\n");
768{
769 PyObject *ret;
770 PyObject *item;
771 int index;
773 int tot, i;
774
775 BPY_BM_CHECK_OBJ(self);
776
777 data = bpy_bm_customdata_get(self->bm, self->htype);
778
779 if (bpy_bm_layer_type_is_skin(self->type)) {
781 if (index == -1) {
782 return PyList_New(0);
783 }
784 ret = PyList_New(1);
785 PyList_SET_ITEM(
786 ret, 0, BPy_BMLayerItem_CreatePyObject(self->bm, self->htype, self->type, index));
787 return ret;
788 }
789
791 tot = (index != -1) ? CustomData_number_of_layers(data, eCustomDataType(self->type)) : 0;
792
793 ret = PyList_New(tot);
794
795 for (i = 0; tot-- > 0; index++) {
796 item = BPy_BMLayerItem_CreatePyObject(self->bm, self->htype, self->type, i);
797 PyList_SET_ITEM(ret, i++, item);
798 }
799
800 return ret;
801}
802
804 /* Wrap. */
805 bpy_bmlayercollection_get_doc,
806 ".. method:: get(key, default=None)\n"
807 "\n"
808 " Returns the value of the layer matching the key or default\n"
809 " when not found (matches Python's dictionary function of the same name).\n"
810 "\n"
811 " :param key: The key associated with the layer.\n"
812 " :type key: str\n"
813 " :param default: Optional argument for the value to return if\n"
814 " *key* is not found.\n"
815 " :type default: Any\n"
816 " :return: The layer matching the key or the default value.\n"
817 " :rtype: :class:`bmesh.types.BMLayerItem` | Any\n");
818static PyObject *bpy_bmlayercollection_get(BPy_BMLayerCollection *self, PyObject *args)
819{
820 const char *key;
821 PyObject *def = Py_None;
822
823 BPY_BM_CHECK_OBJ(self);
824
825 if (!PyArg_ParseTuple(args,
826 "s" /* `key` */
827 "|" /* Optional arguments. */
828 "O" /* `default` */
829 ":get",
830 &key,
831 &def))
832 {
833 return nullptr;
834 }
835
837 int index;
838
839 data = bpy_bm_customdata_get(self->bm, self->htype);
840 if (bpy_bm_layer_type_is_skin(self->type)) {
843 -1;
844 }
845 else {
846 index = CustomData_get_named_layer(data, eCustomDataType(self->type), key); /* type relative */
847 }
848
849 if (index != -1) {
850 return BPy_BMLayerItem_CreatePyObject(self->bm, self->htype, self->type, index);
851 }
852
853 return Py_NewRef(def);
854}
855
856static PyMethodDef bpy_bmlayeritem_methods[] = {
857 {"copy_from",
858 reinterpret_cast<PyCFunction>(bpy_bmlayeritem_copy_from),
859 METH_O,
860 bpy_bmlayeritem_copy_from_doc},
861 {nullptr, nullptr, 0, nullptr},
862};
863
864#ifdef __GNUC__
865# ifdef __clang__
866# pragma clang diagnostic push
867# pragma clang diagnostic ignored "-Wcast-function-type"
868# else
869# pragma GCC diagnostic push
870# pragma GCC diagnostic ignored "-Wcast-function-type"
871# endif
872#endif
873
874static PyMethodDef bpy_bmelemseq_methods[] = {
875 {"verify",
876 reinterpret_cast<PyCFunction>(bpy_bmlayercollection_verify),
877 METH_NOARGS,
878 bpy_bmlayercollection_verify_doc},
879 {"new",
880 reinterpret_cast<PyCFunction>(bpy_bmlayercollection_new),
881 METH_VARARGS,
882 bpy_bmlayercollection_new_doc},
883 {"remove",
884 reinterpret_cast<PyCFunction>(bpy_bmlayercollection_remove),
885 METH_O,
886 bpy_bmlayercollection_remove_doc},
887
888 {"keys",
889 reinterpret_cast<PyCFunction>(bpy_bmlayercollection_keys),
890 METH_NOARGS,
891 bpy_bmlayercollection_keys_doc},
892 {"values",
893 reinterpret_cast<PyCFunction>(bpy_bmlayercollection_values),
894 METH_NOARGS,
895 bpy_bmlayercollection_values_doc},
896 {"items",
897 reinterpret_cast<PyCFunction>(bpy_bmlayercollection_items),
898 METH_NOARGS,
899 bpy_bmlayercollection_items_doc},
900 {"get",
901 reinterpret_cast<PyCFunction>(bpy_bmlayercollection_get),
902 METH_VARARGS,
903 bpy_bmlayercollection_get_doc},
904 {nullptr, nullptr, 0, nullptr},
905};
906
907#ifdef __GNUC__
908# ifdef __clang__
909# pragma clang diagnostic pop
910# else
911# pragma GCC diagnostic pop
912# endif
913#endif
914
915/* Sequences
916 * ========= */
917
932
934 const char *keyname)
935{
937 int index;
938
939 BPY_BM_CHECK_OBJ(self);
940
941 data = bpy_bm_customdata_get(self->bm, self->htype);
942 if (bpy_bm_layer_type_is_skin(self->type)) {
945 -1;
946 }
947 else {
949 data, eCustomDataType(self->type), keyname); /* type relative */
950 }
951
952 if (index != -1) {
953 return BPy_BMLayerItem_CreatePyObject(self->bm, self->htype, self->type, index);
954 }
955
956 PyErr_Format(PyExc_KeyError, "BMLayerCollection[key]: key \"%.200s\" not found", keyname);
957 return nullptr;
958}
959
961 Py_ssize_t keynum)
962{
963 Py_ssize_t len;
964 BPY_BM_CHECK_OBJ(self);
965
967
968 if (keynum < 0) {
969 keynum += len;
970 }
971 if (keynum >= 0) {
972 if (keynum < len) {
973 if (bpy_bm_layer_type_is_skin(self->type)) {
977 BLI_assert(index != -1);
978 return BPy_BMLayerItem_CreatePyObject(self->bm, self->htype, self->type, index);
979 }
980 return BPy_BMLayerItem_CreatePyObject(self->bm, self->htype, self->type, keynum);
981 }
982 }
983
984 PyErr_Format(PyExc_IndexError, "BMLayerCollection[index]: index %d out of range", keynum);
985 return nullptr;
986}
987
989 Py_ssize_t start,
990 Py_ssize_t stop)
991{
992 const Py_ssize_t len = bpy_bmlayercollection_length(self);
993 int count = 0;
994
995 PyObject *tuple;
996
997 BPY_BM_CHECK_OBJ(self);
998
999 start = std::min(start, len);
1000 stop = std::min(stop, len);
1001
1002 tuple = PyTuple_New(stop - start);
1003
1004 for (count = start; count < stop; count++) {
1005 PyTuple_SET_ITEM(tuple, count - start, bpy_bmlayercollection_subscript_int(self, count));
1006 }
1007
1008 return tuple;
1009}
1010
1011static PyObject *bpy_bmlayercollection_subscript(BPy_BMLayerCollection *self, PyObject *key)
1012{
1013 /* don't need error check here */
1014 if (PyUnicode_Check(key)) {
1015 return bpy_bmlayercollection_subscript_str(self, PyUnicode_AsUTF8(key));
1016 }
1017 if (PyIndex_Check(key)) {
1018 const Py_ssize_t i = PyNumber_AsSsize_t(key, PyExc_IndexError);
1019 if (i == -1 && PyErr_Occurred()) {
1020 return nullptr;
1021 }
1023 }
1024 if (PySlice_Check(key)) {
1025 PySliceObject *key_slice = reinterpret_cast<PySliceObject *>(key);
1026 Py_ssize_t step = 1;
1027
1028 if (key_slice->step != Py_None && !_PyEval_SliceIndex(key_slice->step, &step)) {
1029 return nullptr;
1030 }
1031 if (step != 1) {
1032 PyErr_SetString(PyExc_TypeError, "BMLayerCollection[slice]: slice steps not supported");
1033 return nullptr;
1034 }
1035 if (key_slice->start == Py_None && key_slice->stop == Py_None) {
1036 return bpy_bmlayercollection_subscript_slice(self, 0, PY_SSIZE_T_MAX);
1037 }
1038
1039 Py_ssize_t start = 0, stop = PY_SSIZE_T_MAX;
1040
1041 /* avoid PySlice_GetIndicesEx because it needs to know the length ahead of time. */
1042 if (key_slice->start != Py_None && !_PyEval_SliceIndex(key_slice->start, &start)) {
1043 return nullptr;
1044 }
1045 if (key_slice->stop != Py_None && !_PyEval_SliceIndex(key_slice->stop, &stop)) {
1046 return nullptr;
1047 }
1048
1049 if (start < 0 || stop < 0) {
1050 /* only get the length for negative values */
1051 const Py_ssize_t len = bpy_bmlayercollection_length(self);
1052 if (start < 0) {
1053 start += len;
1054 CLAMP_MIN(start, 0);
1055 }
1056 if (stop < 0) {
1057 stop += len;
1058 CLAMP_MIN(stop, 0);
1059 }
1060 }
1061
1062 if (stop - start <= 0) {
1063 return PyTuple_New(0);
1064 }
1065
1066 return bpy_bmlayercollection_subscript_slice(self, start, stop);
1067 }
1068
1069 PyErr_SetString(PyExc_AttributeError, "BMLayerCollection[key]: invalid key, key must be an int");
1070 return nullptr;
1071}
1072
1074{
1075 const char *keyname = PyUnicode_AsUTF8(value);
1077 int index;
1078
1079 BPY_BM_CHECK_INT(self);
1080
1081 if (keyname == nullptr) {
1082 PyErr_SetString(PyExc_TypeError, "BMLayerCollection.__contains__: expected a string");
1083 return -1;
1084 }
1085
1086 data = bpy_bm_customdata_get(self->bm, self->htype);
1087 if (bpy_bm_layer_type_is_skin(self->type)) {
1090 -1;
1091 }
1092 else {
1094 }
1095
1096 return (index != -1) ? 1 : 0;
1097}
1098
1099static PySequenceMethods bpy_bmlayercollection_as_sequence = {
1100 /*sq_length*/ reinterpret_cast<lenfunc>(bpy_bmlayercollection_length),
1101 /*sq_concat*/ nullptr,
1102 /*sq_repeat*/ nullptr,
1103 /* Only set this so `PySequence_Check()` returns True. */
1104 /*sq_item*/ reinterpret_cast<ssizeargfunc>(bpy_bmlayercollection_subscript_int),
1105 /*was_sq_slice*/ nullptr, /* DEPRECATED. */
1106 /*sq_ass_item*/ nullptr,
1107 /*was_sq_ass_slice*/ nullptr, /* DEPRECATED. */
1108 /*sq_contains*/ reinterpret_cast<objobjproc>(bpy_bmlayercollection_contains),
1109 /*sq_inplace_concat*/ nullptr,
1110 /*sq_inplace_repeat*/ nullptr,
1111};
1112
1113static PyMappingMethods bpy_bmlayercollection_as_mapping = {
1114 /*mp_length*/ reinterpret_cast<lenfunc>(bpy_bmlayercollection_length),
1115 /*mp_subscript*/ reinterpret_cast<binaryfunc>(bpy_bmlayercollection_subscript),
1116 /*mp_ass_subscript*/ static_cast<objobjargproc>(nullptr),
1117};
1118
1119/* Iterator
1120 * -------- */
1121
1123{
1124 /* fake it with a list iterator */
1125 PyObject *ret;
1126 PyObject *iter = nullptr;
1127
1128 BPY_BM_CHECK_OBJ(self);
1129
1130 ret = bpy_bmlayercollection_subscript_slice(self, 0, PY_SSIZE_T_MAX);
1131
1132 if (ret) {
1133 iter = PyObject_GetIter(ret);
1134 Py_DECREF(ret);
1135 }
1136
1137 return iter;
1138}
1139
1141 /* Wrap. */
1142 bpy_bmlayeraccess_type_doc,
1143 "Exposes custom-data layer attributes.\n");
1145 /* Wrap. */
1146 bpy_bmlayercollection_type_doc,
1147 "Gives access to a collection of custom-data layers of the same type and behaves "
1148 "like Python dictionaries, "
1149 "except for the ability to do list like index access.\n");
1151 /* Wrap. */
1152 bpy_bmlayeritem_type_doc,
1153 "Exposes a single custom data layer, "
1154 "its main purpose is for use as an item accessor to custom-data when used with "
1155 "vert/edge/face/loop data.\n");
1156
1157PyTypeObject BPy_BMLayerAccessVert_Type; /* bm.verts.layers */
1158PyTypeObject BPy_BMLayerAccessEdge_Type; /* bm.edges.layers */
1159PyTypeObject BPy_BMLayerAccessFace_Type; /* bm.faces.layers */
1160PyTypeObject BPy_BMLayerAccessLoop_Type; /* bm.loops.layers */
1161PyTypeObject BPy_BMLayerCollection_Type; /* bm.loops.layers.uv */
1162PyTypeObject BPy_BMLayerItem_Type; /* bm.loops.layers.uv["UVMap"] */
1163
1164PyObject *BPy_BMLayerAccess_CreatePyObject(BMesh *bm, const char htype)
1165{
1166 BPy_BMLayerAccess *self;
1167 PyTypeObject *type;
1168
1169 switch (htype) {
1170 case BM_VERT:
1172 break;
1173 case BM_EDGE:
1175 break;
1176 case BM_FACE:
1178 break;
1179 case BM_LOOP:
1181 break;
1182 default: {
1184 type = nullptr;
1185 break;
1186 }
1187 }
1188
1189 self = PyObject_New(BPy_BMLayerAccess, type);
1190 self->bm = bm;
1191 self->htype = htype;
1192 return reinterpret_cast<PyObject *>(self);
1193}
1194
1195PyObject *BPy_BMLayerCollection_CreatePyObject(BMesh *bm, const char htype, int type)
1196{
1198 self->bm = bm;
1199 self->htype = htype;
1200 self->type = type;
1201 return reinterpret_cast<PyObject *>(self);
1202}
1203
1204PyObject *BPy_BMLayerItem_CreatePyObject(BMesh *bm, const char htype, int type, int index)
1205{
1207 self->bm = bm;
1208 self->htype = htype;
1209 self->type = type;
1210 self->index = index;
1211 return reinterpret_cast<PyObject *>(self);
1212}
1213
1215{
1216 BPy_BMLayerAccessVert_Type.tp_basicsize = sizeof(BPy_BMLayerAccess);
1217 BPy_BMLayerAccessEdge_Type.tp_basicsize = sizeof(BPy_BMLayerAccess);
1218 BPy_BMLayerAccessFace_Type.tp_basicsize = sizeof(BPy_BMLayerAccess);
1219 BPy_BMLayerAccessLoop_Type.tp_basicsize = sizeof(BPy_BMLayerAccess);
1221 BPy_BMLayerItem_Type.tp_basicsize = sizeof(BPy_BMLayerItem);
1222
1223 BPy_BMLayerAccessVert_Type.tp_name = "BMLayerAccessVert";
1224 BPy_BMLayerAccessEdge_Type.tp_name = "BMLayerAccessEdge";
1225 BPy_BMLayerAccessFace_Type.tp_name = "BMLayerAccessFace";
1226 BPy_BMLayerAccessLoop_Type.tp_name = "BMLayerAccessLoop";
1227 BPy_BMLayerCollection_Type.tp_name = "BMLayerCollection";
1228 BPy_BMLayerItem_Type.tp_name = "BMLayerItem";
1229
1230 /* todo */
1231 BPy_BMLayerAccessVert_Type.tp_doc = bpy_bmlayeraccess_type_doc;
1232 BPy_BMLayerAccessEdge_Type.tp_doc = bpy_bmlayeraccess_type_doc;
1233 BPy_BMLayerAccessFace_Type.tp_doc = bpy_bmlayeraccess_type_doc;
1234 BPy_BMLayerAccessLoop_Type.tp_doc = bpy_bmlayeraccess_type_doc;
1235 BPy_BMLayerCollection_Type.tp_doc = bpy_bmlayercollection_type_doc;
1236 BPy_BMLayerItem_Type.tp_doc = bpy_bmlayeritem_type_doc;
1237
1238 BPy_BMLayerAccessVert_Type.tp_repr = static_cast<reprfunc>(nullptr);
1239 BPy_BMLayerAccessEdge_Type.tp_repr = static_cast<reprfunc>(nullptr);
1240 BPy_BMLayerAccessFace_Type.tp_repr = static_cast<reprfunc>(nullptr);
1241 BPy_BMLayerAccessLoop_Type.tp_repr = static_cast<reprfunc>(nullptr);
1242 BPy_BMLayerCollection_Type.tp_repr = static_cast<reprfunc>(nullptr);
1243 BPy_BMLayerItem_Type.tp_repr = static_cast<reprfunc>(nullptr);
1244
1251
1252 // BPy_BMLayerAccess_Type.tp_methods = bpy_bmeditselseq_methods;
1255
1257
1259
1260 BPy_BMLayerCollection_Type.tp_iter = reinterpret_cast<getiterfunc>(bpy_bmlayercollection_iter);
1261
1262 BPy_BMLayerAccessVert_Type.tp_dealloc = nullptr;
1263 BPy_BMLayerAccessEdge_Type.tp_dealloc = nullptr;
1264 BPy_BMLayerAccessFace_Type.tp_dealloc = nullptr;
1265 BPy_BMLayerAccessLoop_Type.tp_dealloc = nullptr;
1266 BPy_BMLayerCollection_Type.tp_dealloc = nullptr;
1267 BPy_BMLayerItem_Type.tp_dealloc = nullptr;
1268
1269 BPy_BMLayerAccessVert_Type.tp_flags = Py_TPFLAGS_DEFAULT;
1270 BPy_BMLayerAccessEdge_Type.tp_flags = Py_TPFLAGS_DEFAULT;
1271 BPy_BMLayerAccessFace_Type.tp_flags = Py_TPFLAGS_DEFAULT;
1272 BPy_BMLayerAccessLoop_Type.tp_flags = Py_TPFLAGS_DEFAULT;
1273 BPy_BMLayerCollection_Type.tp_flags = Py_TPFLAGS_DEFAULT;
1274 BPy_BMLayerItem_Type.tp_flags = Py_TPFLAGS_DEFAULT;
1275
1276 PyType_Ready(&BPy_BMLayerAccessVert_Type);
1277 PyType_Ready(&BPy_BMLayerAccessEdge_Type);
1278 PyType_Ready(&BPy_BMLayerAccessFace_Type);
1279 PyType_Ready(&BPy_BMLayerAccessLoop_Type);
1280 PyType_Ready(&BPy_BMLayerCollection_Type);
1281 PyType_Ready(&BPy_BMLayerItem_Type);
1282}
1283
1284/* Per Element Get/Set
1285 * ******************* */
1286
1291{
1292 void *value;
1293 BMElem *ele = py_ele->ele;
1295
1296 /* error checking */
1297 if (!BPy_BMLayerItem_Check(py_layer)) [[unlikely]] {
1298 PyErr_SetString(PyExc_AttributeError, "BMElem[key]: invalid key, must be a BMLayerItem");
1299 return nullptr;
1300 }
1301 if (py_ele->bm != py_layer->bm) [[unlikely]] {
1302 PyErr_SetString(PyExc_ValueError, "BMElem[layer]: layer is from another mesh");
1303 return nullptr;
1304 }
1305 if (ele->head.htype != py_layer->htype) [[unlikely]] {
1306 char namestr_1[32], namestr_2[32];
1307 PyErr_Format(PyExc_ValueError,
1308 "Layer/Element type mismatch, expected %.200s got layer type %.200s",
1309 BPy_BMElem_StringFromHType_ex(ele->head.htype, namestr_1),
1310 BPy_BMElem_StringFromHType_ex(py_layer->htype, namestr_2));
1311 return nullptr;
1312 }
1313
1314 data = bpy_bm_customdata_get(py_layer->bm, py_layer->htype);
1315
1316 const eCustomDataType real_type = bpy_bm_layer_type_is_skin(py_layer->type) ?
1318 eCustomDataType(py_layer->type);
1319 value = CustomData_bmesh_get_n(data, ele->head.data, real_type, py_layer->index);
1320
1321 if (value == nullptr) [[unlikely]] {
1322 /* this should be fairly unlikely but possible if layers move about after we get them */
1323 PyErr_SetString(PyExc_KeyError, "BMElem[key]: layer not found");
1324 return nullptr;
1325 }
1326
1327 return value;
1328}
1329
1331{
1332 void *value = bpy_bmlayeritem_ptr_get(py_ele, py_layer);
1333 PyObject *ret;
1334
1335 if (value == nullptr) [[unlikely]] {
1336 return nullptr;
1337 }
1338
1339 switch (py_layer->type) {
1340 case CD_MDEFORMVERT: {
1341 ret = BPy_BMDeformVert_CreatePyObject(static_cast<MDeformVert *>(value));
1342 break;
1343 }
1344 case CD_PROP_FLOAT: {
1345 ret = PyFloat_FromDouble(*static_cast<float *>(value));
1346 break;
1347 }
1348 case CD_PROP_INT32: {
1349 ret = PyLong_FromLong(*static_cast<int *>(value));
1350 break;
1351 }
1352 case CD_PROP_BOOL: {
1353 ret = PyBool_FromLong(*static_cast<bool *>(value));
1354 break;
1355 }
1356 case CD_PROP_FLOAT3: {
1357 ret = Vector_CreatePyObject_wrap(static_cast<float *>(value), 3, nullptr);
1358 break;
1359 }
1360 case CD_PROP_COLOR: {
1361 ret = Vector_CreatePyObject_wrap(static_cast<float *>(value), 4, nullptr);
1362 break;
1363 }
1364 case CD_PROP_STRING: {
1365 MStringProperty *mstring = static_cast<MStringProperty *>(value);
1366 ret = PyBytes_FromStringAndSize(mstring->s, mstring->s_len);
1367 break;
1368 }
1369 case CD_PROP_FLOAT2: {
1370 if (py_ele->bm != py_layer->bm) [[unlikely]] {
1371 PyErr_SetString(PyExc_ValueError, "BMElem[layer]: layer is from another mesh");
1372 return nullptr;
1373 }
1375 py_ele->bm, reinterpret_cast<BMLoop *>(py_ele->ele), py_layer->index);
1376 break;
1377 }
1378 case CD_PROP_BYTE_COLOR: {
1379 ret = BPy_BMLoopColor_CreatePyObject(static_cast<MLoopCol *>(value));
1380 break;
1381 }
1382 case CD_SHAPEKEY: {
1383 ret = Vector_CreatePyObject_wrap(static_cast<float *>(value), 3, nullptr);
1384 break;
1385 }
1387 ret = BPy_BMVertSkin_CreatePyObject(py_ele->bm, reinterpret_cast<BMVert *>(py_ele->ele));
1388 break;
1389 }
1390 default: {
1391 ret = Py_NotImplemented; /* TODO */
1392 Py_INCREF(ret);
1393 break;
1394 }
1395 }
1396
1397 return ret;
1398}
1399
1400int BPy_BMLayerItem_SetItem(BPy_BMElem *py_ele, BPy_BMLayerItem *py_layer, PyObject *py_value)
1401{
1402 int ret = 0;
1403 void *value = bpy_bmlayeritem_ptr_get(py_ele, py_layer);
1404
1405 if (value == nullptr) [[unlikely]] {
1406 return -1;
1407 }
1408
1409 switch (py_layer->type) {
1410 case CD_MDEFORMVERT: {
1411 ret = BPy_BMDeformVert_AssignPyObject(static_cast<MDeformVert *>(value), py_value);
1412 break;
1413 }
1414 case CD_PROP_FLOAT: {
1415 const float tmp_val = PyFloat_AsDouble(py_value);
1416 if (tmp_val == -1 && PyErr_Occurred()) [[unlikely]] {
1417 PyErr_Format(
1418 PyExc_TypeError, "expected a float, not a %.200s", Py_TYPE(py_value)->tp_name);
1419 ret = -1;
1420 }
1421 else {
1422 *static_cast<float *>(value) = tmp_val;
1423 }
1424 break;
1425 }
1426 case CD_PROP_INT32: {
1427 const int tmp_val = PyC_Long_AsI32(py_value);
1428 if (tmp_val == -1 && PyErr_Occurred()) [[unlikely]] {
1429 /* error is set */
1430 ret = -1;
1431 }
1432 else {
1433 *static_cast<int *>(value) = tmp_val;
1434 }
1435 break;
1436 }
1437 case CD_PROP_BOOL: {
1438 const int tmp_val = PyC_Long_AsBool(py_value);
1439 if (tmp_val == -1) [[unlikely]] {
1440 /* The error has been set. */
1441 ret = -1;
1442 }
1443 else {
1444 *static_cast<bool *>(value) = tmp_val;
1445 }
1446 break;
1447 }
1448 case CD_PROP_FLOAT3: {
1450 static_cast<float *>(value), 3, 3, py_value, "BMElem Float Vector") == -1)
1451 {
1452 ret = -1;
1453 }
1454 break;
1455 }
1456 case CD_PROP_COLOR: {
1458 static_cast<float *>(value), 4, 4, py_value, "BMElem Float Color") == -1)
1459 {
1460 ret = -1;
1461 }
1462 break;
1463 }
1464 case CD_PROP_STRING: {
1465 MStringProperty *mstring = static_cast<MStringProperty *>(value);
1466 char *tmp_val;
1467 Py_ssize_t tmp_val_len;
1468 if (PyBytes_AsStringAndSize(py_value, &tmp_val, &tmp_val_len) == -1) [[unlikely]] {
1469 PyErr_Format(PyExc_TypeError, "expected bytes, not a %.200s", Py_TYPE(py_value)->tp_name);
1470 ret = -1;
1471 }
1472 else {
1473 tmp_val_len = std::min<ulong>(tmp_val_len, sizeof(mstring->s));
1474 memcpy(mstring->s, tmp_val, tmp_val_len);
1475 mstring->s_len = tmp_val_len;
1476 }
1477 break;
1478 }
1479 case CD_PROP_FLOAT2: {
1480 if (py_ele->bm != py_layer->bm) [[unlikely]] {
1481 PyErr_SetString(PyExc_ValueError, "BMElem[layer]: layer is from another mesh");
1482 ret = -1;
1483 }
1484 else {
1486 py_ele->bm, reinterpret_cast<BMLoop *>(py_ele->ele), py_value);
1487 }
1488 break;
1489 }
1490 case CD_PROP_BYTE_COLOR: {
1491 ret = BPy_BMLoopColor_AssignPyObject(static_cast<MLoopCol *>(value), py_value);
1492 break;
1493 }
1494 case CD_SHAPEKEY: {
1495 float tmp_val[3];
1496 if (mathutils_array_parse(tmp_val, 3, 3, py_value, "BMVert[shape] = value") == -1)
1497 [[unlikely]]
1498 {
1499 ret = -1;
1500 }
1501 else {
1502 copy_v3_v3(static_cast<float *>(value), tmp_val);
1503 }
1504 break;
1505 }
1508 py_ele->bm, reinterpret_cast<BMVert *>(py_ele->ele), py_value);
1509 break;
1510 }
1511 default: {
1512 PyErr_SetString(PyExc_AttributeError, "readonly / unsupported type");
1513 ret = -1;
1514 break;
1515 }
1516 }
1517
1518 return ret;
1519}
1520
1521} // namespace blender
CustomData interface, see also DNA_customdata_types.h.
#define BLI_assert_unreachable()
#define POINTER_AS_INT(i)
#define STREQ(a, b)
#define CLAMP_MIN(a, b)
#define BPY_BM_CHECK_OBJ(obj)
#define BPY_BM_CHECK_SOURCE_OBJ(bm, errmsg,...)
#define BPY_BM_CHECK_INT(obj)
#define BPy_BMLayerItem_Check(v)
#define layer
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
BMesh Iterators.
static PyObject * bpy_bmlayercollection_verify(BPy_BMLayerCollection *self)
bool BM_data_layer_free_named(BMesh *bm, CustomData *data, StringRef name)
static int bpy_bmlayercollection_contains(BPy_BMLayerCollection *self, PyObject *value)
static PyObject * bpy_bmlayercollection_remove(BPy_BMLayerCollection *self, BPy_BMLayerItem *value)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static PyGetSetDef bpy_bmlayeraccess_edge_getseters[]
PyTypeObject BPy_BMLayerAccessVert_Type
void BM_data_layer_free_n(BMesh *bm, CustomData *data, int type, int n)
void BM_data_layer_add(BMesh *bm, CustomData *data, int type)
PyObject * BPy_BMLayerItem_CreatePyObject(BMesh *bm, const char htype, int type, int index)
static PyObject * bpy_bmlayercollection_subscript_str(BPy_BMLayerCollection *self, const char *keyname)
static PyObject * bpy_bmlayeraccess_collection_get(BPy_BMLayerAccess *self, void *flag)
int CustomData_number_of_layers(const CustomData *data, eCustomDataType type)
PyObject * BPy_BMLayerCollection_CreatePyObject(BMesh *bm, const char htype, int type)
static CustomData * bpy_bm_customdata_get(BMesh *bm, char htype)
MINLINE void copy_v3_v3(float r[3], const float a[3])
PyObject * BPy_BMDeformVert_CreatePyObject(MDeformVert *dvert)
static const char * bpy_bm_layer_skin_radius_name
bool CustomData_has_layer(const CustomData *data, eCustomDataType type)
PyTypeObject BPy_BMLayerAccessFace_Type
static PyMethodDef bpy_bmelemseq_methods[]
static PyObject * bpy_bmlayercollection_active_get(BPy_BMLayerItem *self, void *)
PyObject * Vector_CreatePyObject_wrap(float *vec, const int vec_num, PyTypeObject *base_type)
PyDoc_STRVAR(bpy_bm_new_doc, ".. function:: new(*, use_operators=True)\n" "\n" " :param use_operators: Support calling operators in :mod:`bmesh.ops` (uses some " "extra memory per vert/edge/face).\n" " :type use_operators: bool\n" " :return: Return a new, empty BMesh.\n" " :rtype: :class:`bmesh.types.BMesh`\n")
static PyObject * bpy_bmlayercollection_new(BPy_BMLayerCollection *self, PyObject *args)
static PyObject * bpy_bmlayercollection_subscript_slice(BPy_BMLayerCollection *self, Py_ssize_t start, Py_ssize_t stop)
constexpr int BPY_BM_LAYER_TYPE_SKIN
static void * bpy_bmlayeritem_ptr_get(BPy_BMElem *py_ele, BPy_BMLayerItem *py_layer)
static PyGetSetDef bpy_bmlayeraccess_vert_getseters[]
static PyObject * bpy_bmlayercollection_items(BPy_BMLayerCollection *self)
PyObject * BPy_BMLoopColor_CreatePyObject(MLoopCol *mloopcol)
int CustomData_get_layer_index(const CustomData *data, eCustomDataType type)
static const char * bpy_bm_layer_skin_root_name
PyObject * BPy_BMVertSkin_CreatePyObject(BMesh *bm, BMVert *vert)
PyObject * BPy_BMLayerAccess_CreatePyObject(BMesh *bm, const char htype)
static PyObject * bpy_bmlayercollection_get(BPy_BMLayerCollection *self, PyObject *args)
PyObject * BPy_BMLayerItem_GetItem(BPy_BMElem *py_ele, BPy_BMLayerItem *py_layer)
BMElem.__getitem__() / __setitem__()
bool CustomData_layertype_is_singleton(eCustomDataType type)
PyTypeObject BPy_BMLayerCollection_Type
int BPy_BMLayerItem_SetItem(BPy_BMElem *py_ele, BPy_BMLayerItem *py_layer, PyObject *py_value)
static PyObject * bpy_bmlayercollection_values(BPy_BMLayerCollection *self)
static PyGetSetDef bpy_bmlayeraccess_face_getseters[]
BMesh const char void * data
int BPy_BMLoopUV_AssignPyObject(BMesh *bm, BMLoop *loop, PyObject *value)
static PyObject * bpy_bmlayeritem_name_get(BPy_BMLayerItem *self, void *)
static PyMappingMethods bpy_bmlayercollection_as_mapping
static PyObject * bpy_bmlayeritem_copy_from(BPy_BMLayerItem *self, BPy_BMLayerItem *value)
int mathutils_array_parse(float *array, int array_num_min, int array_num_max, PyObject *value, const char *error_prefix)
Definition mathutils.cc:97
static PyObject * bpy_bmlayercollection_iter(BPy_BMLayerCollection *self)
PyTypeObject BPy_BMLayerItem_Type
static PyObject * bpy_bmlayercollection_is_singleton_get(BPy_BMLayerItem *self, void *)
int CustomData_get_named_layer_index(const CustomData *data, eCustomDataType type, StringRef name)
PyTypeObject BPy_BMLayerAccessLoop_Type
int PyC_Long_AsBool(PyObject *value)
int BPy_BMLoopColor_AssignPyObject(MLoopCol *mloopcol, PyObject *value)
int CustomData_get_layer_index_n(const CustomData *data, eCustomDataType type, int n)
static PyGetSetDef bpy_bmlayeraccess_loop_getseters[]
PyTypeObject BPy_BMLayerAccessEdge_Type
static Py_ssize_t bpy_bmlayercollection_length(BPy_BMLayerCollection *self)
static PyGetSetDef bpy_bmlayercollection_getseters[]
static PyGetSetDef bpy_bmlayeritem_getseters[]
static PyObject * bpy_bmlayercollection_subscript_int(BPy_BMLayerCollection *self, Py_ssize_t keynum)
static CustomDataLayer * bpy_bmlayeritem_get(BPy_BMLayerItem *self)
void BM_data_layer_copy(BMesh *bm, CustomData *data, int type, int src_n, int dst_n)
static PyObject * bpy_bmlayercollection_subscript(BPy_BMLayerCollection *self, PyObject *key)
char * BPy_BMElem_StringFromHType_ex(const char htype, char ret[32])
int CustomData_get_active_layer(const CustomData *data, eCustomDataType type)
PyObject * BPy_BMLoopUV_CreatePyObject(BMesh *bm, BMLoop *loop, int layer)
int BPy_BMDeformVert_AssignPyObject(MDeformVert *dvert, PyObject *value)
void BM_data_layer_add_named(BMesh *bm, CustomData *data, int type, const StringRef name)
void BM_uv_map_attr_pin_ensure_for_all_layers(BMesh *bm)
int BPy_BMVertSkin_AssignPyObject(BMesh *bm, BMVert *vert, PyObject *value)
bool CustomData_has_layer_named(const CustomData *data, eCustomDataType type, StringRef name)
static PySequenceMethods bpy_bmlayercollection_as_sequence
static const char * bpy_bm_layer_skin_loose_name
int CustomData_get_named_layer(const CustomData *data, eCustomDataType type, StringRef name)
static bool bpy_bm_layer_type_is_skin(const int type)
static PyObject * bpy_bmlayercollection_keys(BPy_BMLayerCollection *self)
static PyMethodDef bpy_bmlayeritem_methods[]
void * CustomData_bmesh_get_n(const CustomData *data, void *block, eCustomDataType type, int n)
header-only utilities
#define PyTuple_SET_ITEMS(op_arg,...)
PyObject_HEAD BMesh * bm