diff options
Diffstat (limited to 'source/blender/python/intern/bpy_interface.c')
-rw-r--r-- | source/blender/python/intern/bpy_interface.c | 183 |
1 files changed, 32 insertions, 151 deletions
diff --git a/source/blender/python/intern/bpy_interface.c b/source/blender/python/intern/bpy_interface.c index 2c615a52860..3e3e946ae86 100644 --- a/source/blender/python/intern/bpy_interface.c +++ b/source/blender/python/intern/bpy_interface.c @@ -88,7 +88,7 @@ void bpy_context_set(bContext *C, PyGILState_STATE *gilstate) fprintf(stderr, "ERROR: Python context called with a NULL Context. this should not happen!\n"); } - BPY_update_modules(C); /* can give really bad results if this isnt here */ + BPY_modules_update(C); /* can give really bad results if this isnt here */ #ifdef TIME_PY_RUN if(bpy_timer_count==0) { @@ -128,7 +128,7 @@ void bpy_context_clear(bContext *UNUSED(C), PyGILState_STATE *gilstate) } } -void BPY_free_compiled_text( struct Text *text ) +void BPY_text_free_code(Text *text) { if( text->compiled ) { Py_DECREF( ( PyObject * ) text->compiled ); @@ -136,7 +136,7 @@ void BPY_free_compiled_text( struct Text *text ) } } -void BPY_update_modules(bContext *C) +void BPY_modules_update(bContext *C) { #if 0 // slow, this runs all the time poll, draw etc 100's of time a sec. PyObject *mod= PyImport_ImportModuleLevel("bpy", NULL, NULL, NULL, 0); @@ -150,7 +150,7 @@ void BPY_update_modules(bContext *C) } /* must be called before Py_Initialize */ -void BPY_start_python_path(void) +void BPY_python_start_path(void) { char *py_path_bundle= BLI_get_folder(BLENDER_PYTHON, NULL); @@ -191,7 +191,7 @@ void BPY_start_python_path(void) -void BPY_set_context(bContext *C) +void BPY_context_set(bContext *C) { BPy_SetContext(C); } @@ -214,8 +214,8 @@ static struct _inittab bpy_internal_modules[]= { {NULL, NULL} }; -/* call BPY_set_context first */ -void BPY_start_python( int argc, char **argv ) +/* call BPY_context_set first */ +void BPY_python_start( int argc, char **argv ) { PyThreadState *py_tstate = NULL; @@ -227,7 +227,7 @@ void BPY_start_python( int argc, char **argv ) /* builtin modules */ PyImport_ExtendInittab(bpy_internal_modules); - BPY_start_python_path(); /* allow to use our own included python */ + BPY_python_start_path(); /* allow to use our own included python */ Py_Initialize( ); @@ -278,7 +278,7 @@ void BPY_start_python( int argc, char **argv ) PyEval_ReleaseThread(py_tstate); } -void BPY_end_python( void ) +void BPY_python_end(void) { // fprintf(stderr, "Ending Python!\n"); @@ -312,12 +312,13 @@ void BPY_end_python( void ) } -/* Can run a file or text block */ -int BPY_run_python_script( bContext *C, const char *fn, struct Text *text, struct ReportList *reports) +static int python_script_exec(bContext *C, const char *fn, struct Text *text, struct ReportList *reports) { PyObject *py_dict= NULL, *py_result= NULL; PyGILState_STATE gilstate; - + + BKE_assert(fn || text); + if (fn==NULL && text==NULL) { return 0; } @@ -327,7 +328,7 @@ int BPY_run_python_script( bContext *C, const char *fn, struct Text *text, struc if (text) { char fn_dummy[FILE_MAXDIR]; bpy_text_filename_get(fn_dummy, text); - + if( !text->compiled ) { /* if it wasn't already compiled, do it now */ char *buf = txt_to_buf( text ); @@ -336,8 +337,8 @@ int BPY_run_python_script( bContext *C, const char *fn, struct Text *text, struc MEM_freeN( buf ); - if( PyErr_Occurred( ) ) { - BPY_free_compiled_text( text ); + if(PyErr_Occurred()) { + BPY_text_free_code(text); } } @@ -345,7 +346,7 @@ int BPY_run_python_script( bContext *C, const char *fn, struct Text *text, struc py_dict = PyC_DefaultNameSpace(fn_dummy); py_result = PyEval_EvalCode(text->compiled, py_dict, py_dict); } - + } else { FILE *fp= fopen(fn, "r"); @@ -354,9 +355,9 @@ int BPY_run_python_script( bContext *C, const char *fn, struct Text *text, struc py_dict = PyC_DefaultNameSpace(fn); #ifdef _WIN32 - /* Previously we used PyRun_File to run directly the code on a FILE + /* Previously we used PyRun_File to run directly the code on a FILE * object, but as written in the Python/C API Ref Manual, chapter 2, - * 'FILE structs for different C libraries can be different and + * 'FILE structs for different C libraries can be different and * incompatible'. * So now we load the script file data to a buffer */ { @@ -380,14 +381,14 @@ int BPY_run_python_script( bContext *C, const char *fn, struct Text *text, struc py_result= NULL; } } - + if (!py_result) { BPy_errors_to_report(reports); } else { Py_DECREF( py_result ); } -/* super annoying, undo _PyModule_Clear() */ +/* super annoying, undo _PyModule_Clear() */ #define PYMODULE_CLEAR_WORKAROUND if(py_dict) { @@ -405,92 +406,22 @@ int BPY_run_python_script( bContext *C, const char *fn, struct Text *text, struc #endif #undef PYMODULE_CLEAR_WORKAROUND } - + bpy_context_clear(C, &gilstate); - return py_result ? 1:0; + return (py_result != NULL); } - -/* TODO - move into bpy_space.c ? */ -/* GUI interface routines */ - -/* Copied from Draw.c */ -static void exit_pydraw( SpaceScript * sc, short err ) +/* Can run a file or text block */ +int BPY_filepath_exec(bContext *C, const char *filepath, struct ReportList *reports) { - Script *script = NULL; - - if( !sc || !sc->script ) - return; - - script = sc->script; - - if( err ) { - BPy_errors_to_report(NULL); // TODO, reports - 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; + return python_script_exec(C, filepath, NULL, reports); } -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, NULL, NULL); - - if (sc->script->py_draw==NULL) - return 0; - - return 1; -} -int BPY_run_script_space_draw(const struct bContext *C, SpaceScript * sc) +int BPY_text_exec(bContext *C, struct Text *text, struct ReportList *reports) { - if (bpy_run_script_init( (bContext *)C, sc)) { - PyGILState_STATE gilstate = PyGILState_Ensure(); - PyObject *result = PyObject_CallObject( sc->script->py_draw, NULL ); - - if (result==NULL) - exit_pydraw(sc, 1); - - PyGILState_Release(gilstate); - } - 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 ); - - if (result==NULL) - exit_pydraw(sc, 1); - - PyGILState_Release(gilstate); - } - return 1; + return python_script_exec(C, NULL, text, reports); } void BPY_DECREF(void *pyob_ptr) @@ -500,57 +431,7 @@ void BPY_DECREF(void *pyob_ptr) PyGILState_Release(gilstate); } -#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 = PyC_DefaultNameSpace("<dummy>"); - - 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 { - Py_DECREF(py_func); - 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) { - BPy_errors_to_report(NULL); // TODO - reports - } else - Py_DECREF( py_result ); - - Py_XDECREF(module); - - PyDict_SetItemString(PyThreadState_GET()->interp->modules, "__main__", Py_None); - - PyGILState_Release(gilstate); - return 1; -} -#endif - - -int BPY_eval_button(bContext *C, const char *expr, double *value) +int BPY_button_exec(bContext *C, const char *expr, double *value) { PyGILState_STATE gilstate; PyObject *py_dict, *mod, *retval; @@ -622,7 +503,7 @@ int BPY_eval_button(bContext *C, const char *expr, double *value) return error_ret; } -int BPY_eval_string(bContext *C, const char *expr) +int BPY_string_exec(bContext *C, const char *expr) { PyGILState_STATE gilstate; PyObject *py_dict, *retval; @@ -657,7 +538,7 @@ int BPY_eval_string(bContext *C, const char *expr) } -void BPY_load_user_modules(bContext *C) +void BPY_modules_load_user(bContext *C) { PyGILState_STATE gilstate; Main *bmain= CTX_data_main(C); @@ -690,7 +571,7 @@ void BPY_load_user_modules(bContext *C) bpy_context_clear(C, &gilstate); } -int BPY_context_get(bContext *C, const char *member, bContextDataResult *result) +int BPY_context_member_get(bContext *C, const char *member, bContextDataResult *result) { PyObject *pyctx= (PyObject *)CTX_py_dict_get(C); PyObject *item= PyDict_GetItemString(pyctx, member); |