[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [13780] trunk/blender/source/blender: Python Bugfix,
Campbell Barton
ideasman42 at gmail.com
Wed Feb 20 14:42:17 CET 2008
Revision: 13780
http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=13780
Author: campbellbarton
Date: 2008-02-20 14:42:15 +0100 (Wed, 20 Feb 2008)
Log Message:
-----------
Python Bugfix,
Setting the user preference for python scripts didnt add the bpymodules subdirectory to sys.path (python module search path).
Also problems with entering and exiting- the old path was used until next restart.
Modified Paths:
--------------
trunk/blender/source/blender/python/BPY_extern.h
trunk/blender/source/blender/python/BPY_interface.c
trunk/blender/source/blender/src/header_script.c
trunk/blender/source/blender/src/headerbuttons.c
trunk/blender/source/blender/src/space.c
Modified: trunk/blender/source/blender/python/BPY_extern.h
===================================================================
--- trunk/blender/source/blender/python/BPY_extern.h 2008-02-20 11:07:14 UTC (rev 13779)
+++ trunk/blender/source/blender/python/BPY_extern.h 2008-02-20 13:42:15 UTC (rev 13780)
@@ -85,6 +85,7 @@
void BPY_post_start_python( void );
void init_syspath( int first_time );
void syspath_append( char *dir );
+ void BPY_rebuild_syspath( void );
int BPY_Err_getLinenumber( void );
const char *BPY_Err_getFilename( void );
Modified: trunk/blender/source/blender/python/BPY_interface.c
===================================================================
--- trunk/blender/source/blender/python/BPY_interface.c 2008-02-20 11:07:14 UTC (rev 13779)
+++ trunk/blender/source/blender/python/BPY_interface.c 2008-02-20 13:42:15 UTC (rev 13780)
@@ -95,6 +95,7 @@
/* for pydrivers (ipo drivers defined by one-line Python expressions) */
PyObject *bpy_pydriver_Dict = NULL;
+PyObject *bpy_orig_syspath_List = NULL;
/*
* set up a weakref list for Armatures
@@ -255,6 +256,11 @@
Py_DECREF( bpy_pydriver_Dict );
bpy_pydriver_Dict = NULL;
}
+
+ if( bpy_orig_syspath_List ) {
+ Py_DECREF( bpy_orig_syspath_List );
+ bpy_orig_syspath_List = NULL;
+ }
/* Freeing all scripts here prevents problems with the order in which
* Python is finalized and G.main is freed in exit_usiblender() */
@@ -293,13 +299,14 @@
/* cant get the sys module */
ok = 0;
}
-
- if (ok && PyList_Append( path, dir ) != 0)
- ok = 0; /* append failed */
-
- if( (ok==0) || PyErr_Occurred( ) )
- Py_FatalError( "could import or build sys.path, can't continue" );
-
+
+ if (PySequence_Contains(path, dir)==0) { /* Only add if we need to */
+ if (ok && PyList_Append( path, dir ) != 0)
+ ok = 0; /* append failed */
+
+ if( (ok==0) || PyErr_Occurred( ) )
+ Py_FatalError( "could import or build sys.path, can't continue" );
+ }
Py_XDECREF( mod_sys );
}
@@ -311,7 +318,8 @@
char execdir[FILE_MAXDIR]; /*defines from DNA_space_types.h */
int n;
-
+
+
path = Py_BuildValue( "s", bprogname );
mod = PyImport_ImportModule( "Blender.sys" );
@@ -333,9 +341,10 @@
execdir[n] = '\0';
syspath_append( execdir ); /* append to module search path */
- } else
+ } else {
printf( "Warning: could not determine argv[0] path\n" );
-
+ }
+
/*
attempt to import 'site' module as a check for valid
python install found.
@@ -366,28 +375,48 @@
if( mod ) {
d = PyModule_GetDict( mod ); /* borrowed ref */
- EXPP_dict_set_item_str( d, "executable",
- Py_BuildValue( "s", bprogname ) );
+ EXPP_dict_set_item_str( d, "executable", Py_BuildValue( "s", bprogname ) );
+
+ if (first_time) {
+ /* backup the original sys.path to rebuild later */
+ PyObject *syspath = PyDict_GetItemString( d, "path" ); /* borrowed ref */
+ if (bpy_orig_syspath_List) { /* This should never happen but just incase, be nice */
+ Py_DECREF(bpy_orig_syspath_List);
+ }
+ bpy_orig_syspath_List = PyList_GetSlice(syspath, 0, PyList_Size(syspath));
+ }
+
Py_DECREF( mod );
} else{
printf("import of sys module failed\n");
}
}
-/****************************************************************************
-* Description: This function finishes Python initialization in Blender.
-Because U.pythondir (user defined dir for scripts) isn't
-initialized when BPY_start_Python needs to be executed, we
-postpone adding U.pythondir to sys.path and also BPyMenus
-(mechanism to register scripts in Blender menus) for when
-that dir info is available.
-****************************************************************************/
-void BPY_post_start_python( void )
+void BPY_rebuild_syspath( void )
{
+ PyObject *mod, *dict, *syspath;
char dirpath[FILE_MAX];
char *sdir = NULL;
-
+
+ mod = PyImport_ImportModule( "sys" );
+ if (!mod) {
+ printf("error: could not import python sys module. some modules may not import.");
+ return;
+ }
+
+ if (!bpy_orig_syspath_List) { /* should never happen */
+ printf("error refershing python path");
+ Py_DECREF(mod);
+ return;
+ }
+
+ dict = PyModule_GetDict( mod ); /* borrowed ref */
+
+ /* Reset sys.path */
+ syspath = PyDict_GetItemString( dict, "path" ); /* borrowed ref */
+ PyList_SetSlice(syspath, 0, PyList_Size(syspath), bpy_orig_syspath_List);
+
if(U.pythondir[0] != '\0' ) {
char modpath[FILE_MAX];
int upyslen = strlen(U.pythondir);
@@ -396,10 +425,7 @@
* (for eventual implementations of c library's stat function that might
* not like it) */
if (upyslen > 2) { /* avoids doing anything if dir == '//' */
- char ending = U.pythondir[upyslen - 1];
-
- if (ending == '/' || ending == '\\')
- U.pythondir[upyslen - 1] = '\0';
+ BLI_add_slash(U.pythondir);
}
BLI_strncpy(dirpath, U.pythondir, FILE_MAX);
@@ -409,19 +435,30 @@
BLI_make_file_string("/", modpath, dirpath, "bpymodules");
if (BLI_exists(modpath)) syspath_append(modpath);
}
-
+
sdir = bpy_gethome(1);
if (sdir) {
-
syspath_append(sdir);
-
BLI_make_file_string("/", dirpath, sdir, "bpymodules");
if (BLI_exists(dirpath)) syspath_append(dirpath);
}
+
+ Py_DECREF(mod);
+}
+/****************************************************************************
+* Description: This function finishes Python initialization in Blender.
+
+Because U.pythondir (user defined dir for scripts) isn't
+initialized when BPY_start_Python needs to be executed, we
+postpone adding U.pythondir to sys.path and also BPyMenus
+(mechanism to register scripts in Blender menus) for when
+that dir info is available.
+****************************************************************************/
+void BPY_post_start_python( void )
+{
+ BPY_rebuild_syspath();
BPyMenu_Init( 0 ); /* get dynamic menus (registered scripts) data */
-
- return;
}
/****************************************************************************
Modified: trunk/blender/source/blender/src/header_script.c
===================================================================
--- trunk/blender/source/blender/src/header_script.c 2008-02-20 11:07:14 UTC (rev 13779)
+++ trunk/blender/source/blender/src/header_script.c 2008-02-20 13:42:15 UTC (rev 13780)
@@ -117,6 +117,7 @@
switch(event) {
case 0: /* update menus */
BPyMenu_RemoveAllEntries();
+ BPY_rebuild_syspath();
if (BPyMenu_Init(1) == -1) error("Invalid scripts dir: check console");
break;
}
Modified: trunk/blender/source/blender/src/headerbuttons.c
===================================================================
--- trunk/blender/source/blender/src/headerbuttons.c 2008-02-20 11:07:14 UTC (rev 13779)
+++ trunk/blender/source/blender/src/headerbuttons.c 2008-02-20 13:42:15 UTC (rev 13780)
@@ -1496,6 +1496,7 @@
case B_PYMENUEVAL: /* is button from space.c *info* */
waitcursor( 1 ); /* can take some time */
BPyMenu_RemoveAllEntries(); /* free old data */
+ BPY_rebuild_syspath();
if (BPyMenu_Init(1) == -1) { /* re-eval scripts registration in menus */
waitcursor( 0 );
error("Invalid scripts dir: check console");
Modified: trunk/blender/source/blender/src/space.c
===================================================================
--- trunk/blender/source/blender/src/space.c 2008-02-20 11:07:14 UTC (rev 13779)
+++ trunk/blender/source/blender/src/space.c 2008-02-20 13:42:15 UTC (rev 13780)
@@ -4214,12 +4214,12 @@
uiBlockEndAlign(block);
uiBlockBeginAlign(block);
- uiDefBut(block, TEX, 0, "Python: ",
+ uiDefBut(block, TEX, B_PYMENUEVAL, "Python Scripts: ",
(xpos+edgsp+lpref+midsp),y1,(lpref-2*smfileselbut),buth,
- U.pythondir, 1.0, 63.0, 0, 0, "The default directory to search for Python scripts");
+ U.pythondir, 1.0, 63.0, 0, 0, "The default directory to search for Python scripts (resets python module search path: sys.path)");
uiDefIconBut(block, BUT, B_PYMENUEVAL, ICON_SCRIPT,
(xpos+edgsp+(2*lpref)+midsp-2*smfileselbut),y1,smfileselbut,buth,
- 0, 0, 0, 0, 0, "Re-evaluate scripts registration in menus");
+ 0, 0, 0, 0, 0, "Re-evaluate scripts registration in menus (resets python module search path: sys.path)");
uiDefIconBut(block, BUT, B_PYTHONDIRFILESEL, ICON_FILESEL,
(xpos+edgsp+(2*lpref)+midsp-smfileselbut),y1,smfileselbut,buth,
0, 0, 0, 0, 0, "Select the default Python script location");
More information about the Bf-blender-cvs
mailing list