Revision: 31056
          
http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=31056
Author:   campbellbarton
Date:     2010-08-05 05:25:45 +0200 (Thu, 05 Aug 2010)

Log Message:
-----------
bugfix [#23148] "ImportError: __import__ not found" on changing Render FPS

The BGE was getting the namespace dict directly from __main__ which conflicts
with my recent fix to get the pickle module working which to overwrote the 
__main__ module on script execution.

Simple fix is to have the BGE and Blender use the same method of getting 
namespaces.
Renamed CreateGlobalDictionary() to bpy_namespace_dict_new() and moved into 
bpy_internal_import.c

pickle still wont work in the BGE since we make a copy of __main__ namespace 
but for speed would rather not have to replace the __main__ module many times 
per second.

Modified Paths:
--------------
    trunk/blender/source/blender/python/generic/bpy_internal_import.c
    trunk/blender/source/blender/python/generic/bpy_internal_import.h
    trunk/blender/source/blender/python/intern/bpy_interface.c
    trunk/blender/source/gameengine/Converter/KX_ConvertControllers.cpp
    trunk/blender/source/gameengine/GameLogic/SCA_PythonController.cpp
    trunk/blender/source/gameengine/Ketsji/KX_KetsjiEngine.h
    trunk/blender/source/gameengine/Ketsji/KX_PythonInit.cpp

Modified: trunk/blender/source/blender/python/generic/bpy_internal_import.c
===================================================================
--- trunk/blender/source/blender/python/generic/bpy_internal_import.c   
2010-08-05 01:46:34 UTC (rev 31055)
+++ trunk/blender/source/blender/python/generic/bpy_internal_import.c   
2010-08-05 03:25:45 UTC (rev 31056)
@@ -350,3 +350,26 @@
        Py_DECREF(list); /* removes all references from append */
 }
 #endif
+
+
+/*****************************************************************************
+* Description: This function creates a new Python dictionary object.
+* note: dict is owned by sys.modules["__main__"] module, reference is borrowed
+* note: important we use the dict from __main__, this is what python expects
+  for 'pickle' to work as well as strings like this...
+ >> foo = 10
+ >> print(__import__("__main__").foo)
+*****************************************************************************/
+PyObject *bpy_namespace_dict_new(const char *filename)
+{
+       PyInterpreterState *interp= PyThreadState_GET()->interp;
+       PyObject *mod_main= PyModule_New("__main__");   
+       PyDict_SetItemString(interp->modules, "__main__", mod_main);
+       Py_DECREF(mod_main); /* sys.modules owns now */
+       PyModule_AddStringConstant(mod_main, "__name__", "__main__");
+       if(filename)
+               PyModule_AddStringConstant(mod_main, "__file__", filename); /* 
__file__ only for nice UI'ness */
+       PyModule_AddObject(mod_main, "__builtins__", interp->builtins);
+       Py_INCREF(interp->builtins); /* AddObject steals a reference */
+       return PyModule_GetDict(mod_main);
+}

Modified: trunk/blender/source/blender/python/generic/bpy_internal_import.h
===================================================================
--- trunk/blender/source/blender/python/generic/bpy_internal_import.h   
2010-08-05 01:46:34 UTC (rev 31055)
+++ trunk/blender/source/blender/python/generic/bpy_internal_import.h   
2010-08-05 03:25:45 UTC (rev 31056)
@@ -60,5 +60,7 @@
 struct Main *bpy_import_main_get(void);
 void bpy_import_main_set(struct Main *maggie);
 
+/* name namespace function for bpy & bge */
+PyObject *bpy_namespace_dict_new(const char *filename);
 
 #endif                         /* EXPP_bpy_import_h */

Modified: trunk/blender/source/blender/python/intern/bpy_interface.c
===================================================================
--- trunk/blender/source/blender/python/intern/bpy_interface.c  2010-08-05 
01:46:34 UTC (rev 31055)
+++ trunk/blender/source/blender/python/intern/bpy_interface.c  2010-08-05 
03:25:45 UTC (rev 31056)
@@ -146,27 +146,6 @@
        bpy_context_module->ptr.data= (void *)BPy_GetContext();
 }
 
-/*****************************************************************************
-* Description: This function creates a new Python dictionary object.
-* note: dict is owned by sys.modules["__main__"] module, reference is borrowed
-* note: important we use the dict from __main__, this is what python expects
-  for 'pickle' to work as well as strings like this...
- >> foo = 10
- >> print(__import__("__main__").foo)
-*****************************************************************************/
-static PyObject *CreateGlobalDictionary(bContext *C, const char *filename)
-{
-       PyInterpreterState *interp= PyThreadState_GET()->interp;
-       PyObject *mod_main= PyModule_New("__main__");   
-       PyDict_SetItemString(interp->modules, "__main__", mod_main);
-       Py_DECREF(mod_main); /* sys.modules owns now */
-       PyModule_AddStringConstant(mod_main, "__name__", "__main__");
-       PyModule_AddStringConstant(mod_main, "__file__", filename); /* __file__ 
only for nice UI'ness */
-       PyModule_AddObject(mod_main, "__builtins__", interp->builtins);
-       Py_INCREF(interp->builtins); /* AddObject steals a reference */
-       return PyModule_GetDict(mod_main);
-}
-
 /* must be called before Py_Initialize */
 void BPY_start_python_path(void)
 {
@@ -334,7 +313,7 @@
        if (text) {
                char fn_dummy[FILE_MAXDIR];
                bpy_text_filename_get(fn_dummy, text);
-               py_dict = CreateGlobalDictionary(C, fn_dummy);
+               py_dict = bpy_namespace_dict_new(fn_dummy);
                
                if( !text->compiled ) { /* if it wasn't already compiled, do it 
now */
                        char *buf = txt_to_buf( text );
@@ -355,7 +334,7 @@
        else {
                FILE *fp= fopen(fn, "r");
 
-               py_dict = CreateGlobalDictionary(C, fn);
+               py_dict = bpy_namespace_dict_new(fn);
 
                if(fp) {
 #ifdef _WIN32
@@ -499,7 +478,7 @@
        
        gilstate = PyGILState_Ensure();
        
-       py_dict = CreateGlobalDictionary(C);
+       py_dict = bpy_namespace_dict_new("<dummy>");
        
        PyObject *module = PyImport_ImportModule(scpt->script.filename);
        if (module==NULL) {
@@ -551,7 +530,7 @@
 
        bpy_context_set(C, &gilstate);
        
-       py_dict= CreateGlobalDictionary(C, "<blender button>");
+       py_dict= bpy_namespace_dict_new("<blender button>");
 
        mod = PyImport_ImportModule("math");
        if (mod) {
@@ -622,7 +601,7 @@
 
        bpy_context_set(C, &gilstate);
 
-       py_dict= CreateGlobalDictionary(C, "<blender string>");
+       py_dict= bpy_namespace_dict_new("<blender string>");
 
        retval = PyRun_String(expr, Py_eval_input, py_dict, py_dict);
 

Modified: trunk/blender/source/gameengine/Converter/KX_ConvertControllers.cpp
===================================================================
--- trunk/blender/source/gameengine/Converter/KX_ConvertControllers.cpp 
2010-08-05 01:46:34 UTC (rev 31055)
+++ trunk/blender/source/gameengine/Converter/KX_ConvertControllers.cpp 
2010-08-05 03:25:45 UTC (rev 31056)
@@ -176,11 +176,11 @@
                                        /* let the controller print any 
warnings here when importing */
                                        
pyctrl->SetScriptText(STR_String(pycont->module)); 
                                        pyctrl->SetScriptName(pycont->module); 
/* will be something like module.func so using it as the name is OK */
-                               }
 
-                               if(pycont->flag & CONT_PY_DEBUG) {
-                                       printf("\nDebuging \"%s\", module for 
object %s\n\texpect worse performance.\n", pycont->module, 
blenderobject->id.name+2);
-                                       pyctrl->SetDebug(true);
+                                       if(pycont->flag & CONT_PY_DEBUG) {
+                                               printf("\nDebuging \"%s\", 
module for object %s\n\texpect worse performance.\n", pycont->module, 
blenderobject->id.name+2);
+                                               pyctrl->SetDebug(true);
+                                       }
                                }
                                
 #endif // DISABLE_PYTHON

Modified: trunk/blender/source/gameengine/GameLogic/SCA_PythonController.cpp
===================================================================
--- trunk/blender/source/gameengine/GameLogic/SCA_PythonController.cpp  
2010-08-05 01:46:34 UTC (rev 31055)
+++ trunk/blender/source/gameengine/GameLogic/SCA_PythonController.cpp  
2010-08-05 03:25:45 UTC (rev 31056)
@@ -305,7 +305,7 @@
        char *function_string;
 
        function_string= strrchr(mod_path, '.');
-       
+
        if(function_string == NULL) {
                printf("Python module name formatting error \"%s\":\n\texpected 
\"SomeModule.Func\", got \"%s\"\n", GetName().Ptr(), m_scriptText.Ptr());
                return false;
@@ -316,11 +316,17 @@
 
        // Import the module and print an error if it's not found
        PyObject *mod = PyImport_ImportModule(mod_path);
-       if(mod && m_debug)
+
+       if (mod == NULL) {
+               ErrorPrint("Python module can't be imported");
+               return false;
+       }
+
+       if(m_debug)
                mod = PyImport_ReloadModule(mod);
 
        if (mod == NULL) {
-               ErrorPrint("Python module not found");
+               ErrorPrint("Python module can't be reloaded");
                return false;
        }
 

Modified: trunk/blender/source/gameengine/Ketsji/KX_KetsjiEngine.h
===================================================================
--- trunk/blender/source/gameengine/Ketsji/KX_KetsjiEngine.h    2010-08-05 
01:46:34 UTC (rev 31055)
+++ trunk/blender/source/gameengine/Ketsji/KX_KetsjiEngine.h    2010-08-05 
03:25:45 UTC (rev 31056)
@@ -71,6 +71,7 @@
        class KX_ISceneConverter*                       m_sceneconverter;
        class NG_NetworkDeviceInterface*                m_networkdevice;
 #ifndef DISABLE_PYTHON
+       /* borrowed from sys.modules["__main__"], dont manage ref's */
        PyObject*                                       m_pythondictionary;
 #endif
        class SCA_IInputDevice*                         m_keyboarddevice;

Modified: trunk/blender/source/gameengine/Ketsji/KX_PythonInit.cpp
===================================================================
--- trunk/blender/source/gameengine/Ketsji/KX_PythonInit.cpp    2010-08-05 
01:46:34 UTC (rev 31055)
+++ trunk/blender/source/gameengine/Ketsji/KX_PythonInit.cpp    2010-08-05 
03:25:45 UTC (rev 31056)
@@ -1912,9 +1912,8 @@
        first_time = false;
        
        PyObjectPlus::ClearDeprecationWarning();
-       
-       PyObject* moduleobj = PyImport_AddModule("__main__");
-       return PyModule_GetDict(moduleobj);
+
+       return bpy_namespace_dict_new(NULL);
 }
 
 void exitGamePlayerPythonScripting()
@@ -1949,9 +1948,8 @@
        initPySysObjects(maggie);
 
        PyObjectPlus::NullDeprecationWarning();
-       
-       PyObject* moduleobj = PyImport_AddModule("__main__");
-       return PyModule_GetDict(moduleobj);
+
+       return bpy_namespace_dict_new(NULL);
 }
 
 void exitGamePythonScripting()


_______________________________________________
Bf-blender-cvs mailing list
Bf-blender-cvs@blender.org
http://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to