[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [19743] trunk/blender/source: BGE Python API

Campbell Barton ideasman42 at gmail.com
Wed Apr 15 21:20:12 CEST 2009


Revision: 19743
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=19743
Author:   campbellbarton
Date:     2009-04-15 21:20:12 +0200 (Wed, 15 Apr 2009)

Log Message:
-----------
BGE Python API
Free python modules defined within the blendfile between loading scenes since they would end up accessing old GameLogic, Rasterizer modules as well as old game engine data in the module namespace which can cause problems.

Modified Paths:
--------------
    trunk/blender/source/blender/python/api2_2x/bpy_internal_import.c
    trunk/blender/source/blender/python/api2_2x/bpy_internal_import.h
    trunk/blender/source/gameengine/Ketsji/KX_PythonInit.cpp

Modified: trunk/blender/source/blender/python/api2_2x/bpy_internal_import.c
===================================================================
--- trunk/blender/source/blender/python/api2_2x/bpy_internal_import.c	2009-04-15 18:20:19 UTC (rev 19742)
+++ trunk/blender/source/blender/python/api2_2x/bpy_internal_import.c	2009-04-15 19:20:12 UTC (rev 19743)
@@ -265,3 +265,65 @@
 PyMethodDef bpy_import[] = { {"bpy_import", blender_import, METH_KEYWORDS, "blenders import"} };
 PyMethodDef bpy_reload[] = { {"bpy_reload", blender_reload, METH_VARARGS, "blenders reload"} };
 
+
+/* Clear user modules.
+ * This is to clear any modules that could be defined from running scripts in blender.
+ * 
+ * Its also needed for the BGE Python api so imported scripts are not used between levels
+ * 
+ * This clears every modules that has a __file__ attribute (is not a builtin)
+ * and is a filename only (no path). since pythons bultins include a full path even for win32.
+ * even if we remove a python module a reimport will bring it back again.
+ */
+
+
+#if defined(WIN32) || defined(WIN64)
+#define SEPSTR "\\"
+#else
+#define SEPSTR "/"
+#endif
+
+
+void importClearUserModules(void)
+{
+	PyObject *modules= PySys_GetObject("modules");	
+	
+	char *fname;
+	char *file_extension;
+	
+	/* looping over the dict */
+	PyObject *key, *value;
+	Py_ssize_t pos = 0;
+	
+	/* new list */
+	PyObject *list= PyList_New(0);
+	
+	/* go over sys.modules and remove anything with a 
+	 * sys.modukes[x].__file__ thats ends with a .py and has no path
+	 */
+	while (PyDict_Next(modules, &pos, &key, &value)) {
+		fname= PyModule_GetFilename(value);
+		if(fname) {
+			if ((strstr(fname, SEPSTR))==0) { /* no path ? */
+				file_extension = strstr(fname, ".py");
+				if(file_extension && *(file_extension + 3) == '\0') { /* .py extension ? */
+					/* now we can be fairly sure its a python import from the blendfile */
+					PyList_Append(list, key); /* free'd with the list */
+				}
+			}
+		}
+		else {
+			PyErr_Clear();
+		}
+	}
+	
+	/* remove all our modules */
+	for(pos=0; pos < PyList_Size(list); pos++) {
+		/* PyObject_Print(key, stderr, 0); */
+		key= PyList_GET_ITEM(list, pos);
+		PyDict_DelItem(modules, key);
+	}
+	
+	Py_DECREF(list); /* removes all references from append */
+}
+

Modified: trunk/blender/source/blender/python/api2_2x/bpy_internal_import.h
===================================================================
--- trunk/blender/source/blender/python/api2_2x/bpy_internal_import.h	2009-04-15 18:20:19 UTC (rev 19742)
+++ trunk/blender/source/blender/python/api2_2x/bpy_internal_import.h	2009-04-15 19:20:12 UTC (rev 19743)
@@ -37,6 +37,7 @@
 
 PyObject *importText( char *name, int *found );
 PyObject *reimportText( PyObject *module, int *found );
+void importClearUserModules( void ); /* Clear user modules */
 extern PyMethodDef bpy_import[];
 extern PyMethodDef bpy_reload[];
 

Modified: trunk/blender/source/gameengine/Ketsji/KX_PythonInit.cpp
===================================================================
--- trunk/blender/source/gameengine/Ketsji/KX_PythonInit.cpp	2009-04-15 18:20:19 UTC (rev 19742)
+++ trunk/blender/source/gameengine/Ketsji/KX_PythonInit.cpp	2009-04-15 19:20:12 UTC (rev 19743)
@@ -102,8 +102,8 @@
 #include "GPU_material.h"
 
 static void setSandbox(TPythonSecurityLevel level);
+static void clearGameModules();
 
-
 // 'local' copy of canvas ptr, for window height/width python scripts
 static RAS_ICanvas* gp_Canvas = NULL;
 static KX_Scene*	gp_KetsjiScene = NULL;
@@ -1402,6 +1402,10 @@
 	initPyTypes();
 	
 	bpy_import_main_set(maggie);
+	
+	/* run this to clear game modules and user modules which
+	 * may contain references to in game data */
+	clearGameModules();
 
 	PyObject* moduleobj = PyImport_AddModule("__main__");
 	return PyModule_GetDict(moduleobj);
@@ -1434,6 +1438,9 @@
 	clearModule(modules, "Mathutils");	
 	clearModule(modules, "BGL");	
 	PyErr_Clear(); // incase some of these were alredy removed.
+	
+	/* clear user defined modules */
+	importClearUserModules();
 }
 
 void exitGamePythonScripting()





More information about the Bf-blender-cvs mailing list