From 8b7ea6fc90c43a7d2b04a5ced81614776bbba77c Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Sat, 17 Mar 2012 19:34:02 +0000 Subject: bmesh py api: access to MLoopCol as mathutils.Color type --- source/blender/blenfont/intern/blf_translation.c | 2 + .../python/bmesh/bmesh_py_types_customdata.c | 11 +- .../blender/python/bmesh/bmesh_py_types_meshdata.c | 124 ++++++++++++++++++++- .../blender/python/bmesh/bmesh_py_types_meshdata.h | 11 ++ 4 files changed, 138 insertions(+), 10 deletions(-) diff --git a/source/blender/blenfont/intern/blf_translation.c b/source/blender/blenfont/intern/blf_translation.c index dd9fe57fa82..f3d4242192b 100644 --- a/source/blender/blenfont/intern/blf_translation.c +++ b/source/blender/blenfont/intern/blf_translation.c @@ -166,6 +166,7 @@ const char *BLF_translate_do_iface(const char *context, const char *msgid) else return msgid; #else + (void)context; return msgid; #endif } @@ -182,6 +183,7 @@ const char *BLF_translate_do_tooltip(const char *context, const char *msgid) else return msgid; #else + (void)context; return msgid; #endif } diff --git a/source/blender/python/bmesh/bmesh_py_types_customdata.c b/source/blender/python/bmesh/bmesh_py_types_customdata.c index f3232ef334b..901b1d9fd5f 100644 --- a/source/blender/python/bmesh/bmesh_py_types_customdata.c +++ b/source/blender/python/bmesh/bmesh_py_types_customdata.c @@ -134,7 +134,7 @@ PyDoc_STRVAR(bpy_bmlayercollection_keys_doc, ); static PyObject *bpy_bmlayercollection_keys(BPy_BMLayerCollection *self) { - PyObject *ret = PyList_New(0); + PyObject *ret; PyObject *item; int index; CustomData *data; @@ -655,8 +655,7 @@ PyObject *BPy_BMLayerItem_GetItem(BPy_BMElem *py_ele, BPy_BMLayerItem *py_layer) } case CD_MLOOPCOL: { - ret = Py_NotImplemented; /* TODO */ - Py_INCREF(ret); + ret = BPy_BMLoopColor_CreatePyObject(value); break; } case CD_SHAPEKEY: @@ -746,14 +745,12 @@ int BPy_BMLayerItem_SetItem(BPy_BMElem *py_ele, BPy_BMLayerItem *py_layer, PyObj } case CD_MLOOPUV: { - PyErr_SetString(PyExc_AttributeError, "readonly"); /* could make this writeable later */ - ret = -1; + ret = BPy_BMLoopUV_AssignPyObject(value, py_value); break; } case CD_MLOOPCOL: { - PyErr_SetString(PyExc_AttributeError, "readonly"); - ret = -1; + ret = BPy_BMLoopColor_AssignPyObject(value, py_value); break; } case CD_SHAPEKEY: diff --git a/source/blender/python/bmesh/bmesh_py_types_meshdata.c b/source/blender/python/bmesh/bmesh_py_types_meshdata.c index 2d89b514058..0a45a4688c8 100644 --- a/source/blender/python/bmesh/bmesh_py_types_meshdata.c +++ b/source/blender/python/bmesh/bmesh_py_types_meshdata.c @@ -42,6 +42,8 @@ /* Mesh Loop UV * ************ */ +#define BPy_BMLoopUV_Check(v) (Py_TYPE(v) == &BPy_BMLoopUV_Type) + typedef struct BPy_BMLoopUV { PyObject_VAR_HEAD MLoopUV *data; @@ -55,7 +57,7 @@ static PyObject *bpy_bmloopuv_uv_get(BPy_BMLoopUV *self, void *UNUSED(closure)) static int bpy_bmloopuv_uv_set(BPy_BMLoopUV *self, PyObject *value, void *UNUSED(closure)) { float tvec[2]; - if (mathutils_array_parse(tvec, 2, 2, value, "BMLoop.uv") != -1) { + if (mathutils_array_parse(tvec, 2, 2, value, "BMLoopUV.uv") != -1) { copy_v2_v2(self->data->uv, tvec); return 0; } @@ -115,18 +117,134 @@ static void bm_init_types_bmloopuv(void) PyType_Ready(&BPy_BMLoopUV_Type); } -PyObject *BPy_BMLoopUV_CreatePyObject(struct MLoopUV *data) +int BPy_BMLoopUV_AssignPyObject(struct MLoopUV *mloopuv, PyObject *value) +{ + if (UNLIKELY(!BPy_BMLoopUV_Check(value))) { + PyErr_Format(PyExc_TypeError, "expected BMLoopUV, not a %.200s", Py_TYPE(value)->tp_name); + return -1; + } + else { + *((MLoopUV *)mloopuv) = *((MLoopUV *)((BPy_BMLoopUV *)value)->data); + return 0; + } +} + +PyObject *BPy_BMLoopUV_CreatePyObject(struct MLoopUV *mloopuv) { BPy_BMLoopUV *self = PyObject_New(BPy_BMLoopUV, &BPy_BMLoopUV_Type); - self->data = data; + self->data = mloopuv; return (PyObject *)self; } /* --- End Mesh Loop UV --- */ +/* Mesh Loop Color + * *************** */ + +/* This simply provices a color wrapper for + * color which uses mathutils callbacks for mathutils.Color + */ + +#define MLOOPCOL_FROM_CAPSULE(color_capsule) \ + ((MLoopCol *)PyCapsule_GetPointer(color_capsule, NULL)) + +static void mloopcol_to_float(const MLoopCol *mloopcol, float col_r[3]) +{ + col_r[0] = ((float)mloopcol->b) / 255.0f; + col_r[1] = ((float)mloopcol->g) / 255.0f; + col_r[2] = ((float)mloopcol->r) / 255.0f; +} + +static void mloopcol_from_float(MLoopCol *mloopcol, const float col[3]) +{ + mloopcol->b = FTOCHAR(col[0]); + mloopcol->g = FTOCHAR(col[1]); + mloopcol->r = FTOCHAR(col[2]); +} + +static unsigned char mathutils_bmloopcol_cb_index = -1; + +static int mathutils_bmloopcol_check(BaseMathObject *UNUSED(bmo)) +{ + /* always ok */ + return 0; +} + +static int mathutils_bmloopcol_get(BaseMathObject *bmo, int UNUSED(subtype)) +{ + MLoopCol *mloopcol = MLOOPCOL_FROM_CAPSULE(bmo->cb_user); + mloopcol_to_float(mloopcol, bmo->data); + return 0; +} + +static int mathutils_bmloopcol_set(BaseMathObject *bmo, int UNUSED(subtype)) +{ + MLoopCol *mloopcol = MLOOPCOL_FROM_CAPSULE(bmo->cb_user); + mloopcol_from_float(mloopcol, bmo->data); + return 0; +} + +static int mathutils_bmloopcol_get_index(BaseMathObject *bmo, int subtype, int UNUSED(index)) +{ + /* lazy, avoid repeteing the case statement */ + if(mathutils_bmloopcol_get(bmo, subtype) == -1) + return -1; + return 0; +} + +static int mathutils_bmloopcol_set_index(BaseMathObject *bmo, int subtype, int index) +{ + const float f = bmo->data[index]; + + /* lazy, avoid repeteing the case statement */ + if(mathutils_bmloopcol_get(bmo, subtype) == -1) + return -1; + + bmo->data[index] = f; + return mathutils_bmloopcol_set(bmo, subtype); +} + +Mathutils_Callback mathutils_bmloopcol_cb = { + mathutils_bmloopcol_check, + mathutils_bmloopcol_get, + mathutils_bmloopcol_set, + mathutils_bmloopcol_get_index, + mathutils_bmloopcol_set_index +}; + +static void bm_init_types_bmloopcol(void) +{ + /* pass */ + mathutils_bmloopcol_cb_index = Mathutils_RegisterCallback(&mathutils_bmloopcol_cb); +} + +int BPy_BMLoopColor_AssignPyObject(struct MLoopCol *mloopcol, PyObject *value) +{ + float tvec[3]; + if (mathutils_array_parse(tvec, 3, 3, value, "BMLoopCol") != -1) { + mloopcol_from_float(mloopcol, tvec); + return 0; + } + else { + return -1; + } +} + +PyObject *BPy_BMLoopColor_CreatePyObject(struct MLoopCol *data) +{ + PyObject *color_capsule; + color_capsule = PyCapsule_New(data, NULL, NULL); + return Color_CreatePyObject_cb(color_capsule, mathutils_bmloopcol_cb_index, 0); +} + +#undef MLOOPCOL_FROM_CAPSULE + +/* --- End Mesh Loop Color --- */ + /* call to init all types */ void BPy_BM_init_types_meshdata(void) { bm_init_types_bmloopuv(); + bm_init_types_bmloopcol(); } diff --git a/source/blender/python/bmesh/bmesh_py_types_meshdata.h b/source/blender/python/bmesh/bmesh_py_types_meshdata.h index 4255166ef08..75a4778571c 100644 --- a/source/blender/python/bmesh/bmesh_py_types_meshdata.h +++ b/source/blender/python/bmesh/bmesh_py_types_meshdata.h @@ -32,10 +32,21 @@ extern PyTypeObject BPy_BMLoopUV_Type; +#define BPy_BMLoopUV_Check(v) (Py_TYPE(v) == &BPy_BMLoopUV_Type) + +typedef struct BPy_BMGenericMeshData { + PyObject_VAR_HEAD + void *data; +} BPy_BMGenericMeshData; + struct MLoopUV; +int BPy_BMLoopUV_AssignPyObject(struct MLoopUV *data, PyObject *value); PyObject *BPy_BMLoopUV_CreatePyObject(struct MLoopUV *data); +int BPy_BMLoopColor_AssignPyObject(struct MLoopUV *data, PyObject *value); +PyObject *BPy_BMLoopColor_CreatePyObject(struct MLoopUV *data); + void BPy_BM_init_types_meshdata(void); #endif /* __BMESH_PY_TYPES_MESHDATA_H__ */ -- cgit v1.2.3