run script in the text editor is back. UI scripts dont work yet.

This commit is contained in:
Campbell Barton 2009-03-04 13:26:33 +00:00
parent 7d5ef14d5f
commit 36c3ed2350
7 changed files with 66 additions and 43 deletions

@ -66,7 +66,7 @@ static int run_pyfile_exec(bContext *C, wmOperator *op)
char filename[512]; char filename[512];
RNA_string_get(op->ptr, "filename", filename); RNA_string_get(op->ptr, "filename", filename);
#ifndef DISABLE_PYTHON #ifndef DISABLE_PYTHON
BPY_run_python_script(C, filename); BPY_run_python_script(C, filename, NULL);
#endif #endif
ED_region_tag_redraw(ar); ED_region_tag_redraw(ar);

@ -155,10 +155,10 @@ static void script_main_area_draw(const bContext *C, ARegion *ar)
UI_view2d_view_ortho(C, v2d); UI_view2d_view_ortho(C, v2d);
/* data... */ /* 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) { 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); BPY_run_script_space_draw(C, sscript);
} }

@ -78,6 +78,11 @@ static int text_new_poll(bContext *C)
return 1; return 1;
} }
static int text_valid_poll(bContext *C)
{
return CTX_data_edit_text(C) ? 1:0;
}
static int text_edit_poll(bContext *C) static int text_edit_poll(bContext *C)
{ {
Text *text= CTX_data_edit_text(C); Text *text= CTX_data_edit_text(C);
@ -435,31 +440,12 @@ static int run_script_exec(bContext *C, wmOperator *op)
return OPERATOR_CANCELLED; return OPERATOR_CANCELLED;
#else #else
Text *text= CTX_data_edit_text(C); Text *text= CTX_data_edit_text(C);
char *py_filename;
if(0) { // XXX !BPY_txt_do_python_Text(text)) { if (BPY_run_python_script( C, NULL, text ))
int lineno = 0; // XXX BPY_Err_getLinenumber(); return OPERATOR_FINISHED;
// jump to error if happened in current text:
py_filename = (char*) NULL; // XXX BPY_Err_getFilename(); BKE_report(op->reports, RPT_ERROR, "Python script fail, look in the console for now...");
return OPERATOR_CANCELLED;
/* 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;
#endif #endif
} }
@ -474,6 +460,7 @@ void TEXT_OT_run_script(wmOperatorType *ot)
ot->poll= text_edit_poll; ot->poll= text_edit_poll;
} }
/******************* refresh pyconstraints operator *********************/ /******************* refresh pyconstraints operator *********************/
static int refresh_pyconstraints_exec(bContext *C, wmOperator *op) 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."); RNA_def_boolean(ot->srna, "split_lines", 0, "Split Lines", "Create one object per line in the text.");
} }
/************************ undo ******************************/ /************************ undo ******************************/
void ED_text_undo_step(bContext *C, int step) void ED_text_undo_step(bContext *C, int step)

@ -97,7 +97,7 @@ extern "C" {
int BPY_menu_invoke( struct BPyMenu *pym, short menutype ); int BPY_menu_invoke( struct BPyMenu *pym, short menutype );
/* 2.5 UI Scripts */ /* 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_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 // int BPY_run_script_space_listener(struct bContext *C, struct SpaceScript * sc, struct ARegion *ar, struct wmNotifier *wmn); // 2.5 working

@ -16,6 +16,17 @@
#include "DNA_space_types.h" #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. * Description: This function creates a new Python dictionary object.
@ -84,31 +95,55 @@ void BPY_end_python( void )
return; 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; PyObject *py_dict, *py_result;
char pystring[512];
PyGILState_STATE gilstate; PyGILState_STATE gilstate;
/* TODO - look into a better way to run a file */ if (fn==NULL && text==NULL) {
sprintf(pystring, "exec(open(r'%s').read())", fn); return 0;
}
//BPY_start_python(); //BPY_start_python();
gilstate = PyGILState_Ensure(); gilstate = PyGILState_Ensure();
py_dict = CreateGlobalDictionary(C); 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(); PyErr_Print();
else } else {
Py_DECREF( py_result ); Py_DECREF( py_result );
}
PyGILState_Release(gilstate); PyGILState_Release(gilstate);
//BPY_end_python(); //BPY_end_python();
return py_result ? 1:0;
} }
@ -155,7 +190,7 @@ static int bpy_run_script_init(bContext *C, SpaceScript * sc)
return 0; return 0;
if (sc->script->py_draw==NULL && sc->script->scriptname[0] != '\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) if (sc->script->py_draw==NULL)
return 0; return 0;

@ -36,7 +36,7 @@ void BPY_do_pyscript() {}
void BPY_pydriver_eval() {} void BPY_pydriver_eval() {}
void BPY_pydriver_get_objects() {} void BPY_pydriver_get_objects() {}
void BPY_clear_script() {} void BPY_clear_script() {}
void BPY_free_compiled_text() {} //void BPY_free_compiled_text() {}
void BPY_pyconstraint_eval() {} void BPY_pyconstraint_eval() {}
void BPY_pyconstraint_target() {} void BPY_pyconstraint_target() {}
int BPY_is_pyconstraint() {return 0;} int BPY_is_pyconstraint() {return 0;}

@ -687,7 +687,7 @@ int main(int argc, char **argv)
//XXX //XXX
// FOR TESTING ONLY // FOR TESTING ONLY
a++; a++;
BPY_run_python_script(C, argv[a]); BPY_run_python_script(C, argv[a], NULL);
#if 0 #if 0
a++; a++;
if (a < argc) { if (a < argc) {
@ -696,7 +696,7 @@ int main(int argc, char **argv)
main_init_screen(); main_init_screen();
scr_init = 1; 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"); else printf("\nError: you must specify a Python script after '-P '.\n");
#endif #endif