[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [19582] trunk/blender/source/gameengine: BGE Python API (small changes)

Campbell Barton ideasman42 at gmail.com
Tue Apr 7 18:00:32 CEST 2009


Revision: 19582
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=19582
Author:   campbellbarton
Date:     2009-04-07 18:00:32 +0200 (Tue, 07 Apr 2009)

Log Message:
-----------
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.

Modified Paths:
--------------
    trunk/blender/source/gameengine/Expressions/ListValue.h
    trunk/blender/source/gameengine/Expressions/PyObjectPlus.cpp
    trunk/blender/source/gameengine/Expressions/PyObjectPlus.h
    trunk/blender/source/gameengine/Ketsji/KX_GameObject.cpp
    trunk/blender/source/gameengine/Ketsji/KX_Scene.cpp

Modified: trunk/blender/source/gameengine/Expressions/ListValue.h
===================================================================
--- trunk/blender/source/gameengine/Expressions/ListValue.h	2009-04-07 15:20:12 UTC (rev 19581)
+++ trunk/blender/source/gameengine/Expressions/ListValue.h	2009-04-07 16:00:32 UTC (rev 19582)
@@ -60,6 +60,12 @@
 	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);

Modified: trunk/blender/source/gameengine/Expressions/PyObjectPlus.cpp
===================================================================
--- trunk/blender/source/gameengine/Expressions/PyObjectPlus.cpp	2009-04-07 15:20:12 UTC (rev 19581)
+++ trunk/blender/source/gameengine/Expressions/PyObjectPlus.cpp	2009-04-07 16:00:32 UTC (rev 19582)
@@ -115,7 +115,7 @@
 	PyObject *descr = PyDict_GetItem(Type.tp_dict, attr); \
 	if (descr == NULL) {
 		if (strcmp(PyString_AsString(attr), "__dict__")==0) {
-			return py_getattr_dict(NULL, Methods, NULL); /* no Attributes yet */
+			return py_getattr_dict(NULL, Type.tp_dict); /* no Attributes yet */
 		}
 		PyErr_SetString(PyExc_AttributeError, "attribute not found");
 		return NULL;
@@ -767,25 +767,14 @@
  * Other then making dir() useful the value returned from __dict__() is not useful
  * since every value is a Py_None
  * */
-PyObject *py_getattr_dict(PyObject *pydict, PyMethodDef *meth, PyAttributeDef *attrdef)
+PyObject *py_getattr_dict(PyObject *pydict, PyObject *tp_dict)
 {
     if(pydict==NULL) { /* incase calling __dict__ on the parent of this object raised an error */
     	PyErr_Clear();
     	pydict = PyDict_New();
     }
 	
-    if(meth) {
-		for (; meth->ml_name != NULL; meth++) {
-			PyDict_SetItemString(pydict, meth->ml_name, Py_None);
-		}
-	}
-	
-    if(attrdef) {
-		for (; attrdef->m_name != NULL; attrdef++) {
-			PyDict_SetItemString(pydict, attrdef->m_name, Py_None);
-		}
-	}
-
+	PyDict_Update(pydict, tp_dict);
 	return pydict;
 }
 

Modified: trunk/blender/source/gameengine/Expressions/PyObjectPlus.h
===================================================================
--- trunk/blender/source/gameengine/Expressions/PyObjectPlus.h	2009-04-07 15:20:12 UTC (rev 19581)
+++ trunk/blender/source/gameengine/Expressions/PyObjectPlus.h	2009-04-07 16:00:32 UTC (rev 19582)
@@ -105,15 +105,18 @@
 	if(descr) { \
 		if (PyCObject_Check(descr)) { \
 			return py_get_attrdef((void *)this, (const PyAttributeDef*)PyCObject_AsVoidPtr(descr)); \
+		} else if (descr->ob_type->tp_descr_get) { \
+			return PyCFunction_New(((PyMethodDescrObject *)descr)->d_method, (PyObject *)this); \
 		} else { \
-			return PyCFunction_New(((PyMethodDescrObject *)descr)->d_method, (PyObject *)this); \
+			fprintf(stderr, "unknown attribute type"); \
+			return descr; \
 		} \
 	} else { \
 		PyErr_Clear(); \
 		PyObject *rvalue= Parent::py_getattro(attr); \
 		 \
 		if (strcmp(PyString_AsString(attr), "__dict__")==0) { \
-			return py_getattr_dict(rvalue, Methods, Attributes); \
+			return py_getattr_dict(rvalue, Type.tp_dict); \
 		} \
 		 \
 		return rvalue; \
@@ -434,7 +437,7 @@
 	}
 };
 
-PyObject *py_getattr_dict(PyObject *pydict, PyMethodDef *meth, PyAttributeDef *attrdef);
+PyObject *py_getattr_dict(PyObject *pydict, PyObject *tp_dict);
 
 #endif //  _adr_py_lib_h_
 

Modified: trunk/blender/source/gameengine/Ketsji/KX_GameObject.cpp
===================================================================
--- trunk/blender/source/gameengine/Ketsji/KX_GameObject.cpp	2009-04-07 15:20:12 UTC (rev 19581)
+++ trunk/blender/source/gameengine/Ketsji/KX_GameObject.cpp	2009-04-07 16:00:32 UTC (rev 19582)
@@ -1486,7 +1486,7 @@
 {
 	KX_GameObject* self= static_cast<KX_GameObject*>(self_v);
 	PyObject *dict_str = PyString_FromString("__dict__");
-	PyObject *dict= py_getattr_dict(self->SCA_IObject::py_getattro(dict_str), KX_GameObject::Methods, KX_GameObject::Attributes);
+	PyObject *dict= py_getattr_dict(self->SCA_IObject::py_getattro(dict_str), Type.tp_dict);
 	Py_DECREF(dict_str);
 	
 	if(dict==NULL)

Modified: trunk/blender/source/gameengine/Ketsji/KX_Scene.cpp
===================================================================
--- trunk/blender/source/gameengine/Ketsji/KX_Scene.cpp	2009-04-07 15:20:12 UTC (rev 19581)
+++ trunk/blender/source/gameengine/Ketsji/KX_Scene.cpp	2009-04-07 16:00:32 UTC (rev 19582)
@@ -1574,7 +1574,7 @@
 	KX_Scene* self= static_cast<KX_Scene*>(self_v);
 	/* Useually done by py_getattro_up but in this case we want to include m_attrlist dict */
 	PyObject *dict_str= PyString_FromString("__dict__");
-	PyObject *dict= py_getattr_dict(self->PyObjectPlus::py_getattro(dict_str), KX_Scene::Methods, KX_Scene::Attributes);
+	PyObject *dict= py_getattr_dict(self->PyObjectPlus::py_getattro(dict_str), Type.tp_dict);
 	Py_DECREF(dict_str);
 	
 	PyDict_Update(dict, self->m_attrlist);





More information about the Bf-blender-cvs mailing list