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_PolyProxy.h"
34 #include "KX_MeshProxy.h"
35 #include "RAS_MeshObject.h"
36 #include "KX_BlenderMaterial.h"
37 #include "KX_PolygonMaterial.h"
39 #include "KX_PyMath.h"
41 PyTypeObject KX_PolyProxy::Type = {
42 PyObject_HEAD_INIT(&PyType_Type)
53 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
57 PyParentObject KX_PolyProxy::Parents[] = {
64 PyMethodDef KX_PolyProxy::Methods[] = {
65 KX_PYMETHODTABLE_NOARGS(KX_PolyProxy,getMaterialIndex),
66 KX_PYMETHODTABLE_NOARGS(KX_PolyProxy,getNumVertex),
67 KX_PYMETHODTABLE_NOARGS(KX_PolyProxy,isVisible),
68 KX_PYMETHODTABLE_NOARGS(KX_PolyProxy,isCollider),
69 KX_PYMETHODTABLE_NOARGS(KX_PolyProxy,getMaterialName),
70 KX_PYMETHODTABLE_NOARGS(KX_PolyProxy,getTextureName),
71 KX_PYMETHODTABLE(KX_PolyProxy,getVertexIndex),
72 KX_PYMETHODTABLE_NOARGS(KX_PolyProxy,getMesh),
73 KX_PYMETHODTABLE_NOARGS(KX_PolyProxy,getMaterial),
74 {NULL,NULL} //Sentinel
77 PyAttributeDef KX_PolyProxy::Attributes[] = {
81 PyObject* KX_PolyProxy::_getattr(const char *attr)
83 if (!strcmp(attr, "matname"))
85 return PyString_FromString(m_polygon->GetMaterial()->GetPolyMaterial()->GetMaterialName());
87 if (!strcmp(attr, "texture"))
89 return PyString_FromString(m_polygon->GetMaterial()->GetPolyMaterial()->GetTextureName());
91 if (!strcmp(attr, "material"))
93 RAS_IPolyMaterial *polymat = m_polygon->GetMaterial()->GetPolyMaterial();
94 if(polymat->GetFlag() & RAS_BLENDERMAT)
96 KX_BlenderMaterial* mat = static_cast<KX_BlenderMaterial*>(polymat);
102 KX_PolygonMaterial* mat = static_cast<KX_PolygonMaterial*>(polymat);
107 if (!strcmp(attr, "matid"))
109 // we'll have to scan through the material bucket of the mes and compare with
110 // the one of the polygon
111 RAS_MaterialBucket* polyBucket = m_polygon->GetMaterial();
113 for (matid=0; matid<m_mesh->NumMaterials(); matid++)
115 RAS_MeshMaterial* meshMat = m_mesh->GetMeshMaterial(matid);
116 if (meshMat->m_bucket == polyBucket)
120 return PyInt_FromLong(matid);
122 if (!strcmp(attr, "v1"))
124 return PyInt_FromLong(m_polygon->GetVertexOffset(0));
126 if (!strcmp(attr, "v2"))
128 return PyInt_FromLong(m_polygon->GetVertexOffset(1));
130 if (!strcmp(attr, "v3"))
132 return PyInt_FromLong(m_polygon->GetVertexOffset(2));
134 if (!strcmp(attr, "v4"))
136 return PyInt_FromLong(((m_polygon->VertexCount()>3)?m_polygon->GetVertexOffset(3):0));
138 if (!strcmp(attr, "visible"))
140 return PyInt_FromLong(m_polygon->IsVisible());
142 if (!strcmp(attr, "collide"))
144 return PyInt_FromLong(m_polygon->IsCollider());
146 _getattr_up(SCA_IObject);
149 KX_PolyProxy::KX_PolyProxy(const RAS_MeshObject*mesh, RAS_Polygon* polygon)
150 : m_polygon(polygon),
151 m_mesh((RAS_MeshObject*)mesh)
155 KX_PolyProxy::~KX_PolyProxy()
160 // stuff for cvalue related things
161 CValue* KX_PolyProxy::Calc(VALUE_OPERATOR, CValue *) { return NULL;}
162 CValue* KX_PolyProxy::CalcFinal(VALUE_DATA_TYPE, VALUE_OPERATOR, CValue *) { return NULL;}
163 STR_String sPolyName="polygone";
164 const STR_String & KX_PolyProxy::GetText() {return sPolyName;};
165 float KX_PolyProxy::GetNumber() { return -1;}
166 STR_String KX_PolyProxy::GetName() { return sPolyName;}
167 void KX_PolyProxy::SetName(STR_String) { };
168 CValue* KX_PolyProxy::GetReplica() { return NULL;}
169 void KX_PolyProxy::ReplicaSetName(STR_String) {};
172 // stuff for python integration
174 KX_PYMETHODDEF_DOC_NOARGS(KX_PolyProxy, getMaterialIndex,
175 "getMaterialIndex() : return the material index of the polygon in the mesh\n")
177 RAS_MaterialBucket* polyBucket = m_polygon->GetMaterial();
179 for (matid=0; matid<m_mesh->NumMaterials(); matid++)
181 RAS_MeshMaterial* meshMat = m_mesh->GetMeshMaterial(matid);
182 if (meshMat->m_bucket == polyBucket)
186 return PyInt_FromLong(matid);
189 KX_PYMETHODDEF_DOC_NOARGS(KX_PolyProxy, getNumVertex,
190 "getNumVertex() : returns the number of vertex of the polygon, 3 or 4\n")
192 return PyInt_FromLong(m_polygon->VertexCount());
195 KX_PYMETHODDEF_DOC_NOARGS(KX_PolyProxy, isVisible,
196 "isVisible() : returns whether the polygon is visible or not\n")
198 return PyInt_FromLong(m_polygon->IsVisible());
201 KX_PYMETHODDEF_DOC_NOARGS(KX_PolyProxy, isCollider,
202 "isCollider() : returns whether the polygon is receives collision or not\n")
204 return PyInt_FromLong(m_polygon->IsCollider());
207 KX_PYMETHODDEF_DOC_NOARGS(KX_PolyProxy, getMaterialName,
208 "getMaterialName() : returns the polygon material name, \"NoMaterial\" if no material\n")
210 return PyString_FromString(m_polygon->GetMaterial()->GetPolyMaterial()->GetMaterialName());
213 KX_PYMETHODDEF_DOC_NOARGS(KX_PolyProxy, getTextureName,
214 "getTexturelName() : returns the polygon texture name, \"NULL\" if no texture\n")
216 return PyString_FromString(m_polygon->GetMaterial()->GetPolyMaterial()->GetTextureName());
219 KX_PYMETHODDEF_DOC(KX_PolyProxy, getVertexIndex,
220 "getVertexIndex(vertex) : returns the mesh vertex index of a polygon vertex\n"
221 "vertex: index of the vertex in the polygon: 0->3\n"
222 "return value can be used to retrieve the vertex details through mesh proxy\n"
223 "Note: getVertexIndex(3) on a triangle polygon returns 0\n")
226 if (!PyArg_ParseTuple(args,"i",&index))
230 if (index < 0 || index > 3)
232 PyErr_SetString(PyExc_AttributeError, "Valid range for index is 0-3");
235 if (index < m_polygon->VertexCount())
237 return PyInt_FromLong(m_polygon->GetVertexOffset(index));
239 return PyInt_FromLong(0);
242 KX_PYMETHODDEF_DOC_NOARGS(KX_PolyProxy, getMesh,
243 "getMesh() : returns a mesh proxy\n")
245 KX_MeshProxy* meshproxy = new KX_MeshProxy((RAS_MeshObject*)m_mesh);
249 KX_PYMETHODDEF_DOC_NOARGS(KX_PolyProxy, getMaterial,
250 "getMaterial() : returns a material\n")
252 RAS_IPolyMaterial *polymat = m_polygon->GetMaterial()->GetPolyMaterial();
253 if(polymat->GetFlag() & RAS_BLENDERMAT)
255 KX_BlenderMaterial* mat = static_cast<KX_BlenderMaterial*>(polymat);
261 KX_PolygonMaterial* mat = static_cast<KX_PolygonMaterial*>(polymat);