From bab4cd69913eee9aa30d61db7dc46e21655e4bde Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Sat, 28 Feb 2009 13:27:45 +0000 Subject: Python experimental UI API Can draw panels in the scripts space containing RNA and operator buttons. * Added bpyui.register() so scripts can draw buttons and panels into the scripts space type. * wrapped drawBlock, drawPanels and matchPanelsView2d * Operator buttons take a python dictionary used to set the button defaults. * BPY_getFileAndNum utility function to get the filename and line number python is currently running. --- source/blender/python/intern/bpy_interface.c | 130 +++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) (limited to 'source/blender/python/intern/bpy_interface.c') diff --git a/source/blender/python/intern/bpy_interface.c b/source/blender/python/intern/bpy_interface.c index 17802ec3a29..fcf748c6887 100644 --- a/source/blender/python/intern/bpy_interface.c +++ b/source/blender/python/intern/bpy_interface.c @@ -12,6 +12,9 @@ #include "bpy_rna.h" #include "bpy_operator.h" +#include "bpy_ui.h" + +#include "DNA_space_types.h" /***************************************************************************** @@ -107,3 +110,130 @@ void BPY_run_python_script( bContext *C, const char *fn ) //BPY_end_python(); } + + +/* TODO - move into bpy_space.c ? */ +/* GUI interface routines */ + +/* Copied from Draw.c */ +static void exit_pydraw( SpaceScript * sc, short err ) +{ + Script *script = NULL; + + if( !sc || !sc->script ) + return; + + script = sc->script; + + if( err ) { + PyErr_Print( ); + script->flags = 0; /* mark script struct for deletion */ + SCRIPT_SET_NULL(script); + script->scriptname[0] = '\0'; + script->scriptarg[0] = '\0'; +// XXX 2.5 error_pyscript(); +// XXX 2.5 scrarea_queue_redraw( sc->area ); + } + +#if 0 // XXX 2.5 + BPy_Set_DrawButtonsList(sc->but_refs); + BPy_Free_DrawButtonsList(); /*clear all temp button references*/ +#endif + + sc->but_refs = NULL; + + Py_XDECREF( ( PyObject * ) script->py_draw ); + Py_XDECREF( ( PyObject * ) script->py_event ); + Py_XDECREF( ( PyObject * ) script->py_button ); + + script->py_draw = script->py_event = script->py_button = NULL; +} + +static int bpy_run_script_init(bContext *C, SpaceScript * sc) +{ + if (sc->script==NULL) + return 0; + + if (sc->script->py_draw==NULL && sc->script->scriptname[0] != '\0') + BPY_run_python_script(C, sc->script->scriptname); + + if (sc->script->py_draw==NULL) + return 0; + + return 1; +} + +int BPY_run_script_space_draw(bContext *C, SpaceScript * sc) +{ + if (bpy_run_script_init(C, sc)) { + PyGILState_STATE gilstate = PyGILState_Ensure(); + PyObject *result = PyObject_CallObject( sc->script->py_draw, NULL ); + PyGILState_Release(gilstate); + + if (result==NULL) + exit_pydraw(sc, 1); + } + return 1; +} + +// XXX - not used yet, listeners dont get a context +int BPY_run_script_space_listener(bContext *C, SpaceScript * sc) +{ + if (bpy_run_script_init(C, sc)) { + PyGILState_STATE gilstate = PyGILState_Ensure(); + + PyObject *result = PyObject_CallObject( sc->script->py_draw, NULL ); + PyGILState_Release(gilstate); + + if (result==NULL) + exit_pydraw(sc, 1); + } + return 1; +} + +#if 0 +/* called from the the scripts window, assume context is ok */ +int BPY_run_python_script_space(const char *modulename, const char *func) +{ + PyObject *py_dict, *py_result= NULL; + char pystring[512]; + PyGILState_STATE gilstate; + + /* for calling the module function */ + PyObject *py_func, + + gilstate = PyGILState_Ensure(); + + py_dict = CreateGlobalDictionary(C); + + PyObject *module = PyImport_ImportModule(scpt->script.filename); + if (module==NULL) { + PyErr_SetFormat(PyExc_SystemError, "could not import '%s'", scpt->script.filename); + } + else { + py_func = PyObject_GetAttrString(modulename, func); + if (py_func==NULL) { + PyErr_SetFormat(PyExc_SystemError, "module has no function '%s.%s'\n", scpt->script.filename, func); + } + else { + if (!PyCallable_Check(py_func)) { + PyErr_SetFormat(PyExc_SystemError, "module item is not callable '%s.%s'\n", scpt->script.filename, func); + } + else { + py_result= PyObject_CallObject(py_func, NULL); // XXX will need args eventually + } + } + } + + if (!py_result) + PyErr_Print(); + else + Py_DECREF( py_result ); + + Py_XDECREF(module); + + + PyGILState_Release(gilstate); + return 1; +} +#endif -- cgit v1.2.3