diff options
author | Campbell Barton <ideasman42@gmail.com> | 2009-04-05 23:37:13 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2009-04-05 23:37:13 +0400 |
commit | b4e4ccf92ddc8a7d4ed3da13c589b2f7c44baa0d (patch) | |
tree | 0503fa6df1a4de4802badff3d0c6b1ef429ea98e /source/gameengine/Ketsji/KX_PythonInit.cpp | |
parent | 3dacfbb23161a1b779c2c4ac0f06431c5adda6a2 (diff) |
BGE PyAPI can now import text (within the blend-file)
Previously this only worked with the Blender API.
- bpy_internal_import small C file that Blender scripting and the game engine use.
- Tested with blender, blenderplayer, loading files
- Needed to use a hack to override the Main struct since the game engine doesn't set G.main
- when the sandbox is set, only internal scripts can be imported.
Diffstat (limited to 'source/gameengine/Ketsji/KX_PythonInit.cpp')
-rw-r--r-- | source/gameengine/Ketsji/KX_PythonInit.cpp | 49 |
1 files changed, 41 insertions, 8 deletions
diff --git a/source/gameengine/Ketsji/KX_PythonInit.cpp b/source/gameengine/Ketsji/KX_PythonInit.cpp index f065eb29c44..ef7655f38a2 100644 --- a/source/gameengine/Ketsji/KX_PythonInit.cpp +++ b/source/gameengine/Ketsji/KX_PythonInit.cpp @@ -77,6 +77,7 @@ extern "C" { #include "Mathutils.h" // Blender.Mathutils module copied here so the blenderlayer can use. + #include "bpy_internal_import.h" /* from the blender python api, but we want to import text too! */ } #include "marshal.h" /* python header for loading/saving dicts */ @@ -1107,11 +1108,6 @@ PyObject *KXpy_open(PyObject *self, PyObject *args) { return NULL; } -PyObject *KXpy_reload(PyObject *self, PyObject *args) { - PyErr_SetString(PyExc_RuntimeError, "Sandbox: reload() function disabled!\nGame Scripts should not use this function."); - return NULL; -} - PyObject *KXpy_file(PyObject *self, PyObject *args) { PyErr_SetString(PyExc_RuntimeError, "Sandbox: file() function disabled!\nGame Scripts should not use this function."); return NULL; @@ -1162,6 +1158,11 @@ PyObject *KXpy_import(PyObject *self, PyObject *args) !strcmp(name, "Rasterizer") || !strcmp(name, "Mathutils")) { return PyImport_ImportModuleEx(name, globals, locals, fromlist); } + + /* Import blender texts as python modules */ + m= importText(name); + if (m) + return m; PyErr_Format(PyExc_ImportError, "Import of external Module %.20s not allowed.", name); @@ -1169,6 +1170,29 @@ PyObject *KXpy_import(PyObject *self, PyObject *args) } +PyObject *KXpy_reload(PyObject *self, PyObject *args) { + + /* Used to be sandboxed, bettet to allow importing of internal text only */ +#if 0 + PyErr_SetString(PyExc_RuntimeError, "Sandbox: reload() function disabled!\nGame Scripts should not use this function."); + return NULL; +#endif + + PyObject *module = NULL; + PyObject *newmodule = NULL; + + /* check for a module arg */ + if( !PyArg_ParseTuple( args, "O:bpy_reload", &module ) ) + return NULL; + + newmodule= reimportText( module ); + + if (newmodule==NULL) + PyErr_SetString(PyExc_ImportError, "failed to reload from blenders internal text"); + + return newmodule; +} + /* override python file type functions */ #if 0 static int @@ -1241,6 +1265,9 @@ void setSandbox(TPythonSecurityLevel level) } */ default: + /* Allow importing internal text, from bpy_internal_import.py */ + PyDict_SetItemString(d, "reload", PyCFunction_New(bpy_reload, NULL)); + PyDict_SetItemString(d, "__import__", PyCFunction_New(bpy_import, NULL)); break; } } @@ -1248,7 +1275,7 @@ void setSandbox(TPythonSecurityLevel level) /** * Python is not initialised. */ -PyObject* initGamePlayerPythonScripting(const STR_String& progname, TPythonSecurityLevel level) +PyObject* initGamePlayerPythonScripting(const STR_String& progname, TPythonSecurityLevel level, Main *maggie) { STR_String pname = progname; Py_SetProgramName(pname.Ptr()); @@ -1260,7 +1287,9 @@ PyObject* initGamePlayerPythonScripting(const STR_String& progname, TPythonSecur setSandbox(level); initPyTypes(); - + + bpy_import_main_set(maggie); + PyObject* moduleobj = PyImport_AddModule("__main__"); return PyModule_GetDict(moduleobj); } @@ -1268,12 +1297,13 @@ PyObject* initGamePlayerPythonScripting(const STR_String& progname, TPythonSecur void exitGamePlayerPythonScripting() { Py_Finalize(); + bpy_import_main_set(NULL); } /** * Python is already initialized. */ -PyObject* initGamePythonScripting(const STR_String& progname, TPythonSecurityLevel level) +PyObject* initGamePythonScripting(const STR_String& progname, TPythonSecurityLevel level, Main *maggie) { STR_String pname = progname; Py_SetProgramName(pname.Ptr()); @@ -1282,6 +1312,8 @@ PyObject* initGamePythonScripting(const STR_String& progname, TPythonSecurityLev setSandbox(level); initPyTypes(); + + bpy_import_main_set(maggie); PyObject* moduleobj = PyImport_AddModule("__main__"); return PyModule_GetDict(moduleobj); @@ -1291,6 +1323,7 @@ PyObject* initGamePythonScripting(const STR_String& progname, TPythonSecurityLev void exitGamePythonScripting() { + bpy_import_main_set(NULL); } |