merge with/from trunk at r35190
[blender.git] / source / gameengine / Ketsji / KX_PythonInit.cpp
index dcedade9bd66ccda605f04ed125276b39b472422..9bb7fd0ecc27b43588290b8f0c27c621826d1549 100644 (file)
@@ -1,4 +1,4 @@
-/**
+/*
  * $Id$
  *
  * ***** BEGIN GPL LICENSE BLOCK *****
  * Initialize Python thingies.
  */
 
+/** \file gameengine/Ketsji/KX_PythonInit.cpp
+ *  \ingroup ketsji
+ */
+
+
 #include "GL/glew.h"
 
-#ifdef WIN32
+#if defined(WIN32) && !defined(FREE_WINDOWS)
 #pragma warning (disable : 4786)
 #endif //WIN32
 
-#ifndef DISABLE_PYTHON
+#ifdef WITH_PYTHON
+
+#ifdef _POSIX_C_SOURCE
+#undef _POSIX_C_SOURCE
+#endif
+
+#ifdef _XOPEN_SOURCE
+#undef _XOPEN_SOURCE
+#endif
+
+#include <Python.h>
 
 extern "C" {
        #include "bpy_internal_import.h"  /* from the blender python api, but we want to import text too! */
+       #include "py_capi_utils.h"
        #include "mathutils.h" // Blender.Mathutils module copied here so the blenderlayer can use.
-       #include "geometry.h" // Blender.Geometry module copied here so the blenderlayer can use.
        #include "bgl.h"
-       #include "blf_api.h"
+       #include "blf_py_api.h"
 
        #include "marshal.h" /* python header for loading/saving dicts */
 }
 
-#define WITH_PYTHON
 #include "AUD_PyInit.h"
 
 #endif
