From 9b298109126bd77b5ee219d5e456cdbb96cc6bb9 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Fri, 22 Aug 2008 06:02:01 +0000 Subject: BGE Python API - GameLogic.getBlendFileList(path='//') to return a list of blend's in the current directory. Needed for creating a level selector that lists all files in the level directory. CMakeFile include path to compile with recent changes. --- source/gameengine/Ketsji/KX_PythonInit.cpp | 128 ++++++++++++++--------------- 1 file changed, 62 insertions(+), 66 deletions(-) (limited to 'source/gameengine/Ketsji') diff --git a/source/gameengine/Ketsji/KX_PythonInit.cpp b/source/gameengine/Ketsji/KX_PythonInit.cpp index 472ae759d65..6179d614e8c 100644 --- a/source/gameengine/Ketsji/KX_PythonInit.cpp +++ b/source/gameengine/Ketsji/KX_PythonInit.cpp @@ -31,6 +31,7 @@ #include "GL/glew.h" #include +#include // directory header for py function getBlendFileList #ifdef WIN32 #pragma warning (disable : 4786) @@ -112,9 +113,7 @@ 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)) @@ -138,9 +137,7 @@ file to make a full path name (doesn't change during the game, even if you load\ other .blend).\n\ The function also converts the directory separator to the local file system format."; -static PyObject* gPyExpandPath(PyObject*, - PyObject* args, - PyObject*) +static PyObject* gPyExpandPath(PyObject*, PyObject* args) { char expanded[FILE_MAXDIR + FILE_MAXFILE]; char* filename; @@ -185,9 +182,7 @@ static PyObject* gPyGetSpectrum(PyObject*) -static PyObject* gPyStartDSP(PyObject*, - PyObject* args, - PyObject*) +static PyObject* gPyStartDSP(PyObject*, PyObject* args) { SND_IAudioDevice* audiodevice = SND_DeviceManager::Instance(); @@ -205,9 +200,7 @@ static PyObject* gPyStartDSP(PyObject*, -static PyObject* gPyStopDSP(PyObject*, - PyObject* args, - PyObject*) +static PyObject* gPyStopDSP(PyObject*, PyObject* args) { SND_IAudioDevice* audiodevice = SND_DeviceManager::Instance(); @@ -223,9 +216,7 @@ static PyObject* gPyStopDSP(PyObject*, return NULL; } -static PyObject* gPySetLogicTicRate(PyObject*, - PyObject* args, - PyObject*) +static PyObject* gPySetLogicTicRate(PyObject*, PyObject* args) { float ticrate; if (PyArg_ParseTuple(args, "f", &ticrate)) @@ -242,9 +233,7 @@ 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)) @@ -257,9 +246,7 @@ static PyObject* gPySetPhysicsTicRate(PyObject*, return NULL; } -static PyObject* gPySetPhysicsDebug(PyObject*, - PyObject* args, - PyObject*) +static PyObject* gPySetPhysicsDebug(PyObject*, PyObject* args) { int debugMode; if (PyArg_ParseTuple(args, "i", &debugMode)) @@ -278,6 +265,44 @@ static PyObject* gPyGetPhysicsTicRate(PyObject*) return PyFloat_FromDouble(PHY_GetActiveEnvironment()->getFixedTimeStep()); } +static PyObject* gPyGetBlendFileList(PyObject*, PyObject* args) +{ + char cpath[sizeof(G.sce)]; + char *searchpath = NULL; + PyObject* list; + + 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, G.sce); + } else { + /* Get the dir only */ + BLI_split_dirfile_basic(G.sce, 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 () failed, code %d (%s)\n", cpath, errno, strerror(errno)); + return list; + } + + while ((dirp = readdir(dp)) != NULL) { + if (BLI_testextensie(dirp->d_name, ".blend")) { + PyList_Append(list, PyString_FromString(dirp->d_name)); + } + } + + closedir(dp); + return list; +} + static STR_String gPyGetCurrentScene_doc = "getCurrentScene()\n" "Gets a reference to the current scene.\n"; @@ -377,14 +402,13 @@ static struct PyMethodDef game_methods[] = { {"setLogicTicRate", (PyCFunction) gPySetLogicTicRate, METH_VARARGS, "Sets the logic tic rate"}, {"getPhysicsTicRate", (PyCFunction) gPyGetPhysicsTicRate, METH_NOARGS, "Gets the physics tic rate"}, {"setPhysicsTicRate", (PyCFunction) gPySetPhysicsTicRate, METH_VARARGS, "Sets the physics tic rate"}, + {"getBlendFileList", (PyCFunction)gPyGetBlendFileList, METH_VARARGS, "Gets a list of blend files in the same directory as the current blend file"}, {"PrintGLInfo", (PyCFunction)pyPrintExt, METH_NOARGS, "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); @@ -394,9 +418,7 @@ static PyObject* gPyGetWindowHeight(PyObject*, -static PyObject* gPyGetWindowWidth(PyObject*, - PyObject* args, - PyObject*) +static PyObject* gPyGetWindowWidth(PyObject*, PyObject* args) { @@ -411,9 +433,7 @@ 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)) @@ -429,9 +449,7 @@ static PyObject* gPyEnableVisibility(PyObject*, -static PyObject* gPyShowMouse(PyObject*, - PyObject* args, - PyObject*) +static PyObject* gPyShowMouse(PyObject*, PyObject* args) { int visible; if (PyArg_ParseTuple(args,"i",&visible)) @@ -455,9 +473,7 @@ static PyObject* gPyShowMouse(PyObject*, -static PyObject* gPySetMousePosition(PyObject*, - PyObject* args, - PyObject*) +static PyObject* gPySetMousePosition(PyObject*, PyObject* args) { int x,y; if (PyArg_ParseTuple(args,"ii",&x,&y)) @@ -472,9 +488,7 @@ static PyObject* gPySetMousePosition(PyObject*, Py_Return; } -static PyObject* gPySetEyeSeparation(PyObject*, - PyObject* args, - PyObject*) +static PyObject* gPySetEyeSeparation(PyObject*, PyObject* args) { float sep; if (PyArg_ParseTuple(args, "f", &sep)) @@ -496,9 +510,7 @@ static PyObject* gPyGetEyeSeparation(PyObject*, PyObject*, PyObject*) return NULL; } -static PyObject* gPySetFocalLength(PyObject*, - PyObject* args, - PyObject*) +static PyObject* gPySetFocalLength(PyObject*, PyObject* args) { float focus; if (PyArg_ParseTuple(args, "f", &focus)) @@ -518,9 +530,7 @@ static PyObject* gPyGetFocalLength(PyObject*, PyObject*, PyObject*) return NULL; } -static PyObject* gPySetBackgroundColor(PyObject*, - PyObject* args, - PyObject*) +static PyObject* gPySetBackgroundColor(PyObject*, PyObject* args) { MT_Vector4 vec = MT_Vector4(0., 0., 0.3, 0.); @@ -538,9 +548,7 @@ static PyObject* gPySetBackgroundColor(PyObject*, -static PyObject* gPySetMistColor(PyObject*, - PyObject* args, - PyObject*) +static PyObject* gPySetMistColor(PyObject*, PyObject* args) { MT_Vector3 vec = MT_Vector3(0., 0., 0.); @@ -558,9 +566,7 @@ static PyObject* gPySetMistColor(PyObject*, -static PyObject* gPySetMistStart(PyObject*, - PyObject* args, - PyObject*) +static PyObject* gPySetMistStart(PyObject*, PyObject* args) { float miststart; @@ -579,9 +585,7 @@ static PyObject* gPySetMistStart(PyObject*, -static PyObject* gPySetMistEnd(PyObject*, - PyObject* args, - PyObject*) +static PyObject* gPySetMistEnd(PyObject*, PyObject* args) { float mistend; @@ -599,9 +603,7 @@ static PyObject* gPySetMistEnd(PyObject*, } -static PyObject* gPySetAmbientColor(PyObject*, - PyObject* args, - PyObject*) +static PyObject* gPySetAmbientColor(PyObject*, PyObject* args) { MT_Vector3 vec = MT_Vector3(0., 0., 0.); @@ -620,9 +622,7 @@ static PyObject* gPySetAmbientColor(PyObject*, -static PyObject* gPyMakeScreenshot(PyObject*, - PyObject* args, - PyObject*) +static PyObject* gPyMakeScreenshot(PyObject*, PyObject* args) { char* filename; if (PyArg_ParseTuple(args,"s",&filename)) @@ -638,9 +638,7 @@ static PyObject* gPyMakeScreenshot(PyObject*, Py_Return; } -static PyObject* gPyEnableMotionBlur(PyObject*, - PyObject* args, - PyObject*) +static PyObject* gPyEnableMotionBlur(PyObject*, PyObject* args) { float motionblurvalue; if (PyArg_ParseTuple(args,"f",&motionblurvalue)) @@ -656,9 +654,7 @@ static PyObject* gPyEnableMotionBlur(PyObject*, Py_Return; } -static PyObject* gPyDisableMotionBlur(PyObject*, - PyObject* args, - PyObject*) +static PyObject* gPyDisableMotionBlur(PyObject*, PyObject* args) { if(gp_Rasterizer) { -- cgit v1.2.3