|
Blender
V2.59
|
00001 /* 00002 * $Id: KX_MeshProxy.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_MeshProxy.h" 00037 #include "RAS_IPolygonMaterial.h" 00038 #include "RAS_MeshObject.h" 00039 00040 #include "KX_VertexProxy.h" 00041 #include "KX_PolyProxy.h" 00042 00043 #include "KX_PolygonMaterial.h" 00044 #include "KX_BlenderMaterial.h" 00045 00046 #include "KX_PyMath.h" 00047 #include "KX_ConvertPhysicsObject.h" 00048 00049 #include "PyObjectPlus.h" 00050 00051 PyTypeObject KX_MeshProxy::Type = { 00052 PyVarObject_HEAD_INIT(NULL, 0) 00053 "KX_MeshProxy", 00054 sizeof(PyObjectPlus_Proxy), 00055 0, 00056 py_base_dealloc, 00057 0, 00058 0, 00059 0, 00060 0, 00061 py_base_repr, 00062 0,0,0,0,0,0,0,0,0, 00063 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, 00064 0,0,0,0,0,0,0, 00065 Methods, 00066 0, 00067 0, 00068 &CValue::Type, 00069 0,0,0,0,0,0, 00070 py_base_new 00071 }; 00072 00073 PyMethodDef KX_MeshProxy::Methods[] = { 00074 {"getMaterialName", (PyCFunction)KX_MeshProxy::sPyGetMaterialName,METH_VARARGS}, 00075 {"getTextureName", (PyCFunction)KX_MeshProxy::sPyGetTextureName,METH_VARARGS}, 00076 {"getVertexArrayLength", (PyCFunction)KX_MeshProxy::sPyGetVertexArrayLength,METH_VARARGS}, 00077 {"getVertex", (PyCFunction)KX_MeshProxy::sPyGetVertex,METH_VARARGS}, 00078 {"getPolygon", (PyCFunction)KX_MeshProxy::sPyGetPolygon,METH_VARARGS}, 00079 //{"getIndexArrayLength", (PyCFunction)KX_MeshProxy::sPyGetIndexArrayLength,METH_VARARGS}, 00080 {NULL,NULL} //Sentinel 00081 }; 00082 00083 PyAttributeDef KX_MeshProxy::Attributes[] = { 00084 KX_PYATTRIBUTE_RO_FUNCTION("materials", KX_MeshProxy, pyattr_get_materials), 00085 KX_PYATTRIBUTE_RO_FUNCTION("numPolygons", KX_MeshProxy, pyattr_get_numPolygons), 00086 KX_PYATTRIBUTE_RO_FUNCTION("numMaterials", KX_MeshProxy, pyattr_get_numMaterials), 00087 00088 { NULL } //Sentinel 00089 }; 00090 00091 void KX_MeshProxy::SetMeshModified(bool v) 00092 { 00093 m_meshobj->SetMeshModified(v); 00094 } 00095 00096 KX_MeshProxy::KX_MeshProxy(RAS_MeshObject* mesh) 00097 : CValue(), m_meshobj(mesh) 00098 { 00099 } 00100 00101 KX_MeshProxy::~KX_MeshProxy() 00102 { 00103 } 00104 00105 00106 00107 // stuff for cvalue related things 00108 CValue* KX_MeshProxy::Calc(VALUE_OPERATOR op, CValue *val) { return NULL;} 00109 CValue* KX_MeshProxy::CalcFinal(VALUE_DATA_TYPE dtype, VALUE_OPERATOR op, CValue *val) { return NULL;} 00110 00111 const STR_String & KX_MeshProxy::GetText() {return m_meshobj->GetName();}; 00112 double KX_MeshProxy::GetNumber() { return -1;} 00113 STR_String& KX_MeshProxy::GetName() { return m_meshobj->GetName();} 00114 void KX_MeshProxy::SetName(const char *name) { }; 00115 CValue* KX_MeshProxy::GetReplica() { return NULL;} 00116 00117 00118 // stuff for python integration 00119 00120 PyObject* KX_MeshProxy::PyGetMaterialName(PyObject* args, PyObject* kwds) 00121 { 00122 int matid= 1; 00123 STR_String matname; 00124 00125 if (PyArg_ParseTuple(args,"i:getMaterialName",&matid)) 00126 { 00127 matname = m_meshobj->GetMaterialName(matid); 00128 } 00129 else { 00130 return NULL; 00131 } 00132 00133 return PyUnicode_FromString(matname.Ptr()); 00134 00135 } 00136 00137 00138 PyObject* KX_MeshProxy::PyGetTextureName(PyObject* args, PyObject* kwds) 00139 { 00140 int matid= 1; 00141 STR_String matname; 00142 00143 if (PyArg_ParseTuple(args,"i:getTextureName",&matid)) 00144 { 00145 matname = m_meshobj->GetTextureName(matid); 00146 } 00147 else { 00148 return NULL; 00149 } 00150 00151 return PyUnicode_FromString(matname.Ptr()); 00152 00153 } 00154 00155 PyObject* KX_MeshProxy::PyGetVertexArrayLength(PyObject* args, PyObject* kwds) 00156 { 00157 int matid= 0; 00158 int length = 0; 00159 00160 00161 if (!PyArg_ParseTuple(args,"i:getVertexArrayLength",&matid)) 00162 return NULL; 00163 00164 00165 RAS_MeshMaterial *mmat = m_meshobj->GetMeshMaterial(matid); /* can be NULL*/ 00166 00167 if (mmat) 00168 { 00169 RAS_IPolyMaterial* mat = mmat->m_bucket->GetPolyMaterial(); 00170 if (mat) 00171 length = m_meshobj->NumVertices(mat); 00172 } 00173 00174 return PyLong_FromSsize_t(length); 00175 } 00176 00177 00178 PyObject* KX_MeshProxy::PyGetVertex(PyObject* args, PyObject* kwds) 00179 { 00180 int vertexindex; 00181 int matindex; 00182 00183 if (!PyArg_ParseTuple(args,"ii:getVertex",&matindex,&vertexindex)) 00184 return NULL; 00185 00186 RAS_TexVert* vertex = m_meshobj->GetVertex(matindex,vertexindex); 00187 00188 if(vertex==NULL) { 00189 PyErr_SetString(PyExc_ValueError, "mesh.getVertex(mat_idx, vert_idx): KX_MeshProxy, could not get a vertex at the given indices"); 00190 return NULL; 00191 } 00192 00193 return (new KX_VertexProxy(this, vertex))->NewProxy(true); 00194 } 00195 00196 PyObject* KX_MeshProxy::PyGetPolygon(PyObject* args, PyObject* kwds) 00197 { 00198 int polyindex= 1; 00199 PyObject* polyob = NULL; 00200 00201 if (!PyArg_ParseTuple(args,"i:getPolygon",&polyindex)) 00202 return NULL; 00203 00204 if (polyindex<0 || polyindex >= m_meshobj->NumPolygons()) 00205 { 00206 PyErr_SetString(PyExc_AttributeError, "mesh.getPolygon(int): KX_MeshProxy, invalid polygon index"); 00207 return NULL; 00208 } 00209 00210 00211 RAS_Polygon* polygon = m_meshobj->GetPolygon(polyindex); 00212 if (polygon) 00213 { 00214 polyob = (new KX_PolyProxy(m_meshobj, polygon))->NewProxy(true); 00215 } 00216 else { 00217 PyErr_SetString(PyExc_AttributeError, "mesh.getPolygon(int): KX_MeshProxy, polygon is NULL, unknown reason"); 00218 } 00219 return polyob; 00220 } 00221 00222 PyObject* KX_MeshProxy::pyattr_get_materials(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef) 00223 { 00224 KX_MeshProxy* self= static_cast<KX_MeshProxy*>(self_v); 00225 00226 int tot= self->m_meshobj->NumMaterials(); 00227 int i; 00228 00229 PyObject *materials = PyList_New( tot ); 00230 00231 list<RAS_MeshMaterial>::iterator mit= self->m_meshobj->GetFirstMaterial(); 00232 00233 00234 for(i=0; i<tot; mit++, i++) { 00235 RAS_IPolyMaterial *polymat = mit->m_bucket->GetPolyMaterial(); 00236 00237 /* Why do we need to check for RAS_BLENDERMAT if both are cast to a (PyObject*)? - Campbell */ 00238 if(polymat->GetFlag() & RAS_BLENDERMAT) 00239 { 00240 KX_BlenderMaterial *mat = static_cast<KX_BlenderMaterial*>(polymat); 00241 PyList_SET_ITEM(materials, i, mat->GetProxy()); 00242 } 00243 else { 00244 KX_PolygonMaterial *mat = static_cast<KX_PolygonMaterial*>(polymat); 00245 PyList_SET_ITEM(materials, i, mat->GetProxy()); 00246 } 00247 } 00248 return materials; 00249 } 00250 00251 PyObject * KX_MeshProxy::pyattr_get_numMaterials(void * selfv, const KX_PYATTRIBUTE_DEF * attrdef) { 00252 KX_MeshProxy * self = static_cast<KX_MeshProxy *> (selfv); 00253 return PyLong_FromSsize_t(self->m_meshobj->NumMaterials()); 00254 } 00255 00256 PyObject * KX_MeshProxy::pyattr_get_numPolygons(void * selfv, const KX_PYATTRIBUTE_DEF * attrdef) { 00257 KX_MeshProxy * self = static_cast<KX_MeshProxy *> (selfv); 00258 return PyLong_FromSsize_t(self->m_meshobj->NumPolygons()); 00259 } 00260 00261 /* a close copy of ConvertPythonToGameObject but for meshes */ 00262 bool ConvertPythonToMesh(PyObject * value, RAS_MeshObject **object, bool py_none_ok, const char *error_prefix) 00263 { 00264 if (value==NULL) { 00265 PyErr_Format(PyExc_TypeError, "%s, python pointer NULL, should never happen", error_prefix); 00266 *object = NULL; 00267 return false; 00268 } 00269 00270 if (value==Py_None) { 00271 *object = NULL; 00272 00273 if (py_none_ok) { 00274 return true; 00275 } else { 00276 PyErr_Format(PyExc_TypeError, "%s, expected KX_MeshProxy or a KX_MeshProxy name, None is invalid", error_prefix); 00277 return false; 00278 } 00279 } 00280 00281 if (PyUnicode_Check(value)) { 00282 *object = (RAS_MeshObject*)SCA_ILogicBrick::m_sCurrentLogicManager->GetMeshByName(STR_String( _PyUnicode_AsString(value) )); 00283 00284 if (*object) { 00285 return true; 00286 } else { 00287 PyErr_Format(PyExc_ValueError, "%s, requested name \"%s\" did not match any KX_MeshProxy in this scene", error_prefix, _PyUnicode_AsString(value)); 00288 return false; 00289 } 00290 } 00291 00292 if (PyObject_TypeCheck(value, &KX_MeshProxy::Type)) { 00293 KX_MeshProxy *kx_mesh = static_cast<KX_MeshProxy*>BGE_PROXY_REF(value); 00294 00295 /* sets the error */ 00296 if (kx_mesh==NULL) { 00297 PyErr_Format(PyExc_SystemError, "%s, " BGE_PROXY_ERROR_MSG, error_prefix); 00298 return false; 00299 } 00300 00301 *object = kx_mesh->GetMesh(); 00302 return true; 00303 } 00304 00305 *object = NULL; 00306 00307 if (py_none_ok) { 00308 PyErr_Format(PyExc_TypeError, "%s, expect a KX_MeshProxy, a string or None", error_prefix); 00309 } else { 00310 PyErr_Format(PyExc_TypeError, "%s, expect a KX_MeshProxy or a string", error_prefix); 00311 } 00312 00313 return false; 00314 } 00315 00316 #endif // WITH_PYTHON