@@ -129,18 +143,21 @@ extern "C" {
 
 // 'local' copy of canvas ptr, for window height/width python scripts
 
-#ifndef DISABLE_PYTHON
+#ifdef WITH_PYTHON
 
 static RAS_ICanvas* gp_Canvas = NULL;
 static char gp_GamePythonPath[FILE_MAXDIR + FILE_MAXFILE] = "";
 static char gp_GamePythonPathOrig[FILE_MAXDIR + FILE_MAXFILE] = ""; // not super happy about this, but we need to remember the first loaded file for the global/dict load save
 
-#endif // DISABLE_PYTHON
+static SCA_PythonKeyboard* gp_PythonKeyboard = NULL;
+static SCA_PythonMouse* gp_PythonMouse = NULL;
+#endif // WITH_PYTHON
 
 static KX_Scene*       gp_KetsjiScene = NULL;
 static KX_KetsjiEngine*        gp_KetsjiEngine = NULL;
 static RAS_IRasterizer* gp_Rasterizer = NULL;
 
+
 void KX_SetActiveScene(class KX_Scene* scene)
 {
        gp_KetsjiScene = scene;
@@ -163,7 +180,7 @@ void        KX_RasterizerDrawDebugLine(const MT_Vector3& from,const MT_Vector3& to,cons
                gp_Rasterizer->DrawDebugLine(from,to,color);
 }
 
-#ifndef DISABLE_PYTHON
+#ifdef WITH_PYTHON
 
 static PyObject *gp_OrigPythonSysPath= NULL;
 static PyObject *gp_OrigPythonSysModules= NULL;
@@ -220,7 +237,7 @@ static PyObject* gPyExpandPath(PyObject*, PyObject* args)
 
        BLI_strncpy(expanded, filename, FILE_MAXDIR + FILE_MAXFILE);
        BLI_path_abs(expanded, gp_GamePythonPath);
-       return PyUnicode_FromString(expanded);
+       return PyUnicode_DecodeFSDefault(expanded);
 }
 
 static char gPyStartGame_doc[] =
@@ -486,7 +503,7 @@ static PyObject* gPyGetBlendFileList(PyObject*, PyObject* args)
        
     while ((dirp = readdir(dp)) != NULL) {
                if (BLI_testextensie(dirp->d_name, ".blend")) {
-                       value = PyUnicode_FromString(dirp->d_name);
+                       value= PyUnicode_DecodeFSDefault(dirp->d_name);
                        PyList_Append(list, value);
                        Py_DECREF(value);
                }
@@ -911,6 +928,11 @@ static PyObject* gPySetBackgroundColor(PyObject*, PyObject* value)
        {
                gp_Rasterizer->SetBackColor(vec[0], vec[1], vec[2], vec[3]);
        }
+
+       KX_WorldInfo *wi = gp_KetsjiScene->GetWorldInfo();
+       if (wi->hasWorld())
+               wi->setBackColor(vec[0], vec[1], vec[2]);
+
        Py_RETURN_NONE;
 }
 
@@ -1295,11 +1317,13 @@ PyObject* initGameLogic(KX_KetsjiEngine *engine, KX_Scene* scene) // quick hack
        PyDict_SetItemString(d, "globalDict", item=PyDict_New()); Py_DECREF(item);
 
        // Add keyboard and mouse attributes to this module
-       SCA_PythonKeyboard* pykeyb = new SCA_PythonKeyboard(gp_KetsjiEngine->GetKeyboardDevice());
-       PyDict_SetItemString(d, "keyboard", pykeyb->NewProxy(true));
+       MT_assert(!gp_PythonKeyboard);
+       gp_PythonKeyboard = new SCA_PythonKeyboard(gp_KetsjiEngine->GetKeyboardDevice());
+       PyDict_SetItemString(d, "keyboard", gp_PythonKeyboard->NewProxy(true));
 
-       SCA_PythonMouse* pymouse = new SCA_PythonMouse(gp_KetsjiEngine->GetMouseDevice(), gp_Canvas);
-       PyDict_SetItemString(d, "mouse", pymouse->NewProxy(true));
+       MT_assert(!gp_PythonMouse);
+       gp_PythonMouse = new SCA_PythonMouse(gp_KetsjiEngine->GetMouseDevice(), gp_Canvas);
+       PyDict_SetItemString(d, "mouse", gp_PythonMouse->NewProxy(true));
 
        ErrorObject = PyUnicode_FromString("GameLogic.error");
        PyDict_SetItemString(d, "error", ErrorObject);
@@ -1379,6 +1403,7 @@ PyObject* initGameLogic(KX_KetsjiEngine *engine, KX_Scene* scene) // quick hack
 
        /* 7. Action actuator                                                                                                      */
        KX_MACRO_addTypesToDict(d, KX_ACTIONACT_PLAY,        ACT_ACTION_PLAY);
+       KX_MACRO_addTypesToDict(d, KX_ACTIONACT_PINGPONG,    ACT_ACTION_PINGPONG);
        KX_MACRO_addTypesToDict(d, KX_ACTIONACT_FLIPPER,     ACT_ACTION_FLIPPER);
        KX_MACRO_addTypesToDict(d, KX_ACTIONACT_LOOPSTOP,    ACT_ACTION_LOOP_STOP);
        KX_MACRO_addTypesToDict(d, KX_ACTIONACT_LOOPEND,     ACT_ACTION_LOOP_END);
@@ -1759,8 +1784,8 @@ static void setSandbox(TPythonSecurityLevel level)
        */
        default:
                        /* Allow importing internal text, from bpy_internal_import.py */
-                       PyDict_SetItemString(d, "reload", item=PyCFunction_New(bpy_reload_meth, NULL));         Py_DECREF(item);
-                       PyDict_SetItemString(d, "__import__", item=PyCFunction_New(bpy_import_meth, NULL));     Py_DECREF(item);
+                       PyDict_SetItemString(d, "reload", item=PyCFunction_New(&bpy_reload_meth, NULL));                Py_DECREF(item);
+                       PyDict_SetItemString(d, "__import__", item=PyCFunction_New(&bpy_import_meth, NULL));    Py_DECREF(item);
                break;
        }
 }
@@ -1812,7 +1837,7 @@ static void initPySysObjects__append(PyObject *sys_path, char *filename)
        BLI_split_dirfile(filename, expanded, NULL); /* get the dir part of filename only */
        BLI_path_abs(expanded, gp_GamePythonPath); /* filename from lib->filename is (always?) absolute, so this may not be needed but it wont hurt */
        BLI_cleanup_file(gp_GamePythonPath, expanded); /* Dont use BLI_cleanup_dir because it adds a slash - BREAKS WIN32 ONLY */
-       item= PyUnicode_FromString(expanded);
+       item= PyUnicode_DecodeFSDefault(expanded);
        
 //     printf("SysPath - '%s', '%s', '%s'\n", expanded, filename, gp_GamePythonPath);
        
