Blender  V2.59
KX_PolyProxy.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: KX_PolyProxy.cpp 35171 2011-02-25 13:35:59Z jesterking $
00003  * ***** BEGIN GPL LICENSE BLOCK *****
00004  *
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * as published by the Free Software Foundation; either version 2
00008  * of the License, or (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software Foundation,
00017  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00018  *
00019  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00020  * All rights reserved.
00021  *
00022  * The Original Code is: all of this file.
00023  *
00024  * Contributor(s): none yet.
00025  *
00026  * ***** END GPL LICENSE BLOCK *****
00027  */
00028 
00034 #ifdef WITH_PYTHON
00035 
00036 #include "KX_PolyProxy.h"
00037 #include "KX_MeshProxy.h"
00038 #include "RAS_MeshObject.h"
00039 #include "KX_BlenderMaterial.h"
00040 #include "KX_PolygonMaterial.h"
00041 
00042 #include "KX_PyMath.h"
00043 
00044 PyTypeObject KX_PolyProxy::Type = {
00045         PyVarObject_HEAD_INIT(NULL, 0)
00046         "KX_PolyProxy",
00047         sizeof(PyObjectPlus_Proxy),
00048         0,
00049         py_base_dealloc,
00050         0,
00051         0,
00052         0,
00053         0,
00054         py_base_repr,
00055         0,0,0,0,0,0,0,0,0,
00056         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
00057         0,0,0,0,0,0,0,
00058         Methods,
00059         0,
00060         0,
00061         &CValue::Type,
00062         0,0,0,0,0,0,
00063         py_base_new
00064 };
00065 
00066 PyMethodDef KX_PolyProxy::Methods[] = {
00067         KX_PYMETHODTABLE_NOARGS(KX_PolyProxy,getMaterialIndex),
00068         KX_PYMETHODTABLE_NOARGS(KX_PolyProxy,getNumVertex),
00069         KX_PYMETHODTABLE_NOARGS(KX_PolyProxy,isVisible),
00070         KX_PYMETHODTABLE_NOARGS(KX_PolyProxy,isCollider),
00071         KX_PYMETHODTABLE_NOARGS(KX_PolyProxy,getMaterialName),
00072         KX_PYMETHODTABLE_NOARGS(KX_PolyProxy,getTextureName),
00073         KX_PYMETHODTABLE(KX_PolyProxy,getVertexIndex),
00074         KX_PYMETHODTABLE_NOARGS(KX_PolyProxy,getMesh),
00075         KX_PYMETHODTABLE_NOARGS(KX_PolyProxy,getMaterial),
00076         {NULL,NULL} //Sentinel
00077 };
00078 
00079 PyAttributeDef KX_PolyProxy::Attributes[] = {
00080         KX_PYATTRIBUTE_RO_FUNCTION("material_name", KX_PolyProxy, pyattr_get_material_name),
00081         KX_PYATTRIBUTE_RO_FUNCTION("texture_name", KX_PolyProxy, pyattr_get_texture_name),
00082         KX_PYATTRIBUTE_RO_FUNCTION("material", KX_PolyProxy, pyattr_get_material),
00083         KX_PYATTRIBUTE_RO_FUNCTION("material_id", KX_PolyProxy, pyattr_get_material_id),
00084         KX_PYATTRIBUTE_RO_FUNCTION("v1", KX_PolyProxy, pyattr_get_v1),
00085         KX_PYATTRIBUTE_RO_FUNCTION("v2", KX_PolyProxy, pyattr_get_v2),
00086         KX_PYATTRIBUTE_RO_FUNCTION("v3", KX_PolyProxy, pyattr_get_v3),
00087         KX_PYATTRIBUTE_RO_FUNCTION("v4", KX_PolyProxy, pyattr_get_v4),
00088         KX_PYATTRIBUTE_RO_FUNCTION("visible", KX_PolyProxy, pyattr_get_visible),
00089         KX_PYATTRIBUTE_RO_FUNCTION("collide", KX_PolyProxy, pyattr_get_collide),
00090         { NULL }        //Sentinel
00091 };
00092 
00093 KX_PolyProxy::KX_PolyProxy(const RAS_MeshObject*mesh, RAS_Polygon* polygon)
00094 :       m_polygon(polygon),
00095         m_mesh((RAS_MeshObject*)mesh)
00096 {
00097 }
00098 
00099 KX_PolyProxy::~KX_PolyProxy()
00100 {
00101 }
00102 
00103 
00104 // stuff for cvalue related things
00105 CValue*         KX_PolyProxy::Calc(VALUE_OPERATOR, CValue *) { return NULL;}
00106 CValue*         KX_PolyProxy::CalcFinal(VALUE_DATA_TYPE, VALUE_OPERATOR, CValue *) { return NULL;}      
00107 STR_String      sPolyName="polygone";
00108 const STR_String &      KX_PolyProxy::GetText() {return sPolyName;};
00109 double          KX_PolyProxy::GetNumber() { return -1;}
00110 STR_String&     KX_PolyProxy::GetName() { return sPolyName;}
00111 void            KX_PolyProxy::SetName(const char *) { };
00112 CValue*         KX_PolyProxy::GetReplica() { return NULL;}
00113 
00114 // stuff for python integration
00115 
00116 PyObject* KX_PolyProxy::pyattr_get_material_name(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
00117 {
00118         KX_PolyProxy* self= static_cast<KX_PolyProxy*>(self_v);
00119         return self->PygetMaterialName();
00120 }
00121 
00122 PyObject* KX_PolyProxy::pyattr_get_texture_name(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
00123 {
00124         KX_PolyProxy* self= static_cast<KX_PolyProxy*>(self_v);
00125         return self->PygetTextureName();
00126 }
00127 
00128 PyObject* KX_PolyProxy::pyattr_get_material(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
00129 {
00130         KX_PolyProxy* self= static_cast<KX_PolyProxy*>(self_v);
00131         return self->PygetMaterial();
00132 }
00133 
00134 PyObject* KX_PolyProxy::pyattr_get_material_id(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
00135 {
00136         KX_PolyProxy* self= static_cast<KX_PolyProxy*>(self_v);
00137         return self->PygetMaterialIndex();
00138 }
00139 
00140 PyObject* KX_PolyProxy::pyattr_get_v1(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
00141 {
00142         KX_PolyProxy* self= static_cast<KX_PolyProxy*>(self_v);
00143 
00144         return PyLong_FromSsize_t(self->m_polygon->GetVertexOffsetAbs(self->m_mesh, 0));
00145 }
00146 
00147 PyObject* KX_PolyProxy::pyattr_get_v2(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
00148 {
00149         KX_PolyProxy* self= static_cast<KX_PolyProxy*>(self_v);
00150 
00151         return PyLong_FromSsize_t(self->m_polygon->GetVertexOffsetAbs(self->m_mesh, 1));
00152 }
00153 
00154 PyObject* KX_PolyProxy::pyattr_get_v3(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
00155 {
00156         KX_PolyProxy* self= static_cast<KX_PolyProxy*>(self_v);
00157 
00158         return PyLong_FromSsize_t(self->m_polygon->GetVertexOffsetAbs(self->m_mesh, 2));
00159 }
00160 
00161 PyObject* KX_PolyProxy::pyattr_get_v4(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
00162 {
00163         KX_PolyProxy* self= static_cast<KX_PolyProxy*>(self_v);
00164 
00165         if (3 < self->m_polygon->VertexCount())
00166         {
00167                 return PyLong_FromSsize_t(self->m_polygon->GetVertexOffsetAbs(self->m_mesh, 3));
00168         }
00169         return PyLong_FromSsize_t(0);
00170 }
00171 
00172 PyObject* KX_PolyProxy::pyattr_get_visible(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
00173 {
00174         KX_PolyProxy* self= static_cast<KX_PolyProxy*>(self_v);
00175         return self->PyisVisible();
00176 }
00177 
00178 PyObject* KX_PolyProxy::pyattr_get_collide(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
00179 {
00180         KX_PolyProxy* self= static_cast<KX_PolyProxy*>(self_v);
00181         return self->PyisCollider();
00182 }
00183 
00184 KX_PYMETHODDEF_DOC_NOARGS(KX_PolyProxy, getMaterialIndex,
00185 "getMaterialIndex() : return the material index of the polygon in the mesh\n")
00186 {
00187         RAS_MaterialBucket* polyBucket = m_polygon->GetMaterial();
00188         unsigned int matid;
00189         for (matid=0; matid<(unsigned int)m_mesh->NumMaterials(); matid++)
00190         {
00191                 RAS_MeshMaterial* meshMat = m_mesh->GetMeshMaterial(matid);
00192                 if (meshMat->m_bucket == polyBucket)
00193                         // found it
00194                         break;
00195         }
00196         return PyLong_FromSsize_t(matid);
00197 }
00198 
00199 KX_PYMETHODDEF_DOC_NOARGS(KX_PolyProxy, getNumVertex,
00200 "getNumVertex() : returns the number of vertex of the polygon, 3 or 4\n")
00201 {
00202         return PyLong_FromSsize_t(m_polygon->VertexCount());
00203 }
00204 
00205 KX_PYMETHODDEF_DOC_NOARGS(KX_PolyProxy, isVisible,
00206 "isVisible() : returns whether the polygon is visible or not\n")
00207 {
00208         return PyLong_FromSsize_t(m_polygon->IsVisible());
00209 }
00210 
00211 KX_PYMETHODDEF_DOC_NOARGS(KX_PolyProxy, isCollider,
00212 "isCollider() : returns whether the polygon is receives collision or not\n")
00213 {
00214         return PyLong_FromSsize_t(m_polygon->IsCollider());
00215 }
00216 
00217 KX_PYMETHODDEF_DOC_NOARGS(KX_PolyProxy, getMaterialName,
00218 "getMaterialName() : returns the polygon material name, \"NoMaterial\" if no material\n")
00219 {
00220         return PyUnicode_FromString(m_polygon->GetMaterial()->GetPolyMaterial()->GetMaterialName());
00221 }
00222 
00223 KX_PYMETHODDEF_DOC_NOARGS(KX_PolyProxy, getTextureName,
00224 "getTexturelName() : returns the polygon texture name, \"NULL\" if no texture\n")
00225 {
00226         return PyUnicode_FromString(m_polygon->GetMaterial()->GetPolyMaterial()->GetTextureName());
00227 }
00228 
00229 KX_PYMETHODDEF_DOC(KX_PolyProxy, getVertexIndex,
00230 "getVertexIndex(vertex) : returns the mesh vertex index of a polygon vertex\n"
00231 "vertex: index of the vertex in the polygon: 0->3\n"
00232 "return value can be used to retrieve the vertex details through mesh proxy\n"
00233 "Note: getVertexIndex(3) on a triangle polygon returns 0\n")
00234 {
00235         int index;
00236         if (!PyArg_ParseTuple(args,"i:getVertexIndex",&index))
00237         {
00238                 return NULL;
00239         }
00240         if (index < 0 || index > 3)
00241         {
00242                 PyErr_SetString(PyExc_AttributeError, "poly.getVertexIndex(int): KX_PolyProxy, expected an index between 0-3");
00243                 return NULL;
00244         }
00245         if (index < m_polygon->VertexCount())
00246         {
00247                 return PyLong_FromSsize_t(m_polygon->GetVertexOffsetAbs(m_mesh, index));
00248         }
00249         return PyLong_FromSsize_t(0);
00250 }
00251 
00252 KX_PYMETHODDEF_DOC_NOARGS(KX_PolyProxy, getMesh,
00253 "getMesh() : returns a mesh proxy\n")
00254 {
00255         KX_MeshProxy* meshproxy = new KX_MeshProxy((RAS_MeshObject*)m_mesh);
00256         return meshproxy->NewProxy(true);
00257 }
00258 
00259 KX_PYMETHODDEF_DOC_NOARGS(KX_PolyProxy, getMaterial,
00260 "getMaterial() : returns a material\n")
00261 {
00262         RAS_IPolyMaterial *polymat = m_polygon->GetMaterial()->GetPolyMaterial();
00263         if(polymat->GetFlag() & RAS_BLENDERMAT)
00264         {
00265                 KX_BlenderMaterial* mat = static_cast<KX_BlenderMaterial*>(polymat);
00266                 return mat->GetProxy();
00267         }
00268         else
00269         {
00270                 KX_PolygonMaterial* mat = static_cast<KX_PolygonMaterial*>(polymat);
00271                 return mat->GetProxy();
00272         }
00273 }
00274 
00275 #endif // WITH_PYTHON