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(NULL)
45 sizeof(PyObjectPlus_Proxy),
60 PyParentObject KX_PolyProxy::Parents[] = {
67 PyMethodDef KX_PolyProxy::Methods[] = {
68 KX_PYMETHODTABLE_NOARGS(KX_PolyProxy,getMaterialIndex),
69 KX_PYMETHODTABLE_NOARGS(KX_PolyProxy,getNumVertex),
70 KX_PYMETHODTABLE_NOARGS(KX_PolyProxy,isVisible),
71 KX_PYMETHODTABLE_NOARGS(KX_PolyProxy,isCollider),
72 KX_PYMETHODTABLE_NOARGS(KX_PolyProxy,getMaterialName),
73 KX_PYMETHODTABLE_NOARGS(KX_PolyProxy,getTextureName),
74 KX_PYMETHODTABLE(KX_PolyProxy,getVertexIndex),
75 KX_PYMETHODTABLE_NOARGS(KX_PolyProxy,getMesh),
76 KX_PYMETHODTABLE_NOARGS(KX_PolyProxy,getMaterial),
77 {NULL,NULL} //Sentinel
80 PyAttributeDef KX_PolyProxy::Attributes[] = {
81 /* All dummy's so they come up in a dir() */
82 //KX_PYATTRIBUTE_TODO("DummyProps"),
83 KX_PYATTRIBUTE_DUMMY("matname"),
84 KX_PYATTRIBUTE_DUMMY("texture"),
85 KX_PYATTRIBUTE_DUMMY("material"),
86 KX_PYATTRIBUTE_DUMMY("matid"),
87 KX_PYATTRIBUTE_DUMMY("v1"),
88 KX_PYATTRIBUTE_DUMMY("v2"),
89 KX_PYATTRIBUTE_DUMMY("v3"),
90 KX_PYATTRIBUTE_DUMMY("v4"),
91 KX_PYATTRIBUTE_DUMMY("visible"),
92 KX_PYATTRIBUTE_DUMMY("collide"),
96 PyObject* KX_PolyProxy::py_getattro(PyObject *attr)
98 char *attr_str= PyString_AsString(attr);
99 if (!strcmp(attr_str, "matname"))
101 return PyString_FromString(m_polygon->GetMaterial()->GetPolyMaterial()->GetMaterialName());
103 if (!strcmp(attr_str, "texture"))
105 return PyString_FromString(m_polygon->GetMaterial()->GetPolyMaterial()->GetTextureName());
107 if (!strcmp(attr_str, "material"))
109 RAS_IPolyMaterial *polymat = m_polygon->GetMaterial()->GetPolyMaterial();
110 if(polymat->GetFlag() & RAS_BLENDERMAT)
112 KX_BlenderMaterial* mat = static_cast<KX_BlenderMaterial*>(polymat);
113 return mat->GetProxy();
117 KX_PolygonMaterial* mat = static_cast<KX_PolygonMaterial*>(polymat);
118 return mat->GetProxy();
121 if (!strcmp(attr_str, "matid"))
123 // we'll have to scan through the material bucket of the mes and compare with
124 // the one of the polygon
125 RAS_MaterialBucket* polyBucket = m_polygon->GetMaterial();
127 for (matid=0; matid<(unsigned int)m_mesh->NumMaterials(); matid++)
129 RAS_MeshMaterial* meshMat = m_mesh->GetMeshMaterial(matid);
130 if (meshMat->m_bucket == polyBucket)
134 return PyInt_FromLong(matid);
136 if (!strcmp(attr_str, "v1"))
138 return PyInt_FromLong(m_polygon->GetVertexOffset(0));
140 if (!strcmp(attr_str, "v2"))
142 return PyInt_FromLong(m_polygon->GetVertexOffset(1));
144 if (!strcmp(attr_str, "v3"))
146 return PyInt_FromLong(m_polygon->GetVertexOffset(2));
148 if (!strcmp(attr_str, "v4"))
150 return PyInt_FromLong(((m_polygon->VertexCount()>3)?m_polygon->GetVertexOffset(3):0));
152 if (!strcmp(attr_str, "visible"))
154 return PyInt_FromLong(m_polygon->IsVisible());
156 if (!strcmp(attr_str, "collide"))
158 return PyInt_FromLong(m_polygon->IsCollider());
160 py_getattro_up(SCA_IObject);
163 PyObject* KX_PolyProxy::py_getattro_dict() {
164 py_getattro_dict_up(SCA_IObject);
167 KX_PolyProxy::KX_PolyProxy(const RAS_MeshObject*mesh, RAS_Polygon* polygon)
168 : m_polygon(polygon),
169 m_mesh((RAS_MeshObject*)mesh)
173 KX_PolyProxy::~KX_PolyProxy()
178 // stuff for cvalue related things
179 CValue* KX_PolyProxy::Calc(VALUE_OPERATOR, CValue *) { return NULL;}
180 CValue* KX_PolyProxy::CalcFinal(VALUE_DATA_TYPE, VALUE_OPERATOR, CValue *) { return NULL;}
181 STR_String sPolyName="polygone";
182 const STR_String & KX_PolyProxy::GetText() {return sPolyName;};
183 double KX_PolyProxy::GetNumber() { return -1;}
184 STR_String KX_PolyProxy::GetName() { return sPolyName;}
185 void KX_PolyProxy::SetName(STR_String) { };
186 CValue* KX_PolyProxy::GetReplica() { return NULL;}
188 // stuff for python integration
190 KX_PYMETHODDEF_DOC_NOARGS(KX_PolyProxy, getMaterialIndex,
191 "getMaterialIndex() : return the material index of the polygon in the mesh\n")
193 RAS_MaterialBucket* polyBucket = m_polygon->GetMaterial();
195 for (matid=0; matid<(unsigned int)m_mesh->NumMaterials(); matid++)
197 RAS_MeshMaterial* meshMat = m_mesh->GetMeshMaterial(matid);
198 if (meshMat->m_bucket == polyBucket)
202 return PyInt_FromLong(matid);
205 KX_PYMETHODDEF_DOC_NOARGS(KX_PolyProxy, getNumVertex,
206 "getNumVertex() : returns the number of vertex of the polygon, 3 or 4\n")
208 return PyInt_FromLong(m_polygon->VertexCount());
211 KX_PYMETHODDEF_DOC_NOARGS(KX_PolyProxy, isVisible,
212 "isVisible() : returns whether the polygon is visible or not\n")
214 return PyInt_FromLong(m_polygon->IsVisible());
217 KX_PYMETHODDEF_DOC_NOARGS(KX_PolyProxy, isCollider,
218 "isCollider() : returns whether the polygon is receives collision or not\n")
220 return PyInt_FromLong(m_polygon->IsCollider());
223 KX_PYMETHODDEF_DOC_NOARGS(KX_PolyProxy, getMaterialName,
224 "getMaterialName() : returns the polygon material name, \"NoMaterial\" if no material\n")
226 return PyString_FromString(m_polygon->GetMaterial()->GetPolyMaterial()->GetMaterialName());
229 KX_PYMETHODDEF_DOC_NOARGS(KX_PolyProxy, getTextureName,
230 "getTexturelName() : returns the polygon texture name, \"NULL\" if no texture\n")
232 return PyString_FromString(m_polygon->GetMaterial()->GetPolyMaterial()->GetTextureName());
235 KX_PYMETHODDEF_DOC(KX_PolyProxy, getVertexIndex,
236 "getVertexIndex(vertex) : returns the mesh vertex index of a polygon vertex\n"
237 "vertex: index of the vertex in the polygon: 0->3\n"
238 "return value can be used to retrieve the vertex details through mesh proxy\n"
239 "Note: getVertexIndex(3) on a triangle polygon returns 0\n")
242 if (!PyArg_ParseTuple(args,"i:getVertexIndex",&index))
246 if (index < 0 || index > 3)
248 PyErr_SetString(PyExc_AttributeError, "poly.getVertexIndex(int): KX_PolyProxy, expected an index between 0-3");
251 if (index < m_polygon->VertexCount())
253 return PyInt_FromLong(m_polygon->GetVertexOffset(index));
255 return PyInt_FromLong(0);
258 KX_PYMETHODDEF_DOC_NOARGS(KX_PolyProxy, getMesh,
259 "getMesh() : returns a mesh proxy\n")
261 KX_MeshProxy* meshproxy = new KX_MeshProxy((RAS_MeshObject*)m_mesh);
262 return meshproxy->NewProxy(true);
265 KX_PYMETHODDEF_DOC_NOARGS(KX_PolyProxy, getMaterial,
266 "getMaterial() : returns a material\n")
268 RAS_IPolyMaterial *polymat = m_polygon->GetMaterial()->GetPolyMaterial();
269 if(polymat->GetFlag() & RAS_BLENDERMAT)
271 KX_BlenderMaterial* mat = static_cast<KX_BlenderMaterial*>(polymat);
272 return mat->GetProxy();
276 KX_PolygonMaterial* mat = static_cast<KX_PolygonMaterial*>(polymat);
277 return mat->GetProxy();