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 | |
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')
-rw-r--r-- | source/gameengine/BlenderRoutines/BL_KetsjiEmbedStart.cpp | 4 | ||||
-rw-r--r-- | source/gameengine/GamePlayer/ghost/GPG_Application.cpp | 2 | ||||
-rw-r--r-- | source/gameengine/Ketsji/CMakeLists.txt | 1 | ||||
-rw-r--r-- | source/gameengine/Ketsji/KX_PythonInit.cpp | 49 | ||||
-rw-r--r-- | source/gameengine/Ketsji/KX_PythonInit.h | 4 | ||||
-rw-r--r-- | source/gameengine/Ketsji/SConscript | 6 |
6 files changed, 52 insertions, 14 deletions
diff --git a/source/gameengine/BlenderRoutines/BL_KetsjiEmbedStart.cpp b/source/gameengine/BlenderRoutines/BL_KetsjiEmbedStart.cpp index d89d2d80ab4..1d4cd011fe4 100644 --- a/source/gameengine/BlenderRoutines/BL_KetsjiEmbedStart.cpp +++ b/source/gameengine/BlenderRoutines/BL_KetsjiEmbedStart.cpp @@ -357,7 +357,7 @@ extern "C" void StartKetsjiShell(struct ScrArea *area, blscene); // some python things - PyObject* dictionaryobject = initGamePythonScripting("Ketsji", psl_Lowest); + PyObject* dictionaryobject = initGamePythonScripting("Ketsji", psl_Lowest, blenderdata); ketsjiengine->SetPythonDictionary(dictionaryobject); initRasterizer(rasterizer, canvas); PyObject *gameLogic = initGameLogic(ketsjiengine, startscene); @@ -656,7 +656,7 @@ extern "C" void StartKetsjiShellSimulation(struct ScrArea *area, blscene); // some python things - PyObject* dictionaryobject = initGamePythonScripting("Ketsji", psl_Lowest); + PyObject* dictionaryobject = initGamePythonScripting("Ketsji", psl_Lowest, blenderdata); ketsjiengine->SetPythonDictionary(dictionaryobject); initRasterizer(rasterizer, canvas); PyObject *gameLogic = initGameLogic(ketsjiengine, startscene); diff --git a/source/gameengine/GamePlayer/ghost/GPG_Application.cpp b/source/gameengine/GamePlayer/ghost/GPG_Application.cpp index cca344a03bb..6d846610109 100644 --- a/source/gameengine/GamePlayer/ghost/GPG_Application.cpp +++ b/source/gameengine/GamePlayer/ghost/GPG_Application.cpp @@ -681,7 +681,7 @@ bool GPG_Application::startEngine(void) // some python things - PyObject* dictionaryobject = initGamePlayerPythonScripting("Ketsji", psl_Lowest); + PyObject* dictionaryobject = initGamePlayerPythonScripting("Ketsji", psl_Lowest, m_maggie); m_ketsjiengine->SetPythonDictionary(dictionaryobject); initRasterizer(m_rasterizer, m_canvas); PyObject *gameLogic = initGameLogic(m_ketsjiengine, startscene); diff --git a/source/gameengine/Ketsji/CMakeLists.txt b/source/gameengine/Ketsji/CMakeLists.txt index 58411f6d25e..3bd05ca5137 100644 --- a/source/gameengine/Ketsji/CMakeLists.txt +++ b/source/gameengine/Ketsji/CMakeLists.txt @@ -35,6 +35,7 @@ SET(SRC ../../../source/blender/python/api2_2x/point.c ../../../source/blender/python/api2_2x/quat.c ../../../source/blender/python/api2_2x/vector.c + ../../../source/blender/python/api2_2x/bpy_internal_import.c ) SET(INC 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); } diff --git a/source/gameengine/Ketsji/KX_PythonInit.h b/source/gameengine/Ketsji/KX_PythonInit.h index 57ee0be9400..b709cee2f37 100644 --- a/source/gameengine/Ketsji/KX_PythonInit.h +++ b/source/gameengine/Ketsji/KX_PythonInit.h @@ -43,11 +43,11 @@ extern bool gUseVisibilityTemp; PyObject* initGameLogic(class KX_KetsjiEngine *engine, class KX_Scene* ketsjiscene); PyObject* initGameKeys(); PyObject* initRasterizer(class RAS_IRasterizer* rasty,class RAS_ICanvas* canvas); -PyObject* initGamePlayerPythonScripting(const STR_String& progname, TPythonSecurityLevel level); +PyObject* initGamePlayerPythonScripting(const STR_String& progname, TPythonSecurityLevel level, struct Main *maggie); PyObject* initMathutils(); PyObject* initVideoTexture(void); void exitGamePlayerPythonScripting(); -PyObject* initGamePythonScripting(const STR_String& progname, TPythonSecurityLevel level); +PyObject* initGamePythonScripting(const STR_String& progname, TPythonSecurityLevel level, struct Main *maggie); void exitGamePythonScripting(); void setGamePythonPath(char *path); diff --git a/source/gameengine/Ketsji/SConscript b/source/gameengine/Ketsji/SConscript index 5989d9d8b52..68e5c62ab6c 100644 --- a/source/gameengine/Ketsji/SConscript +++ b/source/gameengine/Ketsji/SConscript @@ -18,7 +18,11 @@ sources.extend([\ '#source/blender/python/api2_2x/vector.c',\ ]) -incs = '. #source/blender/python/api2_2x' # Only for Mathutils! - no other deps +sources.extend([\ + '#source/blender/python/api2_2x/bpy_internal_import.c' +]) + +incs = '. #source/blender/python/api2_2x' # Only for Mathutils! and bpy_internal_import.h, be very careful incs += ' #source/kernel/gen_system #intern/string #intern/guardedalloc' incs += ' #source/gameengine/Rasterizer/RAS_OpenGLRasterizer #intern/bmfont' |