From 8518f6e6603ed93d126771e1b372021ae0b4ff98 Mon Sep 17 00:00:00 2001 From: Benoit Bolsee Date: Tue, 31 Mar 2009 19:02:01 +0000 Subject: BGE API cleanup, patch from Moguri: RaySensor, NetworkMessageActuator, NetworkMessageSensor. --- source/gameengine/Ketsji/KX_RaySensor.cpp | 86 ++++++++++++++++++++++++++++--- 1 file changed, 78 insertions(+), 8 deletions(-) (limited to 'source/gameengine/Ketsji/KX_RaySensor.cpp') diff --git a/source/gameengine/Ketsji/KX_RaySensor.cpp b/source/gameengine/Ketsji/KX_RaySensor.cpp index ce12b983147..253fe11fe05 100644 --- a/source/gameengine/Ketsji/KX_RaySensor.cpp +++ b/source/gameengine/Ketsji/KX_RaySensor.cpp @@ -136,8 +136,13 @@ bool KX_RaySensor::RayHit(KX_ClientObjectInfo* client, KX_RayCast* result, void { m_rayHit = true; m_hitObject = hitKXObj; - m_hitPosition = result->m_hitPoint; - m_hitNormal = result->m_hitNormal; + m_hitPosition[0] = result->m_hitPoint[0]; + m_hitPosition[1] = result->m_hitPoint[1]; + m_hitPosition[2] = result->m_hitPoint[2]; + + m_hitNormal[0] = result->m_hitNormal[0]; + m_hitNormal[1] = result->m_hitNormal[1]; + m_hitNormal[2] = result->m_hitNormal[2]; } // no multi-hit search yet @@ -180,8 +185,13 @@ bool KX_RaySensor::Evaluate(CValue* event) bool reset = m_reset && m_level; m_rayHit = false; m_hitObject = NULL; - m_hitPosition.setValue(0,0,0); - m_hitNormal.setValue(1,0,0); + m_hitPosition[0] = 0; + m_hitPosition[1] = 0; + m_hitPosition[2] = 0; + + m_hitNormal[0] = 1; + m_hitNormal[1] = 0; + m_hitNormal[2] = 0; KX_GameObject* obj = (KX_GameObject*)GetParent(); MT_Point3 frompoint = obj->NodeGetWorldPosition(); @@ -236,7 +246,9 @@ bool KX_RaySensor::Evaluate(CValue* event) } } todir.normalize(); - m_rayDirection = todir; + m_rayDirection[0] = todir[0]; + m_rayDirection[1] = todir[1]; + m_rayDirection[2] = todir[2]; MT_Point3 topoint = frompoint + (m_distance) * todir; PHY_IPhysicsEnvironment* pe = m_scene->GetPhysicsEnvironment(); @@ -336,22 +348,44 @@ PyParentObject KX_RaySensor::Parents[] = { }; PyMethodDef KX_RaySensor::Methods[] = { + // Deprecated -----> {"getHitObject",(PyCFunction) KX_RaySensor::sPyGetHitObject,METH_NOARGS, (PY_METHODCHAR)GetHitObject_doc}, {"getHitPosition",(PyCFunction) KX_RaySensor::sPyGetHitPosition,METH_NOARGS, (PY_METHODCHAR)GetHitPosition_doc}, {"getHitNormal",(PyCFunction) KX_RaySensor::sPyGetHitNormal,METH_NOARGS, (PY_METHODCHAR)GetHitNormal_doc}, {"getRayDirection",(PyCFunction) KX_RaySensor::sPyGetRayDirection,METH_NOARGS, (PY_METHODCHAR)GetRayDirection_doc}, + // <----- {NULL,NULL} //Sentinel }; PyAttributeDef KX_RaySensor::Attributes[] = { + KX_PYATTRIBUTE_BOOL_RW("useMaterial", KX_RaySensor, m_bFindMaterial), + KX_PYATTRIBUTE_BOOL_RW("useXRay", KX_RaySensor, m_bXRay), + KX_PYATTRIBUTE_FLOAT_RW("range", 0, 10000, KX_RaySensor, m_distance), + KX_PYATTRIBUTE_STRING_RW("property", 0, 100, false, KX_RaySensor, m_propertyname), + KX_PYATTRIBUTE_INT_RW("axis", 0, 5, true, KX_RaySensor, m_axis), + KX_PYATTRIBUTE_FLOAT_ARRAY_RO("hitPosition", KX_RaySensor, m_hitPosition, 3), + KX_PYATTRIBUTE_FLOAT_ARRAY_RO("rayDirection", KX_RaySensor, m_rayDirection, 3), + KX_PYATTRIBUTE_FLOAT_ARRAY_RO("hitNormal", KX_RaySensor, m_hitNormal, 3), + KX_PYATTRIBUTE_RO_FUNCTION("hitObject", KX_RaySensor, pyattr_get_hitobject), { NULL } //Sentinel }; +PyObject* KX_RaySensor::pyattr_get_hitobject(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef) +{ + KX_RaySensor* self = static_cast(self_v); + if (self->m_hitObject) + return self->m_hitObject->AddRef(); + + Py_RETURN_NONE; +} + +// Deprecated -----> const char KX_RaySensor::GetHitObject_doc[] = "getHitObject()\n" "\tReturns the name of the object that was hit by this ray.\n"; PyObject* KX_RaySensor::PyGetHitObject(PyObject* self) { + ShowDeprecationWarning("getHitObject()", "the hitObject property"); if (m_hitObject) { return m_hitObject->AddRef(); @@ -365,7 +399,15 @@ const char KX_RaySensor::GetHitPosition_doc[] = "\tReturns the position (in worldcoordinates) where the object was hit by this ray.\n"; PyObject* KX_RaySensor::PyGetHitPosition(PyObject* self) { - return PyObjectFrom(m_hitPosition); + ShowDeprecationWarning("getHitPosition()", "the hitPosition property"); + + PyObject *retVal = PyList_New(3); + + PyList_SetItem(retVal, 0, PyFloat_FromDouble(m_hitPosition[0])); + PyList_SetItem(retVal, 1, PyFloat_FromDouble(m_hitPosition[1])); + PyList_SetItem(retVal, 2, PyFloat_FromDouble(m_hitPosition[2])); + + return retVal; } const char KX_RaySensor::GetRayDirection_doc[] = @@ -373,7 +415,15 @@ const char KX_RaySensor::GetRayDirection_doc[] = "\tReturns the direction from the ray (in worldcoordinates) .\n"; PyObject* KX_RaySensor::PyGetRayDirection(PyObject* self) { - return PyObjectFrom(m_rayDirection); + ShowDeprecationWarning("getRayDirection()", "the rayDirection property"); + + PyObject *retVal = PyList_New(3); + + PyList_SetItem(retVal, 0, PyFloat_FromDouble(m_rayDirection[0])); + PyList_SetItem(retVal, 1, PyFloat_FromDouble(m_rayDirection[1])); + PyList_SetItem(retVal, 2, PyFloat_FromDouble(m_rayDirection[2])); + + return retVal; } const char KX_RaySensor::GetHitNormal_doc[] = @@ -381,11 +431,31 @@ const char KX_RaySensor::GetHitNormal_doc[] = "\tReturns the normal (in worldcoordinates) of the object at the location where the object was hit by this ray.\n"; PyObject* KX_RaySensor::PyGetHitNormal(PyObject* self) { - return PyObjectFrom(m_hitNormal); + ShowDeprecationWarning("getHitNormal()", "the hitNormal property"); + + PyObject *retVal = PyList_New(3); + + PyList_SetItem(retVal, 0, PyFloat_FromDouble(m_hitNormal[0])); + PyList_SetItem(retVal, 1, PyFloat_FromDouble(m_hitNormal[1])); + PyList_SetItem(retVal, 2, PyFloat_FromDouble(m_hitNormal[2])); + + return retVal; } PyObject* KX_RaySensor::_getattr(const char *attr) { + PyObject* object = _getattr_self(Attributes, this, attr); + if (object != NULL) + return object; _getattr_up(SCA_ISensor); } + +int KX_RaySensor::_setattr(const char *attr, PyObject *value) { + int ret = _setattr_self(Attributes, this, attr, value); + if (ret >= 0) + return ret; + return SCA_ISensor::_setattr(attr, value); +} + +// <----- Deprecated \ No newline at end of file -- cgit v1.2.3 From d573e9c5390a438b6e606a12d05dc2c6ad06a174 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Fri, 3 Apr 2009 04:12:20 +0000 Subject: BGE Python api Added the method into the PyType so python knows about the methods (its supposed to work this way). This means in the future the api can use PyType_Ready() to store the methods in the types dictionary. Python3 removes Py_FindMethod and we should not be using it anyway since its not that efficient. --- source/gameengine/Ketsji/KX_RaySensor.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'source/gameengine/Ketsji/KX_RaySensor.cpp') diff --git a/source/gameengine/Ketsji/KX_RaySensor.cpp b/source/gameengine/Ketsji/KX_RaySensor.cpp index 253fe11fe05..fdaf48218b3 100644 --- a/source/gameengine/Ketsji/KX_RaySensor.cpp +++ b/source/gameengine/Ketsji/KX_RaySensor.cpp @@ -330,13 +330,11 @@ PyTypeObject KX_RaySensor::Type = { 0, __getattr, __setattr, - 0, //&MyPyCompare, - __repr, - 0, //&cvalue_as_number, - 0, - 0, 0, - 0 + __repr, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + Methods + }; PyParentObject KX_RaySensor::Parents[] = { -- cgit v1.2.3 From fd2b1156783d52dbb7c93c53fe008d9e14cbffdd Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Fri, 3 Apr 2009 14:51:06 +0000 Subject: Python BGE API - Initialize python types with PyType_Ready, which adds methods to the type dictionary. - use Pythons get/setattro (uses a python string for the attribute rather then char*). Using basic C strings seems nice but internally python converts them to python strings and discards them for most functions that accept char arrays. - Method lookups use the PyTypes dictionary (should be faster then Py_FindMethod) - Renamed __getattr -> py_base_getattro, _getattr -> py_getattro, __repr -> py_base_repr, py_delattro, py_getattro_self etc. From here is possible to put all the parent classes methods into each python types dictionary to avoid nested lookups (api has 4 levels of lookups in some places), tested this but its not ready yet. Simple tests for getting a method within a loop show this to be between 0.5 and 3.2x faster then using Py_FindMethod() --- source/gameengine/Ketsji/KX_RaySensor.cpp | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) (limited to 'source/gameengine/Ketsji/KX_RaySensor.cpp') diff --git a/source/gameengine/Ketsji/KX_RaySensor.cpp b/source/gameengine/Ketsji/KX_RaySensor.cpp index fdaf48218b3..cdcb5d74f30 100644 --- a/source/gameengine/Ketsji/KX_RaySensor.cpp +++ b/source/gameengine/Ketsji/KX_RaySensor.cpp @@ -321,18 +321,21 @@ bool KX_RaySensor::Evaluate(CValue* event) /* Integration hooks ------------------------------------------------------- */ PyTypeObject KX_RaySensor::Type = { - PyObject_HEAD_INIT(&PyType_Type) + PyObject_HEAD_INIT(NULL) 0, "KX_RaySensor", sizeof(KX_RaySensor), 0, PyDestructor, 0, - __getattr, - __setattr, 0, - __repr, - 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0, + 0, + py_base_repr, + 0,0,0,0,0,0, + py_base_getattro, + py_base_setattro, + 0,0,0,0,0,0,0,0,0, Methods }; @@ -442,18 +445,18 @@ PyObject* KX_RaySensor::PyGetHitNormal(PyObject* self) -PyObject* KX_RaySensor::_getattr(const char *attr) { - PyObject* object = _getattr_self(Attributes, this, attr); +PyObject* KX_RaySensor::py_getattro(PyObject *attr) { + PyObject* object = py_getattro_self(Attributes, this, attr); if (object != NULL) return object; - _getattr_up(SCA_ISensor); + py_getattro_up(SCA_ISensor); } -int KX_RaySensor::_setattr(const char *attr, PyObject *value) { - int ret = _setattr_self(Attributes, this, attr, value); +int KX_RaySensor::py_setattro(PyObject *attr, PyObject *value) { + int ret = py_setattro_self(Attributes, this, attr, value); if (ret >= 0) return ret; - return SCA_ISensor::_setattr(attr, value); + return SCA_ISensor::py_setattro(attr, value); } // <----- Deprecated \ No newline at end of file -- cgit v1.2.3 From 5d64dd019e7e8150db40505097d1b4048f4e0153 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Tue, 7 Apr 2009 11:06:35 +0000 Subject: BGE Python API Use each types dictionary to store attributes PyAttributeDef's so it uses pythons hash lookup (which it was already doing for methods) rather then doing a string lookup on the array each time. This also means attributes can be found in the type without having to do a dir() on the instance. --- source/gameengine/Ketsji/KX_RaySensor.cpp | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) (limited to 'source/gameengine/Ketsji/KX_RaySensor.cpp') diff --git a/source/gameengine/Ketsji/KX_RaySensor.cpp b/source/gameengine/Ketsji/KX_RaySensor.cpp index cdcb5d74f30..42e2fdc3e14 100644 --- a/source/gameengine/Ketsji/KX_RaySensor.cpp +++ b/source/gameengine/Ketsji/KX_RaySensor.cpp @@ -446,17 +446,11 @@ PyObject* KX_RaySensor::PyGetHitNormal(PyObject* self) PyObject* KX_RaySensor::py_getattro(PyObject *attr) { - PyObject* object = py_getattro_self(Attributes, this, attr); - if (object != NULL) - return object; py_getattro_up(SCA_ISensor); } int KX_RaySensor::py_setattro(PyObject *attr, PyObject *value) { - int ret = py_setattro_self(Attributes, this, attr, value); - if (ret >= 0) - return ret; - return SCA_ISensor::py_setattro(attr, value); + py_setattro_up(SCA_ISensor); } -// <----- Deprecated \ No newline at end of file +// <----- Deprecated -- cgit v1.2.3 From 8d2cb5bea44f4245dd17f2d82cbd0251d8090fd5 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Sun, 19 Apr 2009 12:46:39 +0000 Subject: BGE Python API This changes how the BGE classes and Python work together, which hasnt changed since blender went opensource. The main difference is PyObjectPlus - the base class for most game engine classes, no longer inherit from PyObject, and cannot be cast to a PyObject. This has the advantage that the BGE does not have to keep 2 reference counts valid for C++ and Python. Previously C++ classes would never be freed while python held a reference, however this reference could be problematic eg: a GameObject that isnt in a scene anymore should not be used by python, doing so could even crash blender in some cases. Instead PyObjectPlus has a member "PyObject *m_proxy" which is lazily initialized when python needs it. m_proxy reference counts are managed by python, though it should never be freed while the C++ class exists since it holds a reference to avoid making and freeing it all the time. When the C++ class is free'd it sets the m_proxy reference to NULL, If python accesses this variable it will raise a RuntimeError, (check the isValid attribute to see if its valid without raising an error). - This replaces the m_zombie bool and IsZombie() tests added recently. In python return values that used to be.. return value->AddRef(); Are now return value->GetProxy(); or... return value->NewProxy(true); // true means python owns this C++ value which will be deleted when the PyObject is freed --- source/gameengine/Ketsji/KX_RaySensor.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'source/gameengine/Ketsji/KX_RaySensor.cpp') diff --git a/source/gameengine/Ketsji/KX_RaySensor.cpp b/source/gameengine/Ketsji/KX_RaySensor.cpp index 42e2fdc3e14..9dfc8243330 100644 --- a/source/gameengine/Ketsji/KX_RaySensor.cpp +++ b/source/gameengine/Ketsji/KX_RaySensor.cpp @@ -375,7 +375,7 @@ PyObject* KX_RaySensor::pyattr_get_hitobject(void *self_v, const KX_PYATTRIBUTE_ { KX_RaySensor* self = static_cast(self_v); if (self->m_hitObject) - return self->m_hitObject->AddRef(); + return self->m_hitObject->GetProxy(); Py_RETURN_NONE; } @@ -389,7 +389,7 @@ PyObject* KX_RaySensor::PyGetHitObject(PyObject* self) ShowDeprecationWarning("getHitObject()", "the hitObject property"); if (m_hitObject) { - return m_hitObject->AddRef(); + return m_hitObject->GetProxy(); } Py_RETURN_NONE; } -- cgit v1.2.3 From 7dbc9dc719c3eb0823e4f9e7ae94a479f9427ea7 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Sun, 19 Apr 2009 14:57:52 +0000 Subject: BGE Python API cleanup - no functionality changes - comments to PyObjectPlus.h - remove unused/commented junk. - renamed PyDestructor to py_base_dealloc for consistency - all the PyTypeObject's were still using the sizeof() their class, can use sizeof(PyObjectPlus_Proxy) now which is smaller too. --- source/gameengine/Ketsji/KX_RaySensor.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'source/gameengine/Ketsji/KX_RaySensor.cpp') diff --git a/source/gameengine/Ketsji/KX_RaySensor.cpp b/source/gameengine/Ketsji/KX_RaySensor.cpp index 9dfc8243330..080a217b9bd 100644 --- a/source/gameengine/Ketsji/KX_RaySensor.cpp +++ b/source/gameengine/Ketsji/KX_RaySensor.cpp @@ -324,9 +324,9 @@ PyTypeObject KX_RaySensor::Type = { PyObject_HEAD_INIT(NULL) 0, "KX_RaySensor", - sizeof(KX_RaySensor), + sizeof(PyObjectPlus_Proxy), 0, - PyDestructor, + py_base_dealloc, 0, 0, 0, -- cgit v1.2.3 From 6bc162e679d8b52b28e205de76985a1735abbf0a Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Sun, 19 Apr 2009 17:29:07 +0000 Subject: BGE Python API removed redundant (PyObject *self) argument from python functions that are not exposed to python directly. --- source/gameengine/Ketsji/KX_RaySensor.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'source/gameengine/Ketsji/KX_RaySensor.cpp') diff --git a/source/gameengine/Ketsji/KX_RaySensor.cpp b/source/gameengine/Ketsji/KX_RaySensor.cpp index 080a217b9bd..06c04dbf10d 100644 --- a/source/gameengine/Ketsji/KX_RaySensor.cpp +++ b/source/gameengine/Ketsji/KX_RaySensor.cpp @@ -384,7 +384,7 @@ PyObject* KX_RaySensor::pyattr_get_hitobject(void *self_v, const KX_PYATTRIBUTE_ const char KX_RaySensor::GetHitObject_doc[] = "getHitObject()\n" "\tReturns the name of the object that was hit by this ray.\n"; -PyObject* KX_RaySensor::PyGetHitObject(PyObject* self) +PyObject* KX_RaySensor::PyGetHitObject() { ShowDeprecationWarning("getHitObject()", "the hitObject property"); if (m_hitObject) @@ -398,7 +398,7 @@ PyObject* KX_RaySensor::PyGetHitObject(PyObject* self) const char KX_RaySensor::GetHitPosition_doc[] = "getHitPosition()\n" "\tReturns the position (in worldcoordinates) where the object was hit by this ray.\n"; -PyObject* KX_RaySensor::PyGetHitPosition(PyObject* self) +PyObject* KX_RaySensor::PyGetHitPosition() { ShowDeprecationWarning("getHitPosition()", "the hitPosition property"); @@ -414,7 +414,7 @@ PyObject* KX_RaySensor::PyGetHitPosition(PyObject* self) const char KX_RaySensor::GetRayDirection_doc[] = "getRayDirection()\n" "\tReturns the direction from the ray (in worldcoordinates) .\n"; -PyObject* KX_RaySensor::PyGetRayDirection(PyObject* self) +PyObject* KX_RaySensor::PyGetRayDirection() { ShowDeprecationWarning("getRayDirection()", "the rayDirection property"); @@ -430,7 +430,7 @@ PyObject* KX_RaySensor::PyGetRayDirection(PyObject* self) const char KX_RaySensor::GetHitNormal_doc[] = "getHitNormal()\n" "\tReturns the normal (in worldcoordinates) of the object at the location where the object was hit by this ray.\n"; -PyObject* KX_RaySensor::PyGetHitNormal(PyObject* self) +PyObject* KX_RaySensor::PyGetHitNormal() { ShowDeprecationWarning("getHitNormal()", "the hitNormal property"); -- cgit v1.2.3