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
This commit is contained in:
2009-06-16 07:16:51 +00:00
parent 6efd2e6439
commit 2ecbe1c81c
9 changed files with 55 additions and 112 deletions

View File

@@ -1104,6 +1104,7 @@ PyObject *BGL_Init(const char *from)
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

View File

@@ -99,6 +99,7 @@ PyObject *Geometry_Init(const char *from)
#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

View File

@@ -139,6 +139,7 @@ PyObject *Mathutils_Init(const char *from)
#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

View File

@@ -636,63 +636,3 @@ void CParser::SetContext(CValue* context)
}
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
}
}
}

View File

@@ -725,54 +725,6 @@ PyObject* CValue::ConvertKeysToPython( void )
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
///////////////////////////////////////////////////////////////////////////////////////////////

View File

@@ -630,6 +630,7 @@ PyObject* initPythonConstraintBinding()
#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,

View File

@@ -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 *pyPrintExt(PyObject *,PyObject *,PyObject *)
}
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 @@ static struct PyMethodDef game_methods[] = {
{"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 @@ PyObject* initGameLogic(KX_KetsjiEngine *engine, KX_Scene* scene) // quick hack
// 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 @@ PyObject* initRasterizer(RAS_IRasterizer* rasty,RAS_ICanvas* canvas)
// 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 @@ PyObject* initGameKeys()
// 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 @@ void setGamePythonPath(char *path)
// engine but loading blend files within the BGE wont overwrite gp_GamePythonPathOrig
void resetGamePythonPath()
{
gp_GamePythonPathOrig[0] == '\0';
gp_GamePythonPathOrig[0] = '\0';
}

View File

@@ -271,7 +271,7 @@ Documentation for the GameLogic Module.
@var KX_PARENT_REMOVE:
@var KX_PARENT_SET:
@group Shader: MODELMATRIX*, MODELVIEWMATRIX*, VIEWMATRIX*, CAM_POS, CONSTANT_TIMER
@group Shader: MODELMATRIX*, MODELVIEWMATRIX*, VIEWMATRIX*, CAM_POS, CONSTANT_TIMER, SHD_TANGENT
@var VIEWMATRIX:
@var VIEWMATRIX_INVERSE:
@var VIEWMATRIX_INVERSETRANSPOSE:
@@ -285,8 +285,8 @@ Documentation for the GameLogic Module.
@var MODELVIEWMATRIX_INVERSETRANSPOSE:
@var MODELVIEWMATRIX_TRANSPOSE:
@var CAM_POS: Current camera position
@var CONSTANT_TIMER: Current camera position
@var SHD_TANGENT: Current camera position
@var CONSTANT_TIMER: User a timer for the uniform value.
@var SHD_TANGENT: Not yet documented.
@group Blender Material: BL_*
@var BL_DST_ALPHA:
@@ -302,6 +302,13 @@ Documentation for the GameLogic Module.
@var BL_ZERO:
@group Deprecated: addActiveActuator
@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 @@ def setPhysicsTicRate(ticrate):
@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():
"""

View File

@@ -204,6 +204,7 @@ PyObject* initVideoTexture(void)
#if (PY_VERSION_HEX >= 0x03000000)
m = PyModule_Create(&VideoTexture_module_def);
PyDict_SetItemString(PySys_GetObject("modules"), VideoTexture_module_def.m_name, m);
#else
m = Py_InitModule4("VideoTexture", moduleMethods,
"Module that allows to play video files on textures in GameBlender.",