get rid of annoying duplicate python initialization code, added setupGamePython() which initializes modules

This commit is contained in:
Campbell Barton 2010-01-10 22:03:26 +00:00
parent 34794eafe4
commit affe84a453
4 changed files with 46 additions and 44 deletions

@ -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

@ -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)

@ -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 */

@ -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 );