[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [20922] trunk/blender/source: BGE Py API

Campbell Barton ideasman42 at gmail.com
Tue Jun 16 09:16:51 CEST 2009


Revision: 20922
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=20922
Author:   campbellbarton
Date:     2009-06-16 09:16:51 +0200 (Tue, 16 Jun 2009)

Log Message:
-----------
BGE Py API
* Removed modules Expression and CValue, neither were ever available.
* Added GameLogic.EvalExpression(exp) from the Expression module, evaluates an expression like the expression controller (not sure if this is really that useful since python is far more advanced).
* resetting the original blend file path didint work (own fault == -> =)
* Py3.x PyModule_Create didnt allow importing since it didn't add to sys.modules,
  Looks like they want us to use init-tab array, but this doesn't suit us since
  it needs to be setup before python is initialized.
* Documented GameLogic.globalDict

Modified Paths:
--------------
    trunk/blender/source/blender/python/api2_2x/BGL.c
    trunk/blender/source/blender/python/api2_2x/Geometry.c
    trunk/blender/source/blender/python/api2_2x/Mathutils.c
    trunk/blender/source/gameengine/Expressions/InputParser.cpp
    trunk/blender/source/gameengine/Expressions/Value.cpp
    trunk/blender/source/gameengine/Ketsji/KX_PyConstraintBinding.cpp
    trunk/blender/source/gameengine/Ketsji/KX_PythonInit.cpp
    trunk/blender/source/gameengine/PyDoc/GameLogic.py
    trunk/blender/source/gameengine/VideoTexture/blendVideoTex.cpp

Modified: trunk/blender/source/blender/python/api2_2x/BGL.c
===================================================================
--- trunk/blender/source/blender/python/api2_2x/BGL.c	2009-06-16 06:47:10 UTC (rev 20921)
+++ trunk/blender/source/blender/python/api2_2x/BGL.c	2009-06-16 07:16:51 UTC (rev 20922)
@@ -1104,6 +1104,7 @@
 	PyObject *mod, *dict, *item;
 #if (PY_VERSION_HEX >= 0x03000000)
 	mod = PyModule_Create(&BGL_module_def);
+	PyDict_SetItemString(PySys_GetObject("modules"), BGL_module_def.m_name, mod);
 #else
 	mod= Py_InitModule(from, BGL_methods);
 #endif

Modified: trunk/blender/source/blender/python/api2_2x/Geometry.c
===================================================================
--- trunk/blender/source/blender/python/api2_2x/Geometry.c	2009-06-16 06:47:10 UTC (rev 20921)
+++ trunk/blender/source/blender/python/api2_2x/Geometry.c	2009-06-16 07:16:51 UTC (rev 20922)
@@ -99,6 +99,7 @@
 	
 #if (PY_VERSION_HEX >= 0x03000000)
 	submodule = PyModule_Create(&M_Geometry_module_def);
+	PyDict_SetItemString(PySys_GetObject("modules"), M_Geometry_module_def.m_name, submodule);
 #else
 	submodule = Py_InitModule3(from, M_Geometry_methods, M_Geometry_doc);
 #endif

Modified: trunk/blender/source/blender/python/api2_2x/Mathutils.c
===================================================================
--- trunk/blender/source/blender/python/api2_2x/Mathutils.c	2009-06-16 06:47:10 UTC (rev 20921)
+++ trunk/blender/source/blender/python/api2_2x/Mathutils.c	2009-06-16 07:16:51 UTC (rev 20922)
@@ -139,6 +139,7 @@
 	
 #if (PY_VERSION_HEX >= 0x03000000)
 	submodule = PyModule_Create(&M_Mathutils_module_def);
+	PyDict_SetItemString(PySys_GetObject("modules"), M_Mathutils_module_def.m_name, submodule);
 #else
 	submodule = Py_InitModule3(from, M_Mathutils_methods, M_Mathutils_doc);
 #endif

Modified: trunk/blender/source/gameengine/Expressions/InputParser.cpp
===================================================================
--- trunk/blender/source/gameengine/Expressions/InputParser.cpp	2009-06-16 06:47:10 UTC (rev 20921)
+++ trunk/blender/source/gameengine/Expressions/InputParser.cpp	2009-06-16 07:16:51 UTC (rev 20922)
@@ -636,63 +636,3 @@
 	}
 	m_identifierContext = context;
 }
