diff options
author | Campbell Barton <ideasman42@gmail.com> | 2009-06-16 11:16:51 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2009-06-16 11:16:51 +0400 |
commit | 2ecbe1c81cec6f537aa10829e94fbc451f9bc823 (patch) | |
tree | feba81632819f672cc62b118b5f73e2e2193d2cf /source | |
parent | 6efd2e643953b293aaf7ef0de50d08badd2f49a8 (diff) |
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
Diffstat (limited to 'source')
-rw-r--r-- | source/blender/python/api2_2x/BGL.c | 1 | ||||
-rw-r--r-- | source/blender/python/api2_2x/Geometry.c | 1 | ||||
-rw-r--r-- | source/blender/python/api2_2x/Mathutils.c | 1 | ||||
-rw-r--r-- | source/gameengine/Expressions/InputParser.cpp | 60 | ||||
-rw-r--r-- | source/gameengine/Expressions/Value.cpp | 48 | ||||
-rw-r--r-- | source/gameengine/Ketsji/KX_PyConstraintBinding.cpp | 1 | ||||
-rw-r--r-- | source/gameengine/Ketsji/KX_PythonInit.cpp | 33 | ||||
-rw-r--r-- | source/gameengine/PyDoc/GameLogic.py | 21 | ||||
-rw-r--r-- | source/gameengine/VideoTexture/blendVideoTex.cpp | 1 |
9 files changed, 55 insertions, 112 deletions
diff --git a/source/blender/python/api2_2x/BGL.c b/source/blender/python/api2_2x/BGL.c index 7735f2b444f..93e110dfbde 100644 --- a/source/blender/python/api2_2x/BGL.c +++ b/source/blender/python/api2_2x/BGL.c @@ -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 diff --git a/source/blender/python/api2_2x/Geometry.c b/source/blender/python/api2_2x/Geometry.c index f875cbc52fb..e5679ea6ed7 100644 --- a/source/blender/python/api2_2x/Geometry.c +++ b/source/blender/python/api2_2x/Geometry.c @@ -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 diff --git a/source/blender/python/api2_2x/Mathutils.c b/source/blender/python/api2_2x/Mathutils.c index 8f99723e12d..3ffc8662cc9 100644 --- a/source/blender/python/api2_2x/Mathutils.c +++ b/source/blender/python/api2_2x/Mathutils.c @@ -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 diff --git a/source/gameengine/Expressions/InputParser.cpp b/source/gameengine/Expressions/InputParser.cpp index b15b206a38a..96a52aec028 100644 --- a/source/gameengine/Expressions/InputParser.cpp +++ b/source/gameengine/Expressions/InputParser.cpp @@ -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 - } - } -} - diff --git a/source/gameengine/Expressions/Value.cpp b/source/gameengine/Expressions/Value.cpp index e6ef9733da8..61dabff510b 100644 --- a/source/gameengine/Expressions/Value.cpp +++ b/source/gameengine/Expressions/Value.cpp @@ -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 /////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/source/gameengine/Ketsji/KX_PyConstraintBinding.cpp b/source/gameengine/Ketsji/KX_PyConstraintBinding.cpp index a098d99864f..4ec901a2f5e 100644 --- a/source/gameengine/Ketsji/KX_PyConstraintBinding.cpp +++ b/source/gameengine/Ketsji/KX_PyConstraintBinding.cpp @@ -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, diff --git a/source/gameengine/Ketsji/KX_PythonInit.cpp b/source/gameengine/Ketsji/KX_PythonInit.cpp index b10ea7e1ab5..75b29481e54 100644 --- a/source/gameengine/Ketsji/KX_PythonInit.cpp +++ b/source/gameengine/Ketsji/KX_PythonInit.cpp @@ -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'; } diff --git a/source/gameengine/PyDoc/GameLogic.py b/source/gameengine/PyDoc/GameLogic.py index 3ec30a63c58..46f00fa7ea6 100644 --- a/source/gameengine/PyDoc/GameLogic.py +++ b/source/gameengine/PyDoc/GameLogic.py @@ -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(): """ diff --git a/source/gameengine/VideoTexture/blendVideoTex.cpp b/source/gameengine/VideoTexture/blendVideoTex.cpp index 1dcc72c8f7d..dad52a426b6 100644 --- a/source/gameengine/VideoTexture/blendVideoTex.cpp +++ b/source/gameengine/VideoTexture/blendVideoTex.cpp @@ -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.", |