game engine python api
[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
39 #include "KX_PolygonMaterial.h"
40 #include "KX_BlenderMaterial.h"
41
42 #include "KX_PyMath.h"
43 #include "KX_ConvertPhysicsObject.h"
44
45 PyTypeObject KX_MeshProxy::Type = {
46         PyObject_HEAD_INIT(&PyType_Type)
47         0,
48         "KX_MeshProxy",
49         sizeof(KX_MeshProxy),
50         0,
51         PyDestructor,
52         0,
53         __getattr,
54         __setattr,
55         0, //&MyPyCompare,
56         __repr,
57         0, //&cvalue_as_number,
58         0,
59         0,
60         0,
61         0
62 };
63
64 PyParentObject KX_MeshProxy::Parents[] = {
65         &KX_MeshProxy::Type,
66         &SCA_IObject::Type,
67         &CValue::Type,
68         &PyObjectPlus::Type,
69         NULL
70 };
71
72 PyMethodDef KX_MeshProxy::Methods[] = {
73 {"getNumMaterials", (PyCFunction)KX_MeshProxy::sPyGetNumMaterials,METH_VARARGS},
74 {"getMaterialName", (PyCFunction)KX_MeshProxy::sPyGetMaterialName,METH_VARARGS},
75 {"getTextureName", (PyCFunction)KX_MeshProxy::sPyGetTextureName,METH_VARARGS},
76 {"getVertexArrayLength", (PyCFunction)KX_MeshProxy::sPyGetVertexArrayLength,METH_VARARGS},
77 {"getVertex", (PyCFunction)KX_MeshProxy::sPyGetVertex,METH_VARARGS},
78 KX_PYMETHODTABLE(KX_MeshProxy, reinstancePhysicsMesh),
79 //{"getIndexArrayLength", (PyCFunction)KX_MeshProxy::sPyGetIndexArrayLength,METH_VARARGS},
80
81   {NULL,NULL} //Sentinel
82 };
83
84 void KX_MeshProxy::SetMeshModified(bool v)
85 {
86         m_meshobj->SetMeshModified(v);
87 }
88
89
90 PyObject*
91 KX_MeshProxy::_getattr(const STR_String& attr)
92 {
93         if (attr == "materials")
94         {
95                 PyObject *materials = PyList_New(0);
96                 RAS_MaterialBucket::Set::iterator mit = m_meshobj->GetFirstMaterial();
97                 for(; mit != m_meshobj->GetLastMaterial(); ++mit)
98                 {
99                         RAS_IPolyMaterial *polymat = (*mit)->GetPolyMaterial();
100                         if(polymat->GetFlag() & RAS_BLENDERMAT)
101                         {
102                                 KX_BlenderMaterial *mat = static_cast<KX_BlenderMaterial*>(polymat);
103                                 PyList_Append(materials, mat);
104                         }else
105                         {
106                                 PyList_Append(materials, static_cast<KX_PolygonMaterial*>(polymat));
107                         }
108                 }
109                 return materials;
110         }
111         _getattr_up(SCA_IObject);
112 }
113
114
115
116 KX_MeshProxy::KX_MeshProxy(RAS_MeshObject* mesh)
117         :       m_meshobj(mesh)
118 {
119 }
120
121 KX_MeshProxy::~KX_MeshProxy()
122 {
123 }
124
125
126
127 // stuff for cvalue related things
128 CValue*         KX_MeshProxy::Calc(VALUE_OPERATOR op, CValue *val) { return NULL;}
129 CValue*         KX_MeshProxy::CalcFinal(VALUE_DATA_TYPE dtype, VALUE_OPERATOR op, CValue *val) { return NULL;}  
130
131 const STR_String &      KX_MeshProxy::GetText() {return m_meshobj->GetName();};
132 float           KX_MeshProxy::GetNumber() { return -1;}
133 STR_String      KX_MeshProxy::GetName() { return m_meshobj->GetName();}
134 void            KX_MeshProxy::SetName(STR_String name) { };
135 CValue*         KX_MeshProxy::GetReplica() { return NULL;}
136 void            KX_MeshProxy::ReplicaSetName(STR_String name) {};
137
138
139 // stuff for python integration
140         
141 PyObject* KX_MeshProxy::PyGetNumMaterials(PyObject* self, 
142                                PyObject* args, 
143                                PyObject* kwds)
144 {
145         int num = m_meshobj->NumMaterials();
146         return PyInt_FromLong(num);
147 }
148
149 PyObject* KX_MeshProxy::PyGetMaterialName(PyObject* self, 
150                                PyObject* args, 
151                                PyObject* kwds)
152 {
153     int matid= 1;
154         STR_String matname;
155
156         if (PyArg_ParseTuple(args,"i",&matid))
157         {
158                 matname = m_meshobj->GetMaterialName(matid);
159         }
160         else {
161                 return NULL;
162         }
163
164         return PyString_FromString(matname.Ptr());
165                 
166 }
167         
168
169 PyObject* KX_MeshProxy::PyGetTextureName(PyObject* self, 
170                                PyObject* args, 
171                                PyObject* kwds)
172 {
173     int matid= 1;
174         STR_String matname;
175
176         if (PyArg_ParseTuple(args,"i",&matid))
177         {
178                 matname = m_meshobj->GetTextureName(matid);
179         }
180         else {
181                 return NULL;
182         }
183
184         return PyString_FromString(matname.Ptr());
185                 
186 }
187
188 PyObject* KX_MeshProxy::PyGetVertexArrayLength(PyObject* self, 
189                                PyObject* args, 
190                                PyObject* kwds)
191 {
192     int matid= -1;
193         int length = -1;
194
195         
196         if (PyArg_ParseTuple(args,"i",&matid))
197         {
198                 RAS_IPolyMaterial* mat = m_meshobj->GetMaterialBucket(matid)->GetPolyMaterial();
199                 if (mat)
200                 {
201                         length = m_meshobj->GetVertexArrayLength(mat);
202                 }
203         }
204         else {
205                 return NULL;
206         }
207
208         return PyInt_FromLong(length);
209                 
210 }
211
212
213 PyObject* KX_MeshProxy::PyGetVertex(PyObject* self, 
214                                PyObject* args, 
215                                PyObject* kwds)
216 {
217     int vertexindex= 1;
218         int matindex= 1;
219         PyObject* vertexob = NULL;
220
221         if (PyArg_ParseTuple(args,"ii",&matindex,&vertexindex))
222         {
223                 RAS_TexVert* vertex = m_meshobj->GetVertex(matindex,vertexindex);
224                 if (vertex)
225                 {
226                         vertexob = new KX_VertexProxy(this, vertex);
227                 }
228         }
229         else {
230                 return NULL;
231         }
232
233         return vertexob;
234                 
235 }
236
237 KX_PYMETHODDEF_DOC(KX_MeshProxy, reinstancePhysicsMesh,
238 "Reinstance the physics mesh.")
239 {
240         //this needs to be reviewed, it is dependend on Sumo/Solid. Who is using this ?
241         return Py_None;//(KX_ReInstanceShapeFromMesh(m_meshobj)) ? Py_RETURN_TRUE : Py_RETURN_FALSE;
242 }