From 36c3ed2350bb504b1b0a7a378d2d40a73bc3a769 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Wed, 4 Mar 2009 13:26:33 +0000 Subject: [PATCH] run script in the text editor is back. UI scripts dont work yet. --- .../editors/space_script/script_edit.c | 2 +- .../editors/space_script/space_script.c | 4 +- source/blender/editors/space_text/text_ops.c | 36 ++++------- source/blender/python/BPY_extern.h | 2 +- source/blender/python/intern/bpy_interface.c | 59 +++++++++++++++---- source/blender/python/intern/stubs.c | 2 +- source/creator/creator.c | 4 +- 7 files changed, 66 insertions(+), 43 deletions(-) diff --git a/source/blender/editors/space_script/script_edit.c b/source/blender/editors/space_script/script_edit.c index 1194d4b254c..da53057c5eb 100644 --- a/source/blender/editors/space_script/script_edit.c +++ b/source/blender/editors/space_script/script_edit.c @@ -66,7 +66,7 @@ static int run_pyfile_exec(bContext *C, wmOperator *op) char filename[512]; RNA_string_get(op->ptr, "filename", filename); #ifndef DISABLE_PYTHON - BPY_run_python_script(C, filename); + BPY_run_python_script(C, filename, NULL); #endif ED_region_tag_redraw(ar); diff --git a/source/blender/editors/space_script/space_script.c b/source/blender/editors/space_script/space_script.c index 8fedcd8761f..054033fd987 100644 --- a/source/blender/editors/space_script/space_script.c +++ b/source/blender/editors/space_script/space_script.c @@ -155,10 +155,10 @@ static void script_main_area_draw(const bContext *C, ARegion *ar) UI_view2d_view_ortho(C, v2d); /* data... */ - // BPY_run_python_script(C, "/root/blender-svn/blender25/test.py"); + // BPY_run_python_script(C, "/root/blender-svn/blender25/test.py", NULL); if (sscript->script) { - //BPY_run_python_script_space(scpt->script.filename); + //BPY_run_python_script_space(scpt->script.filename, NULL); BPY_run_script_space_draw(C, sscript); } diff --git a/source/blender/editors/space_text/text_ops.c b/source/blender/editors/space_text/text_ops.c index 6e7a507b35b..a77bea4c640 100644 --- a/source/blender/editors/space_text/text_ops.c +++ b/source/blender/editors/space_text/text_ops.c @@ -78,6 +78,11 @@ static int text_new_poll(bContext *C) return 1; } +static int text_valid_poll(bContext *C) +{ + return CTX_data_edit_text(C) ? 1:0; +} + static int text_edit_poll(bContext *C) { Text *text= CTX_data_edit_text(C); @@ -435,31 +440,12 @@ static int run_script_exec(bContext *C, wmOperator *op) return OPERATOR_CANCELLED; #else Text *text= CTX_data_edit_text(C); - char *py_filename; - if(0) { // XXX !BPY_txt_do_python_Text(text)) { - int lineno = 0; // XXX BPY_Err_getLinenumber(); - // jump to error if happened in current text: - py_filename = (char*) NULL; // XXX BPY_Err_getFilename(); - - /* st->text can become NULL: user called Blender.Load(blendfile) - * before the end of the script. */ - text= CTX_data_edit_text(C); - - if(!strcmp(py_filename, text->id.name+2)) { - // XXX error_pyscript( ); - if(lineno >= 0) { - txt_move_toline(text, lineno-1, 0); - txt_sel_line(text); - - WM_event_add_notifier(C, NC_TEXT|ND_CURSOR, text); - } - } - else - BKE_report(op->reports, RPT_ERROR, "Error in other (possibly external) file, check console."); - } - - return OPERATOR_FINISHED; + if (BPY_run_python_script( C, NULL, text )) + return OPERATOR_FINISHED; + + BKE_report(op->reports, RPT_ERROR, "Python script fail, look in the console for now..."); + return OPERATOR_CANCELLED; #endif } @@ -474,6 +460,7 @@ void TEXT_OT_run_script(wmOperatorType *ot) ot->poll= text_edit_poll; } + /******************* refresh pyconstraints operator *********************/ static int refresh_pyconstraints_exec(bContext *C, wmOperator *op) @@ -2541,6 +2528,7 @@ void TEXT_OT_to_3d_object(wmOperatorType *ot) RNA_def_boolean(ot->srna, "split_lines", 0, "Split Lines", "Create one object per line in the text."); } + /************************ undo ******************************/ void ED_text_undo_step(bContext *C, int step) diff --git a/source/blender/python/BPY_extern.h b/source/blender/python/BPY_extern.h index 219ab2d3c41..4b7238a2f7c 100644 --- a/source/blender/python/BPY_extern.h +++ b/source/blender/python/BPY_extern.h @@ -97,7 +97,7 @@ extern "C" { int BPY_menu_invoke( struct BPyMenu *pym, short menutype ); /* 2.5 UI Scripts */ - void BPY_run_python_script( struct bContext *C, const char *filename ); // 2.5 working + int BPY_run_python_script( struct bContext *C, const char *filename, struct Text *text ); // 2.5 working int BPY_run_script_space_draw(struct bContext *C, struct SpaceScript * sc); // 2.5 working // int BPY_run_script_space_listener(struct bContext *C, struct SpaceScript * sc, struct ARegion *ar, struct wmNotifier *wmn); // 2.5 working 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;} diff --git a/source/creator/creator.c b/source/creator/creator.c index 0160ac7acb2..ad50e425415 100644 --- a/source/creator/creator.c +++ b/source/creator/creator.c @@ -687,7 +687,7 @@ int main(int argc, char **argv) //XXX // FOR TESTING ONLY a++; - BPY_run_python_script(C, argv[a]); + BPY_run_python_script(C, argv[a], NULL); #if 0 a++; if (a < argc) { @@ -696,7 +696,7 @@ int main(int argc, char **argv) main_init_screen(); scr_init = 1; } - BPY_run_python_script(C, argv[a]); + BPY_run_python_script(C, argv[a], NULL); } else printf("\nError: you must specify a Python script after '-P '.\n"); #endif