Blender V2.61 - r43446

KX_MeshProxy.cpp

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00019  * All rights reserved.
00020  *
00021  * The Original Code is: all of this file.
00022  *
00023  * Contributor(s): none yet.
00024  *
00025  * ***** END GPL LICENSE BLOCK *****
00026  */
00027 
00033 #ifdef WITH_PYTHON
00034 
00035 #include "KX_MeshProxy.h"
00036 #include "RAS_IPolygonMaterial.h"
00037 #include "RAS_MeshObject.h"
00038 
00039 #include "KX_VertexProxy.h"
00040 #include "KX_PolyProxy.h"
00041 
00042 #include "KX_PolygonMaterial.h"
00043 #include "KX_BlenderMaterial.h"
00044 
00045 #include "KX_PyMath.h"
00046 #include "KX_ConvertPhysicsObject.h"
00047 
00048 #include "PyObjectPlus.h" 
00049 
00050 PyTypeObject KX_MeshProxy::Type = {
00051     PyVarObject_HEAD_INIT(NULL, 0)
00052     "KX_MeshProxy",
00053     sizeof(PyObjectPlus_Proxy),
00054     0,
00055     py_base_dealloc,
00056     0,
00057     0,
00058     0,
00059     0,
00060     py_base_repr,
00061     0,0,0,0,0,0,0,0,0,
00062     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
00063     0,0,0,0,0,0,0,
00064     Methods,
00065     0,
00066     0,
00067     &CValue::Type,
00068     0,0,0,0,0,0,
00069     py_base_new
00070 };
00071 
00072 PyMethodDef KX_MeshProxy::Methods[] = {
00073 {"getMaterialName", (PyCFunction)KX_MeshProxy::sPyGetMaterialName,METH_VARARGS},
00074 {"getTextureName", (PyCFunction)KX_MeshProxy::sPyGetTextureName,METH_VARARGS},
00075 {"getVertexArrayLength", (PyCFunction)KX_MeshProxy::sPyGetVertexArrayLength,METH_VARARGS},
00076 {"getVertex", (PyCFunction)KX_MeshProxy::sPyGetVertex,METH_VARARGS},
00077 {"getPolygon", (PyCFunction)KX_MeshProxy::sPyGetPolygon,METH_VARARGS},
00078 //{"getIndexArrayLength", (PyCFunction)KX_MeshProxy::sPyGetIndexArrayLength,METH_VARARGS},
00079   {NULL,NULL} //Sentinel
00080 };
00081 
00082 PyAttributeDef KX_MeshProxy::Attributes[] = {
00083     KX_PYATTRIBUTE_RO_FUNCTION("materials",     KX_MeshProxy, pyattr_get_materials),
00084     KX_PYATTRIBUTE_RO_FUNCTION("numPolygons",   KX_MeshProxy, pyattr_get_numPolygons),
00085     KX_PYATTRIBUTE_RO_FUNCTION("numMaterials",  KX_MeshProxy, pyattr_get_numMaterials),
00086 
00087     { NULL }    //Sentinel
00088 };
00089 
00090 void KX_MeshProxy::SetMeshModified(bool v)
00091 {
00092     m_meshobj->SetMeshModified(v);
00093 }
00094 
00095 KX_MeshProxy::KX_MeshProxy(RAS_MeshObject* mesh)
00096     : CValue(), m_meshobj(mesh)
00097 {
00098 }
00099 
00100 KX_MeshProxy::~KX_MeshProxy()
00101 {
00102 }
00103 
00104 
00105 
00106 // stuff for cvalue related things
00107 CValue*     KX_MeshProxy::Calc(VALUE_OPERATOR op, CValue *val) { return NULL;}
00108 CValue*     KX_MeshProxy::CalcFinal(VALUE_DATA_TYPE dtype, VALUE_OPERATOR op, CValue *val) { return NULL;}  
00109 
00110 const STR_String &  KX_MeshProxy::GetText() {return m_meshobj->GetName();};
00111 double      KX_MeshProxy::GetNumber() { return -1;}
00112 STR_String& KX_MeshProxy::GetName() { return m_meshobj->GetName();}
00113 void        KX_MeshProxy::SetName(const char *name) { };
00114 CValue*     KX_MeshProxy::GetReplica() { return NULL;}
00115 
00116 
00117 // stuff for python integration
00118 
00119 PyObject* KX_MeshProxy::PyGetMaterialName(PyObject* args, PyObject* kwds)
00120 {
00121     int matid= 1;
00122     STR_String matname;
00123 
00124     if (PyArg_ParseTuple(args,"i:getMaterialName",&matid))
00125     {
00126         matname = m_meshobj->GetMaterialName(matid);
00127     }
00128     else {
00129         return NULL;
00130     }
00131 
00132     return PyUnicode_From_STR_String(matname);
00133 
00134 }
00135 
00136 
00137 PyObject* KX_MeshProxy::PyGetTextureName(PyObject* args, PyObject* kwds)
00138 {
00139     int matid= 1;
00140     STR_String matname;
00141 
00142     if (PyArg_ParseTuple(args,"i:getTextureName",&matid))
00143     {
00144         matname = m_meshobj->GetTextureName(matid);
00145     }
00146     else {
00147         return NULL;
00148     }
00149 
00150     return PyUnicode_From_STR_String(matname);
00151         
00152 }
00153 
00154 PyObject* KX_MeshProxy::PyGetVertexArrayLength(PyObject* args, PyObject* kwds)
00155 {
00156     int matid= 0;
00157     int length = 0;
00158 
00159     
00160     if (!PyArg_ParseTuple(args,"i:getVertexArrayLength",&matid))
00161         return NULL;
00162     
00163 
00164     RAS_MeshMaterial *mmat = m_meshobj->GetMeshMaterial(matid); /* can be NULL*/
00165     
00166     if (mmat)
00167     {
00168         RAS_IPolyMaterial* mat = mmat->m_bucket->GetPolyMaterial();
00169         if (mat)
00170             length = m_meshobj->NumVertices(mat);
00171     }
00172     
00173     return PyLong_FromSsize_t(length);
00174 }
00175 
00176 
00177 PyObject* KX_MeshProxy::PyGetVertex(PyObject* args, PyObject* kwds)
00178 {
00179     int vertexindex;
00180     int matindex;
00181 
00182     if (!PyArg_ParseTuple(args,"ii:getVertex",&matindex,&vertexindex))
00183         return NULL;
00184     
00185     RAS_TexVert* vertex = m_meshobj->GetVertex(matindex,vertexindex);
00186     
00187     if(vertex==NULL) {
00188         PyErr_SetString(PyExc_ValueError, "mesh.getVertex(mat_idx, vert_idx): KX_MeshProxy, could not get a vertex at the given indices");
00189         return NULL;
00190     }
00191     
00192     return (new KX_VertexProxy(this, vertex))->NewProxy(true);
00193 }
00194 
00195 PyObject* KX_MeshProxy::PyGetPolygon(PyObject* args, PyObject* kwds)
00196 {
00197     int polyindex= 1;
00198     PyObject* polyob = NULL;
00199 
00200     if (!PyArg_ParseTuple(args,"i:getPolygon",&polyindex))
00201         return NULL;
00202     
00203     if (polyindex<0 || polyindex >= m_meshobj->NumPolygons())
00204     {
00205         PyErr_SetString(PyExc_AttributeError, "mesh.getPolygon(int): KX_MeshProxy, invalid polygon index");
00206         return NULL;
00207     }
00208         
00209 
00210     RAS_Polygon* polygon = m_meshobj->GetPolygon(polyindex);
00211     if (polygon)
00212     {
00213         polyob = (new KX_PolyProxy(m_meshobj, polygon))->NewProxy(true);
00214     }
00215     else {
00216         PyErr_SetString(PyExc_AttributeError, "mesh.getPolygon(int): KX_MeshProxy, polygon is NULL, unknown reason");
00217     }
00218     return polyob;
00219 }
00220 
00221 PyObject* KX_MeshProxy::pyattr_get_materials(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
00222 {
00223     KX_MeshProxy* self= static_cast<KX_MeshProxy*>(self_v);
00224     
00225     int tot= self->m_meshobj->NumMaterials();
00226     int i;
00227     
00228     PyObject *materials = PyList_New( tot );
00229     
00230     list<RAS_MeshMaterial>::iterator mit= self->m_meshobj->GetFirstMaterial();
00231     
00232     
00233     for(i=0; i<tot; mit++, i++) {
00234         RAS_IPolyMaterial *polymat = mit->m_bucket->GetPolyMaterial();   
00235         
00236         /* Why do we need to check for RAS_BLENDERMAT if both are cast to a (PyObject*)? - Campbell */
00237         if(polymat->GetFlag() & RAS_BLENDERMAT)      
00238         {    
00239             KX_BlenderMaterial *mat = static_cast<KX_BlenderMaterial*>(polymat);     
00240             PyList_SET_ITEM(materials, i, mat->GetProxy());
00241         }
00242         else {  
00243             KX_PolygonMaterial *mat = static_cast<KX_PolygonMaterial*>(polymat);
00244             PyList_SET_ITEM(materials, i, mat->GetProxy());
00245         }
00246     }   
00247     return materials;
00248 }
00249 
00250 PyObject * KX_MeshProxy::pyattr_get_numMaterials(void * selfv, const KX_PYATTRIBUTE_DEF * attrdef)
00251 {
00252     KX_MeshProxy * self = static_cast<KX_MeshProxy *> (selfv);
00253     return PyLong_FromSsize_t(self->m_meshobj->NumMaterials());
00254 }
00255 
00256 PyObject * KX_MeshProxy::pyattr_get_numPolygons(void * selfv, const KX_PYATTRIBUTE_DEF * attrdef)
00257 {
00258     KX_MeshProxy * self = static_cast<KX_MeshProxy *> (selfv);
00259     return PyLong_FromSsize_t(self->m_meshobj->NumPolygons());
00260 }
00261 
00262 /* a close copy of ConvertPythonToGameObject but for meshes */
00263 bool ConvertPythonToMesh(PyObject * value, RAS_MeshObject **object, bool py_none_ok, const char *error_prefix)
00264 {
00265     if (value==NULL) {
00266         PyErr_Format(PyExc_TypeError, "%s, python pointer NULL, should never happen", error_prefix);
00267         *object = NULL;
00268         return false;
00269     }
00270         
00271     if (value==Py_None) {
00272         *object = NULL;
00273         
00274         if (py_none_ok) {
00275             return true;
00276         } else {
00277             PyErr_Format(PyExc_TypeError, "%s, expected KX_MeshProxy or a KX_MeshProxy name, None is invalid", error_prefix);
00278             return false;
00279         }
00280     }
00281     
00282     if (PyUnicode_Check(value)) {
00283         *object = (RAS_MeshObject*)SCA_ILogicBrick::m_sCurrentLogicManager->GetMeshByName(STR_String( _PyUnicode_AsString(value) ));
00284         
00285         if (*object) {
00286             return true;
00287         } else {
00288             PyErr_Format(PyExc_ValueError, "%s, requested name \"%s\" did not match any KX_MeshProxy in this scene", error_prefix, _PyUnicode_AsString(value));
00289             return false;
00290         }
00291     }
00292     
00293     if (PyObject_TypeCheck(value, &KX_MeshProxy::Type)) {
00294         KX_MeshProxy *kx_mesh = static_cast<KX_MeshProxy*>BGE_PROXY_REF(value);
00295         
00296         /* sets the error */
00297         if (kx_mesh==NULL) {
00298             PyErr_Format(PyExc_SystemError, "%s, " BGE_PROXY_ERROR_MSG, error_prefix);
00299             return false;
00300         }       
00301         
00302         *object = kx_mesh->GetMesh();
00303         return true;
00304     }
00305     
00306     *object = NULL;
00307     
00308     if (py_none_ok) {
00309         PyErr_Format(PyExc_TypeError, "%s, expect a KX_MeshProxy, a string or None", error_prefix);
00310     } else {
00311         PyErr_Format(PyExc_TypeError, "%s, expect a KX_MeshProxy or a string", error_prefix);
00312     }
00313     
00314     return false;
00315 }
00316 
00317 #endif // WITH_PYTHON