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_BlenderMaterial.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'source/gameengine/Ketsji/KX_BlenderMaterial.cpp') diff --git a/source/gameengine/Ketsji/KX_BlenderMaterial.cpp b/source/gameengine/Ketsji/KX_BlenderMaterial.cpp index b9bd7647f89..668873883af 100644 --- a/source/gameengine/Ketsji/KX_BlenderMaterial.cpp +++ b/source/gameengine/Ketsji/KX_BlenderMaterial.cpp @@ -764,7 +764,8 @@ PyTypeObject KX_BlenderMaterial::Type = { __setattr, 0, __repr, - 0 + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + Methods }; -- 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_BlenderMaterial.cpp | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) (limited to 'source/gameengine/Ketsji/KX_BlenderMaterial.cpp') diff --git a/source/gameengine/Ketsji/KX_BlenderMaterial.cpp b/source/gameengine/Ketsji/KX_BlenderMaterial.cpp index 668873883af..bd137196ac6 100644 --- a/source/gameengine/Ketsji/KX_BlenderMaterial.cpp +++ b/source/gameengine/Ketsji/KX_BlenderMaterial.cpp @@ -753,37 +753,40 @@ PyAttributeDef KX_BlenderMaterial::Attributes[] = { }; PyTypeObject KX_BlenderMaterial::Type = { - PyObject_HEAD_INIT(&PyType_Type) + PyObject_HEAD_INIT(NULL) 0, "KX_BlenderMaterial", sizeof(KX_BlenderMaterial), 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 }; PyParentObject KX_BlenderMaterial::Parents[] = { - &PyObjectPlus::Type, &KX_BlenderMaterial::Type, + &PyObjectPlus::Type, NULL }; -PyObject* KX_BlenderMaterial::_getattr(const char *attr) +PyObject* KX_BlenderMaterial::py_getattro(PyObject *attr) { - _getattr_up(PyObjectPlus); + py_getattro_up(PyObjectPlus); } -int KX_BlenderMaterial::_setattr(const char *attr, PyObject *pyvalue) +int KX_BlenderMaterial::py_setattro(PyObject *attr, PyObject *pyvalue) { - return PyObjectPlus::_setattr(attr, pyvalue); + return PyObjectPlus::py_setattro(attr, pyvalue); } -- cgit v1.2.3 From 2fff90bbb4a922f454c15dbf2d6215bd4d8c519c Mon Sep 17 00:00:00 2001 From: Andre Susano Pinto Date: Fri, 10 Apr 2009 16:45:19 +0000 Subject: Added function name to many of the PyArg_ParseTuple calls in gameengine This way python raises more useful messages. --- source/gameengine/Ketsji/KX_BlenderMaterial.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source/gameengine/Ketsji/KX_BlenderMaterial.cpp') diff --git a/source/gameengine/Ketsji/KX_BlenderMaterial.cpp b/source/gameengine/Ketsji/KX_BlenderMaterial.cpp index bd137196ac6..0e417dde5d2 100644 --- a/source/gameengine/Ketsji/KX_BlenderMaterial.cpp +++ b/source/gameengine/Ketsji/KX_BlenderMaterial.cpp @@ -897,7 +897,7 @@ static unsigned int GL_array[11] = { KX_PYMETHODDEF_DOC( KX_BlenderMaterial, setBlending , "setBlending( GameLogic.src, GameLogic.dest)") { unsigned int b[2]; - if(PyArg_ParseTuple(args, "ii", &b[0], &b[1])) + if(PyArg_ParseTuple(args, "ii:setBlending", &b[0], &b[1])) { bool value_found[2] = {false, false}; for(int i=0; i<11; i++) -- 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_BlenderMaterial.cpp | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) (limited to 'source/gameengine/Ketsji/KX_BlenderMaterial.cpp') diff --git a/source/gameengine/Ketsji/KX_BlenderMaterial.cpp b/source/gameengine/Ketsji/KX_BlenderMaterial.cpp index 0e417dde5d2..bdad21f76eb 100644 --- a/source/gameengine/Ketsji/KX_BlenderMaterial.cpp +++ b/source/gameengine/Ketsji/KX_BlenderMaterial.cpp @@ -827,8 +827,7 @@ KX_PYMETHODDEF_DOC( KX_BlenderMaterial, getShader , "getShader()") m_flag &= ~RAS_BLENDERGLSL; mMaterial->SetSharedMaterial(true); mScene->GetBucketManager()->ReleaseDisplayLists(this); - Py_INCREF(mShader); - return mShader; + return mShader->GetProxy(); }else { // decref all references to the object @@ -836,13 +835,8 @@ KX_PYMETHODDEF_DOC( KX_BlenderMaterial, getShader , "getShader()") // We will then go back to fixed functionality // for this material if(mShader) { - if(mShader->ob_refcnt > 1) { - Py_DECREF(mShader); - } - else { - delete mShader; - mShader=0; - } + delete mShader; /* will handle python de-referencing */ + mShader=0; } } 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_BlenderMaterial.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'source/gameengine/Ketsji/KX_BlenderMaterial.cpp') diff --git a/source/gameengine/Ketsji/KX_BlenderMaterial.cpp b/source/gameengine/Ketsji/KX_BlenderMaterial.cpp index bdad21f76eb..6722cd232fe 100644 --- a/source/gameengine/Ketsji/KX_BlenderMaterial.cpp +++ b/source/gameengine/Ketsji/KX_BlenderMaterial.cpp @@ -756,9 +756,9 @@ PyTypeObject KX_BlenderMaterial::Type = { PyObject_HEAD_INIT(NULL) 0, "KX_BlenderMaterial", - sizeof(KX_BlenderMaterial), + sizeof(PyObjectPlus_Proxy), 0, - PyDestructor, + py_base_dealloc, 0, 0, 0, -- cgit v1.2.3 From f5fc4ebdd8ede5263f4b34f161ebe139d40466dc Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Sun, 19 Apr 2009 21:01:12 +0000 Subject: BGE Python API - More verbose error messages. - BL_Shader wasnt setting error messages on some errors - FilterNormal depth attribute was checking for float which is bad because scripts often expect ints assigned to float attributes. - Added a check to PyVecTo for a tuple rather then always using a generic python sequence. On my system this is over 2x faster with an optmized build. --- source/gameengine/Ketsji/KX_BlenderMaterial.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'source/gameengine/Ketsji/KX_BlenderMaterial.cpp') diff --git a/source/gameengine/Ketsji/KX_BlenderMaterial.cpp b/source/gameengine/Ketsji/KX_BlenderMaterial.cpp index 6722cd232fe..849332008ce 100644 --- a/source/gameengine/Ketsji/KX_BlenderMaterial.cpp +++ b/source/gameengine/Ketsji/KX_BlenderMaterial.cpp @@ -841,7 +841,7 @@ KX_PYMETHODDEF_DOC( KX_BlenderMaterial, getShader , "getShader()") } Py_RETURN_NONE; } - PyErr_Format(PyExc_ValueError, "GLSL Error"); + PyErr_SetString(PyExc_ValueError, "material.getShader(): KX_BlenderMaterial, GLSL Error"); return NULL; } @@ -907,7 +907,7 @@ KX_PYMETHODDEF_DOC( KX_BlenderMaterial, setBlending , "setBlending( GameLogic.sr if(value_found[0] && value_found[1]) break; } if(!value_found[0] || !value_found[1]) { - PyErr_Format(PyExc_ValueError, "invalid enum."); + PyErr_SetString(PyExc_ValueError, "material.setBlending(int, int): KX_BlenderMaterial, invalid enum."); return NULL; } mUserDefBlend = true; -- cgit v1.2.3