From 979bec79c373b5bfae0bfe6e74e6e92bf3214ff2 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Sun, 19 Jul 2009 17:45:14 +0000 Subject: - Support for importing python packages. (directories of python scripts containing an __init__.py) - BLI_add_slash returns the new string length. - BLI_where_am_i() would often have /./ in the path (not incorrect but annoying, got into python exceptions) - release/ui/space_image.py, py error referencing invalid keyword args. --- source/blender/python/intern/bpy_interface.c | 61 ++++++++++++++++++++-------- 1 file changed, 43 insertions(+), 18 deletions(-) (limited to 'source/blender/python/intern/bpy_interface.c') diff --git a/source/blender/python/intern/bpy_interface.c b/source/blender/python/intern/bpy_interface.c index 18dae972db4..520856a8d0b 100644 --- a/source/blender/python/intern/bpy_interface.c +++ b/source/blender/python/intern/bpy_interface.c @@ -28,6 +28,7 @@ #include "MEM_guardedalloc.h" #include "BLI_util.h" +#include "BLI_fileops.h" #include "BLI_string.h" #include "BKE_context.h" @@ -441,6 +442,26 @@ int BPY_run_python_script_space(const char *modulename, const char *func) #include "PIL_time.h" #endif +/* for use by BPY_run_ui_scripts only */ +static int bpy_import_module(char *modname, int reload) +{ + PyObject *mod= PyImport_ImportModuleLevel(modname, NULL, NULL, NULL, 0); + if (mod) { + if (reload) { + PyObject *mod_orig= mod; + mod= PyImport_ReloadModule(mod); + Py_DECREF(mod_orig); + } + } + + if(mod) { + Py_DECREF(mod); /* could be NULL from reloading */ + return 0; + } else { + return -1; + } +} + /* XXX this is temporary, need a proper script registration system for 2.5 */ void BPY_run_ui_scripts(bContext *C, int reload) { @@ -453,10 +474,9 @@ void BPY_run_ui_scripts(bContext *C, int reload) char *dirname; char path[FILE_MAX]; char *dirs[] = {"ui", "io", NULL}; - int a; + int a, err; PyGILState_STATE gilstate; - PyObject *mod; PyObject *sys_path; gilstate = PyGILState_Ensure(); @@ -486,27 +506,32 @@ void BPY_run_ui_scripts(bContext *C, int reload) while((de = readdir(dir)) != NULL) { /* We could stat the file but easier just to let python * import it and complain if theres a problem */ + err = 0; - file_extension = strstr(de->d_name, ".py"); - - if(file_extension && file_extension[3] == '\0') { - BLI_strncpy(path, de->d_name, (file_extension - de->d_name) + 1); /* cut off the .py on copy */ - mod= PyImport_ImportModuleLevel(path, NULL, NULL, NULL, 0); - if (mod) { - if (reload) { - PyObject *mod_orig= mod; - mod= PyImport_ReloadModule(mod); - Py_DECREF(mod_orig); - } + if (de->d_name[0] == '.') { + /* do nothing, probably .svn */ + } + else if(de->d_type==DT_DIR) { + /* support packages */ + BLI_join_dirfile(path, dirname, de->d_name); + BLI_join_dirfile(path, path, "__init__.py"); + + if(BLI_exists(path)) { + bpy_import_module(de->d_name, reload); } + } else { + /* normal py files */ + file_extension = strstr(de->d_name, ".py"); - if(mod) { - Py_DECREF(mod); /* could be NULL from reloading */ - } else { - BPy_errors_to_report(NULL); - fprintf(stderr, "unable to import \"%s\" %s/%s\n", path, dirname, de->d_name); + if(file_extension && file_extension[3] == '\0') { + de->d_name[(file_extension - de->d_name) + 1] = '\0'; + bpy_import_module(de->d_name, reload); } + } + if(err==-1) { + BPy_errors_to_report(NULL); + fprintf(stderr, "unable to import %s/%s\n", dirname, de->d_name); } } -- cgit v1.2.3