Code cleanup: use r_ prefix for return args
[blender.git] / source / blender / python / bmesh / bmesh_py_types_meshdata.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
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.
8  *
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.
13  *
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.
17  *
18  * The Original Code is Copyright (C) 2012 Blender Foundation.
19  * All rights reserved.
20  *
21  * Contributor(s): Campbell Barton
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/python/bmesh/bmesh_py_types_meshdata.c
27  *  \ingroup pybmesh
28  *
29  * This file defines customdata types which can't be accessed as primitive
30  * python types such as MDeformVert, MLoopUV, MTexPoly
31  */
32
33 #include <Python.h>
34
35 #include "../mathutils/mathutils.h"
36
37 #include "DNA_object_types.h"
38 #include "DNA_meshdata_types.h"
39
40 #include "BLI_utildefines.h"
41 #include "BLI_math_vector.h"
42
43 #include "BKE_deform.h"
44 #include "BKE_library.h"
45
46 #include "bmesh_py_types_meshdata.h"
47
48
49 /* Mesh BMTexPoly
50  * ************** */
51
52 #define BPy_BMTexPoly_Check(v)  (Py_TYPE(v) == &BPy_BMTexPoly_Type)
53
54 typedef struct BPy_BMTexPoly {
55         PyObject_VAR_HEAD
56         MTexPoly *data;
57 } BPy_BMTexPoly;
58
59 extern PyObject *pyrna_id_CreatePyObject(ID *id);
60 extern bool      pyrna_id_FromPyObject(PyObject *obj, ID **id);
61
62 PyDoc_STRVAR(bpy_bmtexpoly_image_doc,
63 "Image or None.\n\n:type: :class:`bpy.types.Image`"
64 );
65 static PyObject *bpy_bmtexpoly_image_get(BPy_BMTexPoly *self, void *UNUSED(closure))
66 {
67         return pyrna_id_CreatePyObject((ID *)self->data->tpage);
68 }
69
70 static int bpy_bmtexpoly_image_set(BPy_BMTexPoly *self, PyObject *value, void *UNUSED(closure))
71 {
72         ID *id;
73
74         if (value == Py_None) {
75                 id = NULL;
76         }
77         else if (pyrna_id_FromPyObject(value, &id) && id && GS(id->name) == ID_IM) {
78                 /* pass */
79         }
80         else {
81                 PyErr_Format(PyExc_KeyError, "BMTexPoly.image = x"
82                              "expected an image or None, not '%.200s'",
83                              Py_TYPE(value)->tp_name);
84                 return -1;
85         }
86
87         id_lib_extern(id);
88         self->data->tpage = (struct Image *)id;
89
90         return 0;
91 }
92
93 static PyGetSetDef bpy_bmtexpoly_getseters[] = {
94         /* attributes match rna_def_mtpoly  */
95         {(char *)"image", (getter)bpy_bmtexpoly_image_get, (setter)bpy_bmtexpoly_image_set, (char *)bpy_bmtexpoly_image_doc, NULL},
96
97         {NULL, NULL, NULL, NULL, NULL} /* Sentinel */
98 };
99
100 static PyTypeObject BPy_BMTexPoly_Type = {{{0}}}; /* bm.loops.layers.uv.active */
101
102 static void bm_init_types_bmtexpoly(void)
103 {
104         BPy_BMTexPoly_Type.tp_basicsize = sizeof(BPy_BMTexPoly);
105
106         BPy_BMTexPoly_Type.tp_name = "BMTexPoly";
107
108         BPy_BMTexPoly_Type.tp_doc = NULL; // todo
109
110         BPy_BMTexPoly_Type.tp_getset = bpy_bmtexpoly_getseters;
111
112         BPy_BMTexPoly_Type.tp_flags = Py_TPFLAGS_DEFAULT;
113
114         PyType_Ready(&BPy_BMTexPoly_Type);
115 }
116
117 int BPy_BMTexPoly_AssignPyObject(struct MTexPoly *mtpoly, PyObject *value)
118 {
119         if (UNLIKELY(!BPy_BMTexPoly_Check(value))) {
120                 PyErr_Format(PyExc_TypeError, "expected BMTexPoly, not a %.200s", Py_TYPE(value)->tp_name);
121                 return -1;
122         }
123         else {
124                 *((MTexPoly *)mtpoly) = *(((BPy_BMTexPoly *)value)->data);
125                 return 0;
126         }
127 }
128
129 PyObject *BPy_BMTexPoly_CreatePyObject(struct MTexPoly *mtpoly)
130 {
131         BPy_BMTexPoly *self = PyObject_New(BPy_BMTexPoly, &BPy_BMTexPoly_Type);
132         self->data = mtpoly;
133         return (PyObject *)self;
134 }
135
136 /* --- End Mesh BMTexPoly --- */
137
138 /* Mesh Loop UV
139  * ************ */
140
141 #define BPy_BMLoopUV_Check(v)  (Py_TYPE(v) == &BPy_BMLoopUV_Type)
142
143 typedef struct BPy_BMLoopUV {
144         PyObject_VAR_HEAD
145         MLoopUV *data;
146 } BPy_BMLoopUV;
147
148 PyDoc_STRVAR(bpy_bmloopuv_uv_doc,
149 "Loops UV (as a 2D Vector).\n\n:type: :class:`mathutils.Vector`"
150 );
151 static PyObject *bpy_bmloopuv_uv_get(BPy_BMLoopUV *self, void *UNUSED(closure))
152 {
153         return Vector_CreatePyObject(self->data->uv, 2, Py_WRAP, NULL);
154 }
155
156 static int bpy_bmloopuv_uv_set(BPy_BMLoopUV *self, PyObject *value, void *UNUSED(closure))
157 {
158         float tvec[2];
159         if (mathutils_array_parse(tvec, 2, 2, value, "BMLoopUV.uv") != -1) {
160                 copy_v2_v2(self->data->uv, tvec);
161                 return 0;
162         }
163         else {
164                 return -1;
165         }
166 }
167
168 PyDoc_STRVAR(bpy_bmloopuv_flag__pin_uv_doc,
169 "UV pin state.\n\n:type: boolean"
170 );
171 PyDoc_STRVAR(bpy_bmloopuv_flag__select_doc,
172 "UV select state.\n\n:type: boolean"
173 );
174 PyDoc_STRVAR(bpy_bmloopuv_flag__select_edge_doc,
175 "UV edge select state.\n\n:type: boolean"
176 );
177
178
179 static PyObject *bpy_bmloopuv_flag_get(BPy_BMLoopUV *self, void *flag_p)
180 {
181         const int flag = GET_INT_FROM_POINTER(flag_p);
182         return PyBool_FromLong(self->data->flag & flag);
183 }
184
185 static int bpy_bmloopuv_flag_set(BPy_BMLoopUV *self, PyObject *value, void *flag_p)
186 {
187         const int flag = GET_INT_FROM_POINTER(flag_p);
188
189         switch (PyLong_AsLong(value)) {
190                 case true:
191                         self->data->flag |= flag;
192                         return 0;
193                 case false:
194                         self->data->flag &= ~flag;
195                         return 0;
196                 default:
197                         PyErr_SetString(PyExc_TypeError,
198                                         "expected a boolean type 0/1");
199                         return -1;
200         }
201 }
202
203 static PyGetSetDef bpy_bmloopuv_getseters[] = {
204         /* attributes match rna_def_mloopuv  */
205         {(char *)"uv",          (getter)bpy_bmloopuv_uv_get,   (setter)bpy_bmloopuv_uv_set,   (char *)bpy_bmloopuv_uv_doc, NULL},
206         {(char *)"pin_uv",      (getter)bpy_bmloopuv_flag_get, (setter)bpy_bmloopuv_flag_set, (char *)bpy_bmloopuv_flag__pin_uv_doc, (void *)MLOOPUV_PINNED},
207         {(char *)"select",      (getter)bpy_bmloopuv_flag_get, (setter)bpy_bmloopuv_flag_set, (char *)bpy_bmloopuv_flag__select_doc, (void *)MLOOPUV_VERTSEL},
208         {(char *)"select_edge", (getter)bpy_bmloopuv_flag_get, (setter)bpy_bmloopuv_flag_set, (char *)bpy_bmloopuv_flag__select_edge_doc, (void *)MLOOPUV_EDGESEL},
209
210         {NULL, NULL, NULL, NULL, NULL} /* Sentinel */
211 };
212
213 PyTypeObject BPy_BMLoopUV_Type = {{{0}}}; /* bm.loops.layers.uv.active */
214
215 static void bm_init_types_bmloopuv(void)
216 {
217         BPy_BMLoopUV_Type.tp_basicsize = sizeof(BPy_BMLoopUV);
218
219         BPy_BMLoopUV_Type.tp_name = "BMLoopUV";
220
221         BPy_BMLoopUV_Type.tp_doc = NULL; // todo
222
223         BPy_BMLoopUV_Type.tp_getset = bpy_bmloopuv_getseters;
224
225         BPy_BMLoopUV_Type.tp_flags = Py_TPFLAGS_DEFAULT;
226
227         PyType_Ready(&BPy_BMLoopUV_Type);
228 }
229
230 int BPy_BMLoopUV_AssignPyObject(struct MLoopUV *mloopuv, PyObject *value)
231 {
232         if (UNLIKELY(!BPy_BMLoopUV_Check(value))) {
233                 PyErr_Format(PyExc_TypeError, "expected BMLoopUV, not a %.200s", Py_TYPE(value)->tp_name);
234                 return -1;
235         }
236         else {
237                 *((MLoopUV *)mloopuv) = *(((BPy_BMLoopUV *)value)->data);
238                 return 0;
239         }
240 }
241
242 PyObject *BPy_BMLoopUV_CreatePyObject(struct MLoopUV *mloopuv)
243 {
244         BPy_BMLoopUV *self = PyObject_New(BPy_BMLoopUV, &BPy_BMLoopUV_Type);
245         self->data = mloopuv;
246         return (PyObject *)self;
247 }
248
249 /* --- End Mesh Loop UV --- */
250
251 /* Mesh Vert Skin
252  * ************ */
253
254 #define BPy_BMVertSkin_Check(v)  (Py_TYPE(v) == &BPy_BMVertSkin_Type)
255
256 typedef struct BPy_BMVertSkin {
257         PyObject_VAR_HEAD
258         MVertSkin *data;
259 } BPy_BMVertSkin;
260
261 PyDoc_STRVAR(bpy_bmvertskin_radius_doc,
262 "Vert skin radii (as a 2D Vector).\n\n:type: :class:`mathutils.Vector`"
263 );
264 static PyObject *bpy_bmvertskin_radius_get(BPy_BMVertSkin *self, void *UNUSED(closure))
265 {
266         return Vector_CreatePyObject(self->data->radius, 2, Py_WRAP, NULL);
267 }
268
269 static int bpy_bmvertskin_radius_set(BPy_BMVertSkin *self, PyObject *value, void *UNUSED(closure))
270 {
271         float tvec[2];
272         if (mathutils_array_parse(tvec, 2, 2, value, "BMVertSkin.radius") != -1) {
273                 copy_v2_v2(self->data->radius, tvec);
274                 return 0;
275         }
276         else {
277                 return -1;
278         }
279 }
280
281 PyDoc_STRVAR(bpy_bmvertskin_flag__use_root_doc,
282 "Use as root vertex.\n\n:type: boolean"
283 );
284 PyDoc_STRVAR(bpy_bmvertskin_flag__use_loose_doc,
285 "Use loose vertex.\n\n:type: boolean"
286 );
287
288 static PyObject *bpy_bmvertskin_flag_get(BPy_BMVertSkin *self, void *flag_p)
289 {
290         const int flag = GET_INT_FROM_POINTER(flag_p);
291         return PyBool_FromLong(self->data->flag & flag);
292 }
293
294 static int bpy_bmvertskin_flag_set(BPy_BMVertSkin *self, PyObject *value, void *flag_p)
295 {
296         const int flag = GET_INT_FROM_POINTER(flag_p);
297
298         switch (PyLong_AsLong(value)) {
299                 case true:
300                         self->data->flag |= flag;
301                         return 0;
302                 case false:
303                         self->data->flag &= ~flag;
304                         return 0;
305                 default:
306                         PyErr_SetString(PyExc_TypeError,
307                                         "expected a boolean type 0/1");
308                         return -1;
309         }
310 }
311
312 /* XXX Todo: Make root settable, currently the code to disable all other verts as roots sits within the modifier */
313 static PyGetSetDef bpy_bmvertskin_getseters[] = {
314         /* attributes match rna_mesh_gen  */
315         {(char *)"radius",    (getter)bpy_bmvertskin_radius_get, (setter)bpy_bmvertskin_radius_set, (char *)bpy_bmvertskin_radius_doc, NULL},
316         {(char *)"use_root",  (getter)bpy_bmvertskin_flag_get,   (setter)NULL,                                     (char *)bpy_bmvertskin_flag__use_root_doc,  (void *)MVERT_SKIN_ROOT},
317         {(char *)"use_loose", (getter)bpy_bmvertskin_flag_get,   (setter)bpy_bmvertskin_flag_set,   (char *)bpy_bmvertskin_flag__use_loose_doc, (void *)MVERT_SKIN_LOOSE},
318
319         {NULL, NULL, NULL, NULL, NULL} /* Sentinel */
320 };
321
322 static PyTypeObject BPy_BMVertSkin_Type = {{{0}}}; /* bm.loops.layers.uv.active */
323
324 static void bm_init_types_bmvertskin(void)
325 {
326         BPy_BMVertSkin_Type.tp_basicsize = sizeof(BPy_BMVertSkin);
327
328         BPy_BMVertSkin_Type.tp_name = "BMVertSkin";
329
330         BPy_BMVertSkin_Type.tp_doc = NULL; // todo
331
332         BPy_BMVertSkin_Type.tp_getset = bpy_bmvertskin_getseters;
333
334         BPy_BMVertSkin_Type.tp_flags = Py_TPFLAGS_DEFAULT;
335
336         PyType_Ready(&BPy_BMVertSkin_Type);
337 }
338
339 int BPy_BMVertSkin_AssignPyObject(struct MVertSkin *mvertskin, PyObject *value)
340 {
341         if (UNLIKELY(!BPy_BMVertSkin_Check(value))) {
342                 PyErr_Format(PyExc_TypeError, "expected BMVertSkin, not a %.200s", Py_TYPE(value)->tp_name);
343                 return -1;
344         }
345         else {
346                 *((MVertSkin *)mvertskin) = *(((BPy_BMVertSkin *)value)->data);
347                 return 0;
348         }
349 }
350
351 PyObject *BPy_BMVertSkin_CreatePyObject(struct MVertSkin *mvertskin)
352 {
353         BPy_BMVertSkin *self = PyObject_New(BPy_BMVertSkin, &BPy_BMVertSkin_Type);
354         self->data = mvertskin;
355         return (PyObject *)self;
356 }
357
358 /* --- End Mesh Vert Skin --- */
359
360 /* Mesh Loop Color
361  * *************** */
362
363 /* This simply provides a color wrapper for
364  * color which uses mathutils callbacks for mathutils.Color
365  */
366
367 #define MLOOPCOL_FROM_CAPSULE(color_capsule)  \
368         ((MLoopCol *)PyCapsule_GetPointer(color_capsule, NULL))
369
370 static void mloopcol_to_float(const MLoopCol *mloopcol, float r_col[3])
371 {
372         rgb_uchar_to_float(r_col, (unsigned char *)&mloopcol->r);
373 }
374
375 static void mloopcol_from_float(MLoopCol *mloopcol, const float col[3])
376 {
377         rgb_float_to_uchar((unsigned char *)&mloopcol->r, col);
378 }
379
380 static unsigned char mathutils_bmloopcol_cb_index = -1;
381
382 static int mathutils_bmloopcol_check(BaseMathObject *UNUSED(bmo))
383 {
384         /* always ok */
385         return 0;
386 }
387
388 static int mathutils_bmloopcol_get(BaseMathObject *bmo, int UNUSED(subtype))
389 {
390         MLoopCol *mloopcol = MLOOPCOL_FROM_CAPSULE(bmo->cb_user);
391         mloopcol_to_float(mloopcol, bmo->data);
392         return 0;
393 }
394
395 static int mathutils_bmloopcol_set(BaseMathObject *bmo, int UNUSED(subtype))
396 {
397         MLoopCol *mloopcol = MLOOPCOL_FROM_CAPSULE(bmo->cb_user);
398         mloopcol_from_float(mloopcol, bmo->data);
399         return 0;
400 }
401
402 static int mathutils_bmloopcol_get_index(BaseMathObject *bmo, int subtype, int UNUSED(index))
403 {
404         /* lazy, avoid repeteing the case statement */
405         if (mathutils_bmloopcol_get(bmo, subtype) == -1)
406                 return -1;
407         return 0;
408 }
409
410 static int mathutils_bmloopcol_set_index(BaseMathObject *bmo, int subtype, int index)
411 {
412         const float f = bmo->data[index];
413
414         /* lazy, avoid repeteing the case statement */
415         if (mathutils_bmloopcol_get(bmo, subtype) == -1)
416                 return -1;
417
418         bmo->data[index] = f;
419         return mathutils_bmloopcol_set(bmo, subtype);
420 }
421
422 static Mathutils_Callback mathutils_bmloopcol_cb = {
423         mathutils_bmloopcol_check,
424         mathutils_bmloopcol_get,
425         mathutils_bmloopcol_set,
426         mathutils_bmloopcol_get_index,
427         mathutils_bmloopcol_set_index
428 };
429
430 static void bm_init_types_bmloopcol(void)
431 {
432         /* pass */
433         mathutils_bmloopcol_cb_index = Mathutils_RegisterCallback(&mathutils_bmloopcol_cb);
434 }
435
436 int BPy_BMLoopColor_AssignPyObject(struct MLoopCol *mloopcol, PyObject *value)
437 {
438         float tvec[3];
439         if (mathutils_array_parse(tvec, 3, 3, value, "BMLoopCol") != -1) {
440                 mloopcol_from_float(mloopcol, tvec);
441                 return 0;
442         }
443         else {
444                 return -1;
445         }
446 }
447
448 PyObject *BPy_BMLoopColor_CreatePyObject(struct MLoopCol *data)
449 {
450         PyObject *color_capsule;
451         color_capsule = PyCapsule_New(data, NULL, NULL);
452         return Color_CreatePyObject_cb(color_capsule, mathutils_bmloopcol_cb_index, 0);
453 }
454
455 #undef MLOOPCOL_FROM_CAPSULE
456
457 /* --- End Mesh Loop Color --- */
458
459
460 /* Mesh Deform Vert
461  * **************** */
462
463 /**
464  * This is python type wraps a deform vert as a python dictionary,
465  * hiding the #MDeformWeight on access, since the mapping is very close, eg:
466  *
467  * C:
468  *     weight = defvert_find_weight(dv, group_nr);
469  *     defvert_remove_group(dv, dw)
470  *
471  * Py:
472  *     weight = dv[group_nr]
473  *     del dv[group_nr]
474  *
475  * \note: there is nothing BMesh specific here,
476  * its only that BMesh is the only part of blender that uses a hand written api like this.
477  * This type could eventually be used to access lattice weights.
478  *
479  * \note: Many of blender-api's dict-like-wrappers act like ordered dicts,
480  * This is intentional _not_ ordered, the weights can be in any order and it wont matter,
481  * the order should not be used in the api in any meaningful way (as with a python dict)
482  * only expose as mapping, not a sequence.
483  */
484
485 #define BPy_BMDeformVert_Check(v)  (Py_TYPE(v) == &BPy_BMDeformVert_Type)
486
487 typedef struct BPy_BMDeformVert {
488         PyObject_VAR_HEAD
489         MDeformVert *data;
490 } BPy_BMDeformVert;
491
492
493 /* Mapping Protocols
494  * ================= */
495
496 static int bpy_bmdeformvert_len(BPy_BMDeformVert *self)
497 {
498         return self->data->totweight;
499 }
500
501 static PyObject *bpy_bmdeformvert_subscript(BPy_BMDeformVert *self, PyObject *key)
502 {
503         if (PyIndex_Check(key)) {
504                 int i;
505                 i = PyNumber_AsSsize_t(key, PyExc_IndexError);
506                 if (i == -1 && PyErr_Occurred()) {
507                         return NULL;
508                 }
509                 else {
510                         MDeformWeight *dw = defvert_find_index(self->data, i);
511
512                         if (dw == NULL) {
513                                 PyErr_SetString(PyExc_KeyError, "BMDeformVert[key] = x: "
514                                                 "key not found");
515                                 return NULL;
516                         }
517                         else {
518                                 return PyFloat_FromDouble(dw->weight);
519                         }
520                 }
521         }
522         else {
523                 PyErr_Format(PyExc_TypeError,
524                              "BMDeformVert keys must be integers, not %.200s",
525                              Py_TYPE(key)->tp_name);
526                 return NULL;
527         }
528 }
529
530 static int bpy_bmdeformvert_ass_subscript(BPy_BMDeformVert *self, PyObject *key, PyObject *value)
531 {
532         if (PyIndex_Check(key)) {
533                 int i;
534
535                 i = PyNumber_AsSsize_t(key, PyExc_IndexError);
536                 if (i == -1 && PyErr_Occurred()) {
537                         return -1;
538                 }
539
540                 if (value) {
541                         /* dvert[group_index] = 0.5 */
542                         if (i < 0) {
543                                 PyErr_SetString(PyExc_KeyError, "BMDeformVert[key] = x: "
544                                                 "weight keys can't be negative");
545                                 return -1;
546                         }
547                         else {
548                                 MDeformWeight *dw = defvert_verify_index(self->data, i);
549                                 const float f = PyFloat_AsDouble(value);
550                                 if (f == -1 && PyErr_Occurred()) { // parsed key not a number
551                                         PyErr_SetString(PyExc_TypeError,
552                                                         "BMDeformVert[key] = x: "
553                                                         "assigned value not a number");
554                                         return -1;
555                                 }
556
557                                 dw->weight = CLAMPIS(f, 0.0f, 1.0f);
558                         }
559                 }
560                 else {
561                         /* del dvert[group_index] */
562                         MDeformWeight *dw = defvert_find_index(self->data, i);
563
564                         if (dw == NULL) {
565                                 PyErr_SetString(PyExc_KeyError, "del BMDeformVert[key]: "
566                                                 "key not found");
567                         }
568                         defvert_remove_group(self->data, dw);
569                 }
570
571                 return 0;
572
573         }
574         else {
575                 PyErr_Format(PyExc_TypeError,
576                              "BMDeformVert keys must be integers, not %.200s",
577                              Py_TYPE(key)->tp_name);
578                 return -1;
579         }
580 }
581
582 static int bpy_bmdeformvert_contains(BPy_BMDeformVert *self, PyObject *value)
583 {
584         const int key = PyLong_AsSsize_t(value);
585
586         if (key == -1 && PyErr_Occurred()) {
587                 PyErr_SetString(PyExc_TypeError,
588                                 "BMDeformVert.__contains__: expected an int");
589                 return -1;
590         }
591
592         return (defvert_find_index(self->data, key) != NULL) ? 1 : 0;
593 }
594
595 /* only defined for __contains__ */
596 static PySequenceMethods bpy_bmdeformvert_as_sequence = {
597         (lenfunc)bpy_bmdeformvert_len,               /* sq_length */
598         NULL,                                        /* sq_concat */
599         NULL,                                        /* sq_repeat */
600
601         /* note: if this is set PySequence_Check() returns True,
602          * but in this case we dont want to be treated as a seq */
603         NULL,                                        /* sq_item */
604
605         NULL,                                        /* sq_slice */
606         NULL,                                        /* sq_ass_item */
607         NULL,                                        /* *was* sq_ass_slice */
608         (objobjproc)bpy_bmdeformvert_contains,       /* sq_contains */
609         (binaryfunc) NULL,                           /* sq_inplace_concat */
610         (ssizeargfunc) NULL,                         /* sq_inplace_repeat */
611 };
612
613 static PyMappingMethods bpy_bmdeformvert_as_mapping = {
614         (lenfunc)bpy_bmdeformvert_len,
615         (binaryfunc)bpy_bmdeformvert_subscript,
616         (objobjargproc)bpy_bmdeformvert_ass_subscript
617 };
618
619 /* Methods
620  * ======= */
621
622 PyDoc_STRVAR(bpy_bmdeformvert_keys_doc,
623 ".. method:: keys()\n"
624 "\n"
625 "   Return the group indices used by this vertex\n"
626 "   (matching pythons dict.keys() functionality).\n"
627 "\n"
628 "   :return: the deform group this vertex uses\n"
629 "   :rtype: list of ints\n"
630 );
631 static PyObject *bpy_bmdeformvert_keys(BPy_BMDeformVert *self)
632 {
633         PyObject *ret;
634         int i;
635         MDeformWeight *dw = self->data->dw;
636
637         ret = PyList_New(self->data->totweight);
638         for (i = 0; i < self->data->totweight; i++, dw++) {
639                 PyList_SET_ITEM(ret, i, PyLong_FromLong(dw->def_nr));
640         }
641
642         return ret;
643 }
644
645 PyDoc_STRVAR(bpy_bmdeformvert_values_doc,
646 ".. method:: values()\n"
647 "\n"
648 "   Return the weights of the deform vertex\n"
649 "   (matching pythons dict.values() functionality).\n"
650 "\n"
651 "   :return: The weights that influence this vertex\n"
652 "   :rtype: list of floats\n"
653 );
654 static PyObject *bpy_bmdeformvert_values(BPy_BMDeformVert *self)
655 {
656         PyObject *ret;
657         int i;
658         MDeformWeight *dw = self->data->dw;
659
660         ret = PyList_New(self->data->totweight);
661         for (i = 0; i < self->data->totweight; i++, dw++) {
662                 PyList_SET_ITEM(ret, i, PyFloat_FromDouble(dw->weight));
663         }
664
665         return ret;
666 }
667
668 PyDoc_STRVAR(bpy_bmdeformvert_items_doc,
669 ".. method:: items()\n"
670 "\n"
671 "   Return (group, weight) pairs for this vertex\n"
672 "   (matching pythons dict.items() functionality).\n"
673 "\n"
674 "   :return: (key, value) pairs for each deform weight of this vertex.\n"
675 "   :rtype: list of tuples\n"
676 );
677 static PyObject *bpy_bmdeformvert_items(BPy_BMDeformVert *self)
678 {
679         PyObject *ret;
680         PyObject *item;
681         int i;
682         MDeformWeight *dw = self->data->dw;
683
684         ret = PyList_New(self->data->totweight);
685         for (i = 0; i < self->data->totweight; i++, dw++) {
686                 item = PyTuple_New(2);
687
688                 PyTuple_SET_ITEM(item, 0, PyLong_FromLong(dw->def_nr));
689                 PyTuple_SET_ITEM(item, 1, PyFloat_FromDouble(dw->weight));
690
691                 PyList_SET_ITEM(ret, i, item);
692         }
693
694         return ret;
695 }
696
697 PyDoc_STRVAR(bpy_bmdeformvert_get_doc,
698 ".. method:: get(key, default=None)\n"
699 "\n"
700 "   Returns the deform weight matching the key or default\n"
701 "   when not found (matches pythons dictionary function of the same name).\n"
702 "\n"
703 "   :arg key: The key associated with deform weight.\n"
704 "   :type key: int\n"
705 "   :arg default: Optional argument for the value to return if\n"
706 "      *key* is not found.\n"
707 "   :type default: Undefined\n"
708 );
709 static PyObject *bpy_bmdeformvert_get(BPy_BMDeformVert *self, PyObject *args)
710 {
711         int key;
712         PyObject *def = Py_None;
713
714         if (!PyArg_ParseTuple(args, "i|O:get", &key, &def)) {
715                 return NULL;
716         }
717         else {
718                 MDeformWeight *dw = defvert_find_index(self->data, key);
719
720                 if (dw) {
721                         return PyFloat_FromDouble(dw->weight);
722                 }
723                 else {
724                         return Py_INCREF(def), def;
725                 }
726         }
727 }
728
729
730 PyDoc_STRVAR(bpy_bmdeformvert_clear_doc,
731 ".. method:: clear()\n"
732 "\n"
733 "   Clears all weights.\n"
734 );
735 static PyObject *bpy_bmdeformvert_clear(BPy_BMDeformVert *self)
736 {
737         defvert_clear(self->data);
738
739         Py_RETURN_NONE;
740 }
741
742 static struct PyMethodDef bpy_bmdeformvert_methods[] = {
743         {"keys",    (PyCFunction)bpy_bmdeformvert_keys,    METH_NOARGS,  bpy_bmdeformvert_keys_doc},
744         {"values",  (PyCFunction)bpy_bmdeformvert_values,  METH_NOARGS,  bpy_bmdeformvert_values_doc},
745         {"items",   (PyCFunction)bpy_bmdeformvert_items,   METH_NOARGS,  bpy_bmdeformvert_items_doc},
746         {"get",     (PyCFunction)bpy_bmdeformvert_get,     METH_VARARGS, bpy_bmdeformvert_get_doc},
747         /* BMESH_TODO pop, popitem, update */
748         {"clear",   (PyCFunction)bpy_bmdeformvert_clear,   METH_NOARGS,  bpy_bmdeformvert_clear_doc},
749         {NULL, NULL, 0, NULL}
750 };
751
752 PyTypeObject BPy_BMDeformVert_Type = {{{0}}}; /* bm.loops.layers.uv.active */
753
754 static void bm_init_types_bmdvert(void)
755 {
756         BPy_BMDeformVert_Type.tp_basicsize = sizeof(BPy_BMDeformVert);
757
758         BPy_BMDeformVert_Type.tp_name = "BMDeformVert";
759
760         BPy_BMDeformVert_Type.tp_doc = NULL; // todo
761
762         BPy_BMDeformVert_Type.tp_as_sequence = &bpy_bmdeformvert_as_sequence;
763         BPy_BMDeformVert_Type.tp_as_mapping = &bpy_bmdeformvert_as_mapping;
764
765         BPy_BMDeformVert_Type.tp_methods = bpy_bmdeformvert_methods;
766
767         BPy_BMDeformVert_Type.tp_flags = Py_TPFLAGS_DEFAULT;
768
769         PyType_Ready(&BPy_BMDeformVert_Type);
770 }
771
772 int BPy_BMDeformVert_AssignPyObject(struct MDeformVert *dvert, PyObject *value)
773 {
774         if (UNLIKELY(!BPy_BMDeformVert_Check(value))) {
775                 PyErr_Format(PyExc_TypeError, "expected BMDeformVert, not a %.200s", Py_TYPE(value)->tp_name);
776                 return -1;
777         }
778         else {
779                 MDeformVert *dvert_src = ((BPy_BMDeformVert *)value)->data;
780                 if (LIKELY(dvert != dvert_src)) {
781                         defvert_copy(dvert, dvert_src);
782                 }
783                 return 0;
784         }
785 }
786
787 PyObject *BPy_BMDeformVert_CreatePyObject(struct MDeformVert *dvert)
788 {
789         BPy_BMDeformVert *self = PyObject_New(BPy_BMDeformVert, &BPy_BMDeformVert_Type);
790         self->data = dvert;
791         return (PyObject *)self;
792 }
793
794 /* --- End Mesh Deform Vert --- */
795
796
797 /* call to init all types */
798 void BPy_BM_init_types_meshdata(void)
799 {
800         bm_init_types_bmtexpoly();
801         bm_init_types_bmloopuv();
802         bm_init_types_bmloopcol();
803         bm_init_types_bmdvert();
804         bm_init_types_bmvertskin();
805 }
806