-
-
-
-
-PyObject*	CParserPyMake(PyObject* ignored,PyObject* args)
-{
-	char* txt;
-	if (!PyArg_ParseTuple(args,"s",&txt))
-		return NULL;
-	CParser parser;
-	CExpression* expr = parser.ProcessText(txt);
-	CValue* val = expr->Calculate();
-	expr->Release();
-	return val->GetProxy();
-}
-
-static PyMethodDef	CParserMethods[] = 
-{
-	{ "calc", CParserPyMake , METH_VARARGS},
-	{ NULL,NULL}	// Sentinel
-};
-
-
-#if (PY_VERSION_HEX >= 0x03000000)
-static struct PyModuleDef Expression_module_def = {
-	{}, /* m_base */
-	"Expression",  /* m_name */
-	0,  /* m_doc */
-	0,  /* m_size */
-	CParserMethods,  /* m_methods */
-	0,  /* m_reload */
-	0,  /* m_traverse */
-	0,  /* m_clear */
-	0,  /* m_free */
-};
-#endif
-
-extern "C" {
-	void initExpressionModule(void)
-	{
-		PyObject *m;
-		/* Use existing module where possible
-		 * be careful not to init any runtime vars after this */
-		m = PyImport_ImportModule( "Expression" );
-		if(m) {
-			Py_DECREF(m);
-			//return m;
-		}
-		else {
-			PyErr_Clear();
-		
-#if (PY_VERSION_HEX >= 0x03000000)
-			PyModule_Create(&Expression_module_def);
-#else
-			Py_InitModule("Expression",CParserMethods);
-#endif
-		}
-	}
-}
-

Modified: trunk/blender/source/gameengine/Expressions/Value.cpp
===================================================================
--- trunk/blender/source/gameengine/Expressions/Value.cpp	2009-06-16 06:47:10 UTC (rev 20921)
+++ trunk/blender/source/gameengine/Expressions/Value.cpp	2009-06-16 07:16:51 UTC (rev 20922)
@@ -725,54 +725,6 @@
 	return pylist;
 }
 
-/*
-PyObject*	CValue::PyMake(PyObject* ignored,PyObject* args)
-{
-
-	//if (!PyArg_ParseTuple(args,"s:make",&name)) return NULL;
-	Py_RETURN_NONE;//new CValue();
-}
-*/
-
-#if (PY_VERSION_HEX >= 0x03000000)
-static struct PyModuleDef CValue_module_def = {
-	{}, /* m_base */
-	"CValue",  /* m_name */
-	0,  /* m_doc */
-	0,  /* m_size */
-	CValueMethods,  /* m_methods */
-	0,  /* m_reload */
-	0,  /* m_traverse */
-	0,  /* m_clear */
-	0,  /* m_free */
-};
-#endif
-
-extern "C" {
-	void initCValue(void)
-	{
-		PyObject *m;
-		/* Use existing module where possible
-		 * be careful not to init any runtime vars after this */
-		m = PyImport_ImportModule( "CValue" );
-		if(m) {
-			Py_DECREF(m);
-			//return m;
-		}
-		else {
-			PyErr_Clear();
-		
-#if (PY_VERSION_HEX >= 0x03000000)
-			PyModule_Create(&CValue_module_def);
-#else
-			Py_InitModule("CValue",CValueMethods);
-#endif
-		}
-	}
-}
-
-
-
 #endif //NO_EXP_PYTHON_EMBEDDING
 
 ///////////////////////////////////////////////////////////////////////////////////////////////

Modified: trunk/blender/source/gameengine/Ketsji/KX_PyConstraintBinding.cpp
===================================================================
--- trunk/blender/source/gameengine/Ketsji/KX_PyConstraintBinding.cpp	2009-06-16 06:47:10 UTC (rev 20921)
+++ trunk/blender/source/gameengine/Ketsji/KX_PyConstraintBinding.cpp	2009-06-16 07:16:51 UTC (rev 20922)
@@ -630,6 +630,7 @@
 	
 #if (PY_VERSION_HEX >= 0x03000000)
 		m = PyModule_Create(&PhysicsConstraints_module_def);
+		PyDict_SetItemString(PySys_GetObject("modules"), PhysicsConstraints_module_def.m_name, m);
 #else
 		m = Py_InitModule4("PhysicsConstraints", physicsconstraints_methods,
 		     PhysicsConstraints_module_documentation,

Modified: trunk/blender/source/gameengine/Ketsji/KX_PythonInit.cpp
===================================================================
--- trunk/blender/source/gameengine/Ketsji/KX_PythonInit.cpp	2009-06-16 06:47:10 UTC (rev 20921)
+++ trunk/blender/source/gameengine/Ketsji/KX_PythonInit.cpp	2009-06-16 07:16:51 UTC (rev 20922)
@@ -70,6 +70,7 @@
 #include "MT_Vector3.h"
 #include "MT_Point3.h"
 #include "ListValue.h"
+#include "InputParser.h"
 #include "KX_Scene.h"
 #include "SND_DeviceManager.h"
 
@@ -498,6 +499,32 @@
 }
 
 
