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/Expressions/ListValue.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source/gameengine/Expressions/ListValue.h') diff --git a/source/gameengine/Expressions/ListValue.h b/source/gameengine/Expressions/ListValue.h index 104e3e63283..f936298a8c4 100644 --- a/source/gameengine/Expressions/ListValue.h +++ b/source/gameengine/Expressions/ListValue.h @@ -59,7 +59,7 @@ public: bool CheckEqual(CValue* first,CValue* second); - virtual PyObject* _getattr(const char *attr); + virtual PyObject* py_getattro(PyObject* attr); KX_PYMETHOD_O(CListValue,append); KX_PYMETHOD_NOARGS(CListValue,reverse); -- cgit v1.2.3 From 18511c56d3cbfd0b368be61002960cc161cee526 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Tue, 7 Apr 2009 16:00:32 +0000 Subject: BGE Python API (small changes) - Make BGE's ListValue types convert to python lists for printing since the CValue GetText() function didnt work well- printing lists as [,,,,,] for scene objects and mesh materials for eg. - Check attributes are descriptor types before casting. - py_getattr_dict use the Type dict rather then Method and Attribute array. --- source/gameengine/Expressions/ListValue.h | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'source/gameengine/Expressions/ListValue.h') diff --git a/source/gameengine/Expressions/ListValue.h b/source/gameengine/Expressions/ListValue.h index f936298a8c4..1c01c2d221d 100644 --- a/source/gameengine/Expressions/ListValue.h +++ b/source/gameengine/Expressions/ListValue.h @@ -60,6 +60,12 @@ public: bool CheckEqual(CValue* first,CValue* second); virtual PyObject* py_getattro(PyObject* attr); + virtual PyObject* py_repr(void) { + PyObject *py_list= PySequence_List((PyObject *)this); + PyObject *py_string= PyObject_Repr(py_list); + Py_DECREF(py_list); + return py_string; + } KX_PYMETHOD_O(CListValue,append); KX_PYMETHOD_NOARGS(CListValue,reverse); -- cgit v1.2.3 From 4cd088b1059afa2e7b998c184b2c9deecd4be4a9 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Sat, 11 Apr 2009 20:58:09 +0000 Subject: BGE Py API - setting the scene attributes would always add to the scenes custom dictionary. - new CListValue method from_id(id) so you can store a Game Objects id and use it to get the game object back. ob_id = id(gameOb) ... gameOb = scene.objects.from_id(ob_id) This is useful because names are not always unique. --- source/gameengine/Expressions/ListValue.h | 1 + 1 file changed, 1 insertion(+) (limited to 'source/gameengine/Expressions/ListValue.h') diff --git a/source/gameengine/Expressions/ListValue.h b/source/gameengine/Expressions/ListValue.h index 1c01c2d221d..6f70acb9367 100644 --- a/source/gameengine/Expressions/ListValue.h +++ b/source/gameengine/Expressions/ListValue.h @@ -71,6 +71,7 @@ public: KX_PYMETHOD_NOARGS(CListValue,reverse); KX_PYMETHOD_O(CListValue,index); KX_PYMETHOD_O(CListValue,count); + KX_PYMETHOD_O(CListValue,from_id); private: -- cgit v1.2.3 From 33170295c8a2f3eb815b6086f47147113fd3de13 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Sun, 12 Apr 2009 06:41:01 +0000 Subject: use long long rather then int for storing game logic properties. There were also some problems with int to python conversion - assigning a PyLong to a KX_GameObject from python would raise an error - PyLong were coerced into floats when used with internal CValue arithmetic Changes... - PyLong is converted into CIntValue for coercing and assigning from python - CValue's generic GetNumber() function returns a double rather then a float. - Print an error when a PyType cant be coerced into a CValue Tested with python, expressions and property sensor. --- source/gameengine/Expressions/ListValue.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source/gameengine/Expressions/ListValue.h') diff --git a/source/gameengine/Expressions/ListValue.h b/source/gameengine/Expressions/ListValue.h index 6f70acb9367..cf2976c2bbb 100644 --- a/source/gameengine/Expressions/ListValue.h +++ b/source/gameengine/Expressions/ListValue.h @@ -36,7 +36,7 @@ public: virtual CValue* CalcFinal(VALUE_DATA_TYPE dtype, VALUE_OPERATOR op, CValue* val); - virtual float GetNumber(); + virtual double GetNumber(); virtual CValue* GetReplica(); public: -- 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/Expressions/ListValue.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'source/gameengine/Expressions/ListValue.h') diff --git a/source/gameengine/Expressions/ListValue.h b/source/gameengine/Expressions/ListValue.h index cf2976c2bbb..2af5a330c43 100644 --- a/source/gameengine/Expressions/ListValue.h +++ b/source/gameengine/Expressions/ListValue.h @@ -61,9 +61,11 @@ public: virtual PyObject* py_getattro(PyObject* attr); virtual PyObject* py_repr(void) { - PyObject *py_list= PySequence_List((PyObject *)this); + PyObject *py_proxy= this->GetProxy(); + PyObject *py_list= PySequence_List(py_proxy); PyObject *py_string= PyObject_Repr(py_list); Py_DECREF(py_list); + Py_DECREF(py_proxy); return py_string; } -- cgit v1.2.3