diff options
author | Bastien Montagne <montagne29@wanadoo.fr> | 2014-06-17 18:03:40 +0400 |
---|---|---|
committer | Bastien Montagne <montagne29@wanadoo.fr> | 2014-06-17 18:03:40 +0400 |
commit | f94b87bbb89b2350be2f5b26c2afb2372fd9d99c (patch) | |
tree | a9f96351ba4b8e1e21f1d64c14e861d948c8358e /source/blender/python/generic | |
parent | ab5f4c4dfad3880f60c8555437aeb0c9ba85cd1e (diff) |
New python API for units handling.
Exposes all supported unit systems & types, and to_value()/to_string() functions.
Reviewed and enhanced by CampbellBarton, many thanks!
Differential Revision: https://developer.blender.org/D416
Diffstat (limited to 'source/blender/python/generic')
-rw-r--r-- | source/blender/python/generic/py_capi_utils.c | 70 | ||||
-rw-r--r-- | source/blender/python/generic/py_capi_utils.h | 2 |
2 files changed, 72 insertions, 0 deletions
diff --git a/source/blender/python/generic/py_capi_utils.c b/source/blender/python/generic/py_capi_utils.c index 54e27a30791..8454f5b5887 100644 --- a/source/blender/python/generic/py_capi_utils.c +++ b/source/blender/python/generic/py_capi_utils.c @@ -904,3 +904,73 @@ PyObject *PyC_FlagSet_FromBitfield(PyC_FlagSet *items, int flag) return ret; } + + +/** + * \return -1 on error, else 0 + * + * \note it is caller's responsibility to acquire & release GIL! + */ +int PyC_RunString_AsNumber(const char *expr, double *value, const char *filename) +{ + PyObject *py_dict, *mod, *retval; + int error_ret = 0; + PyObject *main_mod = NULL; + + PyC_MainModule_Backup(&main_mod); + + py_dict = PyC_DefaultNameSpace(filename); + + mod = PyImport_ImportModule("math"); + if (mod) { + PyDict_Merge(py_dict, PyModule_GetDict(mod), 0); /* 0 - don't overwrite existing values */ + Py_DECREF(mod); + } + else { /* highly unlikely but possibly */ + PyErr_Print(); + PyErr_Clear(); + } + + retval = PyRun_String(expr, Py_eval_input, py_dict, py_dict); + + if (retval == NULL) { + error_ret = -1; + } + else { + double val; + + if (PyTuple_Check(retval)) { + /* Users my have typed in 10km, 2m + * add up all values */ + int i; + val = 0.0; + + for (i = 0; i < PyTuple_GET_SIZE(retval); i++) { + const double val_item = PyFloat_AsDouble(PyTuple_GET_ITEM(retval, i)); + if (val_item == -1 && PyErr_Occurred()) { + val = -1; + break; + } + val += val_item; + } + } + else { + val = PyFloat_AsDouble(retval); + } + Py_DECREF(retval); + + if (val == -1 && PyErr_Occurred()) { + error_ret = -1; + } + else if (!finite(val)) { + *value = 0.0; + } + else { + *value = val; + } + } + + PyC_MainModule_Restore(main_mod); + + return error_ret; +} diff --git a/source/blender/python/generic/py_capi_utils.h b/source/blender/python/generic/py_capi_utils.h index 0afc4dd98d9..559a8e15678 100644 --- a/source/blender/python/generic/py_capi_utils.h +++ b/source/blender/python/generic/py_capi_utils.h @@ -73,4 +73,6 @@ int PyC_FlagSet_ValueFromID(PyC_FlagSet *item, const char *identifier, int int PyC_FlagSet_ToBitfield(PyC_FlagSet *items, PyObject *value, int *r_value, const char *error_prefix); PyObject *PyC_FlagSet_FromBitfield(PyC_FlagSet *items, int flag); +int PyC_RunString_AsNumber(const char *expr, double *value, const char *filename); + #endif /* __PY_CAPI_UTILS_H__ */ |