doxygen: prevent GPL license block from being parsed as doxygen comment.
[blender.git] / source / gameengine / Ketsji / KX_MeshProxy.cpp
1 /*
2  * $Id$
3  * ***** BEGIN GPL LICENSE BLOCK *****
4  *
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.
9  *
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.
14  *
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  *
19  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20  * All rights reserved.
21  *
22  * The Original Code is: all of this file.
23  *
24  * Contributor(s): none yet.
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #ifdef WITH_PYTHON
30
31 #include "KX_MeshProxy.h"
32 #include "RAS_IPolygonMaterial.h"
33 #include "RAS_MeshObject.h"
34
35 #include "KX_VertexProxy.h"
36 #include "KX_PolyProxy.h"
37
38 #include "KX_PolygonMaterial.h"
39 #include "KX_BlenderMaterial.h"
40
41 #include "KX_PyMath.h"
42 #include "KX_ConvertPhysicsObject.h"
43
44 #include "PyObjectPlus.h" 
45
46 PyTypeObject KX_MeshProxy::Type = {
47         PyVarObject_HEAD_INIT(NULL, 0)
48         "KX_MeshProxy",
49         sizeof(PyObjectPlus_Proxy),
50         0,
51         py_base_dealloc,
52         0,
53         0,
54         0,
55         0,
56         py_base_repr,
57         0,0,0,0,0,0,0,0,0,
58         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
59         0,0,0,0,0,0,0,
60         Methods,
61         0,
62         0,
63         &CValue::Type,
64         0,0,0,0,0,0,
65         py_base_new
66 };
67
68 PyMethodDef KX_MeshProxy::Methods[] = {
69 {"getMaterialName", (PyCFunction)KX_MeshProxy::sPyGetMaterialName,METH_VARARGS},
70 {"getTextureName", (PyCFunction)KX_MeshProxy::sPyGetTextureName,METH_VARARGS},
71 {"getVertexArrayLength", (PyCFunction)KX_MeshProxy::sPyGetVertexArrayLength,METH_VARARGS},
72 {"getVertex", (PyCFunction)KX_MeshProxy::sPyGetVertex,METH_VARARGS},
73 {"getPolygon", (PyCFunction)KX_MeshProxy::sPyGetPolygon,METH_VARARGS},
74 //{"getIndexArrayLength", (PyCFunction)KX_MeshProxy::sPyGetIndexArrayLength,METH_VARARGS},
75   {NULL,NULL} //Sentinel
76 };
77
78 PyAttributeDef KX_MeshProxy::Attributes[] = {
79         KX_PYATTRIBUTE_RO_FUNCTION("materials",         KX_MeshProxy, pyattr_get_materials),
80         KX_PYATTRIBUTE_RO_FUNCTION("numPolygons",       KX_MeshProxy, pyattr_get_numPolygons),
81         KX_PYATTRIBUTE_RO_FUNCTION("numMaterials",      KX_MeshProxy, pyattr_get_numMaterials),
82
83         { NULL }        //Sentinel
84 };
85
86 void KX_MeshProxy::SetMeshModified(bool v)
87 {
88         m_meshobj->SetMeshModified(v);
89 }
90
91 KX_MeshProxy::KX_MeshProxy(RAS_MeshObject* mesh)
92         : CValue(), m_meshobj(mesh)
93 {
94 }
95
96 KX_MeshProxy::~KX_MeshProxy()
97 {
98 }
99
100
101
102 // stuff for cvalue related things
103 CValue*         KX_MeshProxy::Calc(VALUE_OPERATOR op, CValue *val) { return NULL;}
104 CValue*         KX_MeshProxy::CalcFinal(VALUE_DATA_TYPE dtype, VALUE_OPERATOR op, CValue *val) { return NULL;}  
105
106 const STR_String &      KX_MeshProxy::GetText() {return m_meshobj->GetName();};
107 double          KX_MeshProxy::GetNumber() { return -1;}
108 STR_String&     KX_MeshProxy::GetName() { return m_meshobj->GetName();}
109 void            KX_MeshProxy::SetName(const char *name) { };
110 CValue*         KX_MeshProxy::GetReplica() { return NULL;}
111
112
113 // stuff for python integration
114
115 PyObject* KX_MeshProxy::PyGetMaterialName(PyObject* args, PyObject* kwds)
116 {
117     int matid= 1;
118         STR_String matname;
119
120         if (PyArg_ParseTuple(args,"i:getMaterialName",&matid))
121         {
122                 matname = m_meshobj->GetMaterialName(matid);
123         }
124         else {
125                 return NULL;
126         }
127
128         return PyUnicode_FromString(matname.Ptr());
129                 
130 }
131         
132
133 PyObject* KX_MeshProxy::PyGetTextureName(PyObject* args, PyObject* kwds)
134 {
135     int matid= 1;
136         STR_String matname;
137
138         if (PyArg_ParseTuple(args,"i:getTextureName",&matid))
139         {
140                 matname = m_meshobj->GetTextureName(matid);
141         }
142         else {
143                 return NULL;
144         }
145
146         return PyUnicode_FromString(matname.Ptr());
147                 
148 }
149
150 PyObject* KX_MeshProxy::PyGetVertexArrayLength(PyObject* args, PyObject* kwds)
151 {
152     int matid= 0;
153         int length = 0;
154
155         
156         if (!PyArg_ParseTuple(args,"i:getVertexArrayLength",&matid))
157                 return NULL;
158         
159
160         RAS_MeshMaterial *mmat = m_meshobj->GetMeshMaterial(matid); /* can be NULL*/
161         
162         if (mmat)
163         {
164                 RAS_IPolyMaterial* mat = mmat->m_bucket->GetPolyMaterial();
165                 if (mat)
166                         length = m_meshobj->NumVertices(mat);
167         }
168         
169         return PyLong_FromSsize_t(length);
170 }
171
172
173 PyObject* KX_MeshProxy::PyGetVertex(PyObject* args, PyObject* kwds)
174 {
175     int vertexindex;
176         int matindex;
177
178         if (!PyArg_ParseTuple(args,"ii:getVertex",&matindex,&vertexindex))
179                 return NULL;
180         
181         RAS_TexVert* vertex = m_meshobj->GetVertex(matindex,vertexindex);
182         
183         if(vertex==NULL) {
184                 PyErr_SetString(PyExc_ValueError, "mesh.getVertex(mat_idx, vert_idx): KX_MeshProxy, could not get a vertex at the given indices");
185                 return NULL;
186         }
187         
188         return (new KX_VertexProxy(this, vertex))->NewProxy(true);
189 }
190
191 PyObject* KX_MeshProxy::PyGetPolygon(PyObject* args, PyObject* kwds)
192 {
193     int polyindex= 1;
194         PyObject* polyob = NULL;
195
196         if (!PyArg_ParseTuple(args,"i:getPolygon",&polyindex))
197                 return NULL;
198         
199         if (polyindex<0 || polyindex >= m_meshobj->NumPolygons())
200         {
201                 PyErr_SetString(PyExc_AttributeError, "mesh.getPolygon(int): KX_MeshProxy, invalid polygon index");
202                 return NULL;
203         }
204                 
205
206         RAS_Polygon* polygon = m_meshobj->GetPolygon(polyindex);
207         if (polygon)
208         {
209                 polyob = (new KX_PolyProxy(m_meshobj, polygon))->NewProxy(true);
210         }
211         else {
212                 PyErr_SetString(PyExc_AttributeError, "mesh.getPolygon(int): KX_MeshProxy, polygon is NULL, unknown reason");
213         }
214         return polyob;
215 }
216
217 PyObject* KX_MeshProxy::pyattr_get_materials(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
218 {
219         KX_MeshProxy* self= static_cast<KX_MeshProxy*>(self_v);
220         
221         int tot= self->m_meshobj->NumMaterials();
222         int i;
223         
224         PyObject *materials = PyList_New( tot );
225         
226         list<RAS_MeshMaterial>::iterator mit= self->m_meshobj->GetFirstMaterial();
227         
228         
229         for(i=0; i<tot; mit++, i++) {
230                 RAS_IPolyMaterial *polymat = mit->m_bucket->GetPolyMaterial();   
231                 
232                 /* Why do we need to check for RAS_BLENDERMAT if both are cast to a (PyObject*)? - Campbell */
233                 if(polymat->GetFlag() & RAS_BLENDERMAT)          
234                 {        
235                         KX_BlenderMaterial *mat = static_cast<KX_BlenderMaterial*>(polymat);     
236                         PyList_SET_ITEM(materials, i, mat->GetProxy());
237                 }
238                 else {  
239                         KX_PolygonMaterial *mat = static_cast<KX_PolygonMaterial*>(polymat);
240                         PyList_SET_ITEM(materials, i, mat->GetProxy());
241                 }
242         }       
243         return materials;
244 }
245
246 PyObject * KX_MeshProxy::pyattr_get_numMaterials(void * selfv, const KX_PYATTRIBUTE_DEF * attrdef) {
247         KX_MeshProxy * self = static_cast<KX_MeshProxy *> (selfv);
248         return PyLong_FromSsize_t(self->m_meshobj->NumMaterials());
249 }
250
251 PyObject * KX_MeshProxy::pyattr_get_numPolygons(void * selfv, const KX_PYATTRIBUTE_DEF * attrdef) {
252         KX_MeshProxy * self = static_cast<KX_MeshProxy *> (selfv);
253         return PyLong_FromSsize_t(self->m_meshobj->NumPolygons());
254 }
255
256 /* a close copy of ConvertPythonToGameObject but for meshes */
257 bool ConvertPythonToMesh(PyObject * value, RAS_MeshObject **object, bool py_none_ok, const char *error_prefix)
258 {
259         if (value==NULL) {
260                 PyErr_Format(PyExc_TypeError, "%s, python pointer NULL, should never happen", error_prefix);
261                 *object = NULL;
262                 return false;
263         }
264                 
265         if (value==Py_None) {
266                 *object = NULL;
267                 
268                 if (py_none_ok) {
269                         return true;
270                 } else {
271                         PyErr_Format(PyExc_TypeError, "%s, expected KX_MeshProxy or a KX_MeshProxy name, None is invalid", error_prefix);
272                         return false;
273                 }
274         }
275         
276         if (PyUnicode_Check(value)) {
277                 *object = (RAS_MeshObject*)SCA_ILogicBrick::m_sCurrentLogicManager->GetMeshByName(STR_String( _PyUnicode_AsString(value) ));
278                 
279                 if (*object) {
280                         return true;
281                 } else {
282                         PyErr_Format(PyExc_ValueError, "%s, requested name \"%s\" did not match any KX_MeshProxy in this scene", error_prefix, _PyUnicode_AsString(value));
283                         return false;
284                 }
285         }
286         
287         if (PyObject_TypeCheck(value, &KX_MeshProxy::Type)) {
288                 KX_MeshProxy *kx_mesh = static_cast<KX_MeshProxy*>BGE_PROXY_REF(value);
289                 
290                 /* sets the error */
291                 if (kx_mesh==NULL) {
292                         PyErr_Format(PyExc_SystemError, "%s, " BGE_PROXY_ERROR_MSG, error_prefix);
293                         return false;
294                 }               
295                 
296                 *object = kx_mesh->GetMesh();
297                 return true;
298         }
299         
300         *object = NULL;
301         
302         if (py_none_ok) {
303                 PyErr_Format(PyExc_TypeError, "%s, expect a KX_MeshProxy, a string or None", error_prefix);
304         } else {
305                 PyErr_Format(PyExc_TypeError, "%s, expect a KX_MeshProxy or a string", error_prefix);
306         }
307         
308         return false;
309 }
310
311 #endif // WITH_PYTHON