BGE patch: KX_GameObject::rayCast() improvements to have X-Ray option, return true...
[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 PyTypeObject KX_MeshProxy::Type = {
47         PyObject_HEAD_INIT(&PyType_Type)
48         0,
49         "KX_MeshProxy",
50         sizeof(KX_MeshProxy),
51         0,
52         PyDestructor,
53         0,
54         __getattr,
55         __setattr,
56         0, //&MyPyCompare,
57         __repr,
58         0, //&cvalue_as_number,
59         0,
60         0,
61         0,
62         0
63 };
64
65 PyParentObject KX_MeshProxy::Parents[] = {
66         &KX_MeshProxy::Type,
67         &SCA_IObject::Type,
68         &CValue::Type,
69         &PyObjectPlus::Type,
70         NULL
71 };
72
73 PyMethodDef KX_MeshProxy::Methods[] = {
74 {"getNumMaterials", (PyCFunction)KX_MeshProxy::sPyGetNumMaterials,METH_VARARGS},
75 {"getNumPolygons", (PyCFunction)KX_MeshProxy::sPyGetNumPolygons,METH_NOARGS},
76 {"getMaterialName", (PyCFunction)KX_MeshProxy::sPyGetMaterialName,METH_VARARGS},
77 {"getTextureName", (PyCFunction)KX_MeshProxy::sPyGetTextureName,METH_VARARGS},
78 {"getVertexArrayLength", (PyCFunction)KX_MeshProxy::sPyGetVertexArrayLength,METH_VARARGS},
79 {"getVertex", (PyCFunction)KX_MeshProxy::sPyGetVertex,METH_VARARGS},
80 {"getPolygon", (PyCFunction)KX_MeshProxy::sPyGetPolygon,METH_VARARGS},
81 KX_PYMETHODTABLE(KX_MeshProxy, reinstancePhysicsMesh),
82 //{"getIndexArrayLength", (PyCFunction)KX_MeshProxy::sPyGetIndexArrayLength,METH_VARARGS},
83
84   {NULL,NULL} //Sentinel
85 };
86
87 void KX_MeshProxy::SetMeshModified(bool v)
88 {
89         m_meshobj->SetMeshModified(v);
90 }
91
92
93 PyObject*
94 KX_MeshProxy::_getattr(const STR_String& attr)
95 {
96         if (attr == "materials")
97         {
98                 PyObject *materials = PyList_New(0);
99                 RAS_MaterialBucket::Set::iterator mit = m_meshobj->GetFirstMaterial();
100                 for(; mit != m_meshobj->GetLastMaterial(); ++mit)
101                 {
102                         RAS_IPolyMaterial *polymat = (*mit)->GetPolyMaterial();
103                         if(polymat->GetFlag() & RAS_BLENDERMAT)
104                         {
105                                 KX_BlenderMaterial *mat = static_cast<KX_BlenderMaterial*>(polymat);
106                                 PyList_Append(materials, mat);
107                         }else
108                         {
109                                 PyList_Append(materials, static_cast<KX_PolygonMaterial*>(polymat));
110                         }
111                 }
112                 return materials;
113         }
114         _getattr_up(SCA_IObject);
115 }
116
117
118
119 KX_MeshProxy::KX_MeshProxy(RAS_MeshObject* mesh)
120         :       m_meshobj(mesh)
121 {
122 }
123
124 KX_MeshProxy::~KX_MeshProxy()
125 {
126 }
127
128
129
130 // stuff for cvalue related things
131 CValue*         KX_MeshProxy::Calc(VALUE_OPERATOR op, CValue *val) { return NULL;}
132 CValue*         KX_MeshProxy::CalcFinal(VALUE_DATA_TYPE dtype, VALUE_OPERATOR op, CValue *val) { return NULL;}  
133
134 const STR_String &      KX_MeshProxy::GetText() {return m_meshobj->GetName();};
135 float           KX_MeshProxy::GetNumber() { return -1;}
136 STR_String      KX_MeshProxy::GetName() { return m_meshobj->GetName();}
137 void            KX_MeshProxy::SetName(STR_String name) { };
138 CValue*         KX_MeshProxy::GetReplica() { return NULL;}
139 void            KX_MeshProxy::ReplicaSetName(STR_String name) {};
140
141
142 // stuff for python integration
143         
144 PyObject* KX_MeshProxy::PyGetNumMaterials(PyObject* self, 
145                                PyObject* args, 
146                                PyObject* kwds)
147 {
148         int num = m_meshobj->NumMaterials();
149         return PyInt_FromLong(num);
150 }
151
152 PyObject* KX_MeshProxy::PyGetNumPolygons(PyObject* self)
153 {
154         int num = m_meshobj->NumPolygons();
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",&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",&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= -1;
202         int length = -1;
203
204         
205         if (PyArg_ParseTuple(args,"i",&matid))
206         {
207                 RAS_IPolyMaterial* mat = m_meshobj->GetMaterialBucket(matid)->GetPolyMaterial();
208                 if (mat)
209                 {
210                         length = m_meshobj->GetVertexArrayLength(mat);
211                 }
212         }
213         else {
214                 return NULL;
215         }
216
217         return PyInt_FromLong(length);
218                 
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",&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",&polyindex))
254                 return NULL;
255
256         RAS_Polygon* polygon = m_meshobj->GetPolygon(polyindex);
257         if (polygon)
258         {
259                 polyob = new KX_PolyProxy(m_meshobj, polygon);
260         }
261         else
262         {
263                 PyErr_SetString(PyExc_AttributeError, "Invalid polygon index");
264         }
265         return polyob;
266 }
267
268 KX_PYMETHODDEF_DOC(KX_MeshProxy, reinstancePhysicsMesh,
269 "Reinstance the physics mesh.")
270 {
271         //this needs to be reviewed, it is dependend on Sumo/Solid. Who is using this ?
272         Py_RETURN_NONE;//(KX_ReInstanceShapeFromMesh(m_meshobj)) ? Py_RETURN_TRUE : Py_RETURN_FALSE;
273 }