2 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18 * The Original Code is Copyright (C) 2012 Blender Foundation.
19 * All rights reserved.
21 * Contributor(s): Campbell Barton
23 * ***** END GPL LICENSE BLOCK *****
26 /** \file blender/python/bmesh/bmesh_py_types_meshdata.c
29 * This file defines customdata types which can't be accessed as primitive
30 * python types such as MDeformVert, MLoopUV, MTexPoly
35 #include "../mathutils/mathutils.h"
37 #include "DNA_meshdata_types.h"
39 #include "BLI_utildefines.h"
40 #include "BLI_math_vector.h"
42 #include "BKE_deform.h"
44 #include "bmesh_py_types_meshdata.h"
49 #define BPy_BMLoopUV_Check(v) (Py_TYPE(v) == &BPy_BMLoopUV_Type)
51 typedef struct BPy_BMLoopUV {
56 PyDoc_STRVAR(bpy_bmloopuv_uv_doc,
57 "Loops UV (as a 2D Vector).\n\n:type: :class:`mathutils.Vector`"
59 static PyObject *bpy_bmloopuv_uv_get(BPy_BMLoopUV *self, void *UNUSED(closure))
61 return Vector_CreatePyObject(self->data->uv, 2, Py_WRAP, NULL);
64 static int bpy_bmloopuv_uv_set(BPy_BMLoopUV *self, PyObject *value, void *UNUSED(closure))
67 if (mathutils_array_parse(tvec, 2, 2, value, "BMLoopUV.uv") != -1) {
68 copy_v2_v2(self->data->uv, tvec);
76 PyDoc_STRVAR(bpy_bmloopuv_flag__pin_uv_doc,
77 "UV pin state.\n\n:type: boolean"
79 PyDoc_STRVAR(bpy_bmloopuv_flag__select_doc,
80 "UV select state.\n\n:type: boolean"
82 PyDoc_STRVAR(bpy_bmloopuv_flag__select_edge_doc,
83 "UV edge select state.\n\n:type: boolean"
87 static PyObject *bpy_bmloopuv_flag_get(BPy_BMLoopUV *self, void *flag_p)
89 const int flag = GET_INT_FROM_POINTER(flag_p);
90 return PyBool_FromLong(self->data->flag & flag);
93 static int bpy_bmloopuv_flag_set(BPy_BMLoopUV *self, PyObject *value, void *flag_p)
95 const int flag = GET_INT_FROM_POINTER(flag_p);
97 switch (PyLong_AsLong(value)) {
99 self->data->flag |= flag;
102 self->data->flag &= ~flag;
105 PyErr_SetString(PyExc_TypeError,
106 "expected a boolean type 0/1");
111 static PyGetSetDef bpy_bmloopuv_getseters[] = {
112 /* attributes match rna_def_mloopuv */
113 {(char *)"uv", (getter)bpy_bmloopuv_uv_get, (setter)bpy_bmloopuv_uv_set, (char *)bpy_bmloopuv_uv_doc, NULL},
114 {(char *)"pin_uv", (getter)bpy_bmloopuv_flag_get, (setter)bpy_bmloopuv_flag_set, (char *)bpy_bmloopuv_flag__pin_uv_doc, (void *)MLOOPUV_PINNED},
115 {(char *)"select", (getter)bpy_bmloopuv_flag_get, (setter)bpy_bmloopuv_flag_set, (char *)bpy_bmloopuv_flag__select_doc, (void *)MLOOPUV_VERTSEL},
116 {(char *)"select_edge", (getter)bpy_bmloopuv_flag_get, (setter)bpy_bmloopuv_flag_set, (char *)bpy_bmloopuv_flag__select_edge_doc, (void *)MLOOPUV_EDGESEL},
118 {NULL, NULL, NULL, NULL, NULL} /* Sentinel */
121 PyTypeObject BPy_BMLoopUV_Type = {{{0}}}; /* bm.loops.layers.uv.active */
123 static void bm_init_types_bmloopuv(void)
125 BPy_BMLoopUV_Type.tp_basicsize = sizeof(BPy_BMLoopUV);
127 BPy_BMLoopUV_Type.tp_name = "BMLoopUV";
129 BPy_BMLoopUV_Type.tp_doc = NULL; // todo
131 BPy_BMLoopUV_Type.tp_getset = bpy_bmloopuv_getseters;
133 BPy_BMLoopUV_Type.tp_flags = Py_TPFLAGS_DEFAULT;
135 PyType_Ready(&BPy_BMLoopUV_Type);
138 int BPy_BMLoopUV_AssignPyObject(struct MLoopUV *mloopuv, PyObject *value)
140 if (UNLIKELY(!BPy_BMLoopUV_Check(value))) {
141 PyErr_Format(PyExc_TypeError, "expected BMLoopUV, not a %.200s", Py_TYPE(value)->tp_name);
145 *((MLoopUV *)mloopuv) = *(((BPy_BMLoopUV *)value)->data);
150 PyObject *BPy_BMLoopUV_CreatePyObject(struct MLoopUV *mloopuv)
152 BPy_BMLoopUV *self = PyObject_New(BPy_BMLoopUV, &BPy_BMLoopUV_Type);
153 self->data = mloopuv;
154 return (PyObject *)self;
157 /* --- End Mesh Loop UV --- */
162 /* This simply provices a color wrapper for
163 * color which uses mathutils callbacks for mathutils.Color
166 #define MLOOPCOL_FROM_CAPSULE(color_capsule) \
167 ((MLoopCol *)PyCapsule_GetPointer(color_capsule, NULL))
169 static void mloopcol_to_float(const MLoopCol *mloopcol, float col_r[3])
171 rgb_uchar_to_float(col_r, (unsigned char *)&mloopcol->r);
174 static void mloopcol_from_float(MLoopCol *mloopcol, const float col[3])
176 rgb_float_to_uchar((unsigned char *)&mloopcol->r, col);
179 static unsigned char mathutils_bmloopcol_cb_index = -1;
181 static int mathutils_bmloopcol_check(BaseMathObject *UNUSED(bmo))
187 static int mathutils_bmloopcol_get(BaseMathObject *bmo, int UNUSED(subtype))
189 MLoopCol *mloopcol = MLOOPCOL_FROM_CAPSULE(bmo->cb_user);
190 mloopcol_to_float(mloopcol, bmo->data);
194 static int mathutils_bmloopcol_set(BaseMathObject *bmo, int UNUSED(subtype))
196 MLoopCol *mloopcol = MLOOPCOL_FROM_CAPSULE(bmo->cb_user);
197 mloopcol_from_float(mloopcol, bmo->data);
201 static int mathutils_bmloopcol_get_index(BaseMathObject *bmo, int subtype, int UNUSED(index))
203 /* lazy, avoid repeteing the case statement */
204 if (mathutils_bmloopcol_get(bmo, subtype) == -1)
209 static int mathutils_bmloopcol_set_index(BaseMathObject *bmo, int subtype, int index)
211 const float f = bmo->data[index];
213 /* lazy, avoid repeteing the case statement */
214 if (mathutils_bmloopcol_get(bmo, subtype) == -1)
217 bmo->data[index] = f;
218 return mathutils_bmloopcol_set(bmo, subtype);
221 Mathutils_Callback mathutils_bmloopcol_cb = {
222 mathutils_bmloopcol_check,
223 mathutils_bmloopcol_get,
224 mathutils_bmloopcol_set,
225 mathutils_bmloopcol_get_index,
226 mathutils_bmloopcol_set_index
229 static void bm_init_types_bmloopcol(void)
232 mathutils_bmloopcol_cb_index = Mathutils_RegisterCallback(&mathutils_bmloopcol_cb);
235 int BPy_BMLoopColor_AssignPyObject(struct MLoopCol *mloopcol, PyObject *value)
238 if (mathutils_array_parse(tvec, 3, 3, value, "BMLoopCol") != -1) {
239 mloopcol_from_float(mloopcol, tvec);
247 PyObject *BPy_BMLoopColor_CreatePyObject(struct MLoopCol *data)
249 PyObject *color_capsule;
250 color_capsule = PyCapsule_New(data, NULL, NULL);
251 return Color_CreatePyObject_cb(color_capsule, mathutils_bmloopcol_cb_index, 0);
254 #undef MLOOPCOL_FROM_CAPSULE
256 /* --- End Mesh Loop Color --- */
260 * **************** */
263 * This is python type wraps a deform vert as a python dictionary,
264 * hiding the #MDeformWeight on access, since the mapping is very close, eg:
267 * weight = defvert_find_weight(dv, group_nr);
268 * defvert_remove_group(dv, dw)
271 * weight = dv[group_nr]
274 * \note: there is nothing BMesh spesific here,
275 * its only that BMesh is the only part of blender that uses a hand written api like this.
276 * This type could eventually be used to access lattice weights.
278 * \note: Many of blender-api's dict-like-wrappers act like ordered dicts,
279 * This is intentional _not_ ordered, the weights can be in any order and it wont matter,
280 * the order should not be used in the api in any meaningful way (as with a python dict)
281 * only expose as mapping, not a sequence.
284 #define BPy_BMDeformVert_Check(v) (Py_TYPE(v) == &BPy_BMDeformVert_Type)
286 typedef struct BPy_BMDeformVert {
293 * ================= */
295 static int bpy_bmdeformvert_len(BPy_BMDeformVert *self)
297 return self->data->totweight;
300 static PyObject *bpy_bmdeformvert_subscript(BPy_BMDeformVert *self, PyObject *key)
302 if (PyIndex_Check(key)) {
304 i = PyNumber_AsSsize_t(key, PyExc_IndexError);
305 if (i == -1 && PyErr_Occurred()) {
309 MDeformWeight *dw = defvert_find_index(self->data, i);
312 PyErr_SetString(PyExc_KeyError, "BMDeformVert[key] = x: "
317 return PyFloat_FromDouble(dw->weight);
322 PyErr_Format(PyExc_TypeError,
323 "BMDeformVert keys must be integers, not %.200s",
324 Py_TYPE(key)->tp_name);
329 static int bpy_bmdeformvert_ass_subscript(BPy_BMDeformVert *self, PyObject *key, PyObject *value)
331 if (PyIndex_Check(key)) {
334 i = PyNumber_AsSsize_t(key, PyExc_IndexError);
335 if (i == -1 && PyErr_Occurred()) {
340 /* dvert[group_index] = 0.5 */
342 PyErr_SetString(PyExc_KeyError, "BMDeformVert[key] = x: "
343 "weight keys can't be negative");
347 MDeformWeight *dw = defvert_verify_index(self->data, i);
348 const float f = PyFloat_AsDouble(value);
349 if (f == -1 && PyErr_Occurred()) { // parsed key not a number
350 PyErr_SetString(PyExc_TypeError,
351 "BMDeformVert[key] = x: "
352 "argument not a number");
356 dw->weight = CLAMPIS(f, 0.0f, 1.0f);
360 /* del dvert[group_index] */
361 MDeformWeight *dw = defvert_find_index(self->data, i);
364 PyErr_SetString(PyExc_KeyError, "del BMDeformVert[key]: "
367 defvert_remove_group(self->data, dw);
374 PyErr_Format(PyExc_TypeError,
375 "BMDeformVert keys must be integers, not %.200s",
376 Py_TYPE(key)->tp_name);
381 static int bpy_bmdeformvert_contains(BPy_BMDeformVert *self, PyObject *value)
383 const int key = PyLong_AsSsize_t(value);
385 if (key == -1 && PyErr_Occurred()) {
386 PyErr_SetString(PyExc_TypeError,
387 "BMDeformVert.__contains__: expected an int");
391 return (defvert_find_index(self->data, key) != NULL) ? 1 : 0;
394 /* only defined for __contains__ */
395 static PySequenceMethods bpy_bmdeformvert_as_sequence = {
396 (lenfunc)bpy_bmdeformvert_len, /* sq_length */
397 NULL, /* sq_concat */
398 NULL, /* sq_repeat */
400 /* note: if this is set PySequence_Check() returns True,
401 * but in this case we dont want to be treated as a seq */
405 NULL, /* sq_ass_item */
406 NULL, /* *was* sq_ass_slice */
407 (objobjproc)bpy_bmdeformvert_contains, /* sq_contains */
408 (binaryfunc) NULL, /* sq_inplace_concat */
409 (ssizeargfunc) NULL, /* sq_inplace_repeat */
412 static PyMappingMethods bpy_bmdeformvert_as_mapping = {
413 (lenfunc)bpy_bmdeformvert_len,
414 (binaryfunc)bpy_bmdeformvert_subscript,
415 (objobjargproc)bpy_bmdeformvert_ass_subscript
421 PyDoc_STRVAR(bpy_bmdeformvert_keys_doc,
422 ".. method:: keys()\n"
424 " Return the group indices used by this vertex\n"
425 " (matching pythons dict.keys() functionality).\n"
427 " :return: the deform group this vertex uses\n"
428 " :rtype: list of ints\n"
430 static PyObject *bpy_bmdeformvert_keys(BPy_BMDeformVert *self)
434 MDeformWeight *dw = self->data->dw;
436 ret = PyList_New(self->data->totweight);
437 for (i = 0; i < self->data->totweight; i++, dw++) {
438 PyList_SET_ITEM(ret, i, PyLong_FromSsize_t(dw->def_nr));
444 PyDoc_STRVAR(bpy_bmdeformvert_values_doc,
445 ".. method:: items()\n"
447 " Return (group, weight) pairs for this vertex\n"
448 " (matching pythons dict.items() functionality).\n"
450 " :return: (key, value) pairs for each deform weight of this vertex.\n"
451 " :rtype: list of tuples\n"
453 static PyObject *bpy_bmdeformvert_values(BPy_BMDeformVert *self)
457 MDeformWeight *dw = self->data->dw;
459 ret = PyList_New(self->data->totweight);
460 for (i = 0; i < self->data->totweight; i++, dw++) {
461 PyList_SET_ITEM(ret, i, PyFloat_FromDouble(dw->weight));
467 PyDoc_STRVAR(bpy_bmdeformvert_items_doc,
468 ".. method:: values()\n"
470 " Return the weights of the deform vertex\n"
471 " (matching pythons dict.values() functionality).\n"
473 " :return: The weights that influence this vertex\n"
474 " :rtype: list of floats\n"
476 static PyObject *bpy_bmdeformvert_items(BPy_BMDeformVert *self)
481 MDeformWeight *dw = self->data->dw;
483 ret = PyList_New(self->data->totweight);
484 for (i = 0; i < self->data->totweight; i++, dw++) {
485 item = PyTuple_New(2);
487 PyTuple_SET_ITEM(item, 0, PyLong_FromSsize_t(dw->def_nr));
488 PyTuple_SET_ITEM(item, 1, PyFloat_FromDouble(dw->weight));
490 PyList_SET_ITEM(ret, i, item);
496 PyDoc_STRVAR(bpy_bmdeformvert_get_doc,
497 ".. method:: get(key, default=None)\n"
499 " Returns the deform weight matching the key or default\n"
500 " when not found (matches pythons dictionary function of the same name).\n"
502 " :arg key: The key associated with deform weight.\n"
504 " :arg default: Optional argument for the value to return if\n"
505 " *key* is not found.\n"
506 " :type default: Undefined\n"
508 static PyObject *bpy_bmdeformvert_get(BPy_BMDeformVert *self, PyObject *args)
511 PyObject *def = Py_None;
513 if (!PyArg_ParseTuple(args, "i|O:get", &key, &def)) {
517 MDeformWeight *dw = defvert_find_index(self->data, key);
520 return PyFloat_FromDouble(dw->weight);
523 return Py_INCREF(def), def;
529 PyDoc_STRVAR(bpy_bmdeformvert_clear_doc,
530 ".. method:: clear()\n"
532 " Clears all weights.\n"
534 static PyObject *bpy_bmdeformvert_clear(BPy_BMDeformVert *self)
536 defvert_clear(self->data);
541 static struct PyMethodDef bpy_bmdeformvert_methods[] = {
542 {"keys", (PyCFunction)bpy_bmdeformvert_keys, METH_NOARGS, bpy_bmdeformvert_keys_doc},
543 {"values", (PyCFunction)bpy_bmdeformvert_values, METH_NOARGS, bpy_bmdeformvert_values_doc},
544 {"items", (PyCFunction)bpy_bmdeformvert_items, METH_NOARGS, bpy_bmdeformvert_items_doc},
545 {"get", (PyCFunction)bpy_bmdeformvert_get, METH_VARARGS, bpy_bmdeformvert_get_doc},
546 /* BMESH_TODO pop, popitem, update */
547 {"clear", (PyCFunction)bpy_bmdeformvert_clear, METH_NOARGS, bpy_bmdeformvert_clear_doc},
548 {NULL, NULL, 0, NULL}
551 PyTypeObject BPy_BMDeformVert_Type = {{{0}}}; /* bm.loops.layers.uv.active */
553 static void bm_init_types_bmdvert(void)
555 BPy_BMDeformVert_Type.tp_basicsize = sizeof(BPy_BMDeformVert);
557 BPy_BMDeformVert_Type.tp_name = "BMDeformVert";
559 BPy_BMDeformVert_Type.tp_doc = NULL; // todo
561 BPy_BMDeformVert_Type.tp_as_sequence = &bpy_bmdeformvert_as_sequence;
562 BPy_BMDeformVert_Type.tp_as_mapping = &bpy_bmdeformvert_as_mapping;
564 BPy_BMDeformVert_Type.tp_methods = bpy_bmdeformvert_methods;
566 BPy_BMDeformVert_Type.tp_flags = Py_TPFLAGS_DEFAULT;
568 PyType_Ready(&BPy_BMDeformVert_Type);
571 int BPy_BMDeformVert_AssignPyObject(struct MDeformVert *dvert, PyObject *value)
573 if (UNLIKELY(!BPy_BMDeformVert_Check(value))) {
574 PyErr_Format(PyExc_TypeError, "expected BMDeformVert, not a %.200s", Py_TYPE(value)->tp_name);
578 MDeformVert *dvert_src = ((BPy_BMDeformVert *)value)->data;
579 if (LIKELY(dvert != dvert_src)) {
580 defvert_copy(dvert, dvert_src);
586 PyObject *BPy_BMDeformVert_CreatePyObject(struct MDeformVert *dvert)
588 BPy_BMDeformVert *self = PyObject_New(BPy_BMDeformVert, &BPy_BMDeformVert_Type);
590 return (PyObject *)self;
593 /* --- End Mesh Deform Vert --- */
596 /* call to init all types */
597 void BPy_BM_init_types_meshdata(void)
599 bm_init_types_bmloopuv();
600 bm_init_types_bmloopcol();
601 bm_init_types_bmdvert();