synched with trunk at revision 36569
[blender-staging.git] / source / gameengine / Ketsji / KX_PythonInitTypes.cpp
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): Campbell Barton
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 /** \file gameengine/Ketsji/KX_PythonInitTypes.cpp
31  *  \ingroup ketsji
32  */
33
34
35
36
37 #ifndef _adr_py_init_types_h_                           // only process once,
38 #define _adr_py_init_types_h_                           // even if multiply included
39
40 #ifdef WITH_PYTHON
41
42 /* Only for Class::Parents */
43 #include "BL_BlenderShader.h"
44 #include "BL_ShapeActionActuator.h"
45 #include "BL_ArmatureActuator.h"
46 #include "BL_ArmatureConstraint.h"
47 #include "BL_ArmatureObject.h"
48 #include "BL_ArmatureChannel.h"
49 #include "KX_BlenderMaterial.h"
50 #include "KX_CameraActuator.h"
51 #include "KX_ConstraintActuator.h"
52 #include "KX_ConstraintWrapper.h"
53 #include "KX_GameActuator.h"
54 #include "KX_Light.h"
55 #include "KX_FontObject.h"
56 #include "KX_MeshProxy.h"
57 #include "KX_MouseFocusSensor.h"
58 #include "KX_NetworkMessageActuator.h"
59 #include "KX_NetworkMessageSensor.h"
60 #include "KX_ObjectActuator.h"
61 #include "KX_ParentActuator.h"
62 #include "KX_PhysicsObjectWrapper.h"
63 #include "KX_PolyProxy.h"
64 #include "KX_PolygonMaterial.h"
65 #include "KX_PythonSeq.h"
66 #include "KX_SCA_AddObjectActuator.h"
67 #include "KX_SCA_EndObjectActuator.h"
68 #include "KX_SCA_ReplaceMeshActuator.h"
69 #include "KX_SceneActuator.h"
70 #include "KX_StateActuator.h"
71 #include "KX_SteeringActuator.h"
72 #include "KX_TrackToActuator.h"
73 #include "KX_VehicleWrapper.h"
74 #include "KX_VertexProxy.h"
75 #include "SCA_2DFilterActuator.h"
76 #include "SCA_ANDController.h"
77 #include "SCA_ActuatorSensor.h"
78 #include "SCA_AlwaysSensor.h"
79 #include "SCA_DelaySensor.h"
80 #include "SCA_JoystickSensor.h"
81 #include "SCA_KeyboardSensor.h"
82 #include "SCA_MouseSensor.h"
83 #include "SCA_NANDController.h"
84 #include "SCA_NORController.h"
85 #include "SCA_ORController.h"
86 #include "SCA_RandomSensor.h"
87 #include "SCA_XNORController.h"
88 #include "SCA_XORController.h"
89 #include "SCA_PythonKeyboard.h"
90 #include "SCA_PythonMouse.h"
91 #include "KX_IpoActuator.h"
92 #include "KX_NearSensor.h"
93 #include "KX_RadarSensor.h"
94 #include "KX_RaySensor.h"
95 #include "KX_SCA_DynamicActuator.h"
96 #include "KX_SoundActuator.h"
97 #include "KX_TouchSensor.h"
98 #include "KX_VisibilityActuator.h"
99 #include "SCA_PropertySensor.h"
100 #include "SCA_PythonController.h"
101 #include "SCA_RandomActuator.h"
102 #include "SCA_IController.h"
103 #include "KX_NavMeshObject.h"
104
105 static void PyType_Attr_Set(PyGetSetDef *attr_getset, PyAttributeDef *attr)
106 {
107         attr_getset->name= (char *)attr->m_name;
108         attr_getset->doc= NULL;
109
110         attr_getset->get= reinterpret_cast<getter>(PyObjectPlus::py_get_attrdef);
111
112         if(attr->m_access==KX_PYATTRIBUTE_RO)
113                 attr_getset->set= NULL;
114         else
115                 attr_getset->set= reinterpret_cast<setter>(PyObjectPlus::py_set_attrdef);
116
117         attr_getset->closure= reinterpret_cast<void *>(attr);
118 }
119
120 static void PyType_Ready_ADD(PyObject *dict, PyTypeObject *tp, PyAttributeDef *attributes, PyAttributeDef *attributesPtr, int init_getset)
121 {
122         PyAttributeDef *attr;
123
124         if(init_getset) {
125                 /* we need to do this for all types before calling PyType_Ready
126                  * since they will call the parents PyType_Ready and those might not have initialized vars yet */
127
128                 //if(tp->tp_base==NULL)
129                 //      printf("Debug: No Parents - '%s'\n" , tp->tp_name);
130
131                 if(tp->tp_getset==NULL && ((attributes && attributes->m_name) || (attributesPtr && attributesPtr->m_name))) {
132                         PyGetSetDef *attr_getset;
133                         int attr_tot= 0;
134
135                         if (attributes) {
136                                 for(attr= attributes; attr->m_name; attr++, attr_tot++)
137                                         attr->m_usePtr = false;
138                         }
139                         if (attributesPtr) {
140                                 for(attr= attributesPtr; attr->m_name; attr++, attr_tot++)
141                                         attr->m_usePtr = true;
142                         }
143
144                         tp->tp_getset = attr_getset = reinterpret_cast<PyGetSetDef *>(PyMem_Malloc((attr_tot+1) * sizeof(PyGetSetDef))); // XXX - Todo, free
145
146                         if (attributes) {
147                                 for(attr= attributes; attr->m_name; attr++, attr_getset++) {
148                                         PyType_Attr_Set(attr_getset, attr);
149                                 }
150                         }
151                         if (attributesPtr) {
152                                 for(attr= attributesPtr; attr->m_name; attr++, attr_getset++) {
153                                         PyType_Attr_Set(attr_getset, attr);
154                                 }
155                         }
156                         memset(attr_getset, 0, sizeof(PyGetSetDef));
157                 }
158         } else {
159                 PyType_Ready(tp);
160                 PyDict_SetItemString(dict, tp->tp_name, reinterpret_cast<PyObject *>(tp));
161         }
162         
163 }
164
165
166 #define PyType_Ready_Attr(d, n, i)   PyType_Ready_ADD(d, &n::Type, n::Attributes, NULL, i)
167 #define PyType_Ready_AttrPtr(d, n, i)   PyType_Ready_ADD(d, &n::Type, n::Attributes, n::AttributesPtr, i)
168
169 void initPyTypes(void)
170 {
171         
172 /*
173         initPyObjectPlusType(BL_ActionActuator::Parents);
174         .....
175 */
176
177         /* For now just do PyType_Ready */
178         PyObject *mod= PyModule_New("GameTypes");
179         PyObject *dict= PyModule_GetDict(mod);
180         PyDict_SetItemString(PySys_GetObject((char *)"modules"), (char *)"GameTypes", mod);
181         Py_DECREF(mod);
182         
183         
184         for(int init_getset= 1; init_getset > -1; init_getset--) { /* run twice, once to init the getsets another to run PyType_Ready */
185                 PyType_Ready_Attr(dict, BL_ActionActuator, init_getset);
186                 PyType_Ready_Attr(dict, BL_Shader, init_getset);
187                 PyType_Ready_Attr(dict, BL_ShapeActionActuator, init_getset);
188                 PyType_Ready_Attr(dict, BL_ArmatureObject, init_getset);
189                 PyType_Ready_Attr(dict, BL_ArmatureActuator, init_getset);
190                 PyType_Ready_Attr(dict, BL_ArmatureConstraint, init_getset);
191                 PyType_Ready_AttrPtr(dict, BL_ArmatureBone, init_getset);
192                 PyType_Ready_AttrPtr(dict, BL_ArmatureChannel, init_getset);
193                 PyType_Ready_Attr(dict, CListValue, init_getset);
194                 PyType_Ready_Attr(dict, CValue, init_getset);
195                 PyType_Ready_Attr(dict, KX_BlenderMaterial, init_getset);
196                 PyType_Ready_Attr(dict, KX_Camera, init_getset);
197                 PyType_Ready_Attr(dict, KX_CameraActuator, init_getset);
198                 PyType_Ready_Attr(dict, KX_ConstraintActuator, init_getset);
199                 PyType_Ready_Attr(dict, KX_ConstraintWrapper, init_getset);
200                 PyType_Ready_Attr(dict, KX_GameActuator, init_getset);
201                 PyType_Ready_Attr(dict, KX_GameObject, init_getset);
202                 PyType_Ready_Attr(dict, KX_IpoActuator, init_getset);
203                 PyType_Ready_Attr(dict, KX_LightObject, init_getset);
204                 PyType_Ready_Attr(dict, KX_FontObject, init_getset);
205                 PyType_Ready_Attr(dict, KX_MeshProxy, init_getset);
206                 PyType_Ready_Attr(dict, KX_MouseFocusSensor, init_getset);
207                 PyType_Ready_Attr(dict, KX_NearSensor, init_getset);
208                 PyType_Ready_Attr(dict, KX_NetworkMessageActuator, init_getset);
209                 PyType_Ready_Attr(dict, KX_NetworkMessageSensor, init_getset);
210                 PyType_Ready_Attr(dict, KX_ObjectActuator, init_getset);
211                 PyType_Ready_Attr(dict, KX_ParentActuator, init_getset);
212                 PyType_Ready_Attr(dict, KX_PhysicsObjectWrapper, init_getset);
213                 PyType_Ready_Attr(dict, KX_PolyProxy, init_getset);
214                 PyType_Ready_Attr(dict, KX_PolygonMaterial, init_getset);
215                 PyType_Ready_Attr(dict, KX_RadarSensor, init_getset);
216                 PyType_Ready_Attr(dict, KX_RaySensor, init_getset);
217                 PyType_Ready_Attr(dict, KX_SCA_AddObjectActuator, init_getset);
218                 PyType_Ready_Attr(dict, KX_SCA_DynamicActuator, init_getset);
219                 PyType_Ready_Attr(dict, KX_SCA_EndObjectActuator, init_getset);
220                 PyType_Ready_Attr(dict, KX_SCA_ReplaceMeshActuator, init_getset);
221                 PyType_Ready_Attr(dict, KX_Scene, init_getset);
222                 PyType_Ready_Attr(dict, KX_NavMeshObject, init_getset);
223                 PyType_Ready_Attr(dict, KX_SceneActuator, init_getset);
224                 PyType_Ready_Attr(dict, KX_SoundActuator, init_getset);
225                 PyType_Ready_Attr(dict, KX_StateActuator, init_getset);
226                 PyType_Ready_Attr(dict, KX_SteeringActuator, init_getset);
227                 PyType_Ready_Attr(dict, KX_TouchSensor, init_getset);
228                 PyType_Ready_Attr(dict, KX_TrackToActuator, init_getset);
229                 PyType_Ready_Attr(dict, KX_VehicleWrapper, init_getset);
230                 PyType_Ready_Attr(dict, KX_VertexProxy, init_getset);
231                 PyType_Ready_Attr(dict, KX_VisibilityActuator, init_getset);
232                 PyType_Ready_Attr(dict, PyObjectPlus, init_getset);
233                 PyType_Ready_Attr(dict, SCA_2DFilterActuator, init_getset);
234                 PyType_Ready_Attr(dict, SCA_ANDController, init_getset);
235                 PyType_Ready_Attr(dict, SCA_ActuatorSensor, init_getset);
236                 PyType_Ready_Attr(dict, SCA_AlwaysSensor, init_getset);
237                 PyType_Ready_Attr(dict, SCA_DelaySensor, init_getset);
238                 PyType_Ready_Attr(dict, SCA_ILogicBrick, init_getset);
239                 PyType_Ready_Attr(dict, SCA_IObject, init_getset);
240                 PyType_Ready_Attr(dict, SCA_ISensor, init_getset);
241                 PyType_Ready_Attr(dict, SCA_JoystickSensor, init_getset);
242                 PyType_Ready_Attr(dict, SCA_KeyboardSensor, init_getset);
243                 PyType_Ready_Attr(dict, SCA_MouseSensor, init_getset);
244                 PyType_Ready_Attr(dict, SCA_NANDController, init_getset);
245                 PyType_Ready_Attr(dict, SCA_NORController, init_getset);
246                 PyType_Ready_Attr(dict, SCA_ORController, init_getset);
247                 PyType_Ready_Attr(dict, SCA_PropertyActuator, init_getset);
248                 PyType_Ready_Attr(dict, SCA_PropertySensor, init_getset);
249                 PyType_Ready_Attr(dict, SCA_PythonController, init_getset);
250                 PyType_Ready_Attr(dict, SCA_RandomActuator, init_getset);
251                 PyType_Ready_Attr(dict, SCA_RandomSensor, init_getset);
252                 PyType_Ready_Attr(dict, SCA_XNORController, init_getset);
253                 PyType_Ready_Attr(dict, SCA_XORController, init_getset);
254                 PyType_Ready_Attr(dict, SCA_IController, init_getset);
255                 PyType_Ready_Attr(dict, SCA_PythonKeyboard, init_getset);
256                 PyType_Ready_Attr(dict, SCA_PythonMouse, init_getset);
257         }
258
259
260         /* Normal python type */
261         PyType_Ready(&KX_PythonSeq_Type);
262
263 #ifdef USE_MATHUTILS
264         /* Init mathutils callbacks */
265         KX_GameObject_Mathutils_Callback_Init();
266         KX_ObjectActuator_Mathutils_Callback_Init();
267 #endif
268 }
269
270 #endif // WITH_PYTHON
271
272 #endif