From 36c3ed2350bb504b1b0a7a378d2d40a73bc3a769 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Wed, 4 Mar 2009 13:26:33 +0000 Subject: run script in the text editor is back. UI scripts dont work yet. --- source/blender/python/intern/bpy_interface.c | 59 ++++++++++++++++++++++------ source/blender/python/intern/stubs.c | 2 +- 2 files changed, 48 insertions(+), 13 deletions(-) (limited to 'source/blender/python/intern') diff --git a/source/blender/python/intern/bpy_interface.c b/source/blender/python/intern/bpy_interface.c index 7a460f93e67..bb315cda47a 100644 --- a/source/blender/python/intern/bpy_interface.c +++ b/source/blender/python/intern/bpy_interface.c @@ -16,6 +16,17 @@ #include "DNA_space_types.h" +#include "BKE_text.h" +#include "DNA_text_types.h" +#include "MEM_guardedalloc.h" + +void BPY_free_compiled_text( struct Text *text ) +{ + if( text->compiled ) { + Py_DECREF( ( PyObject * ) text->compiled ); + text->compiled = NULL; + } +} /***************************************************************************** * Description: This function creates a new Python dictionary object. @@ -84,31 +95,55 @@ void BPY_end_python( void ) return; } -void BPY_run_python_script( bContext *C, const char *fn ) +/* Can run a file or text block */ +int BPY_run_python_script( bContext *C, const char *fn, struct Text *text ) { PyObject *py_dict, *py_result; - char pystring[512]; PyGILState_STATE gilstate; - - /* TODO - look into a better way to run a file */ - sprintf(pystring, "exec(open(r'%s').read())", fn); + + if (fn==NULL && text==NULL) { + return 0; + } //BPY_start_python(); gilstate = PyGILState_Ensure(); - + py_dict = CreateGlobalDictionary(C); + + if (text) { + + if( !text->compiled ) { /* if it wasn't already compiled, do it now */ + char *buf = txt_to_buf( text ); + + text->compiled = + Py_CompileString( buf, text->id.name+2, Py_file_input ); + + MEM_freeN( buf ); + + if( PyErr_Occurred( ) ) { + BPY_free_compiled_text( text ); + return NULL; + } + } + py_result = PyEval_EvalCode( text->compiled, py_dict, py_dict ); + + } else { + char pystring[512]; + /* TODO - look into a better way to run a file */ + sprintf(pystring, "exec(open(r'%s').read())", fn); + py_result = PyRun_String( pystring, Py_file_input, py_dict, py_dict ); + } - py_result = PyRun_String( pystring, Py_file_input, py_dict, py_dict ); - - if (!py_result) + if (!py_result) { PyErr_Print(); - else + } else { Py_DECREF( py_result ); - + } PyGILState_Release(gilstate); //BPY_end_python(); + return py_result ? 1:0; } @@ -155,7 +190,7 @@ static int bpy_run_script_init(bContext *C, SpaceScript * sc) return 0; if (sc->script->py_draw==NULL && sc->script->scriptname[0] != '\0') - BPY_run_python_script(C, sc->script->scriptname); + BPY_run_python_script(C, sc->script->scriptname, NULL); if (sc->script->py_draw==NULL) return 0; diff --git a/source/blender/python/intern/stubs.c b/source/blender/python/intern/stubs.c index 7ee2c69960d..e438966f324 100644 --- a/source/blender/python/intern/stubs.c +++ b/source/blender/python/intern/stubs.c @@ -36,7 +36,7 @@ void BPY_do_pyscript() {} void BPY_pydriver_eval() {} void BPY_pydriver_get_objects() {} void BPY_clear_script() {} -void BPY_free_compiled_text() {} +//void BPY_free_compiled_text() {} void BPY_pyconstraint_eval() {} void BPY_pyconstraint_target() {} int BPY_is_pyconstraint() {return 0;} -- cgit v1.2.3