diff options
Diffstat (limited to 'source/gameengine/Ketsji/KX_PythonInit.cpp')
-rw-r--r-- | source/gameengine/Ketsji/KX_PythonInit.cpp | 676 |
1 files changed, 378 insertions, 298 deletions
diff --git a/source/gameengine/Ketsji/KX_PythonInit.cpp b/source/gameengine/Ketsji/KX_PythonInit.cpp index da8a0c0844e..2fef74ca33d 100644 --- a/source/gameengine/Ketsji/KX_PythonInit.cpp +++ b/source/gameengine/Ketsji/KX_PythonInit.cpp @@ -30,7 +30,7 @@ * \ingroup ketsji */ -#include "GL/glew.h" +#include "glew-mx.h" #ifdef _MSC_VER # pragma warning (disable:4786) @@ -51,6 +51,8 @@ # include <Python.h> extern "C" { + # include "BLI_utildefines.h" + # include "python_utildefines.h" # include "bpy_internal_import.h" /* from the blender python api, but we want to import text too! */ # include "py_capi_utils.h" # include "mathutils.h" // 'mathutils' module copied here so the blenderlayer can use. @@ -59,7 +61,7 @@ extern "C" { # include "marshal.h" /* python header for loading/saving dicts */ } -#include "AUD_PyInit.h" +#include "../../../../intern/audaspace/intern/AUD_PyInit.h" #endif /* WITH_PYTHON */ @@ -110,8 +112,8 @@ extern "C" { #include "RAS_2DFilterManager.h" #include "MT_Vector3.h" #include "MT_Point3.h" -#include "ListValue.h" -#include "InputParser.h" +#include "EXP_ListValue.h" +#include "EXP_InputParser.h" #include "KX_Scene.h" #include "NG_NetworkScene.h" //Needed for sendMessage() @@ -121,7 +123,7 @@ extern "C" { #include "KX_PyMath.h" -#include "PyObjectPlus.h" +#include "EXP_PyObjectPlus.h" #include "KX_PythonInitTypes.h" @@ -130,12 +132,17 @@ extern "C" { #include "DNA_scene_types.h" #include "PHY_IPhysicsEnvironment.h" + +extern "C" { #include "BKE_main.h" #include "BKE_global.h" #include "BKE_library.h" +#include "BKE_appdir.h" +#include "BKE_blender.h" #include "BLI_blenlib.h" #include "GPU_material.h" #include "MEM_guardedalloc.h" +} /* for converting new scenes */ #include "KX_BlenderSceneConverter.h" @@ -179,17 +186,17 @@ class KX_KetsjiEngine* KX_GetActiveEngine() } /* why is this in python? */ -void KX_RasterizerDrawDebugLine(const MT_Vector3& from,const MT_Vector3& to,const MT_Vector3& color) +void KX_RasterizerDrawDebugLine(const MT_Vector3& from,const MT_Vector3& to,const MT_Vector3& color) { if (gp_Rasterizer) - gp_Rasterizer->DrawDebugLine(from,to,color); + gp_Rasterizer->DrawDebugLine(gp_KetsjiScene, from, to, color); } void KX_RasterizerDrawDebugCircle(const MT_Vector3& center, const MT_Scalar radius, const MT_Vector3& color, const MT_Vector3& normal, int nsector) { if (gp_Rasterizer) - gp_Rasterizer->DrawDebugCircle(center, radius, color, normal, nsector); + gp_Rasterizer->DrawDebugCircle(gp_KetsjiScene, center, radius, color, normal, nsector); } #ifdef WITH_PYTHON @@ -214,13 +221,16 @@ static void KX_MACRO_addTypesToDict_fn(PyObject *dict, const char *name, long va // temporarily python stuff, will be put in another place later ! -#include "KX_Python.h" +#include "EXP_Python.h" #include "SCA_PythonController.h" // List of methods defined in the module static PyObject *ErrorObject; -static const char *gPyGetRandomFloat_doc="getRandomFloat returns a random floating point value in the range [0..1]"; +PyDoc_STRVAR(gPyGetRandomFloat_doc, +"getRandomFloat()\n" +"returns a random floating point value in the range [0..1]" +); static PyObject *gPyGetRandomFloat(PyObject *) { return PyFloat_FromDouble(MT_random()); @@ -238,15 +248,15 @@ static PyObject *gPySetGravity(PyObject *, PyObject *value) Py_RETURN_NONE; } -static char gPyExpandPath_doc[] = -"(path) - Converts a blender internal path into a proper file system path.\n\ -path - the string path to convert.\n\n\ -Use / as directory separator in path\n\ -You can use '//' at the start of the string to define a relative path;\n\ -Blender replaces that string by the directory of the current .blend or runtime\n\ -file to make a full path name.\n\ -The function also converts the directory separator to the local file system format."; - +PyDoc_STRVAR(gPyExpandPath_doc, +"expandPath(path)\n" +"Converts a blender internal path into a proper file system path.\n" +" path - the string path to convert.\n" +"Use / as directory separator in path\n" +"You can use '//' at the start of the string to define a relative path." +"Blender replaces that string by the directory of the current .blend or runtime file to make a full path name.\n" +"The function also converts the directory separator to the local file system format." +); static PyObject *gPyExpandPath(PyObject *, PyObject *args) { char expanded[FILE_MAX]; @@ -260,10 +270,10 @@ static PyObject *gPyExpandPath(PyObject *, PyObject *args) return PyC_UnicodeFromByte(expanded); } -static char gPyStartGame_doc[] = -"startGame(blend)\n\ -Loads the blend file"; - +PyDoc_STRVAR(gPyStartGame_doc, +"startGame(blend)\n" +"Loads the blend file" +); static PyObject *gPyStartGame(PyObject *, PyObject *args) { char* blendfile; @@ -277,10 +287,10 @@ static PyObject *gPyStartGame(PyObject *, PyObject *args) Py_RETURN_NONE; } -static char gPyEndGame_doc[] = -"endGame()\n\ -Ends the current game"; - +PyDoc_STRVAR(gPyEndGame_doc, +"endGame()\n" +"Ends the current game" +); static PyObject *gPyEndGame(PyObject *) { gp_KetsjiEngine->RequestExit(KX_EXIT_REQUEST_QUIT_GAME); @@ -290,10 +300,10 @@ static PyObject *gPyEndGame(PyObject *) Py_RETURN_NONE; } -static char gPyRestartGame_doc[] = -"restartGame()\n\ -Restarts the current game by reloading the .blend file"; - +PyDoc_STRVAR(gPyRestartGame_doc, +"restartGame()\n" +"Restarts the current game by reloading the .blend file" +); static PyObject *gPyRestartGame(PyObject *) { gp_KetsjiEngine->RequestExit(KX_EXIT_REQUEST_RESTART_GAME); @@ -302,10 +312,10 @@ static PyObject *gPyRestartGame(PyObject *) Py_RETURN_NONE; } -static char gPySaveGlobalDict_doc[] = - "saveGlobalDict()\n" - "Saves bge.logic.globalDict to a file"; - +PyDoc_STRVAR(gPySaveGlobalDict_doc, +"saveGlobalDict()\n" +"Saves bge.logic.globalDict to a file" +); static PyObject *gPySaveGlobalDict(PyObject *) { char marshal_path[512]; @@ -339,15 +349,15 @@ static PyObject *gPySaveGlobalDict(PyObject *) Py_RETURN_NONE; } -static char gPyLoadGlobalDict_doc[] = - "LoadGlobalDict()\n" - "Loads bge.logic.globalDict from a file"; - +PyDoc_STRVAR(gPyLoadGlobalDict_doc, +"LoadGlobalDict()\n" +"Loads bge.logic.globalDict from a file" +); static PyObject *gPyLoadGlobalDict(PyObject *) { char marshal_path[512]; char *marshal_buffer = NULL; - size_t marshal_length; + int marshal_length; FILE *fp = NULL; int result; @@ -358,7 +368,12 @@ static PyObject *gPyLoadGlobalDict(PyObject *) if (fp) { // obtain file size: fseek (fp, 0, SEEK_END); - marshal_length = (size_t)ftell(fp); + marshal_length = ftell(fp); + if (marshal_length == -1) { + printf("Warning: could not read position of '%s'\n", marshal_path); + fclose(fp); + Py_RETURN_NONE; + } rewind(fp); marshal_buffer = (char*)malloc (sizeof(char)*marshal_length); @@ -380,23 +395,23 @@ static PyObject *gPyLoadGlobalDict(PyObject *) Py_RETURN_NONE; } -static char gPyGetProfileInfo_doc[] = +PyDoc_STRVAR(gPyGetProfileInfo_doc, "getProfileInfo()\n" -"returns a dictionary with profiling information"; - +"returns a dictionary with profiling information" +); static PyObject *gPyGetProfileInfo(PyObject *) { return gp_KetsjiEngine->GetPyProfileDict(); } -static char gPySendMessage_doc[] = -"sendMessage(subject, [body, to, from])\n\ -sends a message in same manner as a message actuator\ -subject = Subject of the message\ -body = Message body\ -to = Name of object to send the message to\ -from = Name of object to send the string from"; - +PyDoc_STRVAR(gPySendMessage_doc, +"sendMessage(subject, [body, to, from])\n" +"sends a message in same manner as a message actuator" +" subject = Subject of the message" +" body = Message body" +" to = Name of object to send the message to" +" from = Name of object to send the string from" +); static PyObject *gPySendMessage(PyObject *, PyObject *args) { char* subject; @@ -511,6 +526,27 @@ static PyObject *gPyGetPhysicsTicRate(PyObject *) return PyFloat_FromDouble(PHY_GetActiveEnvironment()->GetFixedTimeStep()); } +static PyObject *gPySetAnimRecordFrame(PyObject *, PyObject *args) +{ + int anim_record_frame; + + if (!PyArg_ParseTuple(args, "i:setAnimRecordFrame", &anim_record_frame)) + return NULL; + + if (anim_record_frame < 0 && (U.flag & USER_NONEGFRAMES)) { + PyErr_Format(PyExc_ValueError, "Frame number must be non-negative (was %i).", anim_record_frame); + return NULL; + } + + gp_KetsjiEngine->setAnimRecordFrame(anim_record_frame); + Py_RETURN_NONE; +} + +static PyObject *gPyGetAnimRecordFrame(PyObject *) +{ + return PyLong_FromLong(gp_KetsjiEngine->getAnimRecordFrame()); +} + static PyObject *gPyGetAverageFrameRate(PyObject *) { return PyFloat_FromDouble(KX_KetsjiEngine::GetAverageFrameRate()); @@ -556,11 +592,12 @@ static PyObject *gPyGetBlendFileList(PyObject *, PyObject *args) return list; } -static char gPyAddScene_doc[] = -"addScene(name, [overlay])\n\ -adds a scene to the game engine\n\ -name = Name of the scene\n\ -overlay = Overlay or underlay"; +PyDoc_STRVAR(gPyAddScene_doc, +"addScene(name, [overlay])\n" +"Adds a scene to the game engine.\n" +" name = Name of the scene\n" +" overlay = Overlay or underlay" +); static PyObject *gPyAddScene(PyObject *, PyObject *args) { char* name; @@ -574,17 +611,19 @@ static PyObject *gPyAddScene(PyObject *, PyObject *args) Py_RETURN_NONE; } -static const char *gPyGetCurrentScene_doc = +PyDoc_STRVAR(gPyGetCurrentScene_doc, "getCurrentScene()\n" -"Gets a reference to the current scene.\n"; +"Gets a reference to the current scene." +); static PyObject *gPyGetCurrentScene(PyObject *self) { return gp_KetsjiScene->GetProxy(); } -static const char *gPyGetSceneList_doc = +PyDoc_STRVAR(gPyGetSceneList_doc, "getSceneList()\n" -"Return a list of converted scenes.\n"; +"Return a list of converted scenes." +); static PyObject *gPyGetSceneList(PyObject *self) { KX_KetsjiEngine* m_engine = KX_GetActiveEngine(); @@ -869,6 +908,8 @@ static struct PyMethodDef game_methods[] = { {"setLogicTicRate", (PyCFunction) gPySetLogicTicRate, METH_VARARGS, (const char *)"Sets the logic tic rate"}, {"getPhysicsTicRate", (PyCFunction) gPyGetPhysicsTicRate, METH_NOARGS, (const char *)"Gets the physics tic rate"}, {"setPhysicsTicRate", (PyCFunction) gPySetPhysicsTicRate, METH_VARARGS, (const char *)"Sets the physics tic rate"}, + {"getAnimRecordFrame", (PyCFunction) gPyGetAnimRecordFrame, METH_NOARGS, (const char *)"Gets the current frame number used for animation recording"}, + {"setAnimRecordFrame", (PyCFunction) gPySetAnimRecordFrame, METH_VARARGS, (const char *)"Sets the current frame number used for animation recording"}, {"getExitKey", (PyCFunction) gPyGetExitKey, METH_NOARGS, (const char *)"Gets the key used to exit the game engine"}, {"setExitKey", (PyCFunction) gPySetExitKey, METH_VARARGS, (const char *)"Sets the key used to exit the game engine"}, {"getAverageFrameRate", (PyCFunction) gPyGetAverageFrameRate, METH_NOARGS, (const char *)"Gets the estimated average frame rate"}, @@ -1002,111 +1043,39 @@ static PyObject *gPyGetFocalLength(PyObject *, PyObject *, PyObject *) Py_RETURN_NONE; } -static PyObject *gPySetBackgroundColor(PyObject *, PyObject *value) +static PyObject *gPyGetStereoEye(PyObject *, PyObject *, PyObject *) { - - MT_Vector4 vec; - if (!PyVecTo(value, vec)) - return NULL; - - if (gp_Canvas) - { - gp_Rasterizer->SetBackColor((float)vec[0], (float)vec[1], (float)vec[2], (float)vec[3]); - } - - KX_WorldInfo *wi = gp_KetsjiScene->GetWorldInfo(); - if (wi->hasWorld()) - wi->setBackColor((float)vec[0], (float)vec[1], (float)vec[2]); + int flag = RAS_IRasterizer::RAS_STEREO_LEFTEYE; - Py_RETURN_NONE; -} - - - -static PyObject *gPySetMistColor(PyObject *, PyObject *value) -{ - - MT_Vector3 vec; - if (!PyVecTo(value, vec)) - return NULL; - if (!gp_Rasterizer) { - PyErr_SetString(PyExc_RuntimeError, "Rasterizer.setMistColor(color), Rasterizer not available"); + PyErr_SetString(PyExc_RuntimeError, "Rasterizer.getStereoEye(), Rasterizer not available"); return NULL; } - gp_Rasterizer->SetFogColor((float)vec[0], (float)vec[1], (float)vec[2]); - - Py_RETURN_NONE; -} -static PyObject *gPyDisableMist(PyObject *) -{ - - if (!gp_Rasterizer) { - PyErr_SetString(PyExc_RuntimeError, "Rasterizer.setMistColor(color), Rasterizer not available"); - return NULL; - } - gp_Rasterizer->DisableFog(); - - Py_RETURN_NONE; -} + if (gp_Rasterizer->Stereo()) + flag = gp_Rasterizer->GetEye(); -static PyObject *gPySetMistStart(PyObject *, PyObject *args) -{ - - float miststart; - if (!PyArg_ParseTuple(args,"f:setMistStart",&miststart)) - return NULL; - - if (!gp_Rasterizer) { - PyErr_SetString(PyExc_RuntimeError, "Rasterizer.setMistStart(float), Rasterizer not available"); - return NULL; - } - - gp_Rasterizer->SetFogStart(miststart); - - Py_RETURN_NONE; + return PyLong_FromLong(flag); } - - -static PyObject *gPySetMistEnd(PyObject *, PyObject *args) +static PyObject *gPySetBackgroundColor(PyObject *, PyObject *value) { - - float mistend; - if (!PyArg_ParseTuple(args,"f:setMistEnd",&mistend)) + MT_Vector4 vec; + if (!PyVecTo(value, vec)) return NULL; - - if (!gp_Rasterizer) { - PyErr_SetString(PyExc_RuntimeError, "Rasterizer.setMistEnd(float), Rasterizer not available"); + + KX_WorldInfo *wi = gp_KetsjiScene->GetWorldInfo(); + if (!wi->hasWorld()) { + PyErr_SetString(PyExc_RuntimeError, "bge.render.SetBackgroundColor(color), World not available"); return NULL; } - - gp_Rasterizer->SetFogEnd(mistend); - - Py_RETURN_NONE; -} + ShowDeprecationWarning("setBackgroundColor()", "KX_WorldInfo.background_color"); + wi->setBackColor((float)vec[0], (float)vec[1], (float)vec[2]); -static PyObject *gPySetAmbientColor(PyObject *, PyObject *value) -{ - - MT_Vector3 vec; - if (!PyVecTo(value, vec)) - return NULL; - - if (!gp_Rasterizer) { - PyErr_SetString(PyExc_RuntimeError, "Rasterizer.setAmbientColor(color), Rasterizer not available"); - return NULL; - } - gp_Rasterizer->SetAmbientColor((float)vec[0], (float)vec[1], (float)vec[2]); - Py_RETURN_NONE; } - - - static PyObject *gPyMakeScreenshot(PyObject *, PyObject *args) { char* filename; @@ -1192,10 +1161,6 @@ static PyObject *gPySetGLSLMaterialSetting(PyObject *, else gs->glslflag |= flag; - /* temporarily store the glsl settings in the scene for the GLSL materials */ - GameData *gm= &(gp_KetsjiScene->GetBlenderScene()->gm); - gm->flag = gs->glslflag; - /* display lists and GLSL materials need to be remade */ if (sceneflag != gs->glslflag) { GPU_materials_free(); @@ -1203,11 +1168,14 @@ static PyObject *gPySetGLSLMaterialSetting(PyObject *, KX_SceneList *scenes = gp_KetsjiEngine->CurrentScenes(); KX_SceneList::iterator it; - for (it=scenes->begin(); it!=scenes->end(); it++) + for (it=scenes->begin(); it!=scenes->end(); it++) { + // temporarily store the glsl settings in the scene for the GLSL materials + (*it)->GetBlenderScene()->gm.flag = gs->glslflag; if ((*it)->GetBucketManager()) { (*it)->GetBucketManager()->ReleaseDisplayLists(); (*it)->GetBucketManager()->ReleaseMaterials(); } + } } } @@ -1320,7 +1288,7 @@ static PyObject *gPyDrawLine(PyObject *, PyObject *args) if (!PyVecTo(ob_color, color)) return NULL; - gp_Rasterizer->DrawDebugLine(from,to,color); + gp_Rasterizer->DrawDebugLine(gp_KetsjiScene, from, to, color); Py_RETURN_NONE; } @@ -1396,7 +1364,9 @@ static PyObject *gPySetVsync(PyObject *, PyObject *args) static PyObject *gPyGetVsync(PyObject *) { - return PyLong_FromLong(gp_Canvas->GetSwapInterval()); + int interval = 0; + gp_Canvas->GetSwapInterval(interval); + return PyLong_FromLong(interval); } static PyObject *gPyShowFramerate(PyObject *, PyObject *args) @@ -1463,6 +1433,24 @@ static PyObject *gPyClearDebugList(PyObject *) Py_RETURN_NONE; } +static PyObject *gPyGetDisplayDimensions(PyObject *) +{ + PyObject *result; + int width, height; + + gp_Canvas->GetDisplayDimensions(width, height); + + result = PyTuple_New(2); + PyTuple_SET_ITEMS(result, + PyLong_FromLong(width), + PyLong_FromLong(height)); + + return result; +} + +PyDoc_STRVAR(Rasterizer_module_documentation, +"This is the Python API for the game engine of Rasterizer" +); static struct PyMethodDef rasterizer_methods[] = { {"getWindowWidth",(PyCFunction) gPyGetWindowWidth, @@ -1478,11 +1466,6 @@ static struct PyMethodDef rasterizer_methods[] = { {"setMousePosition",(PyCFunction) gPySetMousePosition, METH_VARARGS, "setMousePosition(int x,int y)"}, {"setBackgroundColor",(PyCFunction)gPySetBackgroundColor,METH_O,"set Background Color (rgb)"}, - {"setAmbientColor",(PyCFunction)gPySetAmbientColor,METH_O,"set Ambient Color (rgb)"}, - {"disableMist",(PyCFunction)gPyDisableMist,METH_NOARGS,"turn off mist"}, - {"setMistColor",(PyCFunction)gPySetMistColor,METH_O,"set Mist Color (rgb)"}, - {"setMistStart",(PyCFunction)gPySetMistStart,METH_VARARGS,"set Mist Start(rgb)"}, - {"setMistEnd",(PyCFunction)gPySetMistEnd,METH_VARARGS,"set Mist End(rgb)"}, {"enableMotionBlur",(PyCFunction)gPyEnableMotionBlur,METH_VARARGS,"enable motion blur"}, {"disableMotionBlur",(PyCFunction)gPyDisableMotionBlur,METH_NOARGS,"disable motion blur"}, @@ -1490,6 +1473,7 @@ static struct PyMethodDef rasterizer_methods[] = { {"getEyeSeparation", (PyCFunction) gPyGetEyeSeparation, METH_NOARGS, "get the eye separation for stereo mode"}, {"setFocalLength", (PyCFunction) gPySetFocalLength, METH_VARARGS, "set the focal length for stereo mode"}, {"getFocalLength", (PyCFunction) gPyGetFocalLength, METH_VARARGS, "get the focal length for stereo mode"}, + {"getStereoEye", (PyCFunction) gPyGetStereoEye, METH_VARARGS, "get the current stereoscopy eye being rendered"}, {"setMaterialMode",(PyCFunction) gPySetMaterialType, METH_VARARGS, "set the material mode to use for OpenGL rendering"}, {"getMaterialMode",(PyCFunction) gPyGetMaterialType, @@ -1507,6 +1491,8 @@ static struct PyMethodDef rasterizer_methods[] = { {"setWindowSize", (PyCFunction) gPySetWindowSize, METH_VARARGS, ""}, {"setFullScreen", (PyCFunction) gPySetFullScreen, METH_O, ""}, {"getFullScreen", (PyCFunction) gPyGetFullScreen, METH_NOARGS, ""}, + {"getDisplayDimensions", (PyCFunction) gPyGetDisplayDimensions, METH_NOARGS, + "Get the actual dimensions, in pixels, of the physical display (e.g., the monitor)."}, {"setMipmapping", (PyCFunction) gPySetMipmapping, METH_VARARGS, ""}, {"getMipmapping", (PyCFunction) gPyGetMipmapping, METH_NOARGS, ""}, {"setVsync", (PyCFunction) gPySetVsync, METH_VARARGS, ""}, @@ -1519,15 +1505,11 @@ static struct PyMethodDef rasterizer_methods[] = { { NULL, (PyCFunction) NULL, 0, NULL } }; -// Initialization function for the module (*must* be called initGameLogic) -static char GameLogic_module_documentation[] = -"This is the Python API for the game engine of bge.logic" -; -static char Rasterizer_module_documentation[] = -"This is the Python API for the game engine of Rasterizer" -; +PyDoc_STRVAR(GameLogic_module_documentation, +"This is the Python API for the game engine of bge.logic" +); static struct PyModuleDef GameLogic_module_def = { {}, /* m_base */ @@ -1541,33 +1523,20 @@ static struct PyModuleDef GameLogic_module_def = { 0, /* m_free */ }; -PyObject *initGameLogic(KX_KetsjiEngine *engine, KX_Scene* scene) // quick hack to get gravity hook +PyMODINIT_FUNC initGameLogicPythonBinding() { PyObject *m; PyObject *d; PyObject *item; /* temp PyObject *storage */ - - gp_KetsjiEngine = engine; - gp_KetsjiScene = scene; gUseVisibilityTemp=false; - + PyObjectPlus::ClearDeprecationWarning(); /* Not that nice to call here but makes sure warnings are reset between loading scenes */ - - /* Use existing module where possible - * be careful not to init any runtime vars after this */ - m = PyImport_ImportModule( "GameLogic" ); - if (m) { - Py_DECREF(m); - return m; - } - else { - PyErr_Clear(); - // Create the module and add the functions - m = PyModule_Create(&GameLogic_module_def); - PyDict_SetItemString(PySys_GetObject("modules"), GameLogic_module_def.m_name, m); - } - + + m = PyModule_Create(&GameLogic_module_def); + PyDict_SetItemString(PySys_GetObject("modules"), GameLogic_module_def.m_name, m); + + // Add some symbolic constants to the module d = PyModule_GetDict(m); @@ -1587,17 +1556,22 @@ PyObject *initGameLogic(KX_KetsjiEngine *engine, KX_Scene* scene) // quick hack PyObject* joylist = PyList_New(JOYINDEX_MAX); for (int i=0; i<JOYINDEX_MAX; ++i) { - SCA_Joystick* joy = SCA_Joystick::GetInstance(i); + SCA_Joystick *joy = SCA_Joystick::GetInstance(i); + PyObject *item; + if (joy && joy->Connected()) { gp_PythonJoysticks[i] = new SCA_PythonJoystick(joy); - PyObject* tmp = gp_PythonJoysticks[i]->NewProxy(true); - Py_INCREF(tmp); - PyList_SET_ITEM(joylist, i, tmp); - } else { - joy->ReleaseInstance(); - Py_INCREF(Py_None); - PyList_SET_ITEM(joylist, i, Py_None); + item = gp_PythonJoysticks[i]->NewProxy(true); } + else { + if (joy) { + joy->ReleaseInstance(); + } + item = Py_None; + } + + Py_INCREF(item); + PyList_SET_ITEM(joylist, i, item); } PyDict_SetItemString(d, "joysticks", joylist); @@ -2047,7 +2021,77 @@ void removeImportMain(struct Main *maggie) bpy_import_main_extra_remove(maggie); } -// Copied from bpy_interface.c + +PyDoc_STRVAR(BGE_module_documentation, + "This module contains submodules for the Blender Game Engine.\n" +); + +static struct PyModuleDef BGE_module_def = { + PyModuleDef_HEAD_INIT, + "bge", /* m_name */ + BGE_module_documentation, /* m_doc */ + 0, /* m_size */ + NULL, /* m_methods */ + NULL, /* m_reload */ + NULL, /* m_traverse */ + NULL, /* m_clear */ + NULL, /* m_free */ +}; + +PyMODINIT_FUNC initBGE(void) +{ + PyObject *mod; + PyObject *submodule; + PyObject *sys_modules = PyThreadState_GET()->interp->modules; + const char *mod_full; + + mod = PyModule_Create(&BGE_module_def); + + /* skip "bge." */ +#define SUBMOD (mod_full + 4) + + mod_full = "bge.app"; + PyModule_AddObject(mod, SUBMOD, (submodule = initApplicationPythonBinding())); + PyDict_SetItemString(sys_modules, mod_full, submodule); + Py_INCREF(submodule); + + mod_full = "bge.constraints"; + PyModule_AddObject(mod, SUBMOD, (submodule = initConstraintPythonBinding())); + PyDict_SetItemString(sys_modules, mod_full, submodule); + Py_INCREF(submodule); + + mod_full = "bge.events"; + PyModule_AddObject(mod, SUBMOD, (submodule = initGameKeysPythonBinding())); + PyDict_SetItemString(sys_modules, mod_full, submodule); + Py_INCREF(submodule); + + mod_full = "bge.logic"; + PyModule_AddObject(mod, SUBMOD, (submodule = initGameLogicPythonBinding())); + PyDict_SetItemString(sys_modules, mod_full, submodule); + Py_INCREF(submodule); + + mod_full = "bge.render"; + PyModule_AddObject(mod, SUBMOD, (submodule = initRasterizerPythonBinding())); + PyDict_SetItemString(sys_modules, mod_full, submodule); + Py_INCREF(submodule); + + mod_full = "bge.texture"; + PyModule_AddObject(mod, SUBMOD, (submodule = initVideoTexturePythonBinding())); + PyDict_SetItemString(sys_modules, mod_full, submodule); + Py_INCREF(submodule); + + mod_full = "bge.types"; + PyModule_AddObject(mod, SUBMOD, (submodule = initGameTypesPythonBinding())); + PyDict_SetItemString(sys_modules, mod_full, submodule); + Py_INCREF(submodule); + +#undef SUBMOD + + return mod; +} + + +/* minimal required blender modules to run blenderplayer */ static struct _inittab bge_internal_modules[] = { {"mathutils", PyInit_mathutils}, {"bgl", BPyInit_bgl}, @@ -2060,7 +2104,7 @@ static struct _inittab bge_internal_modules[] = { * Python is not initialized. * see bpy_interface.c's BPY_python_start() which shares the same functionality in blender. */ -PyObject *initGamePlayerPythonScripting(const STR_String& progname, TPythonSecurityLevel level, Main *maggie, int argc, char** argv) +PyObject *initGamePlayerPythonScripting(Main *maggie, int argc, char** argv) { /* Yet another gotcha in the py api * Cant run PySys_SetArgv more than once because this adds the @@ -2069,16 +2113,22 @@ PyObject *initGamePlayerPythonScripting(const STR_String& progname, TPythonSecur * somehow it remembers the sys.path - Campbell */ static bool first_time = true; - const char * const py_path_bundle = BLI_get_folder(BLENDER_SYSTEM_PYTHON, NULL); - -#if 0 // TODO - py3 - STR_String pname = progname; - Py_SetProgramName(pname.Ptr()); -#endif - + const char * const py_path_bundle = BKE_appdir_folder_id(BLENDER_SYSTEM_PYTHON, NULL); + + /* not essential but nice to set our name */ + static wchar_t program_path_wchar[FILE_MAX]; /* python holds a reference */ + BLI_strncpy_wchar_from_utf8(program_path_wchar, BKE_appdir_program_path(), ARRAY_SIZE(program_path_wchar)); + Py_SetProgramName(program_path_wchar); + + /* Update, Py3.3 resolves attempting to parse non-existing header */ +#if 0 + /* Python 3.2 now looks for '2.xx/python/include/python3.2d/pyconfig.h' to + * parse from the 'sysconfig' module which is used by 'site', + * so for now disable site. alternatively we could copy the file. */ if (py_path_bundle != NULL) { - Py_NoSiteFlag = 1; + Py_NoSiteFlag = 1; /* inhibits the automatic importing of 'site' */ } +#endif Py_FrozenFlag = 1; @@ -2102,12 +2152,16 @@ PyObject *initGamePlayerPythonScripting(const STR_String& progname, TPythonSecur PySys_SetObject("argv", py_argv); Py_DECREF(py_argv); } - + /* Initialize thread support (also acquires lock) */ PyEval_InitThreads(); bpy_import_init(PyEval_GetBuiltins()); + bpy_import_main_set(maggie); + + initPySysObjects(maggie); + /* mathutils types are used by the BGE even if we don't import them */ { PyObject *mod = PyImport_ImportModuleLevel("mathutils", NULL, NULL, NULL, 0); @@ -2122,12 +2176,8 @@ PyObject *initGamePlayerPythonScripting(const STR_String& progname, TPythonSecur } #endif - initPyTypes(); - - bpy_import_main_set(maggie); - - initPySysObjects(maggie); - + PyDict_SetItemString(PyImport_GetModuleDict(), "bge", initBGE()); + first_time = false; PyObjectPlus::ClearDeprecationWarning(); @@ -2164,12 +2214,13 @@ void exitGamePlayerPythonScripting() /** * Python is already initialized. */ -PyObject *initGamePythonScripting(const STR_String& progname, TPythonSecurityLevel level, Main *maggie) +PyObject *initGamePythonScripting(Main *maggie) { -#if 0 // XXX TODO Py3 - STR_String pname = progname; - Py_SetProgramName(pname.Ptr()); -#endif + /* no need to Py_SetProgramName, it was already taken care of in BPY_python_start */ + + bpy_import_main_set(maggie); + + initPySysObjects(maggie); #ifdef WITH_AUDASPACE /* accessing a SoundActuator's sound results in a crash if aud is not initialized... */ @@ -2179,11 +2230,7 @@ PyObject *initGamePythonScripting(const STR_String& progname, TPythonSecurityLev } #endif - initPyTypes(); - - bpy_import_main_set(maggie); - - initPySysObjects(maggie); + PyDict_SetItemString(PyImport_GetModuleDict(), "bge", initBGE()); PyObjectPlus::NullDeprecationWarning(); @@ -2216,51 +2263,32 @@ void exitGamePythonScripting() void setupGamePython(KX_KetsjiEngine* ketsjiengine, KX_Scene *startscene, Main *blenderdata, PyObject *pyGlobalDict, PyObject **gameLogic, PyObject **gameLogic_keys, int argc, char** argv) { - PyObject *dictionaryobject; + PyObject *modules, *dictionaryobject; + + gp_Canvas = ketsjiengine->GetCanvas(); + gp_Rasterizer = ketsjiengine->GetRasterizer(); + gp_KetsjiEngine = ketsjiengine; + gp_KetsjiScene = startscene; if (argv) /* player only */ - dictionaryobject= initGamePlayerPythonScripting("Ketsji", psl_Lowest, blenderdata, argc, argv); + dictionaryobject= initGamePlayerPythonScripting(blenderdata, argc, argv); else - dictionaryobject= initGamePythonScripting("Ketsji", psl_Lowest, blenderdata); + dictionaryobject= initGamePythonScripting(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 */ + modules = PyImport_GetModuleDict(); + + *gameLogic = PyDict_GetItemString(modules, "GameLogic"); + /* is set in initGameLogicPythonBinding 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)); - - initGameKeys(); - initPythonConstraintBinding(); - initVideoTexture(); - - /* could be done a lot more nicely, but for now a quick way to get bge.* working */ - PyRun_SimpleString("sys = __import__('sys');" - "bge = type(sys)('bge');" - "bge.__dict__.update({'logic':__import__('GameLogic'), " - "'render':__import__('Rasterizer'), " - "'events':__import__('GameKeys'), " - "'constraints':__import__('PhysicsConstraints'), " - "'physics':__import__('PhysicsConstraints')," - "'types':__import__('GameTypes'), " - "'texture':__import__('VideoTexture')});" - /* so we can do 'import bge.foo as bar' */ - "sys.modules.update({'bge': bge, " - "'bge.logic':bge.logic, " - "'bge.render':bge.render, " - "'bge.events':bge.events, " - "'bge.constraints':bge.constraints, " - "'bge.physics':bge.physics," - "'bge.types':bge.types, " - "'bge.texture':bge.texture})" - ); } static struct PyModuleDef Rasterizer_module_def = { - {}, /* m_base */ + PyModuleDef_HEAD_INIT, "Rasterizer", /* m_name */ Rasterizer_module_documentation, /* m_doc */ 0, /* m_size */ @@ -2271,29 +2299,14 @@ static struct PyModuleDef Rasterizer_module_def = { 0, /* m_free */ }; -PyObject *initRasterizer(RAS_IRasterizer* rasty,RAS_ICanvas* canvas) +PyMODINIT_FUNC initRasterizerPythonBinding() { - gp_Canvas = canvas; - gp_Rasterizer = rasty; - - PyObject *m; PyObject *d; - /* Use existing module where possible - * be careful not to init any runtime vars after this */ - m = PyImport_ImportModule( "Rasterizer" ); - if (m) { - Py_DECREF(m); - return m; - } - else { - PyErr_Clear(); + m = PyModule_Create(&Rasterizer_module_def); + PyDict_SetItemString(PySys_GetObject("modules"), Rasterizer_module_def.m_name, m); - // Create the module and add the functions - m = PyModule_Create(&Rasterizer_module_def); - PyDict_SetItemString(PySys_GetObject("modules"), Rasterizer_module_def.m_name, m); - } // Add some symbolic constants to the module d = PyModule_GetDict(m); @@ -2314,6 +2327,10 @@ PyObject *initRasterizer(RAS_IRasterizer* rasty,RAS_ICanvas* canvas) KX_MACRO_addTypesToDict(d, VSYNC_ON, VSYNC_ON); KX_MACRO_addTypesToDict(d, VSYNC_ADAPTIVE, VSYNC_ADAPTIVE); + /* stereoscopy */ + KX_MACRO_addTypesToDict(d, LEFT_EYE, RAS_IRasterizer::RAS_STEREO_LEFTEYE); + KX_MACRO_addTypesToDict(d, RIGHT_EYE, RAS_IRasterizer::RAS_STEREO_RIGHTEYE); + // XXXX Add constants here // Check for errors @@ -2322,7 +2339,7 @@ PyObject *initRasterizer(RAS_IRasterizer* rasty,RAS_ICanvas* canvas) Py_FatalError("can't initialize module Rasterizer"); } - return d; + return m; } @@ -2331,13 +2348,14 @@ PyObject *initRasterizer(RAS_IRasterizer* rasty,RAS_ICanvas* canvas) /* GameKeys: symbolic constants for key mapping */ /* ------------------------------------------------------------------------- */ -static char GameKeys_module_documentation[] = +PyDoc_STRVAR(GameKeys_module_documentation, "This modules provides defines for key-codes" -; +); -static char gPyEventToString_doc[] = -"EventToString(event) - Take a valid event from the GameKeys module or Keyboard Sensor and return a name" -; +PyDoc_STRVAR(gPyEventToString_doc, +"EventToString(event)\n" +"Take a valid event from the GameKeys module or Keyboard Sensor and return a name" +); static PyObject *gPyEventToString(PyObject *, PyObject *value) { @@ -2365,9 +2383,11 @@ static PyObject *gPyEventToString(PyObject *, PyObject *value) return ret; } -static char gPyEventToCharacter_doc[] = -"EventToCharacter(event, is_shift) - Take a valid event from the GameKeys module or Keyboard Sensor and return a character" -; + +PyDoc_STRVAR(gPyEventToCharacter_doc, +"EventToCharacter(event, is_shift)\n" +"Take a valid event from the GameKeys module or Keyboard Sensor and return a character" +); static PyObject *gPyEventToCharacter(PyObject *, PyObject *args) { @@ -2393,7 +2413,7 @@ static struct PyMethodDef gamekeys_methods[] = { }; static struct PyModuleDef GameKeys_module_def = { - {}, /* m_base */ + PyModuleDef_HEAD_INIT, "GameKeys", /* m_name */ GameKeys_module_documentation, /* m_doc */ 0, /* m_size */ @@ -2404,24 +2424,13 @@ static struct PyModuleDef GameKeys_module_def = { 0, /* m_free */ }; -PyObject *initGameKeys() +PyMODINIT_FUNC initGameKeysPythonBinding() { PyObject *m; PyObject *d; - - /* Use existing module where possible */ - m = PyImport_ImportModule( "GameKeys" ); - if (m) { - Py_DECREF(m); - return m; - } - else { - PyErr_Clear(); - - // Create the module and add the functions - m = PyModule_Create(&GameKeys_module_def); - PyDict_SetItemString(PySys_GetObject("modules"), GameKeys_module_def.m_name, m); - } + + m = PyModule_Create(&GameKeys_module_def); + PyDict_SetItemString(PySys_GetObject("modules"), GameKeys_module_def.m_name, m); // Add some symbolic constants to the module d = PyModule_GetDict(m); @@ -2566,9 +2575,80 @@ PyObject *initGameKeys() Py_FatalError("can't initialize module GameKeys"); } - return d; + return m; +} + + + +/* ------------------------------------------------------------------------- */ +/* Application: application values that remain unchanged during runtime */ +/* ------------------------------------------------------------------------- */ + +PyDoc_STRVAR(Application_module_documentation, + "This module contains application values that remain unchanged during runtime." + ); + +static struct PyModuleDef Application_module_def = { + PyModuleDef_HEAD_INIT, + "bge.app", /* m_name */ + Application_module_documentation, /* m_doc */ + 0, /* m_size */ + NULL, /* m_methods */ + 0, /* m_reload */ + 0, /* m_traverse */ + 0, /* m_clear */ + 0, /* m_free */ +}; + +PyMODINIT_FUNC initApplicationPythonBinding() +{ + PyObject *m; + PyObject *d; + + m = PyModule_Create(&Application_module_def); + + // Add some symbolic constants to the module + d = PyModule_GetDict(m); + + PyDict_SetItemString(d, "version", Py_BuildValue("(iii)", + BLENDER_VERSION / 100, BLENDER_VERSION % 100, BLENDER_SUBVERSION)); + PyDict_SetItemString(d, "version_string", PyUnicode_FromFormat("%d.%02d (sub %d)", + BLENDER_VERSION / 100, BLENDER_VERSION % 100, BLENDER_SUBVERSION)); + PyDict_SetItemString(d, "version_char", PyUnicode_FromString( + STRINGIFY(BLENDER_VERSION_CHAR))); + + PyDict_SetItemString(d, "has_texture_ffmpeg", +#ifdef WITH_FFMPEG + Py_True +#else + Py_False +#endif + ); + PyDict_SetItemString(d, "has_joystick", +#ifdef WITH_SDL + Py_True +#else + Py_False +#endif + ); + PyDict_SetItemString(d, "has_physics", +#ifdef WITH_BULLET + Py_True +#else + Py_False +#endif + ); + + // Check for errors + if (PyErr_Occurred()) { + PyErr_Print(); + PyErr_Clear(); + } + + return m; } + // utility function for loading and saving the globalDict int saveGamePythonConfig( char **marshal_buffer) { |