/** * \return success */ bool BPY_execute_string_as_number(bContext *C, const char *expr, double *value, const bool verbose) { PyGILState_STATE gilstate; bool ok = true; if (!value || !expr) return -1; if (expr[0] == '\0') { *value = 0.0; return ok; } bpy_context_set(C, &gilstate); ok = PyC_RunString_AsNumber(expr, value, "<blender button>"); if (ok == false) { if (verbose) { BPy_errors_to_report_ex(CTX_wm_reports(C), false, false); } else { PyErr_Clear(); } } bpy_context_clear(C, &gilstate); return ok; }
static PyObject *bpyunits_to_value(PyObject *UNUSED(self), PyObject *args, PyObject *kw) { static const char *kwlist[] = {"unit_system", "unit_category", "str_input", "str_ref_unit", NULL}; char *usys_str = NULL, *ucat_str = NULL, *inpt = NULL, *uref = NULL; const float scale = 1.0f; char *str; Py_ssize_t str_len; double result; int usys, ucat; PyObject *ret; if (!PyArg_ParseTupleAndKeywords(args, kw, "sss#|z:bpy.utils.units.to_value", (char **)kwlist, &usys_str, &ucat_str, &inpt, &str_len, &uref)) { return NULL; } if (!bpyunits_validate(usys_str, ucat_str, &usys, &ucat)) { return NULL; } str_len = str_len * 2 + 64; str = PyMem_MALLOC(sizeof(*str) * (size_t)str_len); BLI_strncpy(str, inpt, (size_t)str_len); bUnit_ReplaceString(str, (int)str_len, uref, scale, usys, ucat); if (!PyC_RunString_AsNumber(str, &result, "<bpy_units_api>")) { if (PyErr_Occurred()) { PyErr_Print(); PyErr_Clear(); } PyErr_Format(PyExc_ValueError, "'%.200s' (converted as '%s') could not be evaluated.", inpt, str); ret = NULL; } else { ret = PyFloat_FromDouble(result); } PyMem_FREE(str); return ret; }