+static PyObject *gEvalExpression(PyObject*, PyObject* value)
+{
+	char* txt= PyString_AsString(value);
+	
+	if (txt==NULL) {
+		PyErr_SetString(PyExc_TypeError, "Expression.calc(text): expects a single string argument");
+		return NULL;
+	}
+	
+	CParser parser;
+	CExpression* expr = parser.ProcessText(txt);
+	CValue* val = expr->Calculate();
+	expr->Release();
+	
+	if (val) {	
+		PyObject* pyobj = val->ConvertValueToPython();
+		if (pyobj)
+			return pyobj;
+		else
+			return val->GetProxy();
+	}
+	
+	Py_RETURN_NONE;
+}
+
+
 static struct PyMethodDef game_methods[] = {
 	{"expandPath", (PyCFunction)gPyExpandPath, METH_VARARGS, (PY_METHODCHAR)gPyExpandPath_doc},
 	{"sendMessage", (PyCFunction)gPySendMessage, METH_VARARGS, (PY_METHODCHAR)gPySendMessage_doc},
@@ -526,6 +553,7 @@
 	{"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"},
+	{"EvalExpression", (PyCFunction)gEvalExpression, METH_O, (PY_METHODCHAR)"Evaluate a string as a game logic expression"},
 	{NULL, (PyCFunction) NULL, 0, NULL }
 };
 
@@ -1032,6 +1060,7 @@
 		// Create the module and add the functions	
 #if (PY_VERSION_HEX >= 0x03000000)
 		m = PyModule_Create(&GameLogic_module_def);
+		PyDict_SetItemString(PySys_GetObject("modules"), GameLogic_module_def.m_name, m);
 #else
 		m = Py_InitModule4("GameLogic", game_methods,
 						   GameLogic_module_documentation,
@@ -1697,6 +1726,7 @@
 		// Create the module and add the functions
 #if (PY_VERSION_HEX >= 0x03000000)
 		m = PyModule_Create(&Rasterizer_module_def);
+		PyDict_SetItemString(PySys_GetObject("modules"), Rasterizer_module_def.m_name, m);
 #else
 		m = Py_InitModule4("Rasterizer", rasterizer_methods,
 		     Rasterizer_module_documentation,
@@ -1831,6 +1861,7 @@
 		// Create the module and add the functions
 #if (PY_VERSION_HEX >= 0x03000000)
 		m = PyModule_Create(&GameKeys_module_def);
+		PyDict_SetItemString(PySys_GetObject("modules"), GameKeys_module_def.m_name, m);
 #else
 		m = Py_InitModule4("GameKeys", gamekeys_methods,
 					   GameKeys_module_documentation,
@@ -2106,5 +2137,5 @@
 // engine but loading blend files within the BGE wont overwrite gp_GamePythonPathOrig
 void resetGamePythonPath()
 {
-	gp_GamePythonPathOrig[0] == '\0';
+	gp_GamePythonPathOrig[0] = '\0';
 }

Modified: trunk/blender/source/gameengine/PyDoc/GameLogic.py
===================================================================
--- trunk/blender/source/gameengine/PyDoc/GameLogic.py	2009-06-16 06:47:10 UTC (rev 20921)
+++ trunk/blender/source/gameengine/PyDoc/GameLogic.py	2009-06-16 07:16:51 UTC (rev 20922)
@@ -271,7 +271,7 @@
 @var KX_PARENT_REMOVE:
 @var KX_PARENT_SET:
 
- at group Shader: MODELMATRIX*, MODELVIEWMATRIX*, VIEWMATRIX*, CAM_POS, CONSTANT_TIMER
+ at group Shader: MODELMATRIX*, MODELVIEWMATRIX*, VIEWMATRIX*, CAM_POS, CONSTANT_TIMER, SHD_TANGENT
 @var VIEWMATRIX:
 @var VIEWMATRIX_INVERSE:
 @var VIEWMATRIX_INVERSETRANSPOSE:
@@ -285,8 +285,8 @@
 @var MODELVIEWMATRIX_INVERSETRANSPOSE:
 @var MODELVIEWMATRIX_TRANSPOSE:
 @var CAM_POS: Current camera position
- at var CONSTANT_TIMER: Current camera position
- at var SHD_TANGENT: Current camera position
+ at var CONSTANT_TIMER: User a timer for the uniform value.
+ at var SHD_TANGENT: Not yet documented.
 
 @group Blender Material: BL_*
 @var BL_DST_ALPHA:
@@ -302,6 +302,13 @@
 @var BL_ZERO:
 
 @group Deprecated: addActiveActuator
+
+ at var globalDict:	A dictionary that is saved between loading blend files so you can use
+					it to store inventory and other variables you want to store between
+					scenes and blend files. It can also be written to a file and loaded
+					later on with the game load/save actuators.
+					note: only python built in types such as int/string/bool/float/tuples/lists
+					can be saved, GameObjects, Actuators etc will not work as expectred.
 """
 
 import GameTypes
@@ -441,6 +448,14 @@
 	@type ticrate: float
 	"""
 
+def EvalExpression(text):
+	"""
+	Evaluate the string as an expression, similar to the expression controller logic brick.
+	@param text: The expression to evaluate.
+	@type text: string
+	@return: The result of the expression. The type depends on the expression.
+	"""
+
 #{ Utility functions
 def getAverageFrameRate():
 	"""


@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list