diff options
Diffstat (limited to 'source/blender')
-rw-r--r-- | source/blender/editors/interface/SConscript | 1 | ||||
-rw-r--r-- | source/blender/editors/interface/interface.c | 9 | ||||
-rw-r--r-- | source/blender/python/BPY_extern.h | 2 | ||||
-rw-r--r-- | source/blender/python/intern/bpy_interface.c | 62 |
4 files changed, 69 insertions, 5 deletions
diff --git a/source/blender/editors/interface/SConscript b/source/blender/editors/interface/SConscript index bac3742c12f..e44de5410f1 100644 --- a/source/blender/editors/interface/SConscript +++ b/source/blender/editors/interface/SConscript @@ -9,6 +9,7 @@ for source in env.Glob('*_api.c'): incs = '../include ../../blenlib ../../blenfont ../../blenkernel ../../makesdna ../../imbuf' incs += ' ../../makesrna ../../windowmanager #/intern/guardedalloc' incs += ' #/extern/glew/include' +incs += ' ../../python/' # python button eval defs = [] diff --git a/source/blender/editors/interface/interface.c b/source/blender/editors/interface/interface.c index 9e9b6165a73..f3bb975a1f7 100644 --- a/source/blender/editors/interface/interface.c +++ b/source/blender/editors/interface/interface.c @@ -66,6 +66,8 @@ #include "RNA_access.h" #include "RNA_types.h" +#include "BPY_extern.h" + #include "interface_intern.h" #define MENU_WIDTH 120 @@ -1434,10 +1436,9 @@ int ui_set_but_string(bContext *C, uiBut *but, const char *str) double value; /* XXX 2.50 missing python api */ -#if 0 - if(BPY_button_eval(str, &value)) { - BKE_report(CTX_reports(C), RPT_WARNING, "Invalid Python expression, check console"); - value = 0.0f; /* Zero out value on error */ +#ifndef DISABLE_PYTHON + if(BPY_button_eval(C, str, &value)) { + value = ui_get_but_val(but); /* use its original value */ if(str[0]) return 0; diff --git a/source/blender/python/BPY_extern.h b/source/blender/python/BPY_extern.h index db0404c33a8..acb45790ed2 100644 --- a/source/blender/python/BPY_extern.h +++ b/source/blender/python/BPY_extern.h @@ -122,7 +122,7 @@ extern "C" { void BPY_pydriver_update(void); float BPY_pydriver_eval(struct ChannelDriver *driver); - int BPY_button_eval(char *expr, double *value); + int BPY_button_eval(struct bContext *C, char *expr, double *value); /* format importer hook */ int BPY_call_importloader( char *name ); diff --git a/source/blender/python/intern/bpy_interface.c b/source/blender/python/intern/bpy_interface.c index e37e75ab80d..8cd4cdd5dfc 100644 --- a/source/blender/python/intern/bpy_interface.c +++ b/source/blender/python/intern/bpy_interface.c @@ -804,3 +804,65 @@ float BPY_pydriver_eval (ChannelDriver *driver) return result; } + +int BPY_button_eval(bContext *C, char *expr, double *value) +{ + PyGILState_STATE gilstate; + PyObject *dict, *retval, *expr_conv; + int error_ret = 0; + + if (!value || !expr || expr[0]=='\0') return -1; + + bpy_context_set(C, &gilstate); + + // experemental, fun. "button_convert.convert" is currently defined in bpy_ops.py + { + PyObject *mod= PyDict_GetItemString(PySys_GetObject("modules"), "button_convert"); + if(mod && PyModule_Check(mod)) { + PyObject *mod_dict= PyModule_GetDict(mod); + PyObject *func= PyDict_GetItemString(mod_dict, "convert"); + if(func) { + PyObject *expr_conv = PyObject_CallFunction(func, "s", expr); + if(expr_conv==NULL) { + PyErr_Print(); + PyErr_Clear(); + } + else { + expr= _PyUnicode_AsString(expr_conv); /* TODO, check */ + } + } + } + } + + dict= CreateGlobalDictionary(C); + retval = PyRun_String(expr, Py_eval_input, dict, dict); + + if(expr_conv) { + Py_DECREF(expr_conv); /* invalidates expr */ + } + + if (retval == NULL) { + error_ret= -1; + } + else { + double val = PyFloat_AsDouble(retval); + Py_DECREF(retval); + + if(val==-1 && PyErr_Occurred()) { + error_ret= -1; + } + else { + *value= val; + } + } + + if(error_ret) { + BPy_errors_to_report(CTX_wm_reports(C)); + } + + Py_DECREF(dict); + bpy_context_clear(C, &gilstate); + + return error_ret; +} + |