diff --git a/source/gameengine/BlenderRoutines/BL_KetsjiEmbedStart.cpp b/source/gameengine/BlenderRoutines/BL_KetsjiEmbedStart.cpp index 83a6bbb2fe1..254d0c7262d 100644 --- a/source/gameengine/BlenderRoutines/BL_KetsjiEmbedStart.cpp +++ b/source/gameengine/BlenderRoutines/BL_KetsjiEmbedStart.cpp @@ -391,22 +391,8 @@ extern "C" void StartKetsjiShell(struct bContext *C, struct ARegion *ar, rcti *c #ifndef DISABLE_PYTHON // some python things - PyObject* dictionaryobject = initGamePythonScripting("Ketsji", psl_Lowest, blenderdata); - ketsjiengine->SetPyNamespace(dictionaryobject); - initRasterizer(rasterizer, canvas); - PyObject *gameLogic = initGameLogic(ketsjiengine, startscene); - PyDict_SetItemString(PyModule_GetDict(gameLogic), "globalDict", pyGlobalDict); // Same as importing the module. - PyObject *gameLogic_keys = PyDict_Keys(PyModule_GetDict(gameLogic)); - PyDict_SetItemString(dictionaryobject, "GameLogic", gameLogic); // Same as importing the module. - - initGameKeys(); - initPythonConstraintBinding(); - initMathutils(); - initGeometry(); - initBGL(); -#ifdef WITH_FFMPEG - initVideoTexture(); -#endif + PyObject *gameLogic, *gameLogic_keys; + setupGamePython(ketsjiengine, startscene, blenderdata, pyGlobalDict, &gameLogic, &gameLogic_keys, 0, NULL); #endif // DISABLE_PYTHON //initialize Dome Settings @@ -622,6 +608,7 @@ extern "C" void StartKetsjiShellSimulation(struct wmWindow *win, // Acquire Python's GIL (global interpreter lock) // so we can safely run Python code and API calls PyGILState_STATE gilstate = PyGILState_Ensure(); + PyObject *pyGlobalDict = PyDict_New(); /* python utility storage, spans blend file loading */ #endif bgl::InitExtensions(true); @@ -722,19 +709,8 @@ extern "C" void StartKetsjiShellSimulation(struct wmWindow *win, #ifndef DISABLE_PYTHON // some python things - PyObject* dictionaryobject = initGamePythonScripting("Ketsji", psl_Lowest, blenderdata); - ketsjiengine->SetPyNamespace(dictionaryobject); - initRasterizer(rasterizer, canvas); - PyObject *gameLogic = initGameLogic(ketsjiengine, startscene); - PyDict_SetItemString(dictionaryobject, "GameLogic", gameLogic); // Same as importing the module - initGameKeys(); - initPythonConstraintBinding(); - initMathutils(); - initGeometry(); - initBGL(); -#ifdef WITH_FFMPEG - initVideoTexture(); -#endif + PyObject *gameLogic, *gameLogic_keys; + setupGamePython(ketsjiengine, startscene, blenderdata, pyGlobalDict, &gameLogic, &gameLogic_keys, 0, NULL); #endif // DISABLE_PYTHON if (sceneconverter) @@ -820,6 +796,8 @@ extern "C" void StartKetsjiShellSimulation(struct wmWindow *win, } while (exitrequested == KX_EXIT_REQUEST_RESTART_GAME || exitrequested == KX_EXIT_REQUEST_START_OTHER_GAME); #ifndef DISABLE_PYTHON + Py_DECREF(pyGlobalDict); + // Release Python's GIL PyGILState_Release(gilstate); #endif diff --git a/source/gameengine/GamePlayer/ghost/GPG_Application.cpp b/source/gameengine/GamePlayer/ghost/GPG_Application.cpp index c6952a68a58..323e88187bd 100644 --- a/source/gameengine/GamePlayer/ghost/GPG_Application.cpp +++ b/source/gameengine/GamePlayer/ghost/GPG_Application.cpp @@ -679,21 +679,11 @@ bool GPG_Application::startEngine(void) startscenename, m_startScene); - - // some python things - PyObject* dictionaryobject = initGamePlayerPythonScripting("Ketsji", psl_Lowest, m_maggie, m_argc, m_argv); - m_ketsjiengine->SetPyNamespace(dictionaryobject); - initRasterizer(m_rasterizer, m_canvas); - PyObject *gameLogic = initGameLogic(m_ketsjiengine, startscene); - PyDict_SetItemString(dictionaryobject, "GameLogic", gameLogic); // Same as importing the module - initGameKeys(); - initPythonConstraintBinding(); - initMathutils(); - initGeometry(); - initBGL(); -#ifdef WITH_FFMPEG - initVideoTexture(); -#endif +#ifndef DISABLE_PYTHON + // some python things + PyObject *gameLogic, *gameLogic_keys; + setupGamePython(m_ketsjiengine, startscene, m_maggie, NULL, &gameLogic, &gameLogic_keys, m_argc, m_argv); +#endif // DISABLE_PYTHON //initialize Dome Settings if(m_startScene->gm.stereoflag == STEREO_DOME) diff --git a/source/gameengine/Ketsji/KX_PythonInit.cpp b/source/gameengine/Ketsji/KX_PythonInit.cpp index ed422b81ec6..cd0bcefff3b 100644 --- a/source/gameengine/Ketsji/KX_PythonInit.cpp +++ b/source/gameengine/Ketsji/KX_PythonInit.cpp @@ -1926,6 +1926,38 @@ void exitGamePythonScripting() PyObjectPlus::ClearDeprecationWarning(); } +/* similar to the above functions except it sets up the namespace + * and other more general things */ +void setupGamePython(KX_KetsjiEngine* ketsjiengine, KX_Scene* startscene, Main *blenderdata, PyObject * pyGlobalDict, PyObject **gameLogic, PyObject **gameLogic_keys, int argc, char** argv) +{ + PyObject* dictionaryobject; + + if(argv) /* player only */ + dictionaryobject= initGamePlayerPythonScripting("Ketsji", psl_Lowest, blenderdata, argc, argv); + else + dictionaryobject= initGamePythonScripting("Ketsji", psl_Lowest, blenderdata); + + ketsjiengine->SetPyNamespace(dictionaryobject); + initRasterizer(ketsjiengine->GetRasterizer(), ketsjiengine->GetCanvas()); + *gameLogic = initGameLogic(ketsjiengine, startscene); + + /* is set in initGameLogic so only set here if we want it to persist between scenes */ + if(pyGlobalDict) + PyDict_SetItemString(PyModule_GetDict(*gameLogic), "globalDict", pyGlobalDict); // Same as importing the module. + + *gameLogic_keys = PyDict_Keys(PyModule_GetDict(*gameLogic)); + PyDict_SetItemString(dictionaryobject, "GameLogic", *gameLogic); // Same as importing the module. + + initGameKeys(); + initPythonConstraintBinding(); + initMathutils(); + initGeometry(); + initBGL(); + +#ifdef WITH_FFMPEG + initVideoTexture(); +#endif +} static struct PyModuleDef Rasterizer_module_def = { {}, /* m_base */ diff --git a/source/gameengine/Ketsji/KX_PythonInit.h b/source/gameengine/Ketsji/KX_PythonInit.h index ad3cd15ab4a..442c32a0a82 100644 --- a/source/gameengine/Ketsji/KX_PythonInit.h +++ b/source/gameengine/Ketsji/KX_PythonInit.h @@ -52,6 +52,8 @@ void exitGamePlayerPythonScripting(); PyObject* initGamePythonScripting(const STR_String& progname, TPythonSecurityLevel level, struct Main *maggie); void exitGamePythonScripting(); +void setupGamePython(KX_KetsjiEngine* ketsjiengine, KX_Scene* startscene, Main *blenderdata, PyObject *pyGlobalDict, PyObject **gameLogic, PyObject **gameLogic_keys, int argc, char** argv); + void setGamePythonPath(char *path); void resetGamePythonPath(); void pathGamePythonConfig( char *path );