mesh proxy attributes from andrecastelo
[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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 HAVE_CONFIG_H
30 #include <config.h>
31 #endif
32
33 #include "KX_MeshProxy.h"
34 #include "RAS_IPolygonMaterial.h"
35 #include "RAS_MeshObject.h"
36
37 #include "KX_VertexProxy.h"
38 #include "KX_PolyProxy.h"
39
40 #include "KX_PolygonMaterial.h"
41 #include "KX_BlenderMaterial.h"
42
43 #include "KX_PyMath.h"
44 #include "KX_ConvertPhysicsObject.h"
45
46 #include "PyObjectPlus.h" 
47
48 PyTypeObject KX_MeshProxy::Type = {
49         PyObject_HEAD_INIT(NULL)
50         0,
51         "KX_MeshProxy",
52         sizeof(KX_MeshProxy),
53         0,
54         PyDestructor,
55         0,
56         0,
57         0,
58         0,
59         py_base_repr,
60         0,0,0,0,0,0,
61         py_base_getattro,
62         py_base_setattro,
63         0,0,0,0,0,0,0,0,0,
64         Methods
65 };
66
67 PyParentObject KX_MeshProxy::Parents[] = {
68         &KX_MeshProxy::Type,
69         &SCA_IObject::Type,
70         &CValue::Type,
71         &PyObjectPlus::Type,
72         NULL
73 };
74
75 PyMethodDef KX_MeshProxy::Methods[] = {
76 // Deprecated ----->
77 {"getNumMaterials", (PyCFunction)KX_MeshProxy::sPyGetNumMaterials,METH_VARARGS},
78 {"getNumPolygons", (PyCFunction)KX_MeshProxy::sPyGetNumPolygons,METH_NOARGS},
79 // <-----
80
81 {"getMaterialName", (PyCFunction)KX_MeshProxy::sPyGetMaterialName,METH_VARARGS},
82 {"getTextureName", (PyCFunction)KX_MeshProxy::sPyGetTextureName,METH_VARARGS},
83 {"getVertexArrayLength", (PyCFunction)KX_MeshProxy::sPyGetVertexArrayLength,METH_VARARGS},
84 {"getVertex", (PyCFunction)KX_MeshProxy::sPyGetVertex,METH_VARARGS},
85 {"getPolygon", (PyCFunction)KX_MeshProxy::sPyGetPolygon,METH_VARARGS},
86 KX_PYMETHODTABLE(KX_MeshProxy, reinstancePhysicsMesh),
87 //{"getIndexArrayLength", (PyCFunction)KX_MeshProxy::sPyGetIndexArrayLength,METH_VARARGS},
88
89   {NULL,NULL} //Sentinel
90 };
91
92 PyAttributeDef KX_MeshProxy::Attributes[] = {
93         KX_PYATTRIBUTE_RO_FUNCTION("materials",         KX_MeshProxy, pyattr_get_materials),
94         KX_PYATTRIBUTE_RO_FUNCTION("numPolygons",       KX_MeshProxy, pyattr_get_materials),
95         KX_PYATTRIBUTE_RO_FUNCTION("numMaterials",      KX_MeshProxy, pyattr_get_materials),
96
97         { NULL }        //Sentinel
98 };
99
100 void KX_MeshProxy::SetMeshModified(bool v)
101 {
102         m_meshobj->SetMeshModified(v);
103 }
104
105
106 PyObject* KX_MeshProxy::py_getattro(PyObject *attr)
107 {
108         py_getattro_up(SCA_IObject);
109 }
110
111 int KX_MeshProxy::py_setattro(PyObject *attr, PyObject* value)
112 {
113         py_setattro_up(SCA_IObject);
114 }
115
116
117 KX_MeshProxy::KX_MeshProxy(RAS_MeshObject* mesh)
118         : SCA_IObject(&Type), m_meshobj(mesh)
119 {
120 }
121
122 KX_MeshProxy::~KX_MeshProxy()
123 {
124 }
125
126
127
128 // stuff for cvalue related things
129 CValue*         KX_MeshProxy::Calc(VALUE_OPERATOR op, CValue *val) { return NULL;}
130 CValue*         KX_MeshProxy::CalcFinal(VALUE_DATA_TYPE dtype, VALUE_OPERATOR op, CValue *val) { return NULL;}  
131
132 const STR_String &      KX_MeshProxy::GetText() {return m_meshobj->GetName();};
133 double          KX_MeshProxy::GetNumber() { return -1;}
134 STR_String      KX_MeshProxy::GetName() { return m_meshobj->GetName();}
135 void            KX_MeshProxy::SetName(STR_String name) { };
136 CValue*         KX_MeshProxy::GetReplica() { return NULL;}
137 void            KX_MeshProxy::ReplicaSetName(STR_String name) {};
138
139
140 // stuff for python integration
141         
142 PyObject* KX_MeshProxy::PyGetNumMaterials(PyObject* self, 
143                                PyObject* args, 
144                                PyObject* kwds)
145 {
146         int num = m_meshobj->NumMaterials();
147         ShowDeprecationWarning("getNumMaterials()", "the numMaterials property");
148         return PyInt_FromLong(num);
149 }
150
151 PyObject* KX_MeshProxy::PyGetNumPolygons(PyObject* self)
152 {
153         int num = m_meshobj->NumPolygons();
154         ShowDeprecationWarning("getNumPolygons()", "the numPolygons property");
155         return PyInt_FromLong(num);
156 }
157
158 PyObject* KX_MeshProxy::PyGetMaterialName(PyObject* self, 
159                                PyObject* args, 
160                                PyObject* kwds)
161 {
162     int matid= 1;
163         STR_String matname;
164
165         if (PyArg_ParseTuple(args,"i:getMaterialName",&matid))
166         {
167                 matname = m_meshobj->GetMaterialName(matid);
168         }
169         else {
170                 return NULL;
171         }
172
173         return PyString_FromString(matname.Ptr());
174                 
175 }
176         
177
178 PyObject* KX_MeshProxy::PyGetTextureName(PyObject* self, 
179                                PyObject* args, 
180                                PyObject* kwds)
181 {
182     int matid= 1;
183         STR_String matname;
184
185         if (PyArg_ParseTuple(args,"i:getTextureName",&matid))
186         {
187                 matname = m_meshobj->GetTextureName(matid);
188         }
189         else {
190                 return NULL;
191         }
192
193         return PyString_FromString(matname.Ptr());
194                 
195 }
196
197 PyObject* KX_MeshProxy::PyGetVertexArrayLength(PyObject* self, 
198                                PyObject* args, 
199                                PyObject* kwds)
200 {
201     int matid= 0;
202         int length = 0;
203
204         
205         if (!PyArg_ParseTuple(args,"i:getVertexArrayLength",&matid))
206                 return NULL;
207         
208
209         RAS_MeshMaterial *mmat = m_meshobj->GetMeshMaterial(matid); /* can be NULL*/
210         
211         if (mmat)
212         {
213                 RAS_IPolyMaterial* mat = mmat->m_bucket->GetPolyMaterial();
214                 if (mat)
215                         length = m_meshobj->NumVertices(mat);
216         }
217         
218         return PyInt_FromLong(length);
219 }
220
221
222 PyObject* KX_MeshProxy::PyGetVertex(PyObject* self, 
223                                PyObject* args, 
224                                PyObject* kwds)
225 {
226     int vertexindex= 1;
227         int matindex= 1;
228         PyObject* vertexob = NULL;
229
230         if (PyArg_ParseTuple(args,"ii:getVertex",&matindex,&vertexindex))
231         {
232                 RAS_TexVert* vertex = m_meshobj->GetVertex(matindex,vertexindex);
233                 if (vertex)
234                 {
235                         vertexob = new KX_VertexProxy(this, vertex);
236                 }
237         }
238         else {
239                 return NULL;
240         }
241
242         return vertexob;
243                 
244 }
245
246 PyObject* KX_MeshProxy::PyGetPolygon(PyObject* self,
247                                PyObject* args, 
248                                PyObject* kwds)
249 {
250     int polyindex= 1;
251         PyObject* polyob = NULL;
252
253         if (!PyArg_ParseTuple(args,"i:getPolygon",&polyindex))
254                 return NULL;
255         
256         if (polyindex<0 || polyindex >= m_meshobj->NumPolygons())
257         {
258                 PyErr_SetString(PyExc_AttributeError, "Invalid polygon index");
259                 return NULL;
260         }
261                 
262
263         RAS_Polygon* polygon = m_meshobj->GetPolygon(polyindex);
264         if (polygon)
265         {
266                 polyob = new KX_PolyProxy(m_meshobj, polygon);
267         }
268         else {
269                 PyErr_SetString(PyExc_AttributeError, "polygon is NULL, unknown reason");
270         }
271         return polyob;
272 }
273
274 KX_PYMETHODDEF_DOC(KX_MeshProxy, reinstancePhysicsMesh,
275 "Reinstance the physics mesh.")
276 {
277         //this needs to be reviewed, it is dependend on Sumo/Solid. Who is using this ?
278         Py_RETURN_NONE;//(KX_ReInstanceShapeFromMesh(m_meshobj)) ? Py_RETURN_TRUE : Py_RETURN_FALSE;
279 }
280
281 PyObject* KX_MeshProxy::pyattr_get_materials(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
282 {
283         KX_MeshProxy* self= static_cast<KX_MeshProxy*>(self_v);
284         
285         int tot= self->m_meshobj->NumMaterials();
286         int i;
287         
288         PyObject *materials = PyList_New( tot );
289         
290         list<RAS_MeshMaterial>::iterator mit= self->m_meshobj->GetFirstMaterial();
291         
292         
293         for(i=0; i<tot; mit++, i++) {
294                 RAS_IPolyMaterial *polymat = mit->m_bucket->GetPolyMaterial();   
295                 
296                 /* Why do we need to check for RAS_BLENDERMAT if both are cast to a (PyObject*)? - Campbell */
297                 if(polymat->GetFlag() & RAS_BLENDERMAT)          
298                 {        
299                         KX_BlenderMaterial *mat = static_cast<KX_BlenderMaterial*>(polymat);     
300                         PyList_SET_ITEM(materials, i, mat);
301                         Py_INCREF(mat);
302                 }
303                 else {  
304                         KX_PolygonMaterial *mat = static_cast<KX_PolygonMaterial*>(polymat);
305                         PyList_SET_ITEM(materials, i, mat);
306                         Py_INCREF(mat);
307                 }
308         }       
309         return materials;
310 }
311
312 PyObject * KX_MeshProxy::pyattr_get_numMaterials(void * selfv, const KX_PYATTRIBUTE_DEF * attrdef) {
313         KX_MeshProxy * self = static_cast<KX_MeshProxy *> (selfv);
314         int num = self->m_meshobj->NumMaterials();
315         return PyInt_FromLong(num);
316 }
317
318 PyObject * KX_MeshProxy::pyattr_get_numPolygons(void * selfv, const KX_PYATTRIBUTE_DEF * attrdef) {
319         KX_MeshProxy * self = static_cast<KX_MeshProxy *> (selfv);
320         int num = self->m_meshobj->NumPolygons();
321         return PyInt_FromLong(num);
322 }