3 * ***** BEGIN GPL LICENSE BLOCK *****
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20 * All rights reserved.
22 * The Original Code is: all of this file.
24 * Contributor(s): none yet.
26 * ***** END GPL LICENSE BLOCK *****
33 #include "KX_MeshProxy.h"
34 #include "RAS_IPolygonMaterial.h"
35 #include "RAS_MeshObject.h"
37 #include "KX_VertexProxy.h"
38 #include "KX_PolyProxy.h"
40 #include "KX_PolygonMaterial.h"
41 #include "KX_BlenderMaterial.h"
43 #include "KX_PyMath.h"
44 #include "KX_ConvertPhysicsObject.h"
46 #include "PyObjectPlus.h"
48 PyTypeObject KX_MeshProxy::Type = {
49 PyObject_HEAD_INIT(NULL)
67 PyParentObject KX_MeshProxy::Parents[] = {
75 PyMethodDef KX_MeshProxy::Methods[] = {
76 {"getNumMaterials", (PyCFunction)KX_MeshProxy::sPyGetNumMaterials,METH_VARARGS},
77 {"getNumPolygons", (PyCFunction)KX_MeshProxy::sPyGetNumPolygons,METH_NOARGS},
78 {"getMaterialName", (PyCFunction)KX_MeshProxy::sPyGetMaterialName,METH_VARARGS},
79 {"getTextureName", (PyCFunction)KX_MeshProxy::sPyGetTextureName,METH_VARARGS},
80 {"getVertexArrayLength", (PyCFunction)KX_MeshProxy::sPyGetVertexArrayLength,METH_VARARGS},
81 {"getVertex", (PyCFunction)KX_MeshProxy::sPyGetVertex,METH_VARARGS},
82 {"getPolygon", (PyCFunction)KX_MeshProxy::sPyGetPolygon,METH_VARARGS},
83 KX_PYMETHODTABLE(KX_MeshProxy, reinstancePhysicsMesh),
84 //{"getIndexArrayLength", (PyCFunction)KX_MeshProxy::sPyGetIndexArrayLength,METH_VARARGS},
86 {NULL,NULL} //Sentinel
89 PyAttributeDef KX_MeshProxy::Attributes[] = {
90 KX_PYATTRIBUTE_RO_FUNCTION("materials", KX_MeshProxy, pyattr_get_materials),
94 void KX_MeshProxy::SetMeshModified(bool v)
96 m_meshobj->SetMeshModified(v);
100 PyObject* KX_MeshProxy::py_getattro(PyObject *attr)
102 py_getattro_up(SCA_IObject);
107 KX_MeshProxy::KX_MeshProxy(RAS_MeshObject* mesh)
108 : SCA_IObject(&Type), m_meshobj(mesh)
112 KX_MeshProxy::~KX_MeshProxy()
118 // stuff for cvalue related things
119 CValue* KX_MeshProxy::Calc(VALUE_OPERATOR op, CValue *val) { return NULL;}
120 CValue* KX_MeshProxy::CalcFinal(VALUE_DATA_TYPE dtype, VALUE_OPERATOR op, CValue *val) { return NULL;}
122 const STR_String & KX_MeshProxy::GetText() {return m_meshobj->GetName();};
123 double KX_MeshProxy::GetNumber() { return -1;}
124 STR_String KX_MeshProxy::GetName() { return m_meshobj->GetName();}
125 void KX_MeshProxy::SetName(STR_String name) { };
126 CValue* KX_MeshProxy::GetReplica() { return NULL;}
127 void KX_MeshProxy::ReplicaSetName(STR_String name) {};
130 // stuff for python integration
132 PyObject* KX_MeshProxy::PyGetNumMaterials(PyObject* self,
136 int num = m_meshobj->NumMaterials();
137 return PyInt_FromLong(num);
140 PyObject* KX_MeshProxy::PyGetNumPolygons(PyObject* self)
142 int num = m_meshobj->NumPolygons();
143 return PyInt_FromLong(num);
146 PyObject* KX_MeshProxy::PyGetMaterialName(PyObject* self,
153 if (PyArg_ParseTuple(args,"i:getMaterialName",&matid))
155 matname = m_meshobj->GetMaterialName(matid);
161 return PyString_FromString(matname.Ptr());
166 PyObject* KX_MeshProxy::PyGetTextureName(PyObject* self,
173 if (PyArg_ParseTuple(args,"i:getTextureName",&matid))
175 matname = m_meshobj->GetTextureName(matid);
181 return PyString_FromString(matname.Ptr());
185 PyObject* KX_MeshProxy::PyGetVertexArrayLength(PyObject* self,
193 if (!PyArg_ParseTuple(args,"i:getVertexArrayLength",&matid))
197 RAS_MeshMaterial *mmat = m_meshobj->GetMeshMaterial(matid); /* can be NULL*/
201 RAS_IPolyMaterial* mat = mmat->m_bucket->GetPolyMaterial();
203 length = m_meshobj->NumVertices(mat);
206 return PyInt_FromLong(length);
210 PyObject* KX_MeshProxy::PyGetVertex(PyObject* self,
216 PyObject* vertexob = NULL;
218 if (PyArg_ParseTuple(args,"ii:getVertex",&matindex,&vertexindex))
220 RAS_TexVert* vertex = m_meshobj->GetVertex(matindex,vertexindex);
223 vertexob = new KX_VertexProxy(this, vertex);
234 PyObject* KX_MeshProxy::PyGetPolygon(PyObject* self,
239 PyObject* polyob = NULL;
241 if (!PyArg_ParseTuple(args,"i:getPolygon",&polyindex))
244 if (polyindex<0 || polyindex >= m_meshobj->NumPolygons())
246 PyErr_SetString(PyExc_AttributeError, "Invalid polygon index");
251 RAS_Polygon* polygon = m_meshobj->GetPolygon(polyindex);
254 polyob = new KX_PolyProxy(m_meshobj, polygon);
257 PyErr_SetString(PyExc_AttributeError, "polygon is NULL, unknown reason");
262 KX_PYMETHODDEF_DOC(KX_MeshProxy, reinstancePhysicsMesh,
263 "Reinstance the physics mesh.")
265 //this needs to be reviewed, it is dependend on Sumo/Solid. Who is using this ?
266 Py_RETURN_NONE;//(KX_ReInstanceShapeFromMesh(m_meshobj)) ? Py_RETURN_TRUE : Py_RETURN_FALSE;
269 PyObject* KX_MeshProxy::pyattr_get_materials(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
271 KX_MeshProxy* self= static_cast<KX_MeshProxy*>(self_v);
273 int tot= self->m_meshobj->NumMaterials();
276 PyObject *materials = PyList_New( tot );
278 list<RAS_MeshMaterial>::iterator mit= self->m_meshobj->GetFirstMaterial();
281 for(i=0; i<tot; mit++, i++) {
282 RAS_IPolyMaterial *polymat = mit->m_bucket->GetPolyMaterial();
284 /* Why do we need to check for RAS_BLENDERMAT if both are cast to a (PyObject*)? - Campbell */
285 if(polymat->GetFlag() & RAS_BLENDERMAT)
287 KX_BlenderMaterial *mat = static_cast<KX_BlenderMaterial*>(polymat);
288 PyList_SET_ITEM(materials, i, mat);
292 KX_PolygonMaterial *mat = static_cast<KX_PolygonMaterial*>(polymat);
293 PyList_SET_ITEM(materials, i, mat);