diff options
author | Lukas Toenne <lukas.toenne@googlemail.com> | 2013-01-05 18:56:37 +0400 |
---|---|---|
committer | Lukas Toenne <lukas.toenne@googlemail.com> | 2013-01-05 18:56:37 +0400 |
commit | e8b415bdb4ba282e3574cad1463bf2512fe5eb8f (patch) | |
tree | 42d45b749f9442393ef08e41a1c98bc3e4978872 /source/blender/python/generic | |
parent | 5ee3cd6c86391e78a6905d1aa9c5241f623dcfc1 (diff) |
This patch adds support in bpy.props for getter/setter callback functions. We already have update callbacks, but generic get/set functions can come in handy in some cases where the functionality is too complex to use a single value.
The current C callback functions are too simple allow a straightforward implementation, in particular they don't receive the PropertyRNA pointer itself as an argument, which means the callback cannot directly access the PropertyRNA's py_data pointers which store the python function objects. For this reason a second runtime variant of these callbacks has been added. It is only used for runtime callbacks and not in makesrna, but otherwise works the same way.
Diffstat (limited to 'source/blender/python/generic')
-rw-r--r-- | source/blender/python/generic/py_capi_utils.c | 48 | ||||
-rw-r--r-- | source/blender/python/generic/py_capi_utils.h | 2 |
2 files changed, 50 insertions, 0 deletions
diff --git a/source/blender/python/generic/py_capi_utils.c b/source/blender/python/generic/py_capi_utils.c index 9a064923736..86230318896 100644 --- a/source/blender/python/generic/py_capi_utils.c +++ b/source/blender/python/generic/py_capi_utils.c @@ -112,6 +112,54 @@ int PyC_AsArray(void *array, PyObject *value, const Py_ssize_t length, return 0; } +/* array utility function */ +PyObject *PyC_FromArray(const void *array, int length, const PyTypeObject *type, + const short is_double, const char *error_prefix) +{ + PyObject *tuple; + int i; + + tuple = PyTuple_New(length); + + /* for each type */ + if (type == &PyFloat_Type) { + if (is_double) { + const double *array_double = array; + for (i = 0; i < length; ++i) { + PyTuple_SET_ITEM(tuple, i, PyFloat_FromDouble(array_double[i])); + } + } + else { + const float *array_float = array; + for (i = 0; i < length; ++i) { + PyTuple_SET_ITEM(tuple, i, PyFloat_FromDouble(array_float[i])); + } + } + } + else if (type == &PyLong_Type) { + /* could use is_double for 'long int' but no use now */ + const int *array_int = array; + for (i = 0; i < length; ++i) { + PyTuple_SET_ITEM(tuple, i, PyLong_FromLong(array_int[i])); + } + } + else if (type == &PyBool_Type) { + const int *array_bool = array; + for (i = 0; i < length; ++i) { + PyTuple_SET_ITEM(tuple, i, PyBool_FromLong(array_bool[i])); + } + } + else { + Py_DECREF(tuple); + PyErr_Format(PyExc_TypeError, + "%s: internal error %s is invalid", + error_prefix, type->tp_name); + return NULL; + } + + return tuple; +} + /* for debugging */ void PyC_ObSpit(const char *name, PyObject *var) diff --git a/source/blender/python/generic/py_capi_utils.h b/source/blender/python/generic/py_capi_utils.h index db582bd7086..b4eea6ac90c 100644 --- a/source/blender/python/generic/py_capi_utils.h +++ b/source/blender/python/generic/py_capi_utils.h @@ -39,6 +39,8 @@ void PyC_FileAndNum(const char **filename, int *lineno); void PyC_FileAndNum_Safe(const char **filename, int *lineno); /* checks python is running */ int PyC_AsArray(void *array, PyObject *value, const Py_ssize_t length, const PyTypeObject *type, const short is_double, const char *error_prefix); +PyObject * PyC_FromArray(const void *array, int length, const PyTypeObject *type, + const short is_double, const char *error_prefix); /* follow http://www.python.org/dev/peps/pep-0383/ */ PyObject * PyC_UnicodeFromByte(const char *str); |