diff options
author | Campbell Barton <ideasman42@gmail.com> | 2009-11-13 12:28:05 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2009-11-13 12:28:05 +0300 |
commit | 4c7dc3e5c5423ec20ca69841e49718fbae02a22d (patch) | |
tree | 0b3cb4e7d642c40d2ce5cc8f5cf941cc08d4af44 /source/blender/python | |
parent | d0dff582a88da396234fc913f8ed35f275d6c34d (diff) |
changes python initialization
- bpy is now a python package, this makes it easier to add utility modules and adjust python startup which was previously using verbose Py/C api. Access should not be any slower since both C and Python modules use dictionary access.
- loop over scripts and load via python (currently F8 reload isnt working, will add back shortly)
- the C module is kept but renamed to _bpy and not meant for direct access from anything but the bpy package.
- bpy_types.py is an exception since it runs before the bpy package is initialized.
Diffstat (limited to 'source/blender/python')
-rw-r--r-- | source/blender/python/BPY_extern.h | 4 | ||||
-rw-r--r-- | source/blender/python/intern/bpy_interface.c | 131 | ||||
-rw-r--r-- | source/blender/python/intern/stubs.c | 1 |
3 files changed, 13 insertions, 123 deletions
diff --git a/source/blender/python/BPY_extern.h b/source/blender/python/BPY_extern.h index e73dec097a3..580d39b1e3f 100644 --- a/source/blender/python/BPY_extern.h +++ b/source/blender/python/BPY_extern.h @@ -84,7 +84,6 @@ extern "C" { void BPY_start_python( int argc, char **argv ); void BPY_end_python( void ); - void BPY_post_start_python( void ); void init_syspath( int first_time ); void syspath_append( char *dir ); void BPY_rebuild_syspath( void ); @@ -101,7 +100,6 @@ extern "C" { /* 2.5 UI Scripts */ int BPY_run_python_script( struct bContext *C, const char *filename, struct Text *text, struct ReportList *reports ); // 2.5 working int BPY_run_script_space_draw(const struct bContext *C, struct SpaceScript * sc); // 2.5 working - void BPY_run_ui_scripts(struct bContext *C, int reload); // int BPY_run_script_space_listener(struct bContext *C, struct SpaceScript * sc, struct ARegion *ar, struct wmNotifier *wmn); // 2.5 working void BPY_update_modules( void ); // XXX - annoying, need this for pointers that get out of date @@ -137,7 +135,7 @@ extern "C" { void error_pyscript( void ); void BPY_DECREF(void *pyob_ptr); /* Py_DECREF() */ - + void BPY_set_context(struct bContext *C); /* void BPY_Err_Handle(struct Text *text); */ /* int BPY_spacetext_is_pywin(struct SpaceText *st); */ diff --git a/source/blender/python/intern/bpy_interface.c b/source/blender/python/intern/bpy_interface.c index 47505f478af..1eb48b18a5a 100644 --- a/source/blender/python/intern/bpy_interface.c +++ b/source/blender/python/intern/bpy_interface.c @@ -186,10 +186,10 @@ static void bpy_init_modules( void ) Py_DECREF(py_modpath); } - mod = PyModule_New("bpy"); + mod = PyModule_New("_bpy"); /* add the module so we can import it */ - PyDict_SetItemString(PySys_GetObject("modules"), "bpy", mod); + PyDict_SetItemString(PySys_GetObject("modules"), "_bpy", mod); Py_DECREF(mod); /* run first, initializes rna types */ @@ -201,7 +201,7 @@ static void bpy_init_modules( void ) bpy_import_test("bpy_types"); /* PyModule_AddObject( mod, "doc", BPY_rna_doc() ); */ PyModule_AddObject( mod, "props", BPY_rna_props() ); - PyModule_AddObject( mod, "__ops__", BPY_operator_module() ); /* ops is now a python module that does the conversion from SOME_OT_foo -> some.foo */ + PyModule_AddObject( mod, "ops", BPY_operator_module() ); /* ops is now a python module that does the conversion from SOME_OT_foo -> some.foo */ PyModule_AddObject( mod, "ui", BPY_ui_module() ); // XXX very experimental, consider this a test, especially PyCObject is not meant to be permanent @@ -219,11 +219,8 @@ static void bpy_init_modules( void ) Mathutils_Init(); BGL_Init(); - /* add our own modules dir */ - { - bpy_import_test("bpy_ops"); /* adds its self to bpy.ops */ - bpy_import_test("bpy_utils"); /* adds its self to bpy.sys */ - } + /* add our own modules dir, this is a python package */ + bpy_import_test("bpy"); } void BPY_update_modules( void ) @@ -303,6 +300,13 @@ void BPY_start_python_path(void) } + +void BPY_set_context(bContext *C) +{ + BPy_SetContext(C); +} + +/* call BPY_set_context first */ void BPY_start_python( int argc, char **argv ) { PyThreadState *py_tstate = NULL; @@ -600,117 +604,6 @@ 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) -{ -#ifdef TIME_REGISTRATION - double time = PIL_check_seconds_timer(); -#endif - DIR *dir; - struct dirent *de; - char *file_extension; - char *dirname; - char path[FILE_MAX]; - char *dirs[] = {"scripts/ui", "scripts/op", "scripts/io", NULL}; - int path_flags[] = {BLI_GETHOME_LOCAL|BLI_GETHOME_SYSTEM, BLI_GETHOME_USER}; /* SYSTEM / NON-SYSTEM */ - int a, err, flag_iter; - - PyGILState_STATE gilstate; - PyObject *sys_path; - - bpy_context_set(C, &gilstate); - - sys_path= PySys_GetObject("path"); /* borrow */ - PyList_Insert(sys_path, 0, Py_None); /* place holder, resizes the list */ - - /* Scan system scripts first, then local/user */ - for(flag_iter=0; flag_iter < sizeof(path_flags)/sizeof(int); flag_iter++) { - - for(a=0; dirs[a]; a++) { - dirname= BLI_gethome_folder(dirs[a], path_flags[flag_iter]); - - if(!dirname) - continue; - - dir = opendir(dirname); - - if(!dir) - continue; - - /* set the first dir in the sys.path for fast importing of modules */ - PyList_SetItem(sys_path, 0, PyUnicode_FromString(dirname)); /* steals the ref */ - - 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; - - if (de->d_name[0] == '.') { - /* do nothing, probably .svn */ - } - else if ((file_extension = strstr(de->d_name, ".py"))) { - /* normal py files? */ - if(file_extension && file_extension[3] == '\0') { - de->d_name[(file_extension - de->d_name)] = '\0'; - err= bpy_import_module(de->d_name, reload); - } - } -#ifndef __linux__ - else if( BLI_join_dirfile(path, dirname, de->d_name), S_ISDIR(BLI_exist(path))) { -#else - else if(de->d_type==DT_DIR) { - BLI_join_dirfile(path, dirname, de->d_name); -#endif - /* support packages */ - BLI_join_dirfile(path, path, "__init__.py"); - - if(BLI_exists(path)) { - err= 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); - } - } - - closedir(dir); - } - } - - PyList_SetSlice(sys_path, 0, 1, NULL); /* remove the first item */ - - bpy_context_clear(C, &gilstate); - -#ifdef TIME_REGISTRATION - printf("script time %f\n", (PIL_check_seconds_timer()-time)); -#endif - - /* reset the timer so as not to take loading into the stats */ - bpy_timer_count = 0; -} - /* ****************************************** */ /* Drivers - PyExpression Evaluation */ diff --git a/source/blender/python/intern/stubs.c b/source/blender/python/intern/stubs.c index 94bdb6ba6ab..303fd84ee60 100644 --- a/source/blender/python/intern/stubs.c +++ b/source/blender/python/intern/stubs.c @@ -27,7 +27,6 @@ */ /* python, will come back */ -void BPY_post_start_python() {} //void BPY_run_python_script() {} //void BPY_start_python() {} void BPY_call_importloader() {} |