Made KX_MeshProxy use PyAttributeDef. simplified getting the 'materials' attribute...
authorCampbell Barton <ideasman42@gmail.com>
Sat, 4 Apr 2009 15:54:07 +0000 (15:54 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Sat, 4 Apr 2009 15:54:07 +0000 (15:54 +0000)
Added KX_GameObject 'meshes' attribute to replace getMesh(i)

source/gameengine/Ketsji/KX_GameObject.cpp
source/gameengine/Ketsji/KX_GameObject.h
source/gameengine/Ketsji/KX_MeshProxy.cpp
source/gameengine/Ketsji/KX_MeshProxy.h
source/gameengine/PyDoc/KX_GameObject.py

index 5bba9190bd670882132ee1572870676283c92ded..d8fb54086b3d5fd61e359ee7cce2a8caafa8618b 100644 (file)
@@ -1009,7 +1009,6 @@ PyMethodDef KX_GameObject::Methods[] = {
        {"removeParent", (PyCFunction)KX_GameObject::sPyRemoveParent,METH_NOARGS},
        {"getChildren", (PyCFunction)KX_GameObject::sPyGetChildren,METH_NOARGS},
        {"getChildrenRecursive", (PyCFunction)KX_GameObject::sPyGetChildrenRecursive,METH_NOARGS},
-       {"getMesh", (PyCFunction)KX_GameObject::sPyGetMesh,METH_VARARGS},
        {"getPhysicsId", (PyCFunction)KX_GameObject::sPyGetPhysicsId,METH_NOARGS},
        {"getPropertyNames", (PyCFunction)KX_GameObject::sPyGetPropertyNames,METH_NOARGS},
        {"replaceMesh",(PyCFunction) KX_GameObject::sPyReplaceMesh, METH_O},
@@ -1030,6 +1029,7 @@ PyMethodDef KX_GameObject::Methods[] = {
        {"getParent", (PyCFunction)KX_GameObject::sPyGetParent,METH_NOARGS},
        {"getVisible",(PyCFunction) KX_GameObject::sPyGetVisible, METH_NOARGS},
        {"getMass", (PyCFunction) KX_GameObject::sPyGetMass, METH_NOARGS},
+       {"getMesh", (PyCFunction)KX_GameObject::sPyGetMesh,METH_VARARGS},
        {NULL,NULL} //Sentinel
 };
 
@@ -1043,6 +1043,7 @@ PyAttributeDef KX_GameObject::Attributes[] = {
        KX_PYATTRIBUTE_RW_FUNCTION("scaling",   KX_GameObject, pyattr_get_scaling,      pyattr_set_scaling),
        KX_PYATTRIBUTE_RW_FUNCTION("timeOffset",KX_GameObject, pyattr_get_timeOffset,pyattr_set_timeOffset),
        KX_PYATTRIBUTE_RW_FUNCTION("state",             KX_GameObject, pyattr_get_state,        pyattr_set_state),
+       KX_PYATTRIBUTE_RO_FUNCTION("meshes",    KX_GameObject, pyattr_get_meshes),
        KX_PYATTRIBUTE_RO_FUNCTION("__dict__",  KX_GameObject, pyattr_get_dir_dict),
        {NULL} //Sentinel
 };
@@ -1416,6 +1417,22 @@ int KX_GameObject::pyattr_set_state(void *self_v, const KX_PYATTRIBUTE_DEF *attr
        return 0;
 }
 
+PyObject* KX_GameObject::pyattr_get_meshes(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
+{
+       KX_GameObject* self= static_cast<KX_GameObject*>(self_v);
+       PyObject *meshes= PyList_New(self->m_meshes.size());
+       int i;
+       
+       for(i=0; i < self->m_meshes.size(); i++)
+       {
+               KX_MeshProxy* meshproxy = new KX_MeshProxy(self->m_meshes[i]);
+               PyList_SET_ITEM(meshes, i, meshproxy);
+       }
+       
+       return meshes;
+}
+
+
 /* __dict__ only for the purpose of giving useful dir() results */
 PyObject* KX_GameObject::pyattr_get_dir_dict(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
 {
@@ -1758,6 +1775,8 @@ PyObject* KX_GameObject::PyGetChildrenRecursive(PyObject* self)
 
 PyObject* KX_GameObject::PyGetMesh(PyObject* self, PyObject* args)
 {
+       ShowDeprecationWarning("getMesh()", "the meshes property");
+       
        int mesh = 0;
 
        if (!PyArg_ParseTuple(args, "|i:getMesh", &mesh))
index 9ed35b6d26bc50063184b7330edaf0f18cec3cfa..c2b0428240d681535dad6fdabf76c41f288c2e66 100644 (file)
@@ -821,10 +821,13 @@ public:
        static int                      pyattr_set_timeOffset(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
        static PyObject*        pyattr_get_state(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
        static int                      pyattr_set_state(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
+       static PyObject*        pyattr_get_meshes(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef);
        
        /* for dir(), python3 uses __dir__() */
        static PyObject*        pyattr_get_dir_dict(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
        
+       
+       
        /* getitem/setitem */
        static Py_ssize_t                       Map_Len(PyObject* self);
        static PyMappingMethods Mapping;
index f464cb798e81220aa807ea8721e98b6fdd5b5140..4a4c98603e3b203ebdf7c31e67882f85fdc8f4f7 100644 (file)
@@ -87,6 +87,7 @@ KX_PYMETHODTABLE(KX_MeshProxy, reinstancePhysicsMesh),
 };
 
 PyAttributeDef KX_MeshProxy::Attributes[] = {
+       KX_PYATTRIBUTE_RO_FUNCTION("materials",         KX_MeshProxy, pyattr_get_materials),
        { NULL }        //Sentinel
 };
 
@@ -96,30 +97,12 @@ void KX_MeshProxy::SetMeshModified(bool v)
 }
 
 
-PyObject*
-KX_MeshProxy::py_getattro(PyObject *attr)
+PyObject* KX_MeshProxy::py_getattro(PyObject *attr)
 {
-       char *attr_str= PyString_AsString(attr);
+       PyObject* object = py_getattro_self(Attributes, this, attr);
+       if (object != NULL)
+               return object;
        
-       if (!strcmp(attr_str, "materials"))
-       {
-               PyObject *materials = PyList_New(0);
-               list<RAS_MeshMaterial>::iterator mit = m_meshobj->GetFirstMaterial();
-               for(; mit != m_meshobj->GetLastMaterial(); ++mit)
-               {
-                       RAS_IPolyMaterial *polymat = mit->m_bucket->GetPolyMaterial();
-
-                       if(polymat->GetFlag() & RAS_BLENDERMAT)
-                       {
-                               KX_BlenderMaterial *mat = static_cast<KX_BlenderMaterial*>(polymat);
-                               PyList_Append(materials, mat);
-                       }else
-                       {
-                               PyList_Append(materials, static_cast<KX_PolygonMaterial*>(polymat));
-                       }
-               }
-               return materials;
-       }
        py_getattro_up(SCA_IObject);
 }
 
@@ -280,3 +263,23 @@ KX_PYMETHODDEF_DOC(KX_MeshProxy, reinstancePhysicsMesh,
        //this needs to be reviewed, it is dependend on Sumo/Solid. Who is using this ?
        Py_RETURN_NONE;//(KX_ReInstanceShapeFromMesh(m_meshobj)) ? Py_RETURN_TRUE : Py_RETURN_FALSE;
 }
+
+PyObject* KX_MeshProxy::pyattr_get_materials(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
+{
+       KX_MeshProxy* self= static_cast<KX_MeshProxy*>(self_v);
+       
+       int tot= self->m_meshobj->NumMaterials();
+       int i;
+       
+       PyObject *materials = PyList_New( tot );
+       
+       list<RAS_MeshMaterial>::iterator mit= self->m_meshobj->GetFirstMaterial();
+       
+       /* Can be a KX_PolygonMaterial or KX_BlenderMaterial, since both are cast to a PyObject * we dont need to care */
+       for(i=0; i<tot; mit++, i++) {
+               PyObject *py_mat = (PyObject *)mit->m_bucket->GetPolyMaterial();
+               PyList_SET_ITEM(materials, i, py_mat);
+               Py_INCREF(py_mat);
+       }       
+       return materials;
+}
index 0b9738153ff2d8b46efcef7a996a28885a88624f..56a6dfcac42a6750d42aa1a645e95a18dedfc472 100644 (file)
@@ -65,6 +65,8 @@ public:
        KX_PYMETHOD(KX_MeshProxy,GetVertex);
        KX_PYMETHOD(KX_MeshProxy,GetPolygon);
        KX_PYMETHOD_DOC(KX_MeshProxy, reinstancePhysicsMesh);
+       
+       static PyObject*        pyattr_get_materials(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef);
 };
 
 #endif //__KX_MESHPROXY
index 694fe02a7ccac4d2b0bda57716ec1d41b2784b88..12a395287b03787220fa25d8ed3049d89060de38 100644 (file)
@@ -26,6 +26,9 @@ class KX_GameObject:
        @type timeOffset: float
        @ivar state: the game object's state bitmask.
        @type state: int
+       @ivar meshes: a list of L{KX_MeshProxy} objects.
+               B{Note}: Changes to this list will not update the KX_GameObject
+       @type meshes: list
        """
        def endObject(visible):
                """