From 6cb1e38c87168b0ab83ca907bcf537d50c263454 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Mon, 16 Sep 2019 10:31:17 +1000 Subject: ImBuf Py API: add filepath access D5804 by @cmbasnett with setter support added. --- source/blender/python/generic/imbuf_py_api.c | 34 ++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'source/blender/python') diff --git a/source/blender/python/generic/imbuf_py_api.c b/source/blender/python/generic/imbuf_py_api.c index ba8da46d3f6..0b595987d73 100644 --- a/source/blender/python/generic/imbuf_py_api.c +++ b/source/blender/python/generic/imbuf_py_api.c @@ -227,6 +227,35 @@ static int py_imbuf_ppm_set(Py_ImBuf *self, PyObject *value, void *UNUSED(closur return 0; } +PyDoc_STRVAR(py_imbuf_filepath_doc, "filepath associated with this image.\n\n:type: string"); +static PyObject *py_imbuf_filepath_get(Py_ImBuf *self, void *UNUSED(closure)) +{ + PY_IMBUF_CHECK_OBJ(self); + ImBuf *ibuf = self->ibuf; + return PyC_UnicodeFromByte(ibuf->name); +} + +static int py_imbuf_filepath_set(Py_ImBuf *self, PyObject *value, void *UNUSED(closure)) +{ + PY_IMBUF_CHECK_INT(self); + + if (!PyUnicode_Check(value)) { + PyErr_SetString(PyExc_TypeError, "expected a string!"); + return -1; + } + + ImBuf *ibuf = self->ibuf; + Py_ssize_t value_str_len_max = sizeof(ibuf->name); + Py_ssize_t value_str_len; + const char *value_str = _PyUnicode_AsStringAndSize(value, &value_str_len); + if (value_str_len >= value_str_len_max) { + PyErr_Format(PyExc_TypeError, "filepath length over %zd", value_str_len_max - 1); + return -1; + } + memcpy(ibuf->name, value_str, value_str_len + 1); + return 0; +} + static PyGetSetDef Py_ImBuf_getseters[] = { {(char *)"size", (getter)py_imbuf_size_get, (setter)NULL, (char *)py_imbuf_size_doc, NULL}, {(char *)"ppm", @@ -234,6 +263,11 @@ static PyGetSetDef Py_ImBuf_getseters[] = { (setter)py_imbuf_ppm_set, (char *)py_imbuf_ppm_doc, NULL}, + {(char *)"filepath", + (getter)py_imbuf_filepath_get, + (setter)py_imbuf_filepath_set, + (char *)py_imbuf_filepath_doc, + NULL}, {NULL, NULL, NULL, NULL, NULL} /* Sentinel */ }; -- cgit v1.2.3