Blender V2.61 - r43446

KX_PolyProxy.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_PolyProxy.h"
00036 #include "KX_MeshProxy.h"
00037 #include "RAS_MeshObject.h"
00038 #include "KX_BlenderMaterial.h"
00039 #include "KX_PolygonMaterial.h"
00040 
00041 #include "KX_PyMath.h"
00042 
00043 PyTypeObject KX_PolyProxy::Type = {
00044     PyVarObject_HEAD_INIT(NULL, 0)
00045     "KX_PolyProxy",
00046     sizeof(PyObjectPlus_Proxy),
00047     0,
00048     py_base_dealloc,
00049     0,
00050     0,
00051     0,
00052     0,
00053     py_base_repr,
00054     0,0,0,0,0,0,0,0,0,
00055     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
00056     0,0,0,0,0,0,0,
00057     Methods,
00058     0,
00059     0,
00060     &CValue::Type,
00061     0,0,0,0,0,0,
00062     py_base_new
00063 };
00064 
00065 PyMethodDef KX_PolyProxy::Methods[] = {
00066     KX_PYMETHODTABLE_NOARGS(KX_PolyProxy,getMaterialIndex),
00067     KX_PYMETHODTABLE_NOARGS(KX_PolyProxy,getNumVertex),
00068     KX_PYMETHODTABLE_NOARGS(KX_PolyProxy,isVisible),
00069     KX_PYMETHODTABLE_NOARGS(KX_PolyProxy,isCollider),
00070     KX_PYMETHODTABLE_NOARGS(KX_PolyProxy,getMaterialName),
00071     KX_PYMETHODTABLE_NOARGS(KX_PolyProxy,getTextureName),
00072     KX_PYMETHODTABLE(KX_PolyProxy,getVertexIndex),
00073     KX_PYMETHODTABLE_NOARGS(KX_PolyProxy,getMesh),
00074     KX_PYMETHODTABLE_NOARGS(KX_PolyProxy,getMaterial),
00075     {NULL,NULL} //Sentinel
00076 };
00077 
00078 PyAttributeDef KX_PolyProxy::Attributes[] = {
00079     KX_PYATTRIBUTE_RO_FUNCTION("material_name", KX_PolyProxy, pyattr_get_material_name),
00080     KX_PYATTRIBUTE_RO_FUNCTION("texture_name", KX_PolyProxy, pyattr_get_texture_name),
00081     KX_PYATTRIBUTE_RO_FUNCTION("material", KX_PolyProxy, pyattr_get_material),
00082     KX_PYATTRIBUTE_RO_FUNCTION("material_id", KX_PolyProxy, pyattr_get_material_id),
00083     KX_PYATTRIBUTE_RO_FUNCTION("v1", KX_PolyProxy, pyattr_get_v1),
00084     KX_PYATTRIBUTE_RO_FUNCTION("v2", KX_PolyProxy, pyattr_get_v2),
00085     KX_PYATTRIBUTE_RO_FUNCTION("v3", KX_PolyProxy, pyattr_get_v3),
00086     KX_PYATTRIBUTE_RO_FUNCTION("v4", KX_PolyProxy, pyattr_get_v4),
00087     KX_PYATTRIBUTE_RO_FUNCTION("visible", KX_PolyProxy, pyattr_get_visible),
00088     KX_PYATTRIBUTE_RO_FUNCTION("collide", KX_PolyProxy, pyattr_get_collide),
00089     { NULL }    //Sentinel
00090 };
00091 
00092 KX_PolyProxy::KX_PolyProxy(const RAS_MeshObject*mesh, RAS_Polygon* polygon)
00093 :   m_polygon(polygon),
00094     m_mesh((RAS_MeshObject*)mesh)
00095 {
00096 }
00097 
00098 KX_PolyProxy::~KX_PolyProxy()
00099 {
00100 }
00101 
00102 
00103 // stuff for cvalue related things
00104 CValue*     KX_PolyProxy::Calc(VALUE_OPERATOR, CValue *) { return NULL;}
00105 CValue*     KX_PolyProxy::CalcFinal(VALUE_DATA_TYPE, VALUE_OPERATOR, CValue *) { return NULL;}  
00106 STR_String  sPolyName="polygone";
00107 const STR_String &  KX_PolyProxy::GetText() {return sPolyName;};
00108 double      KX_PolyProxy::GetNumber() { return -1;}
00109 STR_String& KX_PolyProxy::GetName() { return sPolyName;}
00110 void        KX_PolyProxy::SetName(const char *) { };
00111 CValue*     KX_PolyProxy::GetReplica() { return NULL;}
00112 
00113 // stuff for python integration
00114 
00115 PyObject* KX_PolyProxy::pyattr_get_material_name(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
00116 {
00117     KX_PolyProxy* self= static_cast<KX_PolyProxy*>(self_v);
00118     return self->PygetMaterialName();
00119 }
00120 
00121 PyObject* KX_PolyProxy::pyattr_get_texture_name(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
00122 {
00123     KX_PolyProxy* self= static_cast<KX_PolyProxy*>(self_v);
00124     return self->PygetTextureName();
00125 }
00126 
00127 PyObject* KX_PolyProxy::pyattr_get_material(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
00128 {
00129     KX_PolyProxy* self= static_cast<KX_PolyProxy*>(self_v);
00130     return self->PygetMaterial();
00131 }
00132 
00133 PyObject* KX_PolyProxy::pyattr_get_material_id(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
00134 {
00135     KX_PolyProxy* self= static_cast<KX_PolyProxy*>(self_v);
00136     return self->PygetMaterialIndex();
00137 }
00138 
00139 PyObject* KX_PolyProxy::pyattr_get_v1(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
00140 {
00141     KX_PolyProxy* self= static_cast<KX_PolyProxy*>(self_v);
00142 
00143     return PyLong_FromSsize_t(self->m_polygon->GetVertexOffsetAbs(self->m_mesh, 0));
00144 }
00145 
00146 PyObject* KX_PolyProxy::pyattr_get_v2(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
00147 {
00148     KX_PolyProxy* self= static_cast<KX_PolyProxy*>(self_v);
00149 
00150     return PyLong_FromSsize_t(self->m_polygon->GetVertexOffsetAbs(self->m_mesh, 1));
00151 }
00152 
00153 PyObject* KX_PolyProxy::pyattr_get_v3(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
00154 {
00155     KX_PolyProxy* self= static_cast<KX_PolyProxy*>(self_v);
00156 
00157     return PyLong_FromSsize_t(self->m_polygon->GetVertexOffsetAbs(self->m_mesh, 2));
00158 }
00159 
00160 PyObject* KX_PolyProxy::pyattr_get_v4(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
00161 {
00162     KX_PolyProxy* self= static_cast<KX_PolyProxy*>(self_v);
00163 
00164     if (3 < self->m_polygon->VertexCount())
00165     {
00166         return PyLong_FromSsize_t(self->m_polygon->GetVertexOffsetAbs(self->m_mesh, 3));
00167     }
00168     return PyLong_FromSsize_t(0);
00169 }
00170 
00171 PyObject* KX_PolyProxy::pyattr_get_visible(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
00172 {
00173     KX_PolyProxy* self= static_cast<KX_PolyProxy*>(self_v);
00174     return self->PyisVisible();
00175 }
00176 
00177 PyObject* KX_PolyProxy::pyattr_get_collide(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
00178 {
00179     KX_PolyProxy* self= static_cast<KX_PolyProxy*>(self_v);
00180     return self->PyisCollider();
00181 }
00182 
00183 KX_PYMETHODDEF_DOC_NOARGS(KX_PolyProxy, getMaterialIndex,
00184 "getMaterialIndex() : return the material index of the polygon in the mesh\n")
00185 {
00186     RAS_MaterialBucket* polyBucket = m_polygon->GetMaterial();
00187     unsigned int matid;
00188     for (matid=0; matid<(unsigned int)m_mesh->NumMaterials(); matid++)
00189     {
00190         RAS_MeshMaterial* meshMat = m_mesh->GetMeshMaterial(matid);
00191         if (meshMat->m_bucket == polyBucket)
00192             // found it
00193             break;
00194     }
00195     return PyLong_FromSsize_t(matid);
00196 }
00197 
00198 KX_PYMETHODDEF_DOC_NOARGS(KX_PolyProxy, getNumVertex,
00199 "getNumVertex() : returns the number of vertex of the polygon, 3 or 4\n")
00200 {
00201     return PyLong_FromSsize_t(m_polygon->VertexCount());
00202 }
00203 
00204 KX_PYMETHODDEF_DOC_NOARGS(KX_PolyProxy, isVisible,
00205 "isVisible() : returns whether the polygon is visible or not\n")
00206 {
00207     return PyLong_FromSsize_t(m_polygon->IsVisible());
00208 }
00209 
00210 KX_PYMETHODDEF_DOC_NOARGS(KX_PolyProxy, isCollider,
00211 "isCollider() : returns whether the polygon is receives collision or not\n")
00212 {
00213     return PyLong_FromSsize_t(m_polygon->IsCollider());
00214 }
00215 
00216 KX_PYMETHODDEF_DOC_NOARGS(KX_PolyProxy, getMaterialName,
00217 "getMaterialName() : returns the polygon material name, \"NoMaterial\" if no material\n")
00218 {
00219     return PyUnicode_From_STR_String(m_polygon->GetMaterial()->GetPolyMaterial()->GetMaterialName());
00220 }
00221 
00222 KX_PYMETHODDEF_DOC_NOARGS(KX_PolyProxy, getTextureName,
00223 "getTexturelName() : returns the polygon texture name, \"NULL\" if no texture\n")
00224 {
00225     return PyUnicode_From_STR_String(m_polygon->GetMaterial()->GetPolyMaterial()->GetTextureName());
00226 }
00227 
00228 KX_PYMETHODDEF_DOC(KX_PolyProxy, getVertexIndex,
00229 "getVertexIndex(vertex) : returns the mesh vertex index of a polygon vertex\n"
00230 "vertex: index of the vertex in the polygon: 0->3\n"
00231 "return value can be used to retrieve the vertex details through mesh proxy\n"
00232 "Note: getVertexIndex(3) on a triangle polygon returns 0\n")
00233 {
00234     int index;
00235     if (!PyArg_ParseTuple(args,"i:getVertexIndex",&index))
00236     {
00237         return NULL;
00238     }
00239     if (index < 0 || index > 3)
00240     {
00241         PyErr_SetString(PyExc_AttributeError, "poly.getVertexIndex(int): KX_PolyProxy, expected an index between 0-3");
00242         return NULL;
00243     }
00244     if (index < m_polygon->VertexCount())
00245     {
00246         return PyLong_FromSsize_t(m_polygon->GetVertexOffsetAbs(m_mesh, index));
00247     }
00248     return PyLong_FromSsize_t(0);
00249 }
00250 
00251 KX_PYMETHODDEF_DOC_NOARGS(KX_PolyProxy, getMesh,
00252 "getMesh() : returns a mesh proxy\n")
00253 {
00254     KX_MeshProxy* meshproxy = new KX_MeshProxy((RAS_MeshObject*)m_mesh);
00255     return meshproxy->NewProxy(true);
00256 }
00257 
00258 KX_PYMETHODDEF_DOC_NOARGS(KX_PolyProxy, getMaterial,
00259 "getMaterial() : returns a material\n")
00260 {
00261     RAS_IPolyMaterial *polymat = m_polygon->GetMaterial()->GetPolyMaterial();
00262     if(polymat->GetFlag() & RAS_BLENDERMAT)
00263     {
00264         KX_BlenderMaterial* mat = static_cast<KX_BlenderMaterial*>(polymat);
00265         return mat->GetProxy();
00266     }
00267     else
00268     {
00269         KX_PolygonMaterial* mat = static_cast<KX_PolygonMaterial*>(polymat);
00270         return mat->GetProxy();
00271     }
00272 }
00273 
00274 #endif // WITH_PYTHON