From 5c93e756823cda49466ce7e546268d551fb7de2f Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Fri, 4 Jul 2008 00:05:50 +0000 Subject: non user visible changes and small optimizations to the game engine pyapi as well as fixing some bugs. * 2 returning errors without exception set another return None instead of NULL. * a missing check for parent relation * BPY matrix length was incorrect in matrix.c, this change could break some scripts, however when a script expects a list of lists for a matrix, the len() function is incorrect and will give an error. This was the only thing stopping apricot game logic running in trunk. Also added a function for GameObjects - getAxisVect(vec), multiplies the vector be the objects worldspace rotation matrix. Very useful if you want to know what the forward direction is for an object and dont want to use Blender.Mathutils which is tedious and not available in BlenderPlayer yet. --- .../gameengine/GameLogic/SCA_PythonController.cpp | 79 ++++++++-------------- 1 file changed, 29 insertions(+), 50 deletions(-) (limited to 'source/gameengine/GameLogic/SCA_PythonController.cpp') diff --git a/source/gameengine/GameLogic/SCA_PythonController.cpp b/source/gameengine/GameLogic/SCA_PythonController.cpp index be00117cd21..76386079bdf 100644 --- a/source/gameengine/GameLogic/SCA_PythonController.cpp +++ b/source/gameengine/GameLogic/SCA_PythonController.cpp @@ -144,9 +144,7 @@ static char* sPyGetCurrentController__doc__; #endif -PyObject* SCA_PythonController::sPyGetCurrentController(PyObject* self, - PyObject* args, - PyObject* kwds) +PyObject* SCA_PythonController::sPyGetCurrentController(PyObject* self) { m_sCurrentController->AddRef(); return m_sCurrentController; @@ -159,8 +157,7 @@ static char* sPyAddActiveActuator__doc__; PyObject* SCA_PythonController::sPyAddActiveActuator( PyObject* self, - PyObject* args, - PyObject* kwds) + PyObject* args) { PyObject* ob1; @@ -187,8 +184,7 @@ PyObject* SCA_PythonController::sPyAddActiveActuator( m_sCurrentLogicManager->AddActiveActuator((SCA_IActuator*)act,boolval); boolval->Release(); } - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } @@ -222,17 +218,13 @@ PyParentObject SCA_PythonController::Parents[] = { NULL }; PyMethodDef SCA_PythonController::Methods[] = { - {"getActuators", (PyCFunction) SCA_PythonController::sPyGetActuators, - METH_VARARGS, SCA_PythonController::GetActuators_doc}, - {"getActuator", (PyCFunction) SCA_PythonController::sPyGetActuator, - METH_VARARGS, SCA_PythonController::GetActuator_doc}, - {"getSensors", (PyCFunction) SCA_PythonController::sPyGetSensors, - METH_VARARGS, SCA_PythonController::GetSensors_doc}, - {"getSensor", (PyCFunction) SCA_PythonController::sPyGetSensor, - METH_VARARGS, SCA_PythonController::GetSensor_doc}, - {"getScript", (PyCFunction) SCA_PythonController::sPyGetScript, METH_VARARGS}, - {"setScript", (PyCFunction) SCA_PythonController::sPySetScript, METH_VARARGS}, - {"getState", (PyCFunction) SCA_PythonController::sPyGetState, METH_VARARGS}, + {"getActuators", (PyCFunction) SCA_PythonController::sPyGetActuators, METH_NOARGS, SCA_PythonController::GetActuators_doc}, + {"getActuator", (PyCFunction) SCA_PythonController::sPyGetActuator, METH_O, SCA_PythonController::GetActuator_doc}, + {"getSensors", (PyCFunction) SCA_PythonController::sPyGetSensors, METH_NOARGS, SCA_PythonController::GetSensors_doc}, + {"getSensor", (PyCFunction) SCA_PythonController::sPyGetSensor, METH_O, SCA_PythonController::GetSensor_doc}, + {"getScript", (PyCFunction) SCA_PythonController::sPyGetScript, METH_NOARGS}, + {"setScript", (PyCFunction) SCA_PythonController::sPySetScript, METH_O}, + {"getState", (PyCFunction) SCA_PythonController::sPyGetState, METH_NOARGS}, {NULL,NULL} //Sentinel }; @@ -330,14 +322,12 @@ PyObject* SCA_PythonController::_getattr(const STR_String& attr) -PyObject* SCA_PythonController::PyGetActuators(PyObject* self, - PyObject* args, - PyObject* kwds) +PyObject* SCA_PythonController::PyGetActuators(PyObject* self) { PyObject* resultlist = PyList_New(m_linkedactuators.size()); for (unsigned int index=0;indexAddRef()); + PyList_SET_ITEM(resultlist,index,m_linkedactuators[index]->AddRef()); } return resultlist; @@ -346,14 +336,12 @@ PyObject* SCA_PythonController::PyGetActuators(PyObject* self, char SCA_PythonController::GetSensor_doc[] = "GetSensor (char sensorname) return linked sensor that is named [sensorname]\n"; PyObject* -SCA_PythonController::PyGetSensor(PyObject* self, - PyObject* args, - PyObject* kwds) +SCA_PythonController::PyGetSensor(PyObject* self, PyObject* value) { - char *scriptArg; - - if (!PyArg_ParseTuple(args, "s", &scriptArg)) { + char *scriptArg = PyString_AsString(value); + if (scriptArg==NULL) { + PyErr_SetString(PyExc_TypeError, "expected a string (sensor name)"); return NULL; } @@ -376,14 +364,12 @@ SCA_PythonController::PyGetSensor(PyObject* self, char SCA_PythonController::GetActuator_doc[] = "GetActuator (char sensorname) return linked actuator that is named [actuatorname]\n"; PyObject* -SCA_PythonController::PyGetActuator(PyObject* self, - PyObject* args, - PyObject* kwds) +SCA_PythonController::PyGetActuator(PyObject* self, PyObject* value) { - char *scriptArg; - - if (!PyArg_ParseTuple(args, "s", &scriptArg)) { + char *scriptArg = PyString_AsString(value); + if (scriptArg==NULL) { + PyErr_SetString(PyExc_TypeError, "expected a string (actuator name)"); return NULL; } @@ -404,34 +390,29 @@ SCA_PythonController::PyGetActuator(PyObject* self, char SCA_PythonController::GetSensors_doc[] = "getSensors returns a list of all attached sensors"; PyObject* -SCA_PythonController::PyGetSensors(PyObject* self, - PyObject* args, - PyObject* kwds) +SCA_PythonController::PyGetSensors(PyObject* self) { PyObject* resultlist = PyList_New(m_linkedsensors.size()); for (unsigned int index=0;indexAddRef()); + PyList_SET_ITEM(resultlist,index,m_linkedsensors[index]->AddRef()); } return resultlist; } /* 1. getScript */ -PyObject* SCA_PythonController::PyGetScript(PyObject* self, - PyObject* args, - PyObject* kwds) +PyObject* SCA_PythonController::PyGetScript(PyObject* self) { return PyString_FromString(m_scriptText); } /* 2. setScript */ -PyObject* SCA_PythonController::PySetScript(PyObject* self, - PyObject* args, - PyObject* kwds) +PyObject* SCA_PythonController::PySetScript(PyObject* self, PyObject* value) { - char *scriptArg; - if (!PyArg_ParseTuple(args, "s", &scriptArg)) { + char *scriptArg = PyString_AsString(value); + if (scriptArg==NULL) { + PyErr_SetString(PyExc_TypeError, "expected a string (script name)"); return NULL; } @@ -440,13 +421,11 @@ PyObject* SCA_PythonController::PySetScript(PyObject* self, this->SetScriptText(scriptArg); - Py_Return; + Py_RETURN_NONE; } /* 1. getScript */ -PyObject* SCA_PythonController::PyGetState(PyObject* self, - PyObject* args, - PyObject* kwds) +PyObject* SCA_PythonController::PyGetState(PyObject* self) { return PyInt_FromLong(m_statemask); } -- cgit v1.2.3