@@ -1903,7 +1928,7 @@ PyObject* initGamePlayerPythonScripting(const STR_String& progname, TPythonSecur
                PyObject *py_argv= PyList_New(argc);
 
                for (i=0; i<argc; i++)
-                       PyList_SET_ITEM(py_argv, i, PyUnicode_FromString(argv[i]));
+                       PyList_SET_ITEM(py_argv, i, PyC_UnicodeFromByte(argv[i]));
 
                PySys_SetObject("argv", py_argv);
                Py_DECREF(py_argv);
@@ -1920,11 +1945,18 @@ PyObject* initGamePlayerPythonScripting(const STR_String& progname, TPythonSecur
        
        PyObjectPlus::ClearDeprecationWarning();
 
-       return bpy_namespace_dict_new(NULL);
+       return PyC_DefaultNameSpace(NULL);
 }
 
 void exitGamePlayerPythonScripting()
 {      
+       /* Clean up the Python mouse and keyboard */
+       delete gp_PythonKeyboard;
+       gp_PythonKeyboard = NULL;
+
+       delete gp_PythonMouse;
+       gp_PythonMouse = NULL;
+
        /* since python restarts we cant let the python backup of the sys.path hang around in a global pointer */
        restorePySysObjects(); /* get back the original sys.path and clear the backup */
        
@@ -1956,11 +1988,18 @@ PyObject* initGamePythonScripting(const STR_String& progname, TPythonSecurityLev
 
        PyObjectPlus::NullDeprecationWarning();
 
-       return bpy_namespace_dict_new(NULL);
+       return PyC_DefaultNameSpace(NULL);
 }
 
 void exitGamePythonScripting()
 {
+       /* Clean up the Python mouse and keyboard */
+       delete gp_PythonKeyboard;
+       gp_PythonKeyboard = NULL;
+
+       delete gp_PythonMouse;
+       gp_PythonMouse = NULL;
+
        restorePySysObjects(); /* get back the original sys.path and clear the backup */
        bpy_import_main_set(NULL);
        PyObjectPlus::ClearDeprecationWarning();
@@ -1997,7 +2036,11 @@ void setupGamePython(KX_KetsjiEngine* ketsjiengine, KX_Scene* startscene, Main *
        initVideoTexture();
 
        /* could be done a lot more nicely, but for now a quick way to get bge.* working */
+<<<<<<< .working
        PyRun_SimpleString("sys = __import__('sys');mod = sys.modules['bge'] = type(sys)('bge');mod.__dict__.update({'logic':__import__('GameLogic'), 'render':__import__('Rasterizer'), 'events':__import__('GameKeys'), 'constraints':__import__('PhysicsConstraints'), 'types':__import__('GameTypes')});");
+=======
+       PyRun_SimpleString("sys = __import__('sys');mod = sys.modules['bge'] = type(sys)('bge');mod.__dict__.update({'logic':__import__('GameLogic'), 'render':__import__('Rasterizer'), 'events':__import__('GameKeys'), 'constraints':__import__('PhysicsConstraints'), 'types':__import__('GameTypes'), 'texture':__import__('VideoTexture')});");
+>>>>>>> .merge-right.r35190
 }
 
 static struct PyModuleDef Rasterizer_module_def = {
@@ -2213,6 +2256,7 @@ PyObject* initGameKeys()
        KX_MACRO_addTypesToDict(d, ESCKEY, SCA_IInputDevice::KX_ESCKEY);
        KX_MACRO_addTypesToDict(d, TABKEY, SCA_IInputDevice::KX_TABKEY);
        KX_MACRO_addTypesToDict(d, RETKEY, SCA_IInputDevice::KX_RETKEY);
+       KX_MACRO_addTypesToDict(d, ENTERKEY, SCA_IInputDevice::KX_RETKEY);
        KX_MACRO_addTypesToDict(d, SPACEKEY, SCA_IInputDevice::KX_SPACEKEY);
        KX_MACRO_addTypesToDict(d, LINEFEEDKEY, SCA_IInputDevice::KX_LINEFEEDKEY);              
        KX_MACRO_addTypesToDict(d, BACKSPACEKEY, SCA_IInputDevice::KX_BACKSPACEKEY);
@@ -2303,22 +2347,22 @@ PyObject* initGameKeys()
 
 PyObject* initMathutils()
 {
-       return Mathutils_Init();
+       return BPyInit_mathutils();
 }
 
 PyObject* initGeometry()
 {
-       return Geometry_Init();
+       return BPyInit_mathutils_geometry();
 }
 
 PyObject* initBGL()
 {
-       return BGL_Init();
+       return BPyInit_bgl();
 }
 
 PyObject* initBLF()
 {
-       return BLF_Init();
+       return BPyInit_blf();
 }
 
 // utility function for loading and saving the globalDict
@@ -2422,4 +2466,4 @@ void resetGamePythonPath()
        gp_GamePythonPathOrig[0] = '\0';
 }
 
-#endif // DISABLE_PYTHON
+#endif // WITH_PYTHON