Initial revision
[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 #include "KX_MeshProxy.h"
33
34 #include "RAS_IPolygonMaterial.h"
35 #include "RAS_MeshObject.h"
36 #include "KX_VertexProxy.h"
37
38
39 PyTypeObject KX_MeshProxy::Type = {
40         PyObject_HEAD_INIT(&PyType_Type)
41         0,
42         "KX_MeshProxy",
43         sizeof(KX_MeshProxy),
44         0,
45         PyDestructor,
46         0,
47         __getattr,
48         __setattr,
49         0, //&MyPyCompare,
50         __repr,
51         0, //&cvalue_as_number,
52         0,
53         0,
54         0,
55         0
56 };
57
58 PyParentObject KX_MeshProxy::Parents[] = {
59         &KX_MeshProxy::Type,
60         &SCA_IObject::Type,
61         &CValue::Type,
62         NULL
63 };
64
65 PyMethodDef KX_MeshProxy::Methods[] = {
66 {"getNumMaterials", (PyCFunction)KX_MeshProxy::sPyGetNumMaterials,METH_VARARGS},
67 {"getMaterialName", (PyCFunction)KX_MeshProxy::sPyGetMaterialName,METH_VARARGS},
68 {"getTextureName", (PyCFunction)KX_MeshProxy::sPyGetTextureName,METH_VARARGS},
69 {"getVertexArrayLength", (PyCFunction)KX_MeshProxy::sPyGetVertexArrayLength,METH_VARARGS},
70 {"getVertex", (PyCFunction)KX_MeshProxy::sPyGetVertex,METH_VARARGS},
71 //{"getIndexArrayLength", (PyCFunction)KX_MeshProxy::sPyGetIndexArrayLength,METH_VARARGS},
72
73   {NULL,NULL} //Sentinel
74 };
75
76 PyObject*
77 KX_MeshProxy::_getattr(char* attr)
78 {
79   _getattr_up(SCA_IObject);
80 }
81
82
83
84 KX_MeshProxy::KX_MeshProxy(RAS_MeshObject* mesh)
85         : m_meshobj(mesh)
86 {
87         
88 }
89
90 KX_MeshProxy::~KX_MeshProxy()
91 {
92         
93 }
94
95
96
97 // stuff for cvalue related things
98 CValue*         KX_MeshProxy::Calc(VALUE_OPERATOR op, CValue *val) { return NULL;}
99 CValue*         KX_MeshProxy::CalcFinal(VALUE_DATA_TYPE dtype, VALUE_OPERATOR op, CValue *val) { return NULL;}  
100
101 const STR_String &      KX_MeshProxy::GetText() {return m_meshobj->GetName();};
102 float           KX_MeshProxy::GetNumber() { return -1;}
103 STR_String      KX_MeshProxy::GetName() { return m_meshobj->GetName();}
104 void            KX_MeshProxy::SetName(STR_String name) { };
105 CValue*         KX_MeshProxy::GetReplica() { return NULL;}
106 void            KX_MeshProxy::ReplicaSetName(STR_String name) {};
107
108
109 // stuff for python integration
110         
111 PyObject* KX_MeshProxy::PyGetNumMaterials(PyObject* self, 
112                                PyObject* args, 
113                                PyObject* kwds)
114 {
115         int num = m_meshobj->NumMaterials();
116         return PyInt_FromLong(num);
117 }
118
119 PyObject* KX_MeshProxy::PyGetMaterialName(PyObject* self, 
120                                PyObject* args, 
121                                PyObject* kwds)
122 {
123     int matid= 1;
124         STR_String matname;
125
126         if (PyArg_ParseTuple(args,"i",&matid))
127         {
128                 matname = m_meshobj->GetMaterialName(matid);
129         }
130
131         return PyString_FromString(matname.Ptr());
132                 
133 }
134         
135
136 PyObject* KX_MeshProxy::PyGetTextureName(PyObject* self, 
137                                PyObject* args, 
138                                PyObject* kwds)
139 {
140     int matid= 1;
141         STR_String matname;
142
143         if (PyArg_ParseTuple(args,"i",&matid))
144         {
145                 matname = m_meshobj->GetTextureName(matid);
146         }
147
148         return PyString_FromString(matname.Ptr());
149                 
150 }
151
152 PyObject* KX_MeshProxy::PyGetVertexArrayLength(PyObject* self, 
153                                PyObject* args, 
154                                PyObject* kwds)
155 {
156     int matid= -1;
157         int length = -1;
158
159         
160         if (PyArg_ParseTuple(args,"i",&matid))
161         {
162                 RAS_IPolyMaterial* mat = m_meshobj->GetMaterialBucket(matid)->GetPolyMaterial();
163                 if (mat)
164                 {
165                         length = m_meshobj->GetVertexArrayLength(mat);
166                 }
167         }
168
169         return PyInt_FromLong(length);
170                 
171 }
172
173
174 PyObject* KX_MeshProxy::PyGetVertex(PyObject* self, 
175                                PyObject* args, 
176                                PyObject* kwds)
177 {
178     int vertexindex= 1;
179         int matindex= 1;
180         PyObject* vertexob = NULL;
181
182         if (PyArg_ParseTuple(args,"ii",&matindex,&vertexindex))
183         {
184                 RAS_TexVert* vertex = m_meshobj->GetVertex(matindex,vertexindex);
185                 if (vertex)
186                 {
187                         vertexob = new KX_VertexProxy(vertex);
188                 }
189         }
190
191         return vertexob;
192                 
193 }