[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [19656] branches/blender2.5/blender: undo previous commit, changing running the script to importing it somehow made it crash when running the class functions .

Campbell Barton ideasman42 at gmail.com
Sat Apr 11 12:01:49 CEST 2009


Revision: 19656
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=19656
Author:   campbellbarton
Date:     2009-04-11 12:01:49 +0200 (Sat, 11 Apr 2009)

Log Message:
-----------
undo previous commit, changing running the script to importing it somehow made it crash when running the class functions. will look into this further.

Modified Paths:
--------------
    branches/blender2.5/blender/release/ui/buttons_objects.py
    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/creator/creator.c

Modified: branches/blender2.5/blender/release/ui/buttons_objects.py
===================================================================
--- branches/blender2.5/blender/release/ui/buttons_objects.py	2009-04-11 08:26:51 UTC (rev 19655)
+++ branches/blender2.5/blender/release/ui/buttons_objects.py	2009-04-11 10:01:49 UTC (rev 19656)
@@ -1,4 +1,4 @@
-import bpy
+# import bpy
 
 class OBJECT_PT_transform(bpy.types.Panel):
 	__label__ = "Transform"

Modified: branches/blender2.5/blender/source/blender/python/BPY_extern.h
===================================================================
--- branches/blender2.5/blender/source/blender/python/BPY_extern.h	2009-04-11 08:26:51 UTC (rev 19655)
+++ branches/blender2.5/blender/source/blender/python/BPY_extern.h	2009-04-11 10:01:49 UTC (rev 19656)
@@ -99,7 +99,7 @@
 	/* 2.5 UI Scripts */
 	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
-	void BPY_run_ui_scripts(void);
+	void BPY_run_ui_scripts(struct bContext *C);
 //	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-04-11 08:26:51 UTC (rev 19655)
+++ branches/blender2.5/blender/source/blender/python/intern/bpy_interface.c	2009-04-11 10:01:49 UTC (rev 19656)
@@ -26,7 +26,6 @@
 #include "MEM_guardedalloc.h"
 
 #include "BLI_util.h"
-#include "BLI_string.h"
 
 #include "BKE_context.h"
 #include "BKE_text.h"
@@ -40,13 +39,22 @@
 }
 
 /*****************************************************************************
-* Description: Creates the bpy module and adds it to sys.modules for importing
+* Description: This function creates a new Python dictionary object.
 *****************************************************************************/
-static void BPY_init_modules( void )
+
+static PyObject *CreateGlobalDictionary( bContext *C )
 {
 	PyObject *mod;
+	PyObject *dict = PyDict_New(  );
+	PyObject *item = PyUnicode_FromString( "__main__" );
+	PyDict_SetItemString( dict, "__builtins__", PyEval_GetBuiltins(  ) );
+	PyDict_SetItemString( dict, "__name__", item );
+	Py_DECREF(item);
 	
+	/* add bpy to global namespace */
 	mod = PyModule_New("bpy");
+	PyDict_SetItemString( dict, "bpy", mod );
+	Py_DECREF(mod);
 	
 	PyModule_AddObject( mod, "data", BPY_rna_module() );
 	/* PyModule_AddObject( mod, "doc", BPY_rna_doc() ); */
@@ -54,28 +62,12 @@
 	PyModule_AddObject( mod, "ops", BPY_operator_module() );
 	PyModule_AddObject( mod, "ui", BPY_ui_module() ); // XXX very experemental, consider this a test, especially PyCObject is not meant to be perminant
 	
-	/* add the module so we can import it */
-	PyDict_SetItemString(PySys_GetObject("modules"), "bpy", mod);
-	Py_DECREF(mod);
-}
-
-/*****************************************************************************
-* Description: This function creates a new Python dictionary object.
-*****************************************************************************/
-static PyObject *CreateGlobalDictionary( bContext *C )
-{
-	PyObject *mod;
-	PyObject *dict = PyDict_New(  );
-	PyObject *item = PyUnicode_FromString( "__main__" );
-	PyDict_SetItemString( dict, "__builtins__", PyEval_GetBuiltins(  ) );
-	PyDict_SetItemString( dict, "__name__", item );
-	Py_DECREF(item);
-	
 	// XXX - evil, need to access context
 	item = PyCObject_FromVoidPtr( C, NULL );
 	PyDict_SetItemString( dict, "__bpy_context__", item );
 	Py_DECREF(item);
 	
+	
 	// XXX - put somewhere more logical
 	{
 		PyMethodDef *ml;
@@ -91,18 +83,13 @@
 		}
 	}
 	
-	/* add bpy to global namespace */
-	mod= PyImport_ImportModuleLevel("bpy", NULL, NULL, NULL, 0);
-	PyDict_SetItemString( dict, "bpy", mod );
-	Py_DECREF(mod);
-	
 	return dict;
 }
 
 void BPY_start_python( void )
 {
 	PyThreadState *py_tstate = NULL;
-	
+
 	Py_Initialize(  );
 	
 	//PySys_SetArgv( argc_copy, argv_copy );
@@ -110,11 +97,8 @@
 	/* Initialize thread support (also acquires lock) */
 	PyEval_InitThreads();
 	
+	// todo - sys paths - our own imports
 	
-	/* bpy.* and lets us import it */
-	BPY_init_modules(); 
-
-	
 	py_tstate = PyGILState_GetThisThreadState();
 	PyEval_ReleaseThread(py_tstate);
 	
@@ -320,30 +304,16 @@
 }
 #endif
 
-// #define TIME_REGISTRATION
-
-#ifdef TIME_REGISTRATION
-#include "PIL_time.h"
-#endif
-
 /* XXX this is temporary, need a proper script registration system for 2.5 */
-void BPY_run_ui_scripts(void)
+void BPY_run_ui_scripts(bContext *C)
 {
-#ifdef TIME_REGISTRATION
-	double time = PIL_check_seconds_timer();
-#endif
 	DIR *dir; 
 	struct dirent *de;
+	struct stat status;
 	char *file_extension;
 	char path[FILE_MAX];
 	char *dirname= BLI_gethome_folder("ui");
-	int filelen; /* filename length */
-	
-	PyGILState_STATE gilstate;
-	PyObject *mod;
-	PyObject *sys_path_orig;
-	PyObject *sys_path_new;
-	
+
 	if(!dirname)
 		return;
 	
@@ -351,49 +321,23 @@
 
 	if(!dir)
 		return;
-	
-	gilstate = PyGILState_Ensure();
-	
-	/* backup sys.path */
-	sys_path_orig= PySys_GetObject("path");
-	Py_INCREF(sys_path_orig); /* dont free it */
-	
-	sys_path_new= PyList_New(1);
-	PyList_SET_ITEM(sys_path_new, 0, PyUnicode_FromString(dirname));
-	PySys_SetObject("path", sys_path_new);
-	Py_DECREF(sys_path_new);
-	
-	
-	while((de = readdir(dir)) != NULL) {
-		/* We could stat the file but easier just to let python
-		 * import it and complain if theres a problem */
-		
-		file_extension = strstr(de->d_name, ".py");
-		
-		if(file_extension && *(file_extension + 3) == '\0') {
-			filelen = strlen(de->d_name);
-			BLI_strncpy(path, de->d_name, filelen-2); /* cut off the .py on copy */
+
+	if (dir != NULL) {
+		while((de = readdir(dir)) != NULL) {
+			BLI_make_file_string("/", path, dirname, de->d_name);
 			
-			mod= PyImport_ImportModuleLevel(path, NULL, NULL, NULL, 0);
-			if (mod) {
-				Py_DECREF(mod);			
+			stat(path, &status);
+
+			/* run if it is a .py file */
+			if(S_ISREG(status.st_mode)) {
+				file_extension = strstr(de->d_name, ".py");
+
+				if(file_extension && *(file_extension + 3) == '\0')
+					BPY_run_python_script(C, path, NULL);
 			}
-			else {
-				PyErr_Print();
-				fprintf(stderr, "unable to import \"%s\"  %s/%s\n", path, dirname, de->d_name);
-			}
-			
 		}
-	}
 
-	closedir(dir);
-	
-	PySys_SetObject("path", sys_path_orig);
-	Py_DECREF(sys_path_orig);
-	
-	PyGILState_Release(gilstate);
-#ifdef TIME_REGISTRATION
-	printf("script time %f\n", (PIL_check_seconds_timer()-time));
-#endif
+		closedir(dir);
+	}
 }
 

Modified: branches/blender2.5/blender/source/creator/creator.c
===================================================================
--- branches/blender2.5/blender/source/creator/creator.c	2009-04-11 08:26:51 UTC (rev 19655)
+++ branches/blender2.5/blender/source/creator/creator.c	2009-04-11 10:01:49 UTC (rev 19656)
@@ -529,7 +529,7 @@
 	 */
 	BPY_post_start_python();
 
-	BPY_run_ui_scripts();
+	BPY_run_ui_scripts(C);
 #endif
 	
 #ifdef WITH_QUICKTIME





More information about the Bf-blender-cvs mailing list