diff options
Diffstat (limited to 'source/gameengine/Ketsji/KX_PythonInit.cpp')
-rw-r--r-- | source/gameengine/Ketsji/KX_PythonInit.cpp | 1063 |
1 files changed, 737 insertions, 326 deletions
diff --git a/source/gameengine/Ketsji/KX_PythonInit.cpp b/source/gameengine/Ketsji/KX_PythonInit.cpp index 681548fcc06..7ac817488fe 100644 --- a/source/gameengine/Ketsji/KX_PythonInit.cpp +++ b/source/gameengine/Ketsji/KX_PythonInit.cpp @@ -28,24 +28,14 @@ * Initialize Python thingies. */ -#ifdef HAVE_CONFIG_H -#include <config.h> -#endif +#include "GL/glew.h" -#ifdef WIN32 -#include <windows.h> -#endif // WIN32 -#ifdef __APPLE__ -#define GL_GLEXT_LEGACY 1 -#include <OpenGL/gl.h> -#include <OpenGL/glu.h> -#else -#include <GL/gl.h> -#if defined(__sun__) && !defined(__sparc__) -#include <mesa/glu.h> +// directory header for py function getBlendFileList +#include <stdlib.h> +#ifndef WIN32 + #include <dirent.h> #else -#include <GL/glu.h> -#endif + #include "BLI_winstuff.h" #endif #ifdef WIN32 @@ -67,17 +57,28 @@ #include "BL_ActionActuator.h" #include "RAS_IRasterizer.h" #include "RAS_ICanvas.h" +#include "RAS_BucketManager.h" #include "MT_Vector3.h" #include "MT_Point3.h" #include "ListValue.h" #include "KX_Scene.h" #include "SND_DeviceManager.h" -#include "RAS_OpenGLRasterizer/RAS_GLExtensionManager.h" #include "BL_Shader.h" #include "KX_PyMath.h" +#include "PyObjectPlus.h" + +//XXX +#if 0 +extern "C" { + #include "Mathutils.h" // Blender.Mathutils module copied here so the blenderlayer can use. +} +#endif + +#include "marshal.h" /* python header for loading/saving dicts */ + #include "PHY_IPhysicsEnvironment.h" // FIXME: Enable for access to blender python modules. This is disabled because // python has dependencies on a lot of other modules and is a pain to link. @@ -86,13 +87,20 @@ //#include "BPY_extern.h" #endif +#include "BKE_utildefines.h" +#include "BKE_global.h" +#include "BLI_blenlib.h" +#include "GPU_material.h" + static void setSandbox(TPythonSecurityLevel level); // 'local' copy of canvas ptr, for window height/width python scripts static RAS_ICanvas* gp_Canvas = NULL; static KX_Scene* gp_KetsjiScene = NULL; +static KX_KetsjiEngine* gp_KetsjiEngine = NULL; static RAS_IRasterizer* gp_Rasterizer = NULL; +static char gp_GamePythonPath[FILE_MAXDIR + FILE_MAXFILE] = ""; void KX_RasterizerDrawDebugLine(const MT_Vector3& from,const MT_Vector3& to,const MT_Vector3& color) { @@ -115,36 +123,51 @@ void KX_RasterizerDrawDebugLine(const MT_Vector3& from,const MT_Vector3& to,cons static PyObject* ErrorObject; STR_String gPyGetRandomFloat_doc="getRandomFloat returns a random floating point value in the range [0..1)"; -static PyObject* gPyGetRandomFloat(PyObject*, - PyObject*, - PyObject*) +static PyObject* gPyGetRandomFloat(PyObject*) { return PyFloat_FromDouble(MT_random()); } -static PyObject* gPySetGravity(PyObject*, - PyObject* args, - PyObject*) +static PyObject* gPySetGravity(PyObject*, PyObject* args) { MT_Vector3 vec = MT_Vector3(0., 0., 0.); - if (PyVecArgTo(args, vec)) - { - if (gp_KetsjiScene) - gp_KetsjiScene->SetGravity(vec); - - Py_Return; - } + if (!PyVecArgTo(args, vec)) + return NULL; + + if (gp_KetsjiScene) + gp_KetsjiScene->SetGravity(vec); - return NULL; + 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 startup .blend or runtime\n\ +file to make a full path name (doesn't change during the game, even if you load\n\ +other .blend).\n\ +The function also converts the directory separator to the local file system format."; + +static PyObject* gPyExpandPath(PyObject*, PyObject* args) +{ + char expanded[FILE_MAXDIR + FILE_MAXFILE]; + char* filename; + + if (!PyArg_ParseTuple(args,"s",&filename)) + return NULL; + + BLI_strncpy(expanded, filename, FILE_MAXDIR + FILE_MAXFILE); + BLI_convertstringcode(expanded, gp_GamePythonPath); + return PyString_FromString(expanded); } static bool usedsp = false; // this gets a pointer to an array filled with floats -static PyObject* gPyGetSpectrum(PyObject*, - PyObject* args, - PyObject*) +static PyObject* gPyGetSpectrum(PyObject*) { SND_IAudioDevice* audiodevice = SND_DeviceManager::Instance(); @@ -165,111 +188,145 @@ static PyObject* gPyGetSpectrum(PyObject*, PyList_SetItem(resultlist, index, PyFloat_FromDouble(spectrum[index])); } } + else { + for (int index = 0; index < 512; index++) + { + PyList_SetItem(resultlist, index, PyFloat_FromDouble(0.0)); + } + } return resultlist; } -static PyObject* gPyStartDSP(PyObject*, - PyObject* args, - PyObject*) +static PyObject* gPyStartDSP(PyObject*, PyObject* args) { SND_IAudioDevice* audiodevice = SND_DeviceManager::Instance(); - if (audiodevice) - { - if (!usedsp) - { - audiodevice->StartUsingDSP(); - usedsp = true; - Py_Return; - } + if (!audiodevice) { + PyErr_SetString(PyExc_RuntimeError, "no audio device available"); + return NULL; } - return NULL; + + if (!usedsp) { + audiodevice->StartUsingDSP(); + usedsp = true; + } + + Py_RETURN_NONE; } -static PyObject* gPyStopDSP(PyObject*, - PyObject* args, - PyObject*) +static PyObject* gPyStopDSP(PyObject*, PyObject* args) { SND_IAudioDevice* audiodevice = SND_DeviceManager::Instance(); - if (audiodevice) - { - if (usedsp) - { - audiodevice->StopUsingDSP(); - usedsp = false; - Py_Return; - } + if (!audiodevice) { + PyErr_SetString(PyExc_RuntimeError, "no audio device available"); + return NULL; } - return NULL; + + if (usedsp) { + audiodevice->StopUsingDSP(); + usedsp = true; + } + + Py_RETURN_NONE; } -static PyObject* gPySetLogicTicRate(PyObject*, - PyObject* args, - PyObject*) +static PyObject* gPySetLogicTicRate(PyObject*, PyObject* args) { float ticrate; - if (PyArg_ParseTuple(args, "f", &ticrate)) - { - KX_KetsjiEngine::SetTicRate(ticrate); - Py_Return; - } + if (!PyArg_ParseTuple(args, "f", &ticrate)) + return NULL; - return NULL; + KX_KetsjiEngine::SetTicRate(ticrate); + Py_RETURN_NONE; } -static PyObject* gPyGetLogicTicRate(PyObject*, PyObject*, PyObject*) +static PyObject* gPyGetLogicTicRate(PyObject*) { return PyFloat_FromDouble(KX_KetsjiEngine::GetTicRate()); } -static PyObject* gPySetPhysicsTicRate(PyObject*, - PyObject* args, - PyObject*) +static PyObject* gPySetPhysicsTicRate(PyObject*, PyObject* args) { float ticrate; - if (PyArg_ParseTuple(args, "f", &ticrate)) - { - - PHY_GetActiveEnvironment()->setFixedTimeStep(true,ticrate); - Py_Return; - } + if (!PyArg_ParseTuple(args, "f", &ticrate)) + return NULL; - return NULL; + PHY_GetActiveEnvironment()->setFixedTimeStep(true,ticrate); + Py_RETURN_NONE; } -static PyObject* gPySetPhysicsDebug(PyObject*, - PyObject* args, - PyObject*) +static PyObject* gPySetPhysicsDebug(PyObject*, PyObject* args) { int debugMode; - if (PyArg_ParseTuple(args, "i", &debugMode)) - { - PHY_GetActiveEnvironment()->setDebugMode(debugMode); - Py_Return; - } + if (!PyArg_ParseTuple(args, "i", &debugMode)) + return NULL; - return NULL; + PHY_GetActiveEnvironment()->setDebugMode(debugMode); + Py_RETURN_NONE; } -static PyObject* gPyGetPhysicsTicRate(PyObject*, PyObject*, PyObject*) +static PyObject* gPyGetPhysicsTicRate(PyObject*) { return PyFloat_FromDouble(PHY_GetActiveEnvironment()->getFixedTimeStep()); } +static PyObject* gPyGetAverageFrameRate(PyObject*) +{ + return PyFloat_FromDouble(KX_KetsjiEngine::GetAverageFrameRate()); +} + +static PyObject* gPyGetBlendFileList(PyObject*, PyObject* args) +{ + char cpath[sizeof(gp_GamePythonPath)]; + char *searchpath = NULL; + PyObject* list, *value; + + DIR *dp; + struct dirent *dirp; + + if (!PyArg_ParseTuple(args, "|s", &searchpath)) + return NULL; + + list = PyList_New(0); + + if (searchpath) { + BLI_strncpy(cpath, searchpath, FILE_MAXDIR + FILE_MAXFILE); + BLI_convertstringcode(cpath, gp_GamePythonPath); + } else { + /* Get the dir only */ + BLI_split_dirfile_basic(gp_GamePythonPath, cpath, NULL); + } + + if((dp = opendir(cpath)) == NULL) { + /* todo, show the errno, this shouldnt happen anyway if the blendfile is readable */ + fprintf(stderr, "Could not read directoty (%s) failed, code %d (%s)\n", cpath, errno, strerror(errno)); + return list; + } + + while ((dirp = readdir(dp)) != NULL) { + if (BLI_testextensie(dirp->d_name, ".blend")) { + value = PyString_FromString(dirp->d_name); + PyList_Append(list, value); + Py_DECREF(value); + } + } + + closedir(dp); + return list; +} + static STR_String gPyGetCurrentScene_doc = "getCurrentScene()\n" "Gets a reference to the current scene.\n"; -static PyObject* gPyGetCurrentScene(PyObject* self, - PyObject* args, - PyObject* kwds) +static PyObject* gPyGetCurrentScene(PyObject* self) { Py_INCREF(gp_KetsjiScene); return (PyObject*) gp_KetsjiScene; @@ -278,17 +335,13 @@ static PyObject* gPyGetCurrentScene(PyObject* self, static PyObject *pyPrintExt(PyObject *,PyObject *,PyObject *) { #define pprint(x) std::cout << x << std::endl; - bgl::BL_EXTInfo ext = bgl::RAS_EXT_support; bool count=0; bool support=0; pprint("Supported Extensions..."); -#ifdef GL_ARB_shader_objects - pprint(" GL_ARB_shader_objects supported? "<< (ext._ARB_shader_objects?"yes.":"no.")); + pprint(" GL_ARB_shader_objects supported? "<< (GLEW_ARB_shader_objects?"yes.":"no.")); count = 1; -#endif -#ifdef GL_ARB_vertex_shader - support= ext._ARB_vertex_shader; + support= GLEW_ARB_vertex_shader; pprint(" GL_ARB_vertex_shader supported? "<< (support?"yes.":"no.")); count = 1; if(support){ @@ -307,9 +360,8 @@ static PyObject *pyPrintExt(PyObject *,PyObject *,PyObject *) pprint(" Max combined texture units." << max); pprint(""); } -#endif -#ifdef GL_ARB_fragment_shader - support=ext._ARB_fragment_shader; + + support=GLEW_ARB_fragment_shader; pprint(" GL_ARB_fragment_shader supported? "<< (support?"yes.":"no.")); count = 1; if(support){ @@ -319,9 +371,8 @@ static PyObject *pyPrintExt(PyObject *,PyObject *,PyObject *) pprint(" Max uniform components." << max); pprint(""); } -#endif -#ifdef GL_ARB_texture_cube_map - support = ext._ARB_texture_cube_map; + + support = GLEW_ARB_texture_cube_map; pprint(" GL_ARB_texture_cube_map supported? "<< (support?"yes.":"no.")); count = 1; if(support){ @@ -331,9 +382,8 @@ static PyObject *pyPrintExt(PyObject *,PyObject *,PyObject *) pprint(" Max cubemap size." << size); pprint(""); } -#endif -#ifdef GL_ARB_multitexture - support = ext._ARB_multitexture; + + support = GLEW_ARB_multitexture; count = 1; pprint(" GL_ARB_multitexture supported? "<< (support?"yes.":"no.")); if(support){ @@ -343,63 +393,52 @@ static PyObject *pyPrintExt(PyObject *,PyObject *,PyObject *) pprint(" Max texture units available. " << units); pprint(""); } -#endif -#ifdef GL_ARB_texture_env_combine - pprint(" GL_ARB_texture_env_combine supported? "<< (ext._ARB_texture_env_combine?"yes.":"no.")); + + pprint(" GL_ARB_texture_env_combine supported? "<< (GLEW_ARB_texture_env_combine?"yes.":"no.")); count = 1; -#endif + if(!count) pprint("No extenstions are used in this build"); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static struct PyMethodDef game_methods[] = { + {"expandPath", (PyCFunction)gPyExpandPath, METH_VARARGS, (PY_METHODCHAR)gPyExpandPath_doc}, {"getCurrentController", (PyCFunction) SCA_PythonController::sPyGetCurrentController, - METH_VARARGS, SCA_PythonController::sPyGetCurrentController__doc__}, + METH_NOARGS, (PY_METHODCHAR)SCA_PythonController::sPyGetCurrentController__doc__}, {"getCurrentScene", (PyCFunction) gPyGetCurrentScene, - METH_VARARGS, gPyGetCurrentScene_doc.Ptr()}, + METH_NOARGS, (PY_METHODCHAR)gPyGetCurrentScene_doc.Ptr()}, {"addActiveActuator",(PyCFunction) SCA_PythonController::sPyAddActiveActuator, - METH_VARARGS, SCA_PythonController::sPyAddActiveActuator__doc__}, + METH_VARARGS, (PY_METHODCHAR)SCA_PythonController::sPyAddActiveActuator__doc__}, {"getRandomFloat",(PyCFunction) gPyGetRandomFloat, - METH_VARARGS,gPyGetRandomFloat_doc.Ptr()}, - {"setGravity",(PyCFunction) gPySetGravity, METH_VARARGS,"set Gravitation"}, - {"getSpectrum",(PyCFunction) gPyGetSpectrum, METH_VARARGS,"get audio spectrum"}, - {"stopDSP",(PyCFunction) gPyStopDSP, METH_VARARGS,"stop using the audio dsp (for performance reasons)"}, - {"getLogicTicRate", (PyCFunction) gPyGetLogicTicRate, METH_VARARGS, "Gets the logic tic rate"}, - {"setLogicTicRate", (PyCFunction) gPySetLogicTicRate, METH_VARARGS, "Sets the logic tic rate"}, - {"getPhysicsTicRate", (PyCFunction) gPyGetPhysicsTicRate, METH_VARARGS, "Gets the physics tic rate"}, - {"setPhysicsTicRate", (PyCFunction) gPySetPhysicsTicRate, METH_VARARGS, "Sets the physics tic rate"}, - {"PrintGLInfo", (PyCFunction)pyPrintExt, METH_NOARGS, "Prints GL Extension Info"}, + METH_NOARGS, (PY_METHODCHAR)gPyGetRandomFloat_doc.Ptr()}, + {"setGravity",(PyCFunction) gPySetGravity, METH_VARARGS, (PY_METHODCHAR)"set Gravitation"}, + {"getSpectrum",(PyCFunction) gPyGetSpectrum, METH_NOARGS, (PY_METHODCHAR)"get audio spectrum"}, + {"stopDSP",(PyCFunction) gPyStopDSP, METH_VARARGS, (PY_METHODCHAR)"stop using the audio dsp (for performance reasons)"}, + {"getLogicTicRate", (PyCFunction) gPyGetLogicTicRate, METH_NOARGS, (PY_METHODCHAR)"Gets the logic tic rate"}, + {"setLogicTicRate", (PyCFunction) gPySetLogicTicRate, METH_VARARGS, (PY_METHODCHAR)"Sets the logic tic rate"}, + {"getPhysicsTicRate", (PyCFunction) gPyGetPhysicsTicRate, METH_NOARGS, (PY_METHODCHAR)"Gets the physics tic rate"}, + {"setPhysicsTicRate", (PyCFunction) gPySetPhysicsTicRate, METH_VARARGS, (PY_METHODCHAR)"Sets the physics tic rate"}, + {"getAverageFrameRate", (PyCFunction) gPyGetAverageFrameRate, METH_NOARGS, (PY_METHODCHAR)"Gets the estimated average frame rate"}, + {"getBlendFileList", (PyCFunction)gPyGetBlendFileList, METH_VARARGS, (PY_METHODCHAR)"Gets a list of blend files in the same directory as the current blend file"}, + {"PrintGLInfo", (PyCFunction)pyPrintExt, METH_NOARGS, (PY_METHODCHAR)"Prints GL Extension Info"}, {NULL, (PyCFunction) NULL, 0, NULL } }; -static PyObject* gPyGetWindowHeight(PyObject*, - PyObject* args, - PyObject*) +static PyObject* gPyGetWindowHeight(PyObject*, PyObject* args) { - int height = (gp_Canvas ? gp_Canvas->GetHeight() : 0); - - PyObject* heightval = PyInt_FromLong(height); - return heightval; + return PyInt_FromLong((gp_Canvas ? gp_Canvas->GetHeight() : 0)); } -static PyObject* gPyGetWindowWidth(PyObject*, - PyObject* args, - PyObject*) +static PyObject* gPyGetWindowWidth(PyObject*, PyObject* args) { - - - int width = (gp_Canvas ? gp_Canvas->GetWidth() : 0); - - PyObject* widthval = PyInt_FromLong(width); - return widthval; + return PyInt_FromLong((gp_Canvas ? gp_Canvas->GetWidth() : 0)); } @@ -407,264 +446,403 @@ static PyObject* gPyGetWindowWidth(PyObject*, // temporarility visibility thing, will be moved to rasterizer/renderer later bool gUseVisibilityTemp = false; -static PyObject* gPyEnableVisibility(PyObject*, - PyObject* args, - PyObject*) +static PyObject* gPyEnableVisibility(PyObject*, PyObject* args) { int visible; - if (PyArg_ParseTuple(args,"i",&visible)) - { - gUseVisibilityTemp = (visible != 0); - } - else - { - Py_Return; - } - Py_Return; + if (!PyArg_ParseTuple(args,"i",&visible)) + return NULL; + + gUseVisibilityTemp = (visible != 0); + Py_RETURN_NONE; } -static PyObject* gPyShowMouse(PyObject*, - PyObject* args, - PyObject*) +static PyObject* gPyShowMouse(PyObject*, PyObject* args) { int visible; - if (PyArg_ParseTuple(args,"i",&visible)) + if (!PyArg_ParseTuple(args,"i",&visible)) + return NULL; + + if (visible) { - if (visible) - { - if (gp_Canvas) - gp_Canvas->SetMouseState(RAS_ICanvas::MOUSE_NORMAL); - } else - { - if (gp_Canvas) - gp_Canvas->SetMouseState(RAS_ICanvas::MOUSE_INVISIBLE); - } + if (gp_Canvas) + gp_Canvas->SetMouseState(RAS_ICanvas::MOUSE_NORMAL); + } else + { + if (gp_Canvas) + gp_Canvas->SetMouseState(RAS_ICanvas::MOUSE_INVISIBLE); } - Py_Return; + Py_RETURN_NONE; } -static PyObject* gPySetMousePosition(PyObject*, - PyObject* args, - PyObject*) +static PyObject* gPySetMousePosition(PyObject*, PyObject* args) { int x,y; - if (PyArg_ParseTuple(args,"ii",&x,&y)) - { - if (gp_Canvas) - gp_Canvas->SetMousePosition(x,y); - } + if (!PyArg_ParseTuple(args,"ii",&x,&y)) + return NULL; - Py_Return; + if (gp_Canvas) + gp_Canvas->SetMousePosition(x,y); + + Py_RETURN_NONE; } -static PyObject* gPySetEyeSeparation(PyObject*, - PyObject* args, - PyObject*) +static PyObject* gPySetEyeSeparation(PyObject*, PyObject* args) { float sep; - if (PyArg_ParseTuple(args, "f", &sep)) - { - if (gp_Rasterizer) - gp_Rasterizer->SetEyeSeparation(sep); - - Py_Return; + if (!PyArg_ParseTuple(args, "f", &sep)) + return NULL; + + if (!gp_Rasterizer) { + PyErr_SetString(PyExc_RuntimeError, "Rasterizer not available"); + return NULL; } - return NULL; + gp_Rasterizer->SetEyeSeparation(sep); + + Py_RETURN_NONE; } static PyObject* gPyGetEyeSeparation(PyObject*, PyObject*, PyObject*) { - if (gp_Rasterizer) - return PyFloat_FromDouble(gp_Rasterizer->GetEyeSeparation()); + if (!gp_Rasterizer) { + PyErr_SetString(PyExc_RuntimeError, "Rasterizer not available"); + return NULL; + } - return NULL; + return PyFloat_FromDouble(gp_Rasterizer->GetEyeSeparation()); } -static PyObject* gPySetFocalLength(PyObject*, - PyObject* args, - PyObject*) +static PyObject* gPySetFocalLength(PyObject*, PyObject* args) { float focus; - if (PyArg_ParseTuple(args, "f", &focus)) - { - if (gp_Rasterizer) - gp_Rasterizer->SetFocalLength(focus); - Py_Return; + if (!PyArg_ParseTuple(args, "f", &focus)) + return NULL; + + if (!gp_Rasterizer) { + PyErr_SetString(PyExc_RuntimeError, "Rasterizer not available"); + return NULL; } + + gp_Rasterizer->SetFocalLength(focus); - return NULL; + Py_RETURN_NONE; } static PyObject* gPyGetFocalLength(PyObject*, PyObject*, PyObject*) { - if (gp_Rasterizer) - return PyFloat_FromDouble(gp_Rasterizer->GetFocalLength()); - return NULL; + if (!gp_Rasterizer) { + PyErr_SetString(PyExc_RuntimeError, "Rasterizer not available"); + return NULL; + } + + return PyFloat_FromDouble(gp_Rasterizer->GetFocalLength()); + + Py_RETURN_NONE; } -static PyObject* gPySetBackgroundColor(PyObject*, - PyObject* args, - PyObject*) +static PyObject* gPySetBackgroundColor(PyObject*, PyObject* args) { MT_Vector4 vec = MT_Vector4(0., 0., 0.3, 0.); - if (PyVecArgTo(args, vec)) + if (!PyVecArgTo(args, vec)) + return NULL; + + if (gp_Canvas) { - if (gp_Canvas) - { - gp_Rasterizer->SetBackColor(vec[0], vec[1], vec[2], vec[3]); - } - Py_Return; + gp_Rasterizer->SetBackColor(vec[0], vec[1], vec[2], vec[3]); } - - return NULL; + Py_RETURN_NONE; } -static PyObject* gPySetMistColor(PyObject*, - PyObject* args, - PyObject*) +static PyObject* gPySetMistColor(PyObject*, PyObject* args) { MT_Vector3 vec = MT_Vector3(0., 0., 0.); - if (PyVecArgTo(args, vec)) - { - if (gp_Rasterizer) - { - gp_Rasterizer->SetFogColor(vec[0], vec[1], vec[2]); - } - Py_Return; - } + if (!PyVecArgTo(args, vec)) + return NULL; - return NULL; + if (!gp_Rasterizer) { + PyErr_SetString(PyExc_RuntimeError, "Rasterizer not available"); + return NULL; + } + gp_Rasterizer->SetFogColor(vec[0], vec[1], vec[2]); + + Py_RETURN_NONE; } -static PyObject* gPySetMistStart(PyObject*, - PyObject* args, - PyObject*) +static PyObject* gPySetMistStart(PyObject*, PyObject* args) { float miststart; - if (PyArg_ParseTuple(args,"f",&miststart)) - { - if (gp_Rasterizer) - { - gp_Rasterizer->SetFogStart(miststart); - } + if (!PyArg_ParseTuple(args,"f",&miststart)) + return NULL; + + if (!gp_Rasterizer) { + PyErr_SetString(PyExc_RuntimeError, "Rasterizer not available"); + return NULL; } - Py_Return; + + gp_Rasterizer->SetFogStart(miststart); + + Py_RETURN_NONE; } -static PyObject* gPySetMistEnd(PyObject*, - PyObject* args, - PyObject*) +static PyObject* gPySetMistEnd(PyObject*, PyObject* args) { float mistend; - if (PyArg_ParseTuple(args,"f",&mistend)) - { - if (gp_Rasterizer) - { - gp_Rasterizer->SetFogEnd(mistend); - } + if (!PyArg_ParseTuple(args,"f",&mistend)) + return NULL; + + if (!gp_Rasterizer) { + PyErr_SetString(PyExc_RuntimeError, "Rasterizer not available"); + return NULL; } - Py_Return; + + gp_Rasterizer->SetFogEnd(mistend); + + Py_RETURN_NONE; } -static PyObject* gPySetAmbientColor(PyObject*, - PyObject* args, - PyObject*) +static PyObject* gPySetAmbientColor(PyObject*, PyObject* args) { MT_Vector3 vec = MT_Vector3(0., 0., 0.); - if (PyVecArgTo(args, vec)) - { - if (gp_Rasterizer) - { - gp_Rasterizer->SetAmbientColor(vec[0], vec[1], vec[2]); - } - Py_Return; - } + if (!PyVecArgTo(args, vec)) + return NULL; - return NULL; + if (!gp_Rasterizer) { + PyErr_SetString(PyExc_RuntimeError, "Rasterizer not available"); + return NULL; + } + gp_Rasterizer->SetAmbientColor(vec[0], vec[1], vec[2]); + + Py_RETURN_NONE; } -static PyObject* gPyMakeScreenshot(PyObject*, - PyObject* args, - PyObject*) +static PyObject* gPyMakeScreenshot(PyObject*, PyObject* args) { char* filename; - if (PyArg_ParseTuple(args,"s",&filename)) + if (!PyArg_ParseTuple(args,"s",&filename)) + return NULL; + + if (gp_Canvas) { - if (gp_Canvas) - { - gp_Canvas->MakeScreenShot(filename); - } + gp_Canvas->MakeScreenShot(filename); } - Py_Return; + + Py_RETURN_NONE; } -static PyObject* gPyEnableMotionBlur(PyObject*, - PyObject* args, - PyObject*) +static PyObject* gPyEnableMotionBlur(PyObject*, PyObject* args) { float motionblurvalue; - if (PyArg_ParseTuple(args,"f",&motionblurvalue)) - { - if(gp_Rasterizer) - { - gp_Rasterizer->EnableMotionBlur(motionblurvalue); + if (!PyArg_ParseTuple(args,"f",&motionblurvalue)) + return NULL; + + if (!gp_Rasterizer) { + PyErr_SetString(PyExc_RuntimeError, "Rasterizer not available"); + return NULL; + } + + gp_Rasterizer->EnableMotionBlur(motionblurvalue); + + Py_RETURN_NONE; +} + +static PyObject* gPyDisableMotionBlur(PyObject*, PyObject* args) +{ + if (!gp_Rasterizer) { + PyErr_SetString(PyExc_RuntimeError, "Rasterizer not available"); + return NULL; + } + + gp_Rasterizer->DisableMotionBlur(); + + Py_RETURN_NONE; +} + +int getGLSLSettingFlag(char *setting) +{ + if(strcmp(setting, "lights") == 0) + return G_FILE_GLSL_NO_LIGHTS; + else if(strcmp(setting, "shaders") == 0) + return G_FILE_GLSL_NO_SHADERS; + else if(strcmp(setting, "shadows") == 0) + return G_FILE_GLSL_NO_SHADOWS; + else if(strcmp(setting, "ramps") == 0) + return G_FILE_GLSL_NO_RAMPS; + else if(strcmp(setting, "nodes") == 0) + return G_FILE_GLSL_NO_NODES; + else if(strcmp(setting, "extra_textures") == 0) + return G_FILE_GLSL_NO_EXTRA_TEX; + else + return -1; +} + +static PyObject* gPySetGLSLMaterialSetting(PyObject*, + PyObject* args, + PyObject*) +{ + char *setting; + int enable, flag, fileflags; + + if (!PyArg_ParseTuple(args,"si",&setting,&enable)) + return NULL; + + flag = getGLSLSettingFlag(setting); + + if (flag==-1) { + PyErr_SetString(PyExc_ValueError, "glsl setting is not known"); + return NULL; + } + + fileflags = G.fileflags; + + if (enable) + G.fileflags &= ~flag; + else + G.fileflags |= flag; + + /* display lists and GLSL materials need to be remade */ + if(G.fileflags != fileflags) { + if(gp_KetsjiEngine) { + KX_SceneList *scenes = gp_KetsjiEngine->CurrentScenes(); + KX_SceneList::iterator it; + + for(it=scenes->begin(); it!=scenes->end(); it++) + if((*it)->GetBucketManager()) + (*it)->GetBucketManager()->ReleaseDisplayLists(); } + + GPU_materials_free(); + } + + Py_RETURN_NONE; +} + +static PyObject* gPyGetGLSLMaterialSetting(PyObject*, + PyObject* args, + PyObject*) +{ + char *setting; + int enabled = 0, flag; + + if (!PyArg_ParseTuple(args,"s",&setting)) + return NULL; + + flag = getGLSLSettingFlag(setting); + + if (flag==-1) { + PyErr_SetString(PyExc_ValueError, "glsl setting is not known"); + return NULL; } - Py_Return; + + enabled = ((G.fileflags & flag) != 0); + return PyInt_FromLong(enabled); } -static PyObject* gPyDisableMotionBlur(PyObject*, +#define KX_TEXFACE_MATERIAL 0 +#define KX_BLENDER_MULTITEX_MATERIAL 1 +#define KX_BLENDER_GLSL_MATERIAL 2 + +static PyObject* gPySetMaterialType(PyObject*, PyObject* args, PyObject*) { - if(gp_Rasterizer) - { - gp_Rasterizer->DisableMotionBlur(); + int flag, type; + + if (!PyArg_ParseTuple(args,"i",&type)) + return NULL; + + if(type == KX_BLENDER_GLSL_MATERIAL) + flag = G_FILE_GAME_MAT|G_FILE_GAME_MAT_GLSL; + else if(type == KX_BLENDER_MULTITEX_MATERIAL) + flag = G_FILE_GAME_MAT; + else if(type == KX_TEXFACE_MATERIAL) + flag = 0; + else { + PyErr_SetString(PyExc_ValueError, "material type is not known"); + return NULL; } - Py_Return; + + G.fileflags &= ~(G_FILE_GAME_MAT|G_FILE_GAME_MAT_GLSL); + G.fileflags |= flag; + + Py_RETURN_NONE; } -STR_String gPyGetWindowHeight__doc__="getWindowHeight doc"; -STR_String gPyGetWindowWidth__doc__="getWindowWidth doc"; -STR_String gPyEnableVisibility__doc__="enableVisibility doc"; -STR_String gPyMakeScreenshot__doc__="make Screenshot doc"; -STR_String gPyShowMouse__doc__="showMouse(bool visible)"; -STR_String gPySetMousePosition__doc__="setMousePosition(int x,int y)"; +static PyObject* gPyGetMaterialType(PyObject*) +{ + int flag; + + if(G.fileflags & (G_FILE_GAME_MAT|G_FILE_GAME_MAT_GLSL)) + flag = KX_BLENDER_GLSL_MATERIAL; + else if(G.fileflags & G_FILE_GAME_MAT) + flag = KX_BLENDER_MULTITEX_MATERIAL; + else + flag = KX_TEXFACE_MATERIAL; + + return PyInt_FromLong(flag); +} + +static PyObject* gPyDrawLine(PyObject*, PyObject* args) +{ + PyObject* ob_from; + PyObject* ob_to; + PyObject* ob_color; + + if (!gp_Rasterizer) { + PyErr_SetString(PyExc_RuntimeError, "Rasterizer not available"); + return NULL; + } + + if (!PyArg_ParseTuple(args,"OOO",&ob_from,&ob_to,&ob_color)) + return NULL; + + MT_Vector3 from(0., 0., 0.); + MT_Vector3 to(0., 0., 0.); + MT_Vector3 color(0., 0., 0.); + if (!PyVecTo(ob_from, from)) + return NULL; + if (!PyVecTo(ob_to, to)) + return NULL; + if (!PyVecTo(ob_color, color)) + return NULL; + + gp_Rasterizer->DrawDebugLine(from,to,color); + + Py_RETURN_NONE; +} static struct PyMethodDef rasterizer_methods[] = { {"getWindowWidth",(PyCFunction) gPyGetWindowWidth, - METH_VARARGS, gPyGetWindowWidth__doc__.Ptr()}, + METH_VARARGS, "getWindowWidth doc"}, {"getWindowHeight",(PyCFunction) gPyGetWindowHeight, - METH_VARARGS, gPyGetWindowHeight__doc__.Ptr()}, + METH_VARARGS, "getWindowHeight doc"}, {"makeScreenshot",(PyCFunction)gPyMakeScreenshot, - METH_VARARGS, gPyMakeScreenshot__doc__.Ptr()}, + METH_VARARGS, "make Screenshot doc"}, {"enableVisibility",(PyCFunction) gPyEnableVisibility, - METH_VARARGS, gPyEnableVisibility__doc__.Ptr()}, + METH_VARARGS, "enableVisibility doc"}, {"showMouse",(PyCFunction) gPyShowMouse, - METH_VARARGS, gPyShowMouse__doc__.Ptr()}, + METH_VARARGS, "showMouse(bool visible)"}, {"setMousePosition",(PyCFunction) gPySetMousePosition, - METH_VARARGS, gPySetMousePosition__doc__.Ptr()}, + METH_VARARGS, "setMousePosition(int x,int y)"}, {"setBackgroundColor",(PyCFunction)gPySetBackgroundColor,METH_VARARGS,"set Background Color (rgb)"}, {"setAmbientColor",(PyCFunction)gPySetAmbientColor,METH_VARARGS,"set Ambient Color (rgb)"}, {"setMistColor",(PyCFunction)gPySetMistColor,METH_VARARGS,"set Mist Color (rgb)"}, @@ -678,11 +856,19 @@ static struct PyMethodDef rasterizer_methods[] = { {"getEyeSeparation", (PyCFunction) gPyGetEyeSeparation, METH_VARARGS, "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"}, + {"setMaterialMode",(PyCFunction) gPySetMaterialType, + METH_VARARGS, "set the material mode to use for OpenGL rendering"}, + {"getMaterialMode",(PyCFunction) gPyGetMaterialType, + METH_NOARGS, "get the material mode being used for OpenGL rendering"}, + {"setGLSLMaterialSetting",(PyCFunction) gPySetGLSLMaterialSetting, + METH_VARARGS, "set the state of a GLSL material setting"}, + {"getGLSLMaterialSetting",(PyCFunction) gPyGetGLSLMaterialSetting, + METH_VARARGS, "get the state of a GLSL material setting"}, + {"drawLine", (PyCFunction) gPyDrawLine, + METH_VARARGS, "draw a line on the screen"}, { NULL, (PyCFunction) NULL, 0, NULL } }; - - // Initialization function for the module (*must* be called initGameLogic) static char GameLogic_module_documentation[] = @@ -695,11 +881,12 @@ static char Rasterizer_module_documentation[] = -PyObject* initGameLogic(KX_Scene* scene) // quick hack to get gravity hook +PyObject* initGameLogic(KX_KetsjiEngine *engine, KX_Scene* scene) // quick hack to get gravity hook { PyObject* m; PyObject* d; + gp_KetsjiEngine = engine; gp_KetsjiScene = scene; gUseVisibilityTemp=false; @@ -711,10 +898,14 @@ PyObject* initGameLogic(KX_Scene* scene) // quick hack to get gravity hook // Add some symbolic constants to the module d = PyModule_GetDict(m); + + // can be overwritten later for gameEngine instances that can load new blend files and re-initialize this module + // for now its safe to make sure it exists for other areas such as the web plugin + PyDict_SetItemString(d, "globalDict", PyDict_New()); ErrorObject = PyString_FromString("GameLogic.error"); PyDict_SetItemString(d, "error", ErrorObject); - + // XXXX Add constants here /* To use logic bricks, we need some sort of constants. Here, we associate */ /* constants and sumbolic names. Add them to dictionary d. */ @@ -737,6 +928,15 @@ PyObject* initGameLogic(KX_Scene* scene) // quick hack to get gravity hook KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_ROTX, KX_ConstraintActuator::KX_ACT_CONSTRAINT_ROTX); KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_ROTY, KX_ConstraintActuator::KX_ACT_CONSTRAINT_ROTY); KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_ROTZ, KX_ConstraintActuator::KX_ACT_CONSTRAINT_ROTZ); + KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_DIRPX, KX_ConstraintActuator::KX_ACT_CONSTRAINT_DIRPX); + KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_DIRPY, KX_ConstraintActuator::KX_ACT_CONSTRAINT_DIRPY); + KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_DIRPY, KX_ConstraintActuator::KX_ACT_CONSTRAINT_DIRPY); + KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_DIRNX, KX_ConstraintActuator::KX_ACT_CONSTRAINT_DIRNX); + KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_DIRNY, KX_ConstraintActuator::KX_ACT_CONSTRAINT_DIRNY); + KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_DIRNY, KX_ConstraintActuator::KX_ACT_CONSTRAINT_DIRNY); + KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_ORIX, KX_ConstraintActuator::KX_ACT_CONSTRAINT_ORIX); + KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_ORIY, KX_ConstraintActuator::KX_ACT_CONSTRAINT_ORIY); + KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_ORIZ, KX_ConstraintActuator::KX_ACT_CONSTRAINT_ORIZ); /* 4. Ipo actuator, simple part */ KX_MACRO_addTypesToDict(d, KX_IPOACT_PLAY, KX_IpoActuator::KX_ACT_IPO_PLAY); @@ -803,37 +1003,75 @@ PyObject* initGameLogic(KX_Scene* scene) // quick hack to get gravity hook KX_MACRO_addTypesToDict(d, CAM_POS, BL_Shader::CAM_POS); KX_MACRO_addTypesToDict(d, CONSTANT_TIMER, BL_Shader::CONSTANT_TIMER); + /* 10 state actuator */ + KX_MACRO_addTypesToDict(d, KX_STATE1, (1<<0)); + KX_MACRO_addTypesToDict(d, KX_STATE2, (1<<1)); + KX_MACRO_addTypesToDict(d, KX_STATE3, (1<<2)); + KX_MACRO_addTypesToDict(d, KX_STATE4, (1<<3)); + KX_MACRO_addTypesToDict(d, KX_STATE5, (1<<4)); + KX_MACRO_addTypesToDict(d, KX_STATE6, (1<<5)); + KX_MACRO_addTypesToDict(d, KX_STATE7, (1<<6)); + KX_MACRO_addTypesToDict(d, KX_STATE8, (1<<7)); + KX_MACRO_addTypesToDict(d, KX_STATE9, (1<<8)); + KX_MACRO_addTypesToDict(d, KX_STATE10, (1<<9)); + KX_MACRO_addTypesToDict(d, KX_STATE11, (1<<10)); + KX_MACRO_addTypesToDict(d, KX_STATE12, (1<<11)); + KX_MACRO_addTypesToDict(d, KX_STATE13, (1<<12)); + KX_MACRO_addTypesToDict(d, KX_STATE14, (1<<13)); + KX_MACRO_addTypesToDict(d, KX_STATE15, (1<<14)); + KX_MACRO_addTypesToDict(d, KX_STATE16, (1<<15)); + KX_MACRO_addTypesToDict(d, KX_STATE17, (1<<16)); + KX_MACRO_addTypesToDict(d, KX_STATE18, (1<<17)); + KX_MACRO_addTypesToDict(d, KX_STATE19, (1<<18)); + KX_MACRO_addTypesToDict(d, KX_STATE20, (1<<19)); + KX_MACRO_addTypesToDict(d, KX_STATE21, (1<<20)); + KX_MACRO_addTypesToDict(d, KX_STATE22, (1<<21)); + KX_MACRO_addTypesToDict(d, KX_STATE23, (1<<22)); + KX_MACRO_addTypesToDict(d, KX_STATE24, (1<<23)); + KX_MACRO_addTypesToDict(d, KX_STATE25, (1<<24)); + KX_MACRO_addTypesToDict(d, KX_STATE26, (1<<25)); + KX_MACRO_addTypesToDict(d, KX_STATE27, (1<<26)); + KX_MACRO_addTypesToDict(d, KX_STATE28, (1<<27)); + KX_MACRO_addTypesToDict(d, KX_STATE29, (1<<28)); + KX_MACRO_addTypesToDict(d, KX_STATE30, (1<<29)); + // Check for errors if (PyErr_Occurred()) { Py_FatalError("can't initialize module GameLogic"); } - return d; -} - -void dictionaryClearByHand(PyObject *dict) -{ - // Clears the dictionary by hand: - // This prevents, extra references to global variables - // inside the GameLogic dictionary when the python interpreter is finalized. - // which allows the scene to safely delete them :) - // see: (space.c)->start_game - if(dict) PyDict_Clear(dict); + return m; } - // Python Sandbox code // override builtin functions import() and open() -PyObject *KXpy_open(PyObject *self, PyObject *args) -{ +PyObject *KXpy_open(PyObject *self, PyObject *args) { PyErr_SetString(PyExc_RuntimeError, "Sandbox: open() function disabled!\nGame Scripts should not use this function."); 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; +} +PyObject *KXpy_execfile(PyObject *self, PyObject *args) { + PyErr_SetString(PyExc_RuntimeError, "Sandbox: execfile() function disabled!\nGame Scripts should not use this function."); + return NULL; +} + +PyObject *KXpy_compile(PyObject *self, PyObject *args) { + PyErr_SetString(PyExc_RuntimeError, "Sandbox: compile() function disabled!\nGame Scripts should not use this function."); + return NULL; +} PyObject *KXpy_import(PyObject *self, PyObject *args) { @@ -843,9 +1081,17 @@ PyObject *KXpy_import(PyObject *self, PyObject *args) PyObject *fromlist = NULL; PyObject *l, *m, *n; +#if (PY_VERSION_HEX >= 0x02060000) + int dummy_val; /* what does this do?*/ + + if (!PyArg_ParseTuple(args, "s|OOOi:m_import", + &name, &globals, &locals, &fromlist, &dummy_val)) + return NULL; +#else if (!PyArg_ParseTuple(args, "s|OOO:m_import", &name, &globals, &locals, &fromlist)) return NULL; +#endif /* check for builtin modules */ m = PyImport_AddModule("sys"); @@ -859,7 +1105,7 @@ PyObject *KXpy_import(PyObject *self, PyObject *args) /* quick hack for GamePython modules TODO: register builtin modules properly by ExtendInittab */ if (!strcmp(name, "GameLogic") || !strcmp(name, "GameKeys") || !strcmp(name, "PhysicsConstraints") || - !strcmp(name, "Rasterizer")) { + !strcmp(name, "Rasterizer") || !strcmp(name, "Mathutils")) { return PyImport_ImportModuleEx(name, globals, locals, fromlist); } @@ -869,40 +1115,67 @@ PyObject *KXpy_import(PyObject *self, PyObject *args) } +/* override python file type functions */ +#if 0 +static int +file_init(PyObject *self, PyObject *args, PyObject *kwds) +{ + KXpy_file(NULL, NULL); + return -1; +} +static PyObject * +file_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +{ + return KXpy_file(NULL, NULL); +} +#endif -static PyMethodDef meth_open[] = { - { "open", KXpy_open, METH_VARARGS, - "(disabled)"} -}; - - -static PyMethodDef meth_import[] = { - { "import", KXpy_import, METH_VARARGS, - "our own import"} -}; - +static PyMethodDef meth_open[] = {{ "open", KXpy_open, METH_VARARGS, "(disabled)"}}; +static PyMethodDef meth_reload[] = {{ "reload", KXpy_reload, METH_VARARGS, "(disabled)"}}; +static PyMethodDef meth_file[] = {{ "file", KXpy_file, METH_VARARGS, "(disabled)"}}; +static PyMethodDef meth_execfile[] = {{ "execfile", KXpy_execfile, METH_VARARGS, "(disabled)"}}; +static PyMethodDef meth_compile[] = {{ "compile", KXpy_compile, METH_VARARGS, "(disabled)"}}; +static PyMethodDef meth_import[] = {{ "import", KXpy_import, METH_VARARGS, "our own import"}}; //static PyObject *g_oldopen = 0; //static PyObject *g_oldimport = 0; //static int g_security = 0; - void setSandbox(TPythonSecurityLevel level) { PyObject *m = PyImport_AddModule("__builtin__"); PyObject *d = PyModule_GetDict(m); - PyObject *meth = PyCFunction_New(meth_open, NULL); switch (level) { case psl_Highest: //if (!g_security) { //g_oldopen = PyDict_GetItemString(d, "open"); - PyDict_SetItemString(d, "open", meth); - meth = PyCFunction_New(meth_import, NULL); - PyDict_SetItemString(d, "__import__", meth); + + // functions we cant trust + PyDict_SetItemString(d, "open", PyCFunction_New(meth_open, NULL)); + PyDict_SetItemString(d, "reload", PyCFunction_New(meth_reload, NULL)); + PyDict_SetItemString(d, "file", PyCFunction_New(meth_file, NULL)); + PyDict_SetItemString(d, "execfile", PyCFunction_New(meth_execfile, NULL)); + PyDict_SetItemString(d, "compile", PyCFunction_New(meth_compile, NULL)); + + // our own import + PyDict_SetItemString(d, "__import__", PyCFunction_New(meth_import, NULL)); //g_security = level; + + // Overiding file dosnt stop it being accessed if your sneaky + // f = [ t for t in (1).__class__.__mro__[-1].__subclasses__() if t.__name__ == 'file'][0]('/some_file.txt', 'w') + // f.write('...') + // so overwrite the file types functions. be very careful here still, since python uses python. + // ps - python devs frown deeply upon this. + + /* this could mess up pythons internals, if we are serious about sandboxing + * issues like the one above need to be solved, possibly modify __subclasses__ is safer? */ +#if 0 + PyFile_Type.tp_init = file_init; + PyFile_Type.tp_new = file_new; +#endif //} break; /* @@ -985,6 +1258,11 @@ PyObject* initRasterizer(RAS_IRasterizer* rasty,RAS_ICanvas* canvas) ErrorObject = PyString_FromString("Rasterizer.error"); PyDict_SetItemString(d, "error", ErrorObject); + /* needed for get/setMaterialType */ + KX_MACRO_addTypesToDict(d, KX_TEXFACE_MATERIAL, KX_TEXFACE_MATERIAL); + KX_MACRO_addTypesToDict(d, KX_BLENDER_MULTITEX_MATERIAL, KX_BLENDER_MULTITEX_MATERIAL); + KX_MACRO_addTypesToDict(d, KX_BLENDER_GLSL_MATERIAL, KX_BLENDER_GLSL_MATERIAL); + // XXXX Add constants here // Check for errors @@ -1006,9 +1284,38 @@ static char GameKeys_module_documentation[] = "This modules provides defines for key-codes" ; +static char gPyEventToString_doc[] = +"Take a valid event from the GameKeys module or Keyboard Sensor and return a name" +; +static PyObject* gPyEventToString(PyObject*, PyObject* value) +{ + PyObject* mod, *dict, *key, *val, *ret = NULL; + Py_ssize_t pos = 0; + + mod = PyImport_ImportModule( "GameKeys" ); + if (!mod) + return NULL; + + dict = PyModule_GetDict(mod); + + while (PyDict_Next(dict, &pos, &key, &val)) { + if (PyObject_Compare(value, val)==0) { + ret = key; + break; + } + } + + PyErr_Clear(); // incase there was an error clearing + Py_DECREF(mod); + if (!ret) PyErr_SetString(PyExc_ValueError, "expected a valid int keyboard event"); + else Py_INCREF(ret); + + return ret; +} static struct PyMethodDef gamekeys_methods[] = { + {"EventToString", (PyCFunction)gPyEventToString, METH_O, (PY_METHODCHAR)gPyEventToString_doc}, { NULL, (PyCFunction) NULL, 0, NULL } }; @@ -1152,7 +1459,111 @@ PyObject* initGameKeys() return d; } -void PHY_SetActiveScene(class KX_Scene* scene) +PyObject* initMathutils() +{ + return NULL; //XXX Mathutils_Init("Mathutils"); // Use as a top level module in BGE +} + +void KX_SetActiveScene(class KX_Scene* scene) { gp_KetsjiScene = scene; } + +class KX_Scene* KX_GetActiveScene() +{ + return gp_KetsjiScene; +} + +class KX_KetsjiEngine* KX_GetActiveEngine() +{ + return gp_KetsjiEngine; +} + +// utility function for loading and saving the globalDict +int saveGamePythonConfig( char **marshal_buffer) +{ + int marshal_length = 0; + PyObject* gameLogic = PyImport_ImportModule("GameLogic"); + if (gameLogic) { + PyObject* pyGlobalDict = PyDict_GetItemString(PyModule_GetDict(gameLogic), "globalDict"); // Same as importing the module + if (pyGlobalDict) { +#ifdef Py_MARSHAL_VERSION + PyObject* pyGlobalDictMarshal = PyMarshal_WriteObjectToString( pyGlobalDict, 2); // Py_MARSHAL_VERSION == 2 as of Py2.5 +#else + PyObject* pyGlobalDictMarshal = PyMarshal_WriteObjectToString( pyGlobalDict ); +#endif + if (pyGlobalDictMarshal) { + // for testing only + // PyObject_Print(pyGlobalDictMarshal, stderr, 0); + + marshal_length= PyString_Size(pyGlobalDictMarshal); + *marshal_buffer = new char[marshal_length + 1]; + memcpy(*marshal_buffer, PyString_AsString(pyGlobalDictMarshal), marshal_length); + + Py_DECREF(pyGlobalDictMarshal); + } else { + printf("Error, GameLogic.globalDict could not be marshal'd\n"); + } + } else { + printf("Error, GameLogic.globalDict was removed\n"); + } + Py_DECREF(gameLogic); + } else { + PyErr_Clear(); + printf("Error, GameLogic failed to import GameLogic.globalDict will be lost\n"); + } + return marshal_length; +} + +int loadGamePythonConfig(char *marshal_buffer, int marshal_length) +{ + /* Restore the dict */ + if (marshal_buffer) { + PyObject* gameLogic = PyImport_ImportModule("GameLogic"); + + if (gameLogic) { + PyObject* pyGlobalDict = PyMarshal_ReadObjectFromString(marshal_buffer, marshal_length); + if (pyGlobalDict) { + PyObject* pyGlobalDict_orig = PyDict_GetItemString(PyModule_GetDict(gameLogic), "globalDict"); // Same as importing the module. + if (pyGlobalDict_orig) { + PyDict_Clear(pyGlobalDict_orig); + PyDict_Update(pyGlobalDict_orig, pyGlobalDict); + } else { + /* this should not happen, but cant find the original globalDict, just assign it then */ + PyDict_SetItemString(PyModule_GetDict(gameLogic), "globalDict", pyGlobalDict); // Same as importing the module. + } + Py_DECREF(gameLogic); + Py_DECREF(pyGlobalDict); + return 1; + } else { + Py_DECREF(gameLogic); + PyErr_Clear(); + printf("Error could not marshall string\n"); + } + } else { + PyErr_Clear(); + printf("Error, GameLogic failed to import GameLogic.globalDict will be lost\n"); + } + } + return 0; +} + +void pathGamePythonConfig( char *path ) +{ + int len = strlen(gp_GamePythonPath); + + BLI_strncpy(path, gp_GamePythonPath, sizeof(gp_GamePythonPath)); + + /* replace extension */ + if (BLI_testextensie(path, ".blend")) { + strcpy(path+(len-6), ".bgeconf"); + } else { + strcpy(path+len, ".bgeconf"); + } +} + +void setGamePythonPath(char *path) +{ + BLI_strncpy(gp_GamePythonPath, path, sizeof(gp_GamePythonPath)); +} + |