[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [19190] branches/blender2.5/blender/source : run script in the text editor is back.

Campbell Barton ideasman42 at gmail.com
Wed Mar 4 14:26:33 CET 2009


Revision: 19190
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=19190
Author:   campbellbarton
Date:     2009-03-04 14:26:33 +0100 (Wed, 04 Mar 2009)

Log Message:
-----------
run script in the text editor is back. UI scripts dont work yet.

Modified Paths:
--------------
    branches/blender2.5/blender/source/blender/editors/space_script/script_edit.c
    branches/blender2.5/blender/source/blender/editors/space_script/space_script.c
    branches/blender2.5/blender/source/blender/editors/space_text/text_ops.c
    branches/blender2.5/blender/source/blender/python/BPY_extern.h
    branches/blender2.5/blender/source/blender/python/intern/bpy_interface.c
    branches/blender2.5/blender/source/blender/python/intern/stubs.c
    branches/blender2.5/blender/source/creator/creator.c

Modified: branches/blender2.5/blender/source/blender/editors/space_script/script_edit.c
===================================================================
--- branches/blender2.5/blender/source/blender/editors/space_script/script_edit.c	2009-03-04 09:38:26 UTC (rev 19189)
+++ branches/blender2.5/blender/source/blender/editors/space_script/script_edit.c	2009-03-04 13:26:33 UTC (rev 19190)
@@ -66,7 +66,7 @@
 	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);
 

Modified: branches/blender2.5/blender/source/blender/editors/space_script/space_script.c
===================================================================
--- branches/blender2.5/blender/source/blender/editors/space_script/space_script.c	2009-03-04 09:38:26 UTC (rev 19189)
+++ branches/blender2.5/blender/source/blender/editors/space_script/space_script.c	2009-03-04 13:26:33 UTC (rev 19190)
@@ -155,10 +155,10 @@
 	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);
 	}
 	

Modified: branches/blender2.5/blender/source/blender/editors/space_text/text_ops.c
===================================================================
--- branches/blender2.5/blender/source/blender/editors/space_text/text_ops.c	2009-03-04 09:38:26 UTC (rev 19189)
+++ branches/blender2.5/blender/source/blender/editors/space_text/text_ops.c	2009-03-04 13:26:33 UTC (rev 19190)
@@ -78,6 +78,11 @@
 	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 @@
 	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 @@
 	ot->poll= text_edit_poll;
 }
 
+
 /******************* refresh pyconstraints operator *********************/
 
 static int refresh_pyconstraints_exec(bContext *C, wmOperator *op)
@@ -2541,6 +2528,7 @@
 	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)

Modified: branches/blender2.5/blender/source/blender/python/BPY_extern.h
===================================================================
--- branches/blender2.5/blender/source/blender/python/BPY_extern.h	2009-03-04 09:38:26 UTC (rev 19189)
+++ branches/blender2.5/blender/source/blender/python/BPY_extern.h	2009-03-04 13:26:33 UTC (rev 19190)
@@ -97,7 +97,7 @@
 	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
 	

Modified: branches/blender2.5/blender/source/blender/python/intern/bpy_interface.c
===================================================================
--- branches/blender2.5/blender/source/blender/python/intern/bpy_interface.c	2009-03-04 09:38:26 UTC (rev 19189)
+++ branches/blender2.5/blender/source/blender/python/intern/bpy_interface.c	2009-03-04 13:26:33 UTC (rev 19190)
@@ -16,7 +16,18 @@
 
 #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 @@
 	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 @@
 		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;

Modified: branches/blender2.5/blender/source/blender/python/intern/stubs.c
===================================================================
--- branches/blender2.5/blender/source/blender/python/intern/stubs.c	2009-03-04 09:38:26 UTC (rev 19189)
+++ branches/blender2.5/blender/source/blender/python/intern/stubs.c	2009-03-04 13:26:33 UTC (rev 19190)
@@ -36,7 +36,7 @@
 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;}

Modified: branches/blender2.5/blender/source/creator/creator.c
===================================================================
--- branches/blender2.5/blender/source/creator/creator.c	2009-03-04 09:38:26 UTC (rev 19189)
+++ branches/blender2.5/blender/source/creator/creator.c	2009-03-04 13:26:33 UTC (rev 19190)
@@ -687,7 +687,7 @@
 				//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 @@
 						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





More information about the Bf-blender-cvs mailing list