[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [16221] trunk/blender/source/gameengine: BGE Python API - GameLogic.getBlendFileList(path='//') to return a list of blend's in the current directory.

Campbell Barton ideasman42 at gmail.com
Fri Aug 22 08:02:05 CEST 2008


Revision: 16221
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=16221
Author:   campbellbarton
Date:     2008-08-22 08:02:01 +0200 (Fri, 22 Aug 2008)

Log Message:
-----------
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.

Modified Paths:
--------------
    trunk/blender/source/gameengine/Ketsji/KX_PythonInit.cpp
    trunk/blender/source/gameengine/Physics/Bullet/CMakeLists.txt
    trunk/blender/source/gameengine/PyDoc/GameLogic.py

Modified: trunk/blender/source/gameengine/Ketsji/KX_PythonInit.cpp
===================================================================
--- trunk/blender/source/gameengine/Ketsji/KX_PythonInit.cpp	2008-08-22 02:33:19 UTC (rev 16220)
+++ trunk/blender/source/gameengine/Ketsji/KX_PythonInit.cpp	2008-08-22 06:02:01 UTC (rev 16221)
@@ -31,6 +31,7 @@
 #include "GL/glew.h"
 
 #include <stdlib.h>
+#include <dirent.h> // directory header for py function getBlendFileList
 
 #ifdef WIN32
 #pragma warning (disable : 4786)
@@ -112,9 +113,7 @@
 	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 @@
 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* gPyStartDSP(PyObject*,
-						PyObject* args, 
-						PyObject*)
+static PyObject* gPyStartDSP(PyObject*, PyObject* args)
 {
 	SND_IAudioDevice* audiodevice = SND_DeviceManager::Instance();
 
@@ -205,9 +200,7 @@
 
 
 
-static PyObject* gPyStopDSP(PyObject*,
-					   PyObject* args, 
-					   PyObject*)
+static PyObject* gPyStopDSP(PyObject*, PyObject* args)
 {
 	SND_IAudioDevice* audiodevice = SND_DeviceManager::Instance();
 
@@ -223,9 +216,7 @@
 	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 @@
 	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 @@
 	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 @@
 	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 @@
 	{"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* gPyGetWindowWidth(PyObject*, 
-										 PyObject* args, 
-										 PyObject*)
+static PyObject* gPyGetWindowWidth(PyObject*, PyObject* args)
 {
 		
 
@@ -411,9 +433,7 @@
 // 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* 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* 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 @@
    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 @@
 	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 @@
 	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* 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* gPySetMistStart(PyObject*, 
-										 PyObject* args, 
-										 PyObject*)
+static PyObject* gPySetMistStart(PyObject*, PyObject* args)
 {
 
 	float miststart;
@@ -579,9 +585,7 @@
 
 
 
-static PyObject* gPySetMistEnd(PyObject*, 
-										 PyObject* args, 
-										 PyObject*)
+static PyObject* gPySetMistEnd(PyObject*, PyObject* args)
 {
 
 	float mistend;
@@ -599,9 +603,7 @@
 }
 
 
-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* gPyMakeScreenshot(PyObject*,
-									PyObject* args,
-									PyObject*)
+static PyObject* gPyMakeScreenshot(PyObject*, PyObject* args)
 {
 	char* filename;
 	if (PyArg_ParseTuple(args,"s",&filename))
@@ -638,9 +638,7 @@
 	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 @@
 	Py_Return;
 }
 
-static PyObject* gPyDisableMotionBlur(PyObject*,
-									PyObject* args,
-									PyObject*)
+static PyObject* gPyDisableMotionBlur(PyObject*, PyObject* args)
 {
 	if(gp_Rasterizer)
 	{

Modified: trunk/blender/source/gameengine/Physics/Bullet/CMakeLists.txt
===================================================================
--- trunk/blender/source/gameengine/Physics/Bullet/CMakeLists.txt	2008-08-22 02:33:19 UTC (rev 16220)
+++ trunk/blender/source/gameengine/Physics/Bullet/CMakeLists.txt	2008-08-22 06:02:01 UTC (rev 16221)
@@ -30,6 +30,10 @@
   .
   ../common
   ../../../../extern/bullet2/src
+  ../../../../intern/moto/include
+  ../../../kernel/gen_system
+  ../../../../intern/string
+  ../../Rasterizer
 )
 
 BLENDERLIB(bf_bullet "${SRC}" "${INC}")

Modified: trunk/blender/source/gameengine/PyDoc/GameLogic.py
===================================================================
--- trunk/blender/source/gameengine/PyDoc/GameLogic.py	2008-08-22 02:33:19 UTC (rev 16220)
+++ trunk/blender/source/gameengine/PyDoc/GameLogic.py	2008-08-22 06:02:01 UTC (rev 16221)
@@ -14,8 +14,7 @@
 	
 	Examples::
 		# To get a controller:
-		import GameLogic
-		co = GameLogic.getCurrentController()
+		co = GameLogic.getCurrentController() # GameLogic is automatically imported
 		
 		# To get the game object associated with this controller:
 		obj = co.getOwner()
@@ -237,3 +236,13 @@
 	@return: The converted string
 	@rtype: string
 	"""
+
+def getBlendFileList(path = "//"):
+	"""
+	Returns a list of blend files in the same directory as the open blend file, or from using the option argument.
+
+	@param path: Optional directory argument, will be expanded (like expandPath) into the full path.
+	@type path: string
+	@return: A list of filenames, with no directory prefix
+	@rtype: list
+	"""
\ No newline at end of file





More information about the Bf-blender-cvs mailing list