2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
21 * The Original Code is: all of this file.
23 * Contributor(s): none yet.
25 * ***** END GPL LICENSE BLOCK *****
28 /** \file gameengine/Ketsji/KX_MeshProxy.cpp
35 #include "KX_MeshProxy.h"
36 #include "RAS_IPolygonMaterial.h"
37 #include "RAS_MeshObject.h"
39 #include "KX_VertexProxy.h"
40 #include "KX_PolyProxy.h"
42 #include "KX_PolygonMaterial.h"
43 #include "KX_BlenderMaterial.h"
45 #include "KX_PyMath.h"
46 #include "KX_ConvertPhysicsObject.h"
48 #include "PyObjectPlus.h"
50 PyTypeObject KX_MeshProxy::Type = {
51 PyVarObject_HEAD_INIT(NULL, 0)
53 sizeof(PyObjectPlus_Proxy),
62 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
72 PyMethodDef KX_MeshProxy::Methods[] = {
73 {"getMaterialName", (PyCFunction)KX_MeshProxy::sPyGetMaterialName,METH_VARARGS},
74 {"getTextureName", (PyCFunction)KX_MeshProxy::sPyGetTextureName,METH_VARARGS},
75 {"getVertexArrayLength", (PyCFunction)KX_MeshProxy::sPyGetVertexArrayLength,METH_VARARGS},
76 {"getVertex", (PyCFunction)KX_MeshProxy::sPyGetVertex,METH_VARARGS},
77 {"getPolygon", (PyCFunction)KX_MeshProxy::sPyGetPolygon,METH_VARARGS},
78 //{"getIndexArrayLength", (PyCFunction)KX_MeshProxy::sPyGetIndexArrayLength,METH_VARARGS},
79 {NULL,NULL} //Sentinel
82 PyAttributeDef KX_MeshProxy::Attributes[] = {
83 KX_PYATTRIBUTE_RO_FUNCTION("materials", KX_MeshProxy, pyattr_get_materials),
84 KX_PYATTRIBUTE_RO_FUNCTION("numPolygons", KX_MeshProxy, pyattr_get_numPolygons),
85 KX_PYATTRIBUTE_RO_FUNCTION("numMaterials", KX_MeshProxy, pyattr_get_numMaterials),
90 void KX_MeshProxy::SetMeshModified(bool v)
92 m_meshobj->SetMeshModified(v);
95 KX_MeshProxy::KX_MeshProxy(RAS_MeshObject* mesh)
96 : CValue(), m_meshobj(mesh)
100 KX_MeshProxy::~KX_MeshProxy()
106 // stuff for cvalue related things
107 CValue* KX_MeshProxy::Calc(VALUE_OPERATOR op, CValue *val) { return NULL;}
108 CValue* KX_MeshProxy::CalcFinal(VALUE_DATA_TYPE dtype, VALUE_OPERATOR op, CValue *val) { return NULL;}
110 const STR_String & KX_MeshProxy::GetText() {return m_meshobj->GetName();};
111 double KX_MeshProxy::GetNumber() { return -1;}
112 STR_String& KX_MeshProxy::GetName() { return m_meshobj->GetName();}
113 void KX_MeshProxy::SetName(const char *name) { };
114 CValue* KX_MeshProxy::GetReplica() { return NULL;}
117 // stuff for python integration
119 PyObject *KX_MeshProxy::PyGetMaterialName(PyObject *args, PyObject *kwds)
124 if (PyArg_ParseTuple(args,"i:getMaterialName",&matid))
126 matname = m_meshobj->GetMaterialName(matid);
132 return PyUnicode_From_STR_String(matname);
137 PyObject *KX_MeshProxy::PyGetTextureName(PyObject *args, PyObject *kwds)
142 if (PyArg_ParseTuple(args,"i:getTextureName",&matid))
144 matname = m_meshobj->GetTextureName(matid);
150 return PyUnicode_From_STR_String(matname);
154 PyObject *KX_MeshProxy::PyGetVertexArrayLength(PyObject *args, PyObject *kwds)
160 if (!PyArg_ParseTuple(args,"i:getVertexArrayLength",&matid))
164 RAS_MeshMaterial *mmat = m_meshobj->GetMeshMaterial(matid); /* can be NULL*/
168 RAS_IPolyMaterial* mat = mmat->m_bucket->GetPolyMaterial();
170 length = m_meshobj->NumVertices(mat);
173 return PyLong_FromSsize_t(length);
177 PyObject *KX_MeshProxy::PyGetVertex(PyObject *args, PyObject *kwds)
182 if (!PyArg_ParseTuple(args,"ii:getVertex",&matindex,&vertexindex))
185 RAS_TexVert* vertex = m_meshobj->GetVertex(matindex,vertexindex);
188 PyErr_SetString(PyExc_ValueError, "mesh.getVertex(mat_idx, vert_idx): KX_MeshProxy, could not get a vertex at the given indices");
192 return (new KX_VertexProxy(this, vertex))->NewProxy(true);
195 PyObject *KX_MeshProxy::PyGetPolygon(PyObject *args, PyObject *kwds)
198 PyObject *polyob = NULL;
200 if (!PyArg_ParseTuple(args,"i:getPolygon",&polyindex))
203 if (polyindex<0 || polyindex >= m_meshobj->NumPolygons())
205 PyErr_SetString(PyExc_AttributeError, "mesh.getPolygon(int): KX_MeshProxy, invalid polygon index");
210 RAS_Polygon* polygon = m_meshobj->GetPolygon(polyindex);
213 polyob = (new KX_PolyProxy(m_meshobj, polygon))->NewProxy(true);
216 PyErr_SetString(PyExc_AttributeError, "mesh.getPolygon(int): KX_MeshProxy, polygon is NULL, unknown reason");
221 PyObject *KX_MeshProxy::pyattr_get_materials(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
223 KX_MeshProxy* self = static_cast<KX_MeshProxy*>(self_v);
225 int tot= self->m_meshobj->NumMaterials();
228 PyObject *materials = PyList_New( tot );
230 list<RAS_MeshMaterial>::iterator mit= self->m_meshobj->GetFirstMaterial();
233 for (i=0; i<tot; mit++, i++) {
234 RAS_IPolyMaterial *polymat = mit->m_bucket->GetPolyMaterial();
236 /* Why do we need to check for RAS_BLENDERMAT if both are cast to a (PyObject *)? - Campbell */
237 if (polymat->GetFlag() & RAS_BLENDERMAT)
239 KX_BlenderMaterial *mat = static_cast<KX_BlenderMaterial*>(polymat);
240 PyList_SET_ITEM(materials, i, mat->GetProxy());
243 KX_PolygonMaterial *mat = static_cast<KX_PolygonMaterial*>(polymat);
244 PyList_SET_ITEM(materials, i, mat->GetProxy());
250 PyObject * KX_MeshProxy::pyattr_get_numMaterials(void * selfv, const KX_PYATTRIBUTE_DEF * attrdef)
252 KX_MeshProxy * self = static_cast<KX_MeshProxy *> (selfv);
253 return PyLong_FromSsize_t(self->m_meshobj->NumMaterials());
256 PyObject * KX_MeshProxy::pyattr_get_numPolygons(void * selfv, const KX_PYATTRIBUTE_DEF * attrdef)
258 KX_MeshProxy * self = static_cast<KX_MeshProxy *> (selfv);
259 return PyLong_FromSsize_t(self->m_meshobj->NumPolygons());
262 /* a close copy of ConvertPythonToGameObject but for meshes */
263 bool ConvertPythonToMesh(PyObject *value, RAS_MeshObject **object, bool py_none_ok, const char *error_prefix)
266 PyErr_Format(PyExc_TypeError, "%s, python pointer NULL, should never happen", error_prefix);
271 if (value==Py_None) {
277 PyErr_Format(PyExc_TypeError, "%s, expected KX_MeshProxy or a KX_MeshProxy name, None is invalid", error_prefix);
282 if (PyUnicode_Check(value)) {
283 *object = (RAS_MeshObject*)SCA_ILogicBrick::m_sCurrentLogicManager->GetMeshByName(STR_String( _PyUnicode_AsString(value) ));
288 PyErr_Format(PyExc_ValueError, "%s, requested name \"%s\" did not match any KX_MeshProxy in this scene", error_prefix, _PyUnicode_AsString(value));
293 if (PyObject_TypeCheck(value, &KX_MeshProxy::Type)) {
294 KX_MeshProxy *kx_mesh = static_cast<KX_MeshProxy*>BGE_PROXY_REF(value);
298 PyErr_Format(PyExc_SystemError, "%s, " BGE_PROXY_ERROR_MSG, error_prefix);
302 *object = kx_mesh->GetMesh();
309 PyErr_Format(PyExc_TypeError, "%s, expect a KX_MeshProxy, a string or None", error_prefix);
311 PyErr_Format(PyExc_TypeError, "%s, expect a KX_MeshProxy or a string", error_prefix);
317 #endif // WITH_PYTHON