3 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
19 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20 * All rights reserved.
22 * The Original Code is: all of this file.
24 * Contributor(s): none yet.
26 * ***** END GPL LICENSE BLOCK *****
33 #include "KX_VertexProxy.h"
34 #include "KX_MeshProxy.h"
35 #include "RAS_TexVert.h"
37 #include "KX_PyMath.h"
39 PyTypeObject KX_VertexProxy::Type = {
40 PyObject_HEAD_INIT(NULL)
43 sizeof(KX_VertexProxy),
58 PyParentObject KX_VertexProxy::Parents[] = {
59 &KX_VertexProxy::Type,
65 PyMethodDef KX_VertexProxy::Methods[] = {
66 {"getXYZ", (PyCFunction)KX_VertexProxy::sPyGetXYZ,METH_NOARGS},
67 {"setXYZ", (PyCFunction)KX_VertexProxy::sPySetXYZ,METH_O},
68 {"getUV", (PyCFunction)KX_VertexProxy::sPyGetUV,METH_NOARGS},
69 {"setUV", (PyCFunction)KX_VertexProxy::sPySetUV,METH_O},
71 {"getUV2", (PyCFunction)KX_VertexProxy::sPyGetUV2,METH_NOARGS},
72 {"setUV2", (PyCFunction)KX_VertexProxy::sPySetUV2,METH_VARARGS},
74 {"getRGBA", (PyCFunction)KX_VertexProxy::sPyGetRGBA,METH_NOARGS},
75 {"setRGBA", (PyCFunction)KX_VertexProxy::sPySetRGBA,METH_O},
76 {"getNormal", (PyCFunction)KX_VertexProxy::sPyGetNormal,METH_NOARGS},
77 {"setNormal", (PyCFunction)KX_VertexProxy::sPySetNormal,METH_O},
78 {NULL,NULL} //Sentinel
81 PyAttributeDef KX_VertexProxy::Attributes[] = {
83 KX_PYATTRIBUTE_DUMMY("x"),
84 KX_PYATTRIBUTE_DUMMY("y"),
85 KX_PYATTRIBUTE_DUMMY("z"),
87 KX_PYATTRIBUTE_DUMMY("r"),
88 KX_PYATTRIBUTE_DUMMY("g"),
89 KX_PYATTRIBUTE_DUMMY("b"),
90 KX_PYATTRIBUTE_DUMMY("a"),
92 KX_PYATTRIBUTE_DUMMY("u"),
93 KX_PYATTRIBUTE_DUMMY("v"),
95 KX_PYATTRIBUTE_DUMMY("u2"),
96 KX_PYATTRIBUTE_DUMMY("v2"),
98 KX_PYATTRIBUTE_DUMMY("XYZ"),
99 KX_PYATTRIBUTE_DUMMY("UV"),
101 KX_PYATTRIBUTE_DUMMY("color"),
102 KX_PYATTRIBUTE_DUMMY("colour"),
104 KX_PYATTRIBUTE_DUMMY("normal"),
110 KX_VertexProxy::py_getattro(PyObject *attr)
112 char *attr_str= PyString_AsString(attr);
113 if (attr_str[1]=='\0') { // Group single letters
115 if (attr_str[0]=='x')
116 return PyFloat_FromDouble(m_vertex->getXYZ()[0]);
117 if (attr_str[0]=='y')
118 return PyFloat_FromDouble(m_vertex->getXYZ()[1]);
119 if (attr_str[0]=='z')
120 return PyFloat_FromDouble(m_vertex->getXYZ()[2]);
123 if (attr_str[0]=='r')
124 return PyFloat_FromDouble(m_vertex->getRGBA()[0]/255.0);
125 if (attr_str[0]=='g')
126 return PyFloat_FromDouble(m_vertex->getRGBA()[1]/255.0);
127 if (attr_str[0]=='b')
128 return PyFloat_FromDouble(m_vertex->getRGBA()[2]/255.0);
129 if (attr_str[0]=='a')
130 return PyFloat_FromDouble(m_vertex->getRGBA()[3]/255.0);
133 if (attr_str[0]=='u')
134 return PyFloat_FromDouble(m_vertex->getUV1()[0]);
135 if (attr_str[0]=='v')
136 return PyFloat_FromDouble(m_vertex->getUV1()[1]);
140 if (!strcmp(attr_str, "XYZ"))
141 return PyObjectFrom(MT_Vector3(m_vertex->getXYZ()));
143 if (!strcmp(attr_str, "UV"))
144 return PyObjectFrom(MT_Point2(m_vertex->getUV1()));
146 if (!strcmp(attr_str, "color") || !strcmp(attr_str, "colour"))
148 const unsigned char *colp = m_vertex->getRGBA();
149 MT_Vector4 color(colp[0], colp[1], colp[2], colp[3]);
151 return PyObjectFrom(color);
154 if (!strcmp(attr_str, "normal"))
156 return PyObjectFrom(MT_Vector3(m_vertex->getNormal()));
159 py_getattro_up(SCA_IObject);
162 int KX_VertexProxy::py_setattro(PyObject *attr, PyObject *pyvalue)
164 char *attr_str= PyString_AsString(attr);
165 if (PySequence_Check(pyvalue))
167 if (!strcmp(attr_str, "XYZ"))
170 if (PyVecTo(pyvalue, vec))
172 m_vertex->SetXYZ(vec);
173 m_mesh->SetMeshModified(true);
179 if (!strcmp(attr_str, "UV"))
182 if (PyVecTo(pyvalue, vec))
184 m_vertex->SetUV(vec);
185 m_mesh->SetMeshModified(true);
191 if (!strcmp(attr_str, "color") || !strcmp(attr_str, "colour"))
194 if (PyVecTo(pyvalue, vec))
196 m_vertex->SetRGBA(vec);
197 m_mesh->SetMeshModified(true);
203 if (!strcmp(attr_str, "normal"))
206 if (PyVecTo(pyvalue, vec))
208 m_vertex->SetNormal(vec);
209 m_mesh->SetMeshModified(true);
216 if (PyFloat_Check(pyvalue))
218 float val = PyFloat_AsDouble(pyvalue);
220 MT_Point3 pos(m_vertex->getXYZ());
221 if (!strcmp(attr_str, "x"))
224 m_vertex->SetXYZ(pos);
225 m_mesh->SetMeshModified(true);
229 if (!strcmp(attr_str, "y"))
232 m_vertex->SetXYZ(pos);
233 m_mesh->SetMeshModified(true);
237 if (!strcmp(attr_str, "z"))
240 m_vertex->SetXYZ(pos);
241 m_mesh->SetMeshModified(true);
246 MT_Point2 uv = m_vertex->getUV1();
247 if (!strcmp(attr_str, "u"))
251 m_mesh->SetMeshModified(true);
255 if (!strcmp(attr_str, "v"))
259 m_mesh->SetMeshModified(true);
264 MT_Point2 uv2 = m_vertex->getUV2();
265 if (!strcmp(attr_str, "u2"))
268 m_vertex->SetUV2(uv);
269 m_mesh->SetMeshModified(true);
273 if (!strcmp(attr_str, "v2"))
276 m_vertex->SetUV2(uv);
277 m_mesh->SetMeshModified(true);
282 unsigned int icol = *((const unsigned int *)m_vertex->getRGBA());
283 unsigned char *cp = (unsigned char*) &icol;
285 if (!strcmp(attr_str, "r"))
287 cp[0] = (unsigned char) val;
288 m_vertex->SetRGBA(icol);
289 m_mesh->SetMeshModified(true);
292 if (!strcmp(attr_str, "g"))
294 cp[1] = (unsigned char) val;
295 m_vertex->SetRGBA(icol);
296 m_mesh->SetMeshModified(true);
299 if (!strcmp(attr_str, "b"))
301 cp[2] = (unsigned char) val;
302 m_vertex->SetRGBA(icol);
303 m_mesh->SetMeshModified(true);
306 if (!strcmp(attr_str, "a"))
308 cp[3] = (unsigned char) val;
309 m_vertex->SetRGBA(icol);
310 m_mesh->SetMeshModified(true);
315 return SCA_IObject::py_setattro(attr, pyvalue);
318 KX_VertexProxy::KX_VertexProxy(KX_MeshProxy*mesh, RAS_TexVert* vertex)
324 KX_VertexProxy::~KX_VertexProxy()
330 // stuff for cvalue related things
331 CValue* KX_VertexProxy::Calc(VALUE_OPERATOR, CValue *) { return NULL;}
332 CValue* KX_VertexProxy::CalcFinal(VALUE_DATA_TYPE, VALUE_OPERATOR, CValue *) { return NULL;}
333 STR_String sVertexName="vertex";
334 const STR_String & KX_VertexProxy::GetText() {return sVertexName;};
335 double KX_VertexProxy::GetNumber() { return -1;}
336 STR_String KX_VertexProxy::GetName() { return sVertexName;}
337 void KX_VertexProxy::SetName(STR_String) { };
338 CValue* KX_VertexProxy::GetReplica() { return NULL;}
339 void KX_VertexProxy::ReplicaSetName(STR_String) {};
342 // stuff for python integration
344 PyObject* KX_VertexProxy::PyGetXYZ(PyObject*)
346 return PyObjectFrom(MT_Point3(m_vertex->getXYZ()));
349 PyObject* KX_VertexProxy::PySetXYZ(PyObject*, PyObject* value)
352 if (!PyVecTo(value, vec))
355 m_vertex->SetXYZ(vec);
356 m_mesh->SetMeshModified(true);
360 PyObject* KX_VertexProxy::PyGetNormal(PyObject*)
362 return PyObjectFrom(MT_Vector3(m_vertex->getNormal()));
365 PyObject* KX_VertexProxy::PySetNormal(PyObject*, PyObject* value)
368 if (!PyVecTo(value, vec))
371 m_vertex->SetNormal(vec);
372 m_mesh->SetMeshModified(true);
377 PyObject* KX_VertexProxy::PyGetRGBA(PyObject*)
379 int *rgba = (int *) m_vertex->getRGBA();
380 return PyInt_FromLong(*rgba);
383 PyObject* KX_VertexProxy::PySetRGBA(PyObject*, PyObject* value)
385 if PyInt_Check(value) {
386 int rgba = PyInt_AsLong(value);
387 m_vertex->SetRGBA(rgba);
388 m_mesh->SetMeshModified(true);
393 if (PyVecTo(value, vec))
395 m_vertex->SetRGBA(vec);
396 m_mesh->SetMeshModified(true);
401 PyErr_SetString(PyExc_TypeError, "expected a 4D vector or an int");
406 PyObject* KX_VertexProxy::PyGetUV(PyObject*)
408 return PyObjectFrom(MT_Vector2(m_vertex->getUV1()));
411 PyObject* KX_VertexProxy::PySetUV(PyObject*, PyObject* value)
414 if (!PyVecTo(value, vec))
417 m_vertex->SetUV(vec);
418 m_mesh->SetMeshModified(true);
422 PyObject* KX_VertexProxy::PyGetUV2(PyObject*)
424 return PyObjectFrom(MT_Vector2(m_vertex->getUV2()));
427 PyObject* KX_VertexProxy::PySetUV2(PyObject*, PyObject* args)
431 PyObject* list= NULL;
432 if(!PyArg_ParseTuple(args, "Oi:setUV2", &list, &unit))
435 if (!PyVecTo(list, vec))
438 m_vertex->SetFlag((m_vertex->getFlag()|RAS_TexVert::SECOND_UV));
439 m_vertex->SetUnit(unit);
440 m_vertex->SetUV2(vec);
441 m_mesh->SetMeshModified(true);