Added the docs & use Python booleans for physics reinstance.
[blender.git] / source / gameengine / Ketsji / KX_MeshProxy.cpp
1 /**
2  * $Id$
3  * ***** BEGIN GPL/BL DUAL 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. The Blender
9  * Foundation also sells licenses for use in proprietary software under
10  * the Blender License.  See http://www.blender.org/BL/ for information
11  * about this.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Contributor(s): none yet.
28  *
29  * ***** END GPL/BL DUAL LICENSE BLOCK *****
30  */
31
32 #ifdef HAVE_CONFIG_H
33 #include <config.h>
34 #endif
35
36 #include "KX_MeshProxy.h"
37 #include "RAS_IPolygonMaterial.h"
38 #include "RAS_MeshObject.h"
39
40 #include "KX_VertexProxy.h"
41
42 #include "KX_PolygonMaterial.h"
43
44 #include "KX_PyMath.h"
45 #include "KX_ConvertPhysicsObject.h"
46
47 PyTypeObject KX_MeshProxy::Type = {
48         PyObject_HEAD_INIT(&PyType_Type)
49         0,
50         "KX_MeshProxy",
51         sizeof(KX_MeshProxy),
52         0,
53         PyDestructor,
54         0,
55         __getattr,
56         __setattr,
57         0, //&MyPyCompare,
58         __repr,
59         0, //&cvalue_as_number,
60         0,
61         0,
62         0,
63         0
64 };
65
66 PyParentObject KX_MeshProxy::Parents[] = {
67         &KX_MeshProxy::Type,
68         &SCA_IObject::Type,
69         &CValue::Type,
70         NULL
71 };
72
73 PyMethodDef KX_MeshProxy::Methods[] = {
74 {"getNumMaterials", (PyCFunction)KX_MeshProxy::sPyGetNumMaterials,METH_VARARGS},
75 {"getMaterialName", (PyCFunction)KX_MeshProxy::sPyGetMaterialName,METH_VARARGS},
76 {"getTextureName", (PyCFunction)KX_MeshProxy::sPyGetTextureName,METH_VARARGS},
77 {"getVertexArrayLength", (PyCFunction)KX_MeshProxy::sPyGetVertexArrayLength,METH_VARARGS},
78 {"getVertex", (PyCFunction)KX_MeshProxy::sPyGetVertex,METH_VARARGS},
79 KX_PYMETHODTABLE(KX_MeshProxy, reinstancePhysicsMesh),
80 //{"getIndexArrayLength", (PyCFunction)KX_MeshProxy::sPyGetIndexArrayLength,METH_VARARGS},
81
82   {NULL,NULL} //Sentinel
83 };
84
85 PyObject*
86 KX_MeshProxy::_getattr(const STR_String& attr)
87 {
88         if (attr == "materials")
89         {
90                 PyObject *materials = PyList_New(0); /* new ref */
91                 RAS_MaterialBucket::Set::iterator mit = m_meshobj->GetFirstMaterial();
92                 for(; mit != m_meshobj->GetLastMaterial(); ++mit)
93                         PyList_Append(materials, static_cast<KX_PolygonMaterial*>((*mit)->GetPolyMaterial()));
94                 return materials;
95         }
96                 
97         _getattr_up(SCA_IObject);
98 }
99
100
101
102 KX_MeshProxy::KX_MeshProxy(RAS_MeshObject* mesh)
103         : m_meshobj(mesh)
104 {
105         
106 }
107
108 KX_MeshProxy::~KX_MeshProxy()
109 {
110         
111 }
112
113
114
115 // stuff for cvalue related things
116 CValue*         KX_MeshProxy::Calc(VALUE_OPERATOR op, CValue *val) { return NULL;}
117 CValue*         KX_MeshProxy::CalcFinal(VALUE_DATA_TYPE dtype, VALUE_OPERATOR op, CValue *val) { return NULL;}  
118
119 const STR_String &      KX_MeshProxy::GetText() {return m_meshobj->GetName();};
120 float           KX_MeshProxy::GetNumber() { return -1;}
121 STR_String      KX_MeshProxy::GetName() { return m_meshobj->GetName();}
122 void            KX_MeshProxy::SetName(STR_String name) { };
123 CValue*         KX_MeshProxy::GetReplica() { return NULL;}
124 void            KX_MeshProxy::ReplicaSetName(STR_String name) {};
125
126
127 // stuff for python integration
128         
129 PyObject* KX_MeshProxy::PyGetNumMaterials(PyObject* self, 
130                                PyObject* args, 
131                                PyObject* kwds)
132 {
133         int num = m_meshobj->NumMaterials();
134         return PyInt_FromLong(num);
135 }
136
137 PyObject* KX_MeshProxy::PyGetMaterialName(PyObject* self, 
138                                PyObject* args, 
139                                PyObject* kwds)
140 {
141     int matid= 1;
142         STR_String matname;
143
144         if (PyArg_ParseTuple(args,"i",&matid))
145         {
146                 matname = m_meshobj->GetMaterialName(matid);
147         }
148
149         return PyString_FromString(matname.Ptr());
150                 
151 }
152         
153
154 PyObject* KX_MeshProxy::PyGetTextureName(PyObject* self, 
155                                PyObject* args, 
156                                PyObject* kwds)
157 {
158     int matid= 1;
159         STR_String matname;
160
161         if (PyArg_ParseTuple(args,"i",&matid))
162         {
163                 matname = m_meshobj->GetTextureName(matid);
164         }
165
166         return PyString_FromString(matname.Ptr());
167                 
168 }
169
170 PyObject* KX_MeshProxy::PyGetVertexArrayLength(PyObject* self, 
171                                PyObject* args, 
172                                PyObject* kwds)
173 {
174     int matid= -1;
175         int length = -1;
176
177         
178         if (PyArg_ParseTuple(args,"i",&matid))
179         {
180                 RAS_IPolyMaterial* mat = m_meshobj->GetMaterialBucket(matid)->GetPolyMaterial();
181                 if (mat)
182                 {
183                         length = m_meshobj->GetVertexArrayLength(mat);
184                 }
185         }
186
187         return PyInt_FromLong(length);
188                 
189 }
190
191
192 PyObject* KX_MeshProxy::PyGetVertex(PyObject* self, 
193                                PyObject* args, 
194                                PyObject* kwds)
195 {
196     int vertexindex= 1;
197         int matindex= 1;
198         PyObject* vertexob = NULL;
199
200         if (PyArg_ParseTuple(args,"ii",&matindex,&vertexindex))
201         {
202                 RAS_TexVert* vertex = m_meshobj->GetVertex(matindex,vertexindex);
203                 if (vertex)
204                 {
205                         vertexob = new KX_VertexProxy(vertex);
206                 }
207         }
208
209         return vertexob;
210                 
211 }
212
213 KX_PYMETHODDEF_DOC(KX_MeshProxy, reinstancePhysicsMesh,
214 "Reinstance the physics mesh.")
215 {
216         return Py_Success(KX_ReInstanceShapeFromMesh(m_meshobj));
217 }