game engine python api
[blender.git] / source / gameengine / Ketsji / KX_PythonInit.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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  * Initialize Python thingies.
29  */
30
31 #include "GL/glew.h"
32
33 #include <stdlib.h>
34
35 #ifdef WIN32
36 #pragma warning (disable : 4786)
37 #endif //WIN32
38
39 #include "KX_PythonInit.h"
40 //python physics binding
41 #include "KX_PyConstraintBinding.h"
42
43 #include "KX_KetsjiEngine.h"
44
45 #include "SCA_IInputDevice.h"
46 #include "SCA_PropertySensor.h"
47 #include "SCA_RandomActuator.h"
48 #include "KX_ConstraintActuator.h"
49 #include "KX_IpoActuator.h"
50 #include "KX_SoundActuator.h"
51 #include "BL_ActionActuator.h"
52 #include "RAS_IRasterizer.h"
53 #include "RAS_ICanvas.h"
54 #include "MT_Vector3.h"
55 #include "MT_Point3.h"
56 #include "ListValue.h"
57 #include "KX_Scene.h"
58 #include "SND_DeviceManager.h"
59
60 #include "RAS_OpenGLRasterizer/RAS_GLExtensionManager.h"
61 #include "BL_Shader.h"
62
63 #include "KX_PyMath.h"
64
65 extern "C" {
66         #include "Mathutils.h" // Blender.Mathutils module copied here so the blenderlayer can use.
67 }
68
69 #include "PHY_IPhysicsEnvironment.h"
70 // FIXME: Enable for access to blender python modules.  This is disabled because
71 // python has dependencies on a lot of other modules and is a pain to link.
72 //#define USE_BLENDER_PYTHON
73 #ifdef USE_BLENDER_PYTHON
74 //#include "BPY_extern.h"
75 #endif 
76
77 #include "BKE_utildefines.h"
78 #include "BKE_global.h"
79 #include "BLI_blenlib.h"
80
81 static void setSandbox(TPythonSecurityLevel level);
82
83
84 // 'local' copy of canvas ptr, for window height/width python scripts
85 static RAS_ICanvas* gp_Canvas = NULL;
86 static KX_Scene*        gp_KetsjiScene = NULL;
87 static RAS_IRasterizer* gp_Rasterizer = NULL;
88
89 void    KX_RasterizerDrawDebugLine(const MT_Vector3& from,const MT_Vector3& to,const MT_Vector3& color)
90 {
91         if (gp_Rasterizer)
92                 gp_Rasterizer->DrawDebugLine(from,to,color);
93 }
94
95 /* Macro for building the keyboard translation */
96 //#define KX_MACRO_addToDict(dict, name) PyDict_SetItemString(dict, #name, PyInt_FromLong(SCA_IInputDevice::KX_##name))
97 #define KX_MACRO_addToDict(dict, name) PyDict_SetItemString(dict, #name, PyInt_FromLong(name))
98 /* For the defines for types from logic bricks, we do stuff explicitly... */
99 #define KX_MACRO_addTypesToDict(dict, name, name2) PyDict_SetItemString(dict, #name, PyInt_FromLong(name2))
100
101
102 // temporarily python stuff, will be put in another place later !
103 #include "KX_Python.h"
104 #include "SCA_PythonController.h"
105 // List of methods defined in the module
106
107 static PyObject* ErrorObject;
108 STR_String gPyGetRandomFloat_doc="getRandomFloat returns a random floating point value in the range [0..1)";
109
110 static PyObject* gPyGetRandomFloat(PyObject*)
111 {
112         return PyFloat_FromDouble(MT_random());
113 }
114
115 static PyObject* gPySetGravity(PyObject*,
116                                                                                  PyObject* args, 
117                                                                                  PyObject*)
118 {
119         MT_Vector3 vec = MT_Vector3(0., 0., 0.);
120         if (PyVecArgTo(args, vec))
121         {
122                 if (gp_KetsjiScene)
123                         gp_KetsjiScene->SetGravity(vec);
124                 
125                 Py_Return;
126         }
127         
128         return NULL;
129 }
130
131 static char gPyExpandPath_doc[] =
132 "(path) - Converts a blender internal path into a proper file system path.\n\
133 path - the string path to convert.\n\n\
134 Use / as directory separator in path\n\
135 You can use '//' at the start of the string to define a relative path;\n\
136 Blender replaces that string by the directory of the startup .blend or runtime\n\
137 file to make a full path name (doesn't change during the game, even if you load\n\
138 other .blend).\n\
139 The function also converts the directory separator to the local file system format.";
140
141 static PyObject* gPyExpandPath(PyObject*,
142                                                                 PyObject* args, 
143                                                                 PyObject*)
144 {
145         char expanded[FILE_MAXDIR + FILE_MAXFILE];
146         char* filename;
147         
148         if (PyArg_ParseTuple(args,"s",&filename))
149         {
150                 BLI_strncpy(expanded, filename, FILE_MAXDIR + FILE_MAXFILE);
151                 BLI_convertstringcode(expanded, G.sce);
152                 return PyString_FromString(expanded);
153         }
154         return NULL;
155 }
156
157
158 static bool usedsp = false;
159
160 // this gets a pointer to an array filled with floats
161 static PyObject* gPyGetSpectrum(PyObject*)
162 {
163         SND_IAudioDevice* audiodevice = SND_DeviceManager::Instance();
164
165         PyObject* resultlist = PyList_New(512);
166
167         if (audiodevice)
168         {
169                 if (!usedsp)
170                 {
171                         audiodevice->StartUsingDSP();
172                         usedsp = true;
173                 }
174                         
175                 float* spectrum = audiodevice->GetSpectrum();
176
177                 for (int index = 0; index < 512; index++)
178                 {
179                         PyList_SetItem(resultlist, index, PyFloat_FromDouble(spectrum[index]));
180                 }
181         }
182
183         return resultlist;
184 }
185
186
187
188 static PyObject* gPyStartDSP(PyObject*,
189                                                 PyObject* args, 
190                                                 PyObject*)
191 {
192         SND_IAudioDevice* audiodevice = SND_DeviceManager::Instance();
193
194         if (audiodevice)
195         {
196                 if (!usedsp)
197                 {
198                         audiodevice->StartUsingDSP();
199                         usedsp = true;
200                         Py_Return;
201                 }
202         }
203         return NULL;
204 }
205
206
207
208 static PyObject* gPyStopDSP(PyObject*,
209                                            PyObject* args, 
210                                            PyObject*)
211 {
212         SND_IAudioDevice* audiodevice = SND_DeviceManager::Instance();
213
214         if (audiodevice)
215         {
216                 if (usedsp)
217                 {
218                         audiodevice->StopUsingDSP();
219                         usedsp = false;
220                         Py_Return;
221                 }
222         }
223         return NULL;
224 }
225
226 static PyObject* gPySetLogicTicRate(PyObject*,
227                                         PyObject* args,
228                                         PyObject*)
229 {
230         float ticrate;
231         if (PyArg_ParseTuple(args, "f", &ticrate))
232         {
233                 KX_KetsjiEngine::SetTicRate(ticrate);
234                 Py_Return;
235         }
236         
237         return NULL;
238 }
239
240 static PyObject* gPyGetLogicTicRate(PyObject*)
241 {
242         return PyFloat_FromDouble(KX_KetsjiEngine::GetTicRate());
243 }
244
245 static PyObject* gPySetPhysicsTicRate(PyObject*,
246                                         PyObject* args,
247                                         PyObject*)
248 {
249         float ticrate;
250         if (PyArg_ParseTuple(args, "f", &ticrate))
251         {
252
253                 PHY_GetActiveEnvironment()->setFixedTimeStep(true,ticrate);
254                 Py_Return;
255         }
256         
257         return NULL;
258 }
259
260 static PyObject* gPySetPhysicsDebug(PyObject*,
261                                         PyObject* args,
262                                         PyObject*)
263 {
264         int debugMode;
265         if (PyArg_ParseTuple(args, "i", &debugMode))
266         {
267                 PHY_GetActiveEnvironment()->setDebugMode(debugMode);
268                 Py_Return;
269         }
270         
271         return NULL;
272 }
273
274
275
276 static PyObject* gPyGetPhysicsTicRate(PyObject*)
277 {
278         return PyFloat_FromDouble(PHY_GetActiveEnvironment()->getFixedTimeStep());
279 }
280
281 static STR_String gPyGetCurrentScene_doc =  
282 "getCurrentScene()\n"
283 "Gets a reference to the current scene.\n";
284 static PyObject* gPyGetCurrentScene(PyObject* self)
285 {
286         Py_INCREF(gp_KetsjiScene);
287         return (PyObject*) gp_KetsjiScene;
288 }
289
290 static PyObject *pyPrintExt(PyObject *,PyObject *,PyObject *)
291 {
292 #define pprint(x) std::cout << x << std::endl;
293         bool count=0;
294         bool support=0;
295         pprint("Supported Extensions...");
296         pprint(" GL_ARB_shader_objects supported?       "<< (GLEW_ARB_shader_objects?"yes.":"no."));
297         count = 1;
298
299         support= GLEW_ARB_vertex_shader;
300         pprint(" GL_ARB_vertex_shader supported?        "<< (support?"yes.":"no."));
301         count = 1;
302         if(support){
303                 pprint(" ----------Details----------");
304                 int max=0;
305                 glGetIntegerv(GL_MAX_VERTEX_UNIFORM_COMPONENTS_ARB, (GLint*)&max);
306                 pprint("  Max uniform components." << max);
307
308                 glGetIntegerv(GL_MAX_VARYING_FLOATS_ARB, (GLint*)&max);
309                 pprint("  Max varying floats." << max);
310
311                 glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS_ARB, (GLint*)&max);
312                 pprint("  Max vertex texture units." << max);
313         
314                 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS_ARB, (GLint*)&max);
315                 pprint("  Max combined texture units." << max);
316                 pprint("");
317         }
318
319         support=GLEW_ARB_fragment_shader;
320         pprint(" GL_ARB_fragment_shader supported?      "<< (support?"yes.":"no."));
321         count = 1;
322         if(support){
323                 pprint(" ----------Details----------");
324                 int max=0;
325                 glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_COMPONENTS_ARB, (GLint*)&max);
326                 pprint("  Max uniform components." << max);
327                 pprint("");
328         }
329
330         support = GLEW_ARB_texture_cube_map;
331         pprint(" GL_ARB_texture_cube_map supported?     "<< (support?"yes.":"no."));
332         count = 1;
333         if(support){
334                 pprint(" ----------Details----------");
335                 int size=0;
336                 glGetIntegerv(GL_MAX_CUBE_MAP_TEXTURE_SIZE_ARB, (GLint*)&size);
337                 pprint("  Max cubemap size." << size);
338                 pprint("");
339         }
340
341         support = GLEW_ARB_multitexture;
342         count = 1;
343         pprint(" GL_ARB_multitexture supported?         "<< (support?"yes.":"no."));
344         if(support){
345                 pprint(" ----------Details----------");
346                 int units=0;
347                 glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, (GLint*)&units);
348                 pprint("  Max texture units available.  " << units);
349                 pprint("");
350         }
351
352         pprint(" GL_ARB_texture_env_combine supported?  "<< (GLEW_ARB_texture_env_combine?"yes.":"no."));
353         count = 1;
354
355         if(!count)
356                 pprint("No extenstions are used in this build");
357
358         Py_RETURN_NONE;
359 }
360
361
362 static struct PyMethodDef game_methods[] = {
363         {"expandPath", (PyCFunction)gPyExpandPath, METH_VARARGS, gPyExpandPath_doc},
364         {"getCurrentController",
365         (PyCFunction) SCA_PythonController::sPyGetCurrentController,
366         METH_NOARGS, SCA_PythonController::sPyGetCurrentController__doc__},
367         {"getCurrentScene", (PyCFunction) gPyGetCurrentScene,
368         METH_NOARGS, gPyGetCurrentScene_doc.Ptr()},
369         {"addActiveActuator",(PyCFunction) SCA_PythonController::sPyAddActiveActuator,
370         METH_VARARGS, SCA_PythonController::sPyAddActiveActuator__doc__},
371         {"getRandomFloat",(PyCFunction) gPyGetRandomFloat,
372         METH_NOARGS,gPyGetRandomFloat_doc.Ptr()},
373         {"setGravity",(PyCFunction) gPySetGravity, METH_VARARGS,"set Gravitation"},
374         {"getSpectrum",(PyCFunction) gPyGetSpectrum, METH_NOARGS,"get audio spectrum"},
375         {"stopDSP",(PyCFunction) gPyStopDSP, METH_VARARGS,"stop using the audio dsp (for performance reasons)"},
376         {"getLogicTicRate", (PyCFunction) gPyGetLogicTicRate, METH_NOARGS, "Gets the logic tic rate"},
377         {"setLogicTicRate", (PyCFunction) gPySetLogicTicRate, METH_VARARGS, "Sets the logic tic rate"},
378         {"getPhysicsTicRate", (PyCFunction) gPyGetPhysicsTicRate, METH_NOARGS, "Gets the physics tic rate"},
379         {"setPhysicsTicRate", (PyCFunction) gPySetPhysicsTicRate, METH_VARARGS, "Sets the physics tic rate"},
380         {"PrintGLInfo", (PyCFunction)pyPrintExt, METH_NOARGS, "Prints GL Extension Info"},
381         {NULL, (PyCFunction) NULL, 0, NULL }
382 };
383
384
385 static PyObject* gPyGetWindowHeight(PyObject*, 
386                                                                                  PyObject* args, 
387                                                                                  PyObject*)
388 {
389         int height = (gp_Canvas ? gp_Canvas->GetHeight() : 0);
390
391                 PyObject* heightval = PyInt_FromLong(height);
392                 return heightval;
393 }
394
395
396
397 static PyObject* gPyGetWindowWidth(PyObject*, 
398                                                                                  PyObject* args, 
399                                                                                  PyObject*)
400 {
401                 
402
403         int width = (gp_Canvas ? gp_Canvas->GetWidth() : 0);
404         
405                 PyObject* widthval = PyInt_FromLong(width);
406                 return widthval;
407 }
408
409
410
411 // temporarility visibility thing, will be moved to rasterizer/renderer later
412 bool gUseVisibilityTemp = false;
413
414 static PyObject* gPyEnableVisibility(PyObject*, 
415                                                                                  PyObject* args, 
416                                                                                  PyObject*)
417 {
418         int visible;
419         if (PyArg_ParseTuple(args,"i",&visible))
420         {
421             gUseVisibilityTemp = (visible != 0);
422         }
423         else
424         {
425                 return NULL;
426         }
427    Py_Return;
428 }
429
430
431
432 static PyObject* gPyShowMouse(PyObject*, 
433                                                                                  PyObject* args, 
434                                                                                  PyObject*)
435 {
436         int visible;
437         if (PyArg_ParseTuple(args,"i",&visible))
438         {
439             if (visible)
440                 {
441                         if (gp_Canvas)
442                                 gp_Canvas->SetMouseState(RAS_ICanvas::MOUSE_NORMAL);
443                 } else
444                 {
445                         if (gp_Canvas)
446                                 gp_Canvas->SetMouseState(RAS_ICanvas::MOUSE_INVISIBLE);
447                 }
448         }
449         else {
450                 return NULL;
451         }
452         
453    Py_Return;
454 }
455
456
457
458 static PyObject* gPySetMousePosition(PyObject*, 
459                                                                                  PyObject* args, 
460                                                                                  PyObject*)
461 {
462         int x,y;
463         if (PyArg_ParseTuple(args,"ii",&x,&y))
464         {
465             if (gp_Canvas)
466                         gp_Canvas->SetMousePosition(x,y);
467         }
468         else {
469                 return NULL;
470         }
471         
472    Py_Return;
473 }
474
475 static PyObject* gPySetEyeSeparation(PyObject*,
476                                                 PyObject* args,
477                                                 PyObject*)
478 {
479         float sep;
480         if (PyArg_ParseTuple(args, "f", &sep))
481         {
482                 if (gp_Rasterizer)
483                         gp_Rasterizer->SetEyeSeparation(sep);
484                         
485                 Py_Return;
486         }
487         
488         return NULL;
489 }
490
491 static PyObject* gPyGetEyeSeparation(PyObject*, PyObject*, PyObject*)
492 {
493         if (gp_Rasterizer)
494                 return PyFloat_FromDouble(gp_Rasterizer->GetEyeSeparation());
495         
496         return NULL;
497 }
498
499 static PyObject* gPySetFocalLength(PyObject*,
500                                         PyObject* args,
501                                         PyObject*)
502 {
503         float focus;
504         if (PyArg_ParseTuple(args, "f", &focus))
505         {
506                 if (gp_Rasterizer)
507                         gp_Rasterizer->SetFocalLength(focus);
508                 Py_Return;
509         }
510         
511         return NULL;
512 }
513
514 static PyObject* gPyGetFocalLength(PyObject*, PyObject*, PyObject*)
515 {
516         if (gp_Rasterizer)
517                 return PyFloat_FromDouble(gp_Rasterizer->GetFocalLength());
518         return NULL;
519 }
520
521 static PyObject* gPySetBackgroundColor(PyObject*, 
522                                                                                  PyObject* args, 
523                                                                                  PyObject*)
524 {
525         
526         MT_Vector4 vec = MT_Vector4(0., 0., 0.3, 0.);
527         if (PyVecArgTo(args, vec))
528         {
529                 if (gp_Canvas)
530                 {
531                         gp_Rasterizer->SetBackColor(vec[0], vec[1], vec[2], vec[3]);
532                 }
533                 Py_Return;
534         }
535         
536         return NULL;
537 }
538
539
540
541 static PyObject* gPySetMistColor(PyObject*, 
542                                                                                  PyObject* args, 
543                                                                                  PyObject*)
544 {
545         
546         MT_Vector3 vec = MT_Vector3(0., 0., 0.);
547         if (PyVecArgTo(args, vec))
548         {
549                 if (gp_Rasterizer)
550                 {
551                         gp_Rasterizer->SetFogColor(vec[0], vec[1], vec[2]);
552                 }
553                 Py_Return;
554         }
555         
556         return NULL;
557 }
558
559
560
561 static PyObject* gPySetMistStart(PyObject*, 
562                                                                                  PyObject* args, 
563                                                                                  PyObject*)
564 {
565
566         float miststart;
567         if (PyArg_ParseTuple(args,"f",&miststart))
568         {
569                 if (gp_Rasterizer)
570                 {
571                         gp_Rasterizer->SetFogStart(miststart);
572                 }
573         }
574         else {
575                 return NULL;
576         }
577    Py_Return;
578 }
579
580
581
582 static PyObject* gPySetMistEnd(PyObject*, 
583                                                                                  PyObject* args, 
584                                                                                  PyObject*)
585 {
586
587         float mistend;
588         if (PyArg_ParseTuple(args,"f",&mistend))
589         {
590                 if (gp_Rasterizer)
591                 {
592                         gp_Rasterizer->SetFogEnd(mistend);
593                 }
594         }
595         else {
596                 return NULL;
597         }
598    Py_Return;
599 }
600
601
602 static PyObject* gPySetAmbientColor(PyObject*, 
603                                                                                  PyObject* args, 
604                                                                                  PyObject*)
605 {
606         
607         MT_Vector3 vec = MT_Vector3(0., 0., 0.);
608         if (PyVecArgTo(args, vec))
609         {
610                 if (gp_Rasterizer)
611                 {
612                         gp_Rasterizer->SetAmbientColor(vec[0], vec[1], vec[2]);
613                 }
614                 Py_Return;
615         }
616         
617         return NULL;
618 }
619
620
621
622
623 static PyObject* gPyMakeScreenshot(PyObject*,
624                                                                         PyObject* args,
625                                                                         PyObject*)
626 {
627         char* filename;
628         if (PyArg_ParseTuple(args,"s",&filename))
629         {
630                 if (gp_Canvas)
631                 {
632                         gp_Canvas->MakeScreenShot(filename);
633                 }
634         }
635         else {
636                 return NULL;
637         }
638         Py_Return;
639 }
640
641 static PyObject* gPyEnableMotionBlur(PyObject*,
642                                                                         PyObject* args,
643                                                                         PyObject*)
644 {
645         float motionblurvalue;
646         if (PyArg_ParseTuple(args,"f",&motionblurvalue))
647         {
648                 if(gp_Rasterizer)
649                 {
650                         gp_Rasterizer->EnableMotionBlur(motionblurvalue);
651                 }
652         }
653         else {
654                 return NULL;
655         }
656         Py_Return;
657 }
658
659 static PyObject* gPyDisableMotionBlur(PyObject*,
660                                                                         PyObject* args,
661                                                                         PyObject*)
662 {
663         if(gp_Rasterizer)
664         {
665                 gp_Rasterizer->DisableMotionBlur();
666         }
667         Py_Return;
668 }
669
670 STR_String      gPyGetWindowHeight__doc__="getWindowHeight doc";
671 STR_String      gPyGetWindowWidth__doc__="getWindowWidth doc";
672 STR_String      gPyEnableVisibility__doc__="enableVisibility doc";
673 STR_String      gPyMakeScreenshot__doc__="make Screenshot doc";
674 STR_String      gPyShowMouse__doc__="showMouse(bool visible)";
675 STR_String      gPySetMousePosition__doc__="setMousePosition(int x,int y)";
676
677 static struct PyMethodDef rasterizer_methods[] = {
678   {"getWindowWidth",(PyCFunction) gPyGetWindowWidth,
679    METH_VARARGS, gPyGetWindowWidth__doc__.Ptr()},
680    {"getWindowHeight",(PyCFunction) gPyGetWindowHeight,
681    METH_VARARGS, gPyGetWindowHeight__doc__.Ptr()},
682   {"makeScreenshot",(PyCFunction)gPyMakeScreenshot,
683         METH_VARARGS, gPyMakeScreenshot__doc__.Ptr()},
684    {"enableVisibility",(PyCFunction) gPyEnableVisibility,
685    METH_VARARGS, gPyEnableVisibility__doc__.Ptr()},
686         {"showMouse",(PyCFunction) gPyShowMouse,
687    METH_VARARGS, gPyShowMouse__doc__.Ptr()},
688    {"setMousePosition",(PyCFunction) gPySetMousePosition,
689    METH_VARARGS, gPySetMousePosition__doc__.Ptr()},
690   {"setBackgroundColor",(PyCFunction)gPySetBackgroundColor,METH_VARARGS,"set Background Color (rgb)"},
691         {"setAmbientColor",(PyCFunction)gPySetAmbientColor,METH_VARARGS,"set Ambient Color (rgb)"},
692  {"setMistColor",(PyCFunction)gPySetMistColor,METH_VARARGS,"set Mist Color (rgb)"},
693   {"setMistStart",(PyCFunction)gPySetMistStart,METH_VARARGS,"set Mist Start(rgb)"},
694   {"setMistEnd",(PyCFunction)gPySetMistEnd,METH_VARARGS,"set Mist End(rgb)"},
695   {"enableMotionBlur",(PyCFunction)gPyEnableMotionBlur,METH_VARARGS,"enable motion blur"},
696   {"disableMotionBlur",(PyCFunction)gPyDisableMotionBlur,METH_VARARGS,"disable motion blur"},
697
698   
699   {"setEyeSeparation", (PyCFunction) gPySetEyeSeparation, METH_VARARGS, "set the eye separation for stereo mode"},
700   {"getEyeSeparation", (PyCFunction) gPyGetEyeSeparation, METH_VARARGS, "get the eye separation for stereo mode"},
701   {"setFocalLength", (PyCFunction) gPySetFocalLength, METH_VARARGS, "set the focal length for stereo mode"},
702   {"getFocalLength", (PyCFunction) gPyGetFocalLength, METH_VARARGS, "get the focal length for stereo mode"},
703   { NULL, (PyCFunction) NULL, 0, NULL }
704 };
705
706
707
708 // Initialization function for the module (*must* be called initGameLogic)
709
710 static char GameLogic_module_documentation[] =
711 "This is the Python API for the game engine of GameLogic"
712 ;
713
714 static char Rasterizer_module_documentation[] =
715 "This is the Python API for the game engine of Rasterizer"
716 ;
717
718
719
720 PyObject* initGameLogic(KX_Scene* scene) // quick hack to get gravity hook
721 {
722         PyObject* m;
723         PyObject* d;
724
725         gp_KetsjiScene = scene;
726
727         gUseVisibilityTemp=false;
728
729         // Create the module and add the functions
730         m = Py_InitModule4("GameLogic", game_methods,
731                                            GameLogic_module_documentation,
732                                            (PyObject*)NULL,PYTHON_API_VERSION);
733
734         // Add some symbolic constants to the module
735         d = PyModule_GetDict(m);
736
737         ErrorObject = PyString_FromString("GameLogic.error");
738         PyDict_SetItemString(d, "error", ErrorObject);
739         
740         // XXXX Add constants here
741         /* To use logic bricks, we need some sort of constants. Here, we associate */
742         /* constants and sumbolic names. Add them to dictionary d.                 */
743
744         /* 1. true and false: needed for everyone                                  */
745         KX_MACRO_addTypesToDict(d, KX_TRUE,  SCA_ILogicBrick::KX_TRUE);
746         KX_MACRO_addTypesToDict(d, KX_FALSE, SCA_ILogicBrick::KX_FALSE);
747
748         /* 2. Property sensor                                                      */
749         KX_MACRO_addTypesToDict(d, KX_PROPSENSOR_EQUAL,      SCA_PropertySensor::KX_PROPSENSOR_EQUAL);
750         KX_MACRO_addTypesToDict(d, KX_PROPSENSOR_NOTEQUAL,   SCA_PropertySensor::KX_PROPSENSOR_NOTEQUAL);
751         KX_MACRO_addTypesToDict(d, KX_PROPSENSOR_INTERVAL,   SCA_PropertySensor::KX_PROPSENSOR_INTERVAL);
752         KX_MACRO_addTypesToDict(d, KX_PROPSENSOR_CHANGED,    SCA_PropertySensor::KX_PROPSENSOR_CHANGED);
753         KX_MACRO_addTypesToDict(d, KX_PROPSENSOR_EXPRESSION, SCA_PropertySensor::KX_PROPSENSOR_EXPRESSION);
754
755         /* 3. Constraint actuator                                                  */
756         KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_LOCX, KX_ConstraintActuator::KX_ACT_CONSTRAINT_LOCX);
757         KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_LOCY, KX_ConstraintActuator::KX_ACT_CONSTRAINT_LOCY);
758         KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_LOCZ, KX_ConstraintActuator::KX_ACT_CONSTRAINT_LOCZ);
759         KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_ROTX, KX_ConstraintActuator::KX_ACT_CONSTRAINT_ROTX);
760         KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_ROTY, KX_ConstraintActuator::KX_ACT_CONSTRAINT_ROTY);
761         KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_ROTZ, KX_ConstraintActuator::KX_ACT_CONSTRAINT_ROTZ);
762         KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_DIRPX, KX_ConstraintActuator::KX_ACT_CONSTRAINT_DIRPX);
763         KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_DIRPY, KX_ConstraintActuator::KX_ACT_CONSTRAINT_DIRPY);
764         KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_DIRPY, KX_ConstraintActuator::KX_ACT_CONSTRAINT_DIRPY);
765         KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_DIRNX, KX_ConstraintActuator::KX_ACT_CONSTRAINT_DIRNX);
766         KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_DIRNY, KX_ConstraintActuator::KX_ACT_CONSTRAINT_DIRNY);
767         KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_DIRNY, KX_ConstraintActuator::KX_ACT_CONSTRAINT_DIRNY);
768         KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_ORIX, KX_ConstraintActuator::KX_ACT_CONSTRAINT_ORIX);
769         KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_ORIY, KX_ConstraintActuator::KX_ACT_CONSTRAINT_ORIY);
770         KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_ORIZ, KX_ConstraintActuator::KX_ACT_CONSTRAINT_ORIZ);
771
772         /* 4. Ipo actuator, simple part                                            */
773         KX_MACRO_addTypesToDict(d, KX_IPOACT_PLAY,     KX_IpoActuator::KX_ACT_IPO_PLAY);
774         KX_MACRO_addTypesToDict(d, KX_IPOACT_PINGPONG, KX_IpoActuator::KX_ACT_IPO_PINGPONG);
775         KX_MACRO_addTypesToDict(d, KX_IPOACT_FLIPPER,  KX_IpoActuator::KX_ACT_IPO_FLIPPER);
776         KX_MACRO_addTypesToDict(d, KX_IPOACT_LOOPSTOP, KX_IpoActuator::KX_ACT_IPO_LOOPSTOP);
777         KX_MACRO_addTypesToDict(d, KX_IPOACT_LOOPEND,  KX_IpoActuator::KX_ACT_IPO_LOOPEND);
778
779         /* 5. Random distribution types                                            */
780         KX_MACRO_addTypesToDict(d, KX_RANDOMACT_BOOL_CONST,      SCA_RandomActuator::KX_RANDOMACT_BOOL_CONST);
781         KX_MACRO_addTypesToDict(d, KX_RANDOMACT_BOOL_UNIFORM,    SCA_RandomActuator::KX_RANDOMACT_BOOL_UNIFORM);
782         KX_MACRO_addTypesToDict(d, KX_RANDOMACT_BOOL_BERNOUILLI, SCA_RandomActuator::KX_RANDOMACT_BOOL_BERNOUILLI);
783         KX_MACRO_addTypesToDict(d, KX_RANDOMACT_INT_CONST,       SCA_RandomActuator::KX_RANDOMACT_INT_CONST);
784         KX_MACRO_addTypesToDict(d, KX_RANDOMACT_INT_UNIFORM,     SCA_RandomActuator::KX_RANDOMACT_INT_UNIFORM);
785         KX_MACRO_addTypesToDict(d, KX_RANDOMACT_INT_POISSON,     SCA_RandomActuator::KX_RANDOMACT_INT_POISSON);
786         KX_MACRO_addTypesToDict(d, KX_RANDOMACT_FLOAT_CONST,     SCA_RandomActuator::KX_RANDOMACT_FLOAT_CONST);
787         KX_MACRO_addTypesToDict(d, KX_RANDOMACT_FLOAT_UNIFORM,   SCA_RandomActuator::KX_RANDOMACT_FLOAT_UNIFORM);
788         KX_MACRO_addTypesToDict(d, KX_RANDOMACT_FLOAT_NORMAL,    SCA_RandomActuator::KX_RANDOMACT_FLOAT_NORMAL);
789         KX_MACRO_addTypesToDict(d, KX_RANDOMACT_FLOAT_NEGATIVE_EXPONENTIAL, SCA_RandomActuator::KX_RANDOMACT_FLOAT_NEGATIVE_EXPONENTIAL);
790
791         /* 6. Sound actuator                                                      */
792         KX_MACRO_addTypesToDict(d, KX_SOUNDACT_PLAYSTOP,              KX_SoundActuator::KX_SOUNDACT_PLAYSTOP);
793         KX_MACRO_addTypesToDict(d, KX_SOUNDACT_PLAYEND,               KX_SoundActuator::KX_SOUNDACT_PLAYEND);
794         KX_MACRO_addTypesToDict(d, KX_SOUNDACT_LOOPSTOP,              KX_SoundActuator::KX_SOUNDACT_LOOPSTOP);
795         KX_MACRO_addTypesToDict(d, KX_SOUNDACT_LOOPEND,               KX_SoundActuator::KX_SOUNDACT_LOOPEND);
796         KX_MACRO_addTypesToDict(d, KX_SOUNDACT_LOOPBIDIRECTIONAL,     KX_SoundActuator::KX_SOUNDACT_LOOPBIDIRECTIONAL);
797         KX_MACRO_addTypesToDict(d, KX_SOUNDACT_LOOPBIDIRECTIONAL_STOP,     KX_SoundActuator::KX_SOUNDACT_LOOPBIDIRECTIONAL_STOP);
798
799         /* 7. Action actuator                                                                                                      */
800         KX_MACRO_addTypesToDict(d, KX_ACTIONACT_PLAY,     BL_ActionActuator::KX_ACT_ACTION_PLAY);
801         KX_MACRO_addTypesToDict(d, KX_ACTIONACT_FLIPPER,     BL_ActionActuator::KX_ACT_ACTION_FLIPPER);
802         KX_MACRO_addTypesToDict(d, KX_ACTIONACT_LOOPSTOP,     BL_ActionActuator::KX_ACT_ACTION_LOOPSTOP);
803         KX_MACRO_addTypesToDict(d, KX_ACTIONACT_LOOPEND,     BL_ActionActuator::KX_ACT_ACTION_LOOPEND);
804         KX_MACRO_addTypesToDict(d, KX_ACTIONACT_PROPERTY,     BL_ActionActuator::KX_ACT_ACTION_PROPERTY);
805         
806         /*8. GL_BlendFunc */
807         KX_MACRO_addTypesToDict(d, BL_ZERO, GL_ZERO);
808         KX_MACRO_addTypesToDict(d, BL_ONE, GL_ONE);
809         KX_MACRO_addTypesToDict(d, BL_SRC_COLOR, GL_SRC_COLOR);
810         KX_MACRO_addTypesToDict(d, BL_ONE_MINUS_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR);
811         KX_MACRO_addTypesToDict(d, BL_DST_COLOR, GL_DST_COLOR);
812         KX_MACRO_addTypesToDict(d, BL_ONE_MINUS_DST_COLOR, GL_ONE_MINUS_DST_COLOR);
813         KX_MACRO_addTypesToDict(d, BL_SRC_ALPHA, GL_SRC_ALPHA);
814         KX_MACRO_addTypesToDict(d, BL_ONE_MINUS_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
815         KX_MACRO_addTypesToDict(d, BL_DST_ALPHA, GL_DST_ALPHA);
816         KX_MACRO_addTypesToDict(d, BL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA);
817         KX_MACRO_addTypesToDict(d, BL_SRC_ALPHA_SATURATE, GL_SRC_ALPHA_SATURATE);
818
819
820         /* 9. UniformTypes */
821         KX_MACRO_addTypesToDict(d, SHD_TANGENT, BL_Shader::SHD_TANGENT);
822         KX_MACRO_addTypesToDict(d, MODELVIEWMATRIX, BL_Shader::MODELVIEWMATRIX);
823         KX_MACRO_addTypesToDict(d, MODELVIEWMATRIX_TRANSPOSE, BL_Shader::MODELVIEWMATRIX_TRANSPOSE);
824         KX_MACRO_addTypesToDict(d, MODELVIEWMATRIX_INVERSE, BL_Shader::MODELVIEWMATRIX_INVERSE);
825         KX_MACRO_addTypesToDict(d, MODELVIEWMATRIX_INVERSETRANSPOSE, BL_Shader::MODELVIEWMATRIX_INVERSETRANSPOSE);
826         KX_MACRO_addTypesToDict(d, MODELMATRIX, BL_Shader::MODELMATRIX);
827         KX_MACRO_addTypesToDict(d, MODELMATRIX_TRANSPOSE, BL_Shader::MODELMATRIX_TRANSPOSE);
828         KX_MACRO_addTypesToDict(d, MODELMATRIX_INVERSE, BL_Shader::MODELMATRIX_INVERSE);
829         KX_MACRO_addTypesToDict(d, MODELMATRIX_INVERSETRANSPOSE, BL_Shader::MODELMATRIX_INVERSETRANSPOSE);
830         KX_MACRO_addTypesToDict(d, VIEWMATRIX, BL_Shader::VIEWMATRIX);
831         KX_MACRO_addTypesToDict(d, VIEWMATRIX_TRANSPOSE, BL_Shader::VIEWMATRIX_TRANSPOSE);
832         KX_MACRO_addTypesToDict(d, VIEWMATRIX_INVERSE, BL_Shader::VIEWMATRIX_INVERSE);
833         KX_MACRO_addTypesToDict(d, VIEWMATRIX_INVERSETRANSPOSE, BL_Shader::VIEWMATRIX_INVERSETRANSPOSE);
834         KX_MACRO_addTypesToDict(d, CAM_POS, BL_Shader::CAM_POS);
835         KX_MACRO_addTypesToDict(d, CONSTANT_TIMER, BL_Shader::CONSTANT_TIMER);
836
837         // Check for errors
838         if (PyErr_Occurred())
839     {
840                 Py_FatalError("can't initialize module GameLogic");
841     }
842
843         return m;
844 }
845
846 // Python Sandbox code
847 // override builtin functions import() and open()
848
849
850 PyObject *KXpy_open(PyObject *self, PyObject *args)
851 {
852         PyErr_SetString(PyExc_RuntimeError, "Sandbox: open() function disabled!\nGame Scripts should not use this function.");
853         return NULL;
854 }
855
856
857
858 PyObject *KXpy_import(PyObject *self, PyObject *args)
859 {
860         char *name;
861         PyObject *globals = NULL;
862         PyObject *locals = NULL;
863         PyObject *fromlist = NULL;
864         PyObject *l, *m, *n;
865
866         if (!PyArg_ParseTuple(args, "s|OOO:m_import",
867                 &name, &globals, &locals, &fromlist))
868             return NULL;
869
870         /* check for builtin modules */
871         m = PyImport_AddModule("sys");
872         l = PyObject_GetAttrString(m, "builtin_module_names");
873         n = PyString_FromString(name);
874         
875         if (PySequence_Contains(l, n)) {
876                 return PyImport_ImportModuleEx(name, globals, locals, fromlist);
877         }
878
879         /* quick hack for GamePython modules 
880                 TODO: register builtin modules properly by ExtendInittab */
881         if (!strcmp(name, "GameLogic") || !strcmp(name, "GameKeys") || !strcmp(name, "PhysicsConstraints") ||
882                 !strcmp(name, "Rasterizer") || !strcmp(name, "Mathutils")) {
883                 return PyImport_ImportModuleEx(name, globals, locals, fromlist);
884         }
885                 
886         PyErr_Format(PyExc_ImportError,
887                  "Import of external Module %.20s not allowed.", name);
888         return NULL;
889
890 }
891
892
893
894 static PyMethodDef meth_open[] = {
895         { "open", KXpy_open, METH_VARARGS,
896                 "(disabled)"}
897 };
898
899
900 static PyMethodDef meth_import[] = {
901         { "import", KXpy_import, METH_VARARGS,
902                 "our own import"}
903 };
904
905
906
907 //static PyObject *g_oldopen = 0;
908 //static PyObject *g_oldimport = 0;
909 //static int g_security = 0;
910
911
912 void setSandbox(TPythonSecurityLevel level)
913 {
914     PyObject *m = PyImport_AddModule("__builtin__");
915     PyObject *d = PyModule_GetDict(m);
916         PyObject *meth = PyCFunction_New(meth_open, NULL);
917
918         switch (level) {
919         case psl_Highest:
920                 //if (!g_security) {
921                         //g_oldopen = PyDict_GetItemString(d, "open");
922                         PyDict_SetItemString(d, "open", meth);
923                         meth = PyCFunction_New(meth_import, NULL);
924                         PyDict_SetItemString(d, "__import__", meth);
925                         //g_security = level;
926                 //}
927                 break;
928         /*
929         case psl_Lowest:
930                 if (g_security) {
931                         PyDict_SetItemString(d, "open", g_oldopen);
932                         PyDict_SetItemString(d, "__import__", g_oldimport);
933                         g_security = level;
934                 }
935         */
936         default:
937                 break;
938         }
939 }
940
941 /**
942  * Python is not initialised.
943  */
944 PyObject* initGamePlayerPythonScripting(const STR_String& progname, TPythonSecurityLevel level)
945 {
946         STR_String pname = progname;
947         Py_SetProgramName(pname.Ptr());
948         Py_NoSiteFlag=1;
949         Py_FrozenFlag=1;
950         Py_Initialize();
951
952         //importBlenderModules()
953         
954         setSandbox(level);
955
956         PyObject* moduleobj = PyImport_AddModule("__main__");
957         return PyModule_GetDict(moduleobj);
958 }
959
960 void exitGamePlayerPythonScripting()
961 {
962         Py_Finalize();
963 }
964
965 /**
966  * Python is already initialized.
967  */
968 PyObject* initGamePythonScripting(const STR_String& progname, TPythonSecurityLevel level)
969 {
970         STR_String pname = progname;
971         Py_SetProgramName(pname.Ptr());
972         Py_NoSiteFlag=1;
973         Py_FrozenFlag=1;
974
975         setSandbox(level);
976
977         PyObject* moduleobj = PyImport_AddModule("__main__");
978         return PyModule_GetDict(moduleobj);
979 }
980
981
982
983 void exitGamePythonScripting()
984 {
985 }
986
987
988
989 PyObject* initRasterizer(RAS_IRasterizer* rasty,RAS_ICanvas* canvas)
990 {
991         gp_Canvas = canvas;
992         gp_Rasterizer = rasty;
993
994
995   PyObject* m;
996   PyObject* d;
997
998   // Create the module and add the functions
999   m = Py_InitModule4("Rasterizer", rasterizer_methods,
1000                      Rasterizer_module_documentation,
1001                      (PyObject*)NULL,PYTHON_API_VERSION);
1002
1003   // Add some symbolic constants to the module
1004   d = PyModule_GetDict(m);
1005   ErrorObject = PyString_FromString("Rasterizer.error");
1006   PyDict_SetItemString(d, "error", ErrorObject);
1007
1008   // XXXX Add constants here
1009
1010   // Check for errors
1011   if (PyErr_Occurred())
1012     {
1013       Py_FatalError("can't initialize module Rasterizer");
1014     }
1015
1016   return d;
1017 }
1018
1019
1020
1021 /* ------------------------------------------------------------------------- */
1022 /* GameKeys: symbolic constants for key mapping                              */
1023 /* ------------------------------------------------------------------------- */
1024
1025 static char GameKeys_module_documentation[] =
1026 "This modules provides defines for key-codes"
1027 ;
1028
1029
1030
1031 static struct PyMethodDef gamekeys_methods[] = {
1032         { NULL, (PyCFunction) NULL, 0, NULL }
1033 };
1034
1035
1036
1037 PyObject* initGameKeys()
1038 {
1039         PyObject* m;
1040         PyObject* d;
1041
1042         // Create the module and add the functions
1043         m = Py_InitModule4("GameKeys", gamekeys_methods,
1044                                            GameKeys_module_documentation,
1045                                            (PyObject*)NULL,PYTHON_API_VERSION);
1046
1047         // Add some symbolic constants to the module
1048         d = PyModule_GetDict(m);
1049
1050         // XXXX Add constants here
1051
1052         KX_MACRO_addTypesToDict(d, AKEY, SCA_IInputDevice::KX_AKEY);
1053         KX_MACRO_addTypesToDict(d, BKEY, SCA_IInputDevice::KX_BKEY);
1054         KX_MACRO_addTypesToDict(d, CKEY, SCA_IInputDevice::KX_CKEY);
1055         KX_MACRO_addTypesToDict(d, DKEY, SCA_IInputDevice::KX_DKEY);
1056         KX_MACRO_addTypesToDict(d, EKEY, SCA_IInputDevice::KX_EKEY);
1057         KX_MACRO_addTypesToDict(d, FKEY, SCA_IInputDevice::KX_FKEY);
1058         KX_MACRO_addTypesToDict(d, GKEY, SCA_IInputDevice::KX_GKEY);
1059         KX_MACRO_addTypesToDict(d, HKEY, SCA_IInputDevice::KX_HKEY);
1060         KX_MACRO_addTypesToDict(d, IKEY, SCA_IInputDevice::KX_IKEY);
1061         KX_MACRO_addTypesToDict(d, JKEY, SCA_IInputDevice::KX_JKEY);
1062         KX_MACRO_addTypesToDict(d, KKEY, SCA_IInputDevice::KX_KKEY);
1063         KX_MACRO_addTypesToDict(d, LKEY, SCA_IInputDevice::KX_LKEY);
1064         KX_MACRO_addTypesToDict(d, MKEY, SCA_IInputDevice::KX_MKEY);
1065         KX_MACRO_addTypesToDict(d, NKEY, SCA_IInputDevice::KX_NKEY);
1066         KX_MACRO_addTypesToDict(d, OKEY, SCA_IInputDevice::KX_OKEY);
1067         KX_MACRO_addTypesToDict(d, PKEY, SCA_IInputDevice::KX_PKEY);
1068         KX_MACRO_addTypesToDict(d, QKEY, SCA_IInputDevice::KX_QKEY);
1069         KX_MACRO_addTypesToDict(d, RKEY, SCA_IInputDevice::KX_RKEY);
1070         KX_MACRO_addTypesToDict(d, SKEY, SCA_IInputDevice::KX_SKEY);
1071         KX_MACRO_addTypesToDict(d, TKEY, SCA_IInputDevice::KX_TKEY);
1072         KX_MACRO_addTypesToDict(d, UKEY, SCA_IInputDevice::KX_UKEY);
1073         KX_MACRO_addTypesToDict(d, VKEY, SCA_IInputDevice::KX_VKEY);
1074         KX_MACRO_addTypesToDict(d, WKEY, SCA_IInputDevice::KX_WKEY);
1075         KX_MACRO_addTypesToDict(d, XKEY, SCA_IInputDevice::KX_XKEY);
1076         KX_MACRO_addTypesToDict(d, YKEY, SCA_IInputDevice::KX_YKEY);
1077         KX_MACRO_addTypesToDict(d, ZKEY, SCA_IInputDevice::KX_ZKEY);
1078         
1079         KX_MACRO_addTypesToDict(d, ZEROKEY, SCA_IInputDevice::KX_ZEROKEY);              
1080         KX_MACRO_addTypesToDict(d, ONEKEY, SCA_IInputDevice::KX_ONEKEY);                
1081         KX_MACRO_addTypesToDict(d, TWOKEY, SCA_IInputDevice::KX_TWOKEY);                
1082         KX_MACRO_addTypesToDict(d, THREEKEY, SCA_IInputDevice::KX_THREEKEY);
1083         KX_MACRO_addTypesToDict(d, FOURKEY, SCA_IInputDevice::KX_FOURKEY);              
1084         KX_MACRO_addTypesToDict(d, FIVEKEY, SCA_IInputDevice::KX_FIVEKEY);              
1085         KX_MACRO_addTypesToDict(d, SIXKEY, SCA_IInputDevice::KX_SIXKEY);                
1086         KX_MACRO_addTypesToDict(d, SEVENKEY, SCA_IInputDevice::KX_SEVENKEY);
1087         KX_MACRO_addTypesToDict(d, EIGHTKEY, SCA_IInputDevice::KX_EIGHTKEY);
1088         KX_MACRO_addTypesToDict(d, NINEKEY, SCA_IInputDevice::KX_NINEKEY);              
1089                 
1090         KX_MACRO_addTypesToDict(d, CAPSLOCKKEY, SCA_IInputDevice::KX_CAPSLOCKKEY);
1091                 
1092         KX_MACRO_addTypesToDict(d, LEFTCTRLKEY, SCA_IInputDevice::KX_LEFTCTRLKEY);      
1093         KX_MACRO_addTypesToDict(d, LEFTALTKEY, SCA_IInputDevice::KX_LEFTALTKEY);                
1094         KX_MACRO_addTypesToDict(d, RIGHTALTKEY, SCA_IInputDevice::KX_RIGHTALTKEY);      
1095         KX_MACRO_addTypesToDict(d, RIGHTCTRLKEY, SCA_IInputDevice::KX_RIGHTCTRLKEY);    
1096         KX_MACRO_addTypesToDict(d, RIGHTSHIFTKEY, SCA_IInputDevice::KX_RIGHTSHIFTKEY);  
1097         KX_MACRO_addTypesToDict(d, LEFTSHIFTKEY, SCA_IInputDevice::KX_LEFTSHIFTKEY);
1098                 
1099         KX_MACRO_addTypesToDict(d, ESCKEY, SCA_IInputDevice::KX_ESCKEY);
1100         KX_MACRO_addTypesToDict(d, TABKEY, SCA_IInputDevice::KX_TABKEY);
1101         KX_MACRO_addTypesToDict(d, RETKEY, SCA_IInputDevice::KX_RETKEY);
1102         KX_MACRO_addTypesToDict(d, SPACEKEY, SCA_IInputDevice::KX_SPACEKEY);
1103         KX_MACRO_addTypesToDict(d, LINEFEEDKEY, SCA_IInputDevice::KX_LINEFEEDKEY);              
1104         KX_MACRO_addTypesToDict(d, BACKSPACEKEY, SCA_IInputDevice::KX_BACKSPACEKEY);
1105         KX_MACRO_addTypesToDict(d, DELKEY, SCA_IInputDevice::KX_DELKEY);
1106         KX_MACRO_addTypesToDict(d, SEMICOLONKEY, SCA_IInputDevice::KX_SEMICOLONKEY);
1107         KX_MACRO_addTypesToDict(d, PERIODKEY, SCA_IInputDevice::KX_PERIODKEY);          
1108         KX_MACRO_addTypesToDict(d, COMMAKEY, SCA_IInputDevice::KX_COMMAKEY);            
1109         KX_MACRO_addTypesToDict(d, QUOTEKEY, SCA_IInputDevice::KX_QUOTEKEY);            
1110         KX_MACRO_addTypesToDict(d, ACCENTGRAVEKEY, SCA_IInputDevice::KX_ACCENTGRAVEKEY);        
1111         KX_MACRO_addTypesToDict(d, MINUSKEY, SCA_IInputDevice::KX_MINUSKEY);            
1112         KX_MACRO_addTypesToDict(d, SLASHKEY, SCA_IInputDevice::KX_SLASHKEY);            
1113         KX_MACRO_addTypesToDict(d, BACKSLASHKEY, SCA_IInputDevice::KX_BACKSLASHKEY);
1114         KX_MACRO_addTypesToDict(d, EQUALKEY, SCA_IInputDevice::KX_EQUALKEY);            
1115         KX_MACRO_addTypesToDict(d, LEFTBRACKETKEY, SCA_IInputDevice::KX_LEFTBRACKETKEY);        
1116         KX_MACRO_addTypesToDict(d, RIGHTBRACKETKEY, SCA_IInputDevice::KX_RIGHTBRACKETKEY);      
1117                 
1118         KX_MACRO_addTypesToDict(d, LEFTARROWKEY, SCA_IInputDevice::KX_LEFTARROWKEY);
1119         KX_MACRO_addTypesToDict(d, DOWNARROWKEY, SCA_IInputDevice::KX_DOWNARROWKEY);
1120         KX_MACRO_addTypesToDict(d, RIGHTARROWKEY, SCA_IInputDevice::KX_RIGHTARROWKEY);  
1121         KX_MACRO_addTypesToDict(d, UPARROWKEY, SCA_IInputDevice::KX_UPARROWKEY);                
1122         
1123         KX_MACRO_addTypesToDict(d, PAD2 , SCA_IInputDevice::KX_PAD2);
1124         KX_MACRO_addTypesToDict(d, PAD4 , SCA_IInputDevice::KX_PAD4);
1125         KX_MACRO_addTypesToDict(d, PAD6 , SCA_IInputDevice::KX_PAD6);
1126         KX_MACRO_addTypesToDict(d, PAD8 , SCA_IInputDevice::KX_PAD8);
1127                 
1128         KX_MACRO_addTypesToDict(d, PAD1 , SCA_IInputDevice::KX_PAD1);
1129         KX_MACRO_addTypesToDict(d, PAD3 , SCA_IInputDevice::KX_PAD3);
1130         KX_MACRO_addTypesToDict(d, PAD5 , SCA_IInputDevice::KX_PAD5);
1131         KX_MACRO_addTypesToDict(d, PAD7 , SCA_IInputDevice::KX_PAD7);
1132         KX_MACRO_addTypesToDict(d, PAD9 , SCA_IInputDevice::KX_PAD9);
1133                 
1134         KX_MACRO_addTypesToDict(d, PADPERIOD, SCA_IInputDevice::KX_PADPERIOD);
1135         KX_MACRO_addTypesToDict(d, PADSLASHKEY, SCA_IInputDevice::KX_PADSLASHKEY);
1136         KX_MACRO_addTypesToDict(d, PADASTERKEY, SCA_IInputDevice::KX_PADASTERKEY);
1137                 
1138                 
1139         KX_MACRO_addTypesToDict(d, PAD0, SCA_IInputDevice::KX_PAD0);
1140         KX_MACRO_addTypesToDict(d, PADMINUS, SCA_IInputDevice::KX_PADMINUS);
1141         KX_MACRO_addTypesToDict(d, PADENTER, SCA_IInputDevice::KX_PADENTER);
1142         KX_MACRO_addTypesToDict(d, PADPLUSKEY, SCA_IInputDevice::KX_PADPLUSKEY);
1143                 
1144                 
1145         KX_MACRO_addTypesToDict(d, F1KEY , SCA_IInputDevice::KX_F1KEY);
1146         KX_MACRO_addTypesToDict(d, F2KEY , SCA_IInputDevice::KX_F2KEY);
1147         KX_MACRO_addTypesToDict(d, F3KEY , SCA_IInputDevice::KX_F3KEY);
1148         KX_MACRO_addTypesToDict(d, F4KEY , SCA_IInputDevice::KX_F4KEY);
1149         KX_MACRO_addTypesToDict(d, F5KEY , SCA_IInputDevice::KX_F5KEY);
1150         KX_MACRO_addTypesToDict(d, F6KEY , SCA_IInputDevice::KX_F6KEY);
1151         KX_MACRO_addTypesToDict(d, F7KEY , SCA_IInputDevice::KX_F7KEY);
1152         KX_MACRO_addTypesToDict(d, F8KEY , SCA_IInputDevice::KX_F8KEY);
1153         KX_MACRO_addTypesToDict(d, F9KEY , SCA_IInputDevice::KX_F9KEY);
1154         KX_MACRO_addTypesToDict(d, F10KEY, SCA_IInputDevice::KX_F10KEY);
1155         KX_MACRO_addTypesToDict(d, F11KEY, SCA_IInputDevice::KX_F11KEY);
1156         KX_MACRO_addTypesToDict(d, F12KEY, SCA_IInputDevice::KX_F12KEY);
1157                 
1158         KX_MACRO_addTypesToDict(d, PAUSEKEY, SCA_IInputDevice::KX_PAUSEKEY);
1159         KX_MACRO_addTypesToDict(d, INSERTKEY, SCA_IInputDevice::KX_INSERTKEY);
1160         KX_MACRO_addTypesToDict(d, HOMEKEY , SCA_IInputDevice::KX_HOMEKEY);
1161         KX_MACRO_addTypesToDict(d, PAGEUPKEY, SCA_IInputDevice::KX_PAGEUPKEY);
1162         KX_MACRO_addTypesToDict(d, PAGEDOWNKEY, SCA_IInputDevice::KX_PAGEDOWNKEY);
1163         KX_MACRO_addTypesToDict(d, ENDKEY, SCA_IInputDevice::KX_ENDKEY);
1164
1165
1166         // Check for errors
1167         if (PyErr_Occurred())
1168     {
1169                 Py_FatalError("can't initialize module GameKeys");
1170     }
1171
1172         return d;
1173 }
1174
1175 PyObject* initMathutils()
1176 {
1177         return Mathutils_Init("Mathutils"); // Use as a top level module in BGE
1178 }
1179
1180 void PHY_SetActiveScene(class KX_Scene* scene)
1181 {
1182         gp_KetsjiScene = scene;
1183 }
1184
1185 class KX_Scene* PHY_GetActiveScene()
1186 {
1187         return gp_KetsjiScene;
1188 }