Port Python updates from Tuhopuu2:
[blender.git] / source / gameengine / Ketsji / KX_VertexProxy.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_VertexProxy.h"
37 #include "KX_MeshProxy.h"
38 #include "RAS_TexVert.h"
39
40 #include "KX_PyMath.h"
41
42 PyTypeObject KX_VertexProxy::Type = {
43         PyObject_HEAD_INIT(&PyType_Type)
44         0,
45         "KX_VertexProxy",
46         sizeof(KX_VertexProxy),
47         0,
48         PyDestructor,
49         0,
50         __getattr,
51         __setattr,
52         0, //&MyPyCompare,
53         __repr,
54         0, //&cvalue_as_number,
55         0,
56         0,
57         0,
58         0
59 };
60
61 PyParentObject KX_VertexProxy::Parents[] = {
62         &KX_VertexProxy::Type,
63         &SCA_IObject::Type,
64         &CValue::Type,
65         NULL
66 };
67
68 PyMethodDef KX_VertexProxy::Methods[] = {
69 {"getXYZ", (PyCFunction)KX_VertexProxy::sPyGetXYZ,METH_VARARGS},
70 {"setXYZ", (PyCFunction)KX_VertexProxy::sPySetXYZ,METH_VARARGS},
71 {"getUV", (PyCFunction)KX_VertexProxy::sPyGetUV,METH_VARARGS},
72 {"setUV", (PyCFunction)KX_VertexProxy::sPySetUV,METH_VARARGS},
73 {"getRGBA", (PyCFunction)KX_VertexProxy::sPyGetRGBA,METH_VARARGS},
74 {"setRGBA", (PyCFunction)KX_VertexProxy::sPySetRGBA,METH_VARARGS},
75 {"getNormal", (PyCFunction)KX_VertexProxy::sPyGetNormal,METH_VARARGS},
76 {"setNormal", (PyCFunction)KX_VertexProxy::sPySetNormal,METH_VARARGS},
77   {NULL,NULL} //Sentinel
78 };
79
80 PyObject*
81 KX_VertexProxy::_getattr(const STR_String& attr)
82 {
83   if (attr == "XYZ")
84         return PyObjectFrom(MT_Vector3(m_vertex->getLocalXYZ()));
85
86   if (attr == "UV")
87         return PyObjectFrom(MT_Point2(m_vertex->getUV1()));
88
89   if (attr == "colour" || attr == "color")
90   {
91         const unsigned char *colp = m_vertex->getRGBA();
92         MT_Vector4 colour(colp[0], colp[1], colp[2], colp[3]);
93         colour /= 255.0;
94         return PyObjectFrom(colour);
95   }
96   
97   if (attr == "normal")
98   {
99         return PyObjectFrom(MT_Vector3(m_vertex->getNormal()));
100   }
101
102   // pos
103   if (attr == "x")
104         return PyFloat_FromDouble(m_vertex->getLocalXYZ()[0]);
105   if (attr == "y")
106         return PyFloat_FromDouble(m_vertex->getLocalXYZ()[1]);
107   if (attr == "z")
108         return PyFloat_FromDouble(m_vertex->getLocalXYZ()[2]);
109
110   // Col
111   if (attr == "r")
112         return PyFloat_FromDouble(m_vertex->getRGBA()[0]/255.0);
113   if (attr == "g")
114         return PyFloat_FromDouble(m_vertex->getRGBA()[1]/255.0);
115   if (attr == "b")
116         return PyFloat_FromDouble(m_vertex->getRGBA()[2]/255.0);
117   if (attr == "a")
118         return PyFloat_FromDouble(m_vertex->getRGBA()[3]/255.0);
119
120   // UV
121   if (attr == "u")
122         return PyFloat_FromDouble(m_vertex->getUV1()[0]);
123   if (attr == "v")
124         return PyFloat_FromDouble(m_vertex->getUV1()[1]);
125
126   _getattr_up(SCA_IObject);
127 }
128
129 int    KX_VertexProxy::_setattr(const STR_String& attr, PyObject *pyvalue)
130 {
131   if (PySequence_Check(pyvalue))
132   {
133         if (attr == "XYZ")
134         {
135                 MT_Point3 vec;
136                 if (PyVecTo(pyvalue, vec))
137                 {
138                         m_vertex->SetXYZ(vec);
139                         return 0;
140                 }
141                 return 1;
142         }
143         
144         if (attr == "UV")
145         {
146                 MT_Point2 vec;
147                 if (PyVecTo(pyvalue, vec))
148                 {
149                         m_vertex->SetUV(vec);
150                         return 0;
151                 }
152                 return 1;
153         }
154         
155         if (attr == "colour" || attr == "color")
156         {
157                 MT_Vector4 vec;
158                 if (PyVecTo(pyvalue, vec))
159                 {
160                         m_vertex->SetRGBA(vec);
161                         return 0;
162                 }
163                 return 1;
164         }
165         
166         if (attr == "normal")
167         {
168                 MT_Vector3 vec;
169                 if (PyVecTo(pyvalue, vec))
170                 {
171                         m_vertex->SetNormal(vec);
172                         return 0;
173                 }
174                 return 1;
175         }
176   }
177   
178   if (PyFloat_Check(pyvalue))
179   {
180         float val = PyFloat_AsDouble(pyvalue);
181         // pos
182         MT_Point3 pos(m_vertex->getLocalXYZ());
183         if (attr == "x")
184         {
185                 pos.x() = val;
186                 m_vertex->SetXYZ(pos);
187                 return 0;
188         }
189         
190         if (attr == "y")
191         {
192                 pos.y() = val;
193                 m_vertex->SetXYZ(pos);
194                 return 0;
195         }
196         
197         if (attr == "z")
198         {
199                 pos.z() = val;
200                 m_vertex->SetXYZ(pos);
201                 return 0;
202         }
203         
204         // uv
205         MT_Point2 uv = m_vertex->getUV1();
206         if (attr == "u")
207         {
208                 uv[0] = val;
209                 m_vertex->SetUV(uv);
210                 return 0;
211         }
212
213         if (attr == "v")
214         {
215                 uv[1] = val;
216                 m_vertex->SetUV(uv);
217                 return 0;
218         }
219         
220         // col
221         unsigned int icol = *((const unsigned int *)m_vertex->getRGBA());
222         unsigned char *cp = (unsigned char*) &icol;
223         val *= 255.0;
224         if (attr == "r")
225         {
226                 cp[0] = (unsigned char) val;
227                 m_vertex->SetRGBA(icol);
228                 return 0;
229         }
230         if (attr == "g")
231         {
232                 cp[1] = (unsigned char) val;
233                 m_vertex->SetRGBA(icol);
234                 return 0;
235         }
236         if (attr == "b")
237         {
238                 cp[2] = (unsigned char) val;
239                 m_vertex->SetRGBA(icol);
240                 return 0;
241         }
242         if (attr == "a")
243         {
244                 cp[3] = (unsigned char) val;
245                 m_vertex->SetRGBA(icol);
246                 return 0;
247         }
248   }
249   
250   return SCA_IObject::_setattr(attr, pyvalue);
251 }
252
253 KX_VertexProxy::KX_VertexProxy(RAS_TexVert* vertex)
254 :m_vertex(vertex)
255 {
256 }
257
258 KX_VertexProxy::~KX_VertexProxy()
259 {
260 }
261
262
263
264 // stuff for cvalue related things
265 CValue*         KX_VertexProxy::Calc(VALUE_OPERATOR, CValue *) { return NULL;}
266 CValue*         KX_VertexProxy::CalcFinal(VALUE_DATA_TYPE, VALUE_OPERATOR, CValue *) { return NULL;}    
267 STR_String      sVertexName="vertex";
268 const STR_String &      KX_VertexProxy::GetText() {return sVertexName;};
269 float           KX_VertexProxy::GetNumber() { return -1;}
270 STR_String      KX_VertexProxy::GetName() { return sVertexName;}
271 void            KX_VertexProxy::SetName(STR_String) { };
272 CValue*         KX_VertexProxy::GetReplica() { return NULL;}
273 void            KX_VertexProxy::ReplicaSetName(STR_String) {};
274
275
276 // stuff for python integration
277         
278 PyObject* KX_VertexProxy::PyGetXYZ(PyObject*, 
279                                PyObject*, 
280                                PyObject*)
281 {
282         return PyObjectFrom(MT_Point3(m_vertex->getLocalXYZ()));
283 }
284
285 PyObject* KX_VertexProxy::PySetXYZ(PyObject*, 
286                                PyObject* args, 
287                                PyObject*)
288 {
289         MT_Point3 vec;
290         if (PyVecArgTo(args, vec))
291         {
292                 m_vertex->SetXYZ(vec);
293         
294                 Py_Return;
295         }
296         
297         return NULL;
298 }
299
300 PyObject* KX_VertexProxy::PyGetNormal(PyObject*, 
301                                PyObject*, 
302                                PyObject*)
303 {
304         return PyObjectFrom(MT_Vector3(m_vertex->getNormal()));
305 }
306
307 PyObject* KX_VertexProxy::PySetNormal(PyObject*, 
308                                PyObject* args, 
309                                PyObject*)
310 {
311         MT_Vector3 vec;
312         if (PyVecArgTo(args, vec))
313         {
314                 m_vertex->SetNormal(vec);
315                 
316                 Py_Return;
317         }
318         
319         return NULL;
320 }
321
322
323 PyObject* KX_VertexProxy::PyGetRGBA(PyObject*,
324                                PyObject*, 
325                                PyObject*)
326 {
327         int *rgba = (int *) m_vertex->getRGBA();
328         return PyInt_FromLong(*rgba);
329 }
330
331 PyObject* KX_VertexProxy::PySetRGBA(PyObject*, 
332                                PyObject* args, 
333                                PyObject*)
334 {
335         float r, g, b, a;
336         if (PyArg_ParseTuple(args, "(ffff)", &r, &g, &b, &a))
337         {
338                 m_vertex->SetRGBA(MT_Vector4(r, g, b, a));
339                 Py_Return;
340         }
341         
342         int rgba;
343         if (PyArg_ParseTuple(args,"i",&rgba))
344         {
345                 m_vertex->SetRGBA(rgba);
346                 Py_Return;
347         }
348         Py_Return;
349 }
350
351
352 PyObject* KX_VertexProxy::PyGetUV(PyObject*, 
353                                PyObject*, 
354                                PyObject*)
355 {
356         return PyObjectFrom(MT_Vector2(m_vertex->getUV1()));
357 }
358
359 PyObject* KX_VertexProxy::PySetUV(PyObject*, 
360                                PyObject* args, 
361                                PyObject*)
362 {
363         MT_Point2 vec;
364         if (PyVecArgTo(args, vec))
365         {
366                 m_vertex->SetUV(vec);
367                 
368                 Py_Return;
369         }
370         
371         return NULL;
372 }
373
374
375