diff options
-rw-r--r-- | source/blender/blenkernel/bad_level_call_stubs/stubs.c | 2 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/library.c | 2 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/script.c | 2 | ||||
-rw-r--r-- | source/blender/blenlib/BLI_blenlib.h | 2 | ||||
-rw-r--r-- | source/blender/blenlib/intern/util.c | 6 | ||||
-rw-r--r-- | source/blender/blenloader/intern/readfile.c | 33 | ||||
-rw-r--r-- | source/blender/blenloader/intern/writefile.c | 13 | ||||
-rw-r--r-- | source/blender/include/BPI_script.h | 71 | ||||
-rw-r--r-- | source/blender/makesdna/DNA_space_types.h | 18 | ||||
-rw-r--r-- | source/blender/python/BPY_extern.h | 4 | ||||
-rw-r--r-- | source/blender/python/BPY_interface.c | 330 | ||||
-rw-r--r-- | source/blender/python/api2_2x/Blender.c | 2 | ||||
-rw-r--r-- | source/blender/python/api2_2x/Draw.c | 4 | ||||
-rw-r--r-- | source/blender/python/api2_2x/Window.c | 1 | ||||
-rw-r--r-- | source/blender/src/drawscript.c | 31 | ||||
-rw-r--r-- | source/blender/src/editimasel.c | 2 | ||||
-rw-r--r-- | source/blender/src/filesel.c | 1 | ||||
-rw-r--r-- | source/blender/src/header_script.c | 1 | ||||
-rw-r--r-- | source/blender/src/headerbuttons.c | 2 | ||||
-rw-r--r-- | source/blender/src/space.c | 1 |
20 files changed, 305 insertions, 223 deletions
diff --git a/source/blender/blenkernel/bad_level_call_stubs/stubs.c b/source/blender/blenkernel/bad_level_call_stubs/stubs.c index 5e827ca766b..1da46149eac 100644 --- a/source/blender/blenkernel/bad_level_call_stubs/stubs.c +++ b/source/blender/blenkernel/bad_level_call_stubs/stubs.c @@ -36,7 +36,7 @@ #include "BKE_bad_level_calls.h" #include "BLI_blenlib.h" -#include "BPI_script.h" +#include "DNA_space_types.h" #include "DNA_texture_types.h" #include "DNA_material_types.h" #include "DNA_node_types.h" diff --git a/source/blender/blenkernel/intern/library.c b/source/blender/blenkernel/intern/library.c index e81d3bac655..4cd7e1c6d38 100644 --- a/source/blender/blenkernel/intern/library.c +++ b/source/blender/blenkernel/intern/library.c @@ -114,7 +114,7 @@ #include "BKE_brush.h" #include "BKE_idprop.h" -#include "BPI_script.h" +#include "DNA_space_types.h" #define MAX_IDPUP 60 /* was 24 */ diff --git a/source/blender/blenkernel/intern/script.c b/source/blender/blenkernel/intern/script.c index b99c2c51441..099fedf460a 100644 --- a/source/blender/blenkernel/intern/script.c +++ b/source/blender/blenkernel/intern/script.c @@ -35,7 +35,7 @@ */ #include "BKE_script.h" -#include "BPI_script.h" +#include "DNA_space_types.h" #include "MEM_guardedalloc.h" #include "BKE_bad_level_calls.h" /* for BPY_clear_script */ diff --git a/source/blender/blenlib/BLI_blenlib.h b/source/blender/blenlib/BLI_blenlib.h index 551b6732333..e8c73398635 100644 --- a/source/blender/blenlib/BLI_blenlib.h +++ b/source/blender/blenlib/BLI_blenlib.h @@ -98,7 +98,7 @@ char *BLI_gethome(void); void BLI_make_file_string(const char *relabase, char *string, const char *dir, const char *file); void BLI_make_exist(char *dir); void BLI_make_existing_file(char *name); -void BLI_split_dirfile(const char *string, char *dir, char *file); +void BLI_split_dirfile(char *string, char *dir, char *file); void BLI_join_dirfile(char *string, const char *dir, const char *file); int BLI_testextensie(const char *str, const char *ext); void addlisttolist(ListBase *list1, ListBase *list2); diff --git a/source/blender/blenlib/intern/util.c b/source/blender/blenlib/intern/util.c index 4610c887476..51691499ba8 100644 --- a/source/blender/blenlib/intern/util.c +++ b/source/blender/blenlib/intern/util.c @@ -1102,7 +1102,7 @@ int BLI_convertstringcode(char *path, const char *basepath, int framenum) return wasrelative; } -/* copy di to fi without directory only */ +/* copy di to fi, filename only */ void BLI_splitdirstring(char *di, char *fi) { char *lslash= BLI_last_slash(di); @@ -1348,8 +1348,8 @@ int BLI_testextensie(const char *str, const char *ext) } - -void BLI_split_dirfile(const char *string, char *dir, char *file) +/* warning, can modify 'string' */ +void BLI_split_dirfile(char *string, char *dir, char *file) { int a; #ifdef WIN32 diff --git a/source/blender/blenloader/intern/readfile.c b/source/blender/blenloader/intern/readfile.c index ecc11d080b5..a48ca83c381 100644 --- a/source/blender/blenloader/intern/readfile.c +++ b/source/blender/blenloader/intern/readfile.c @@ -1422,6 +1422,17 @@ static void direct_link_brush(FileData *fd, Brush *brush) brush->mtex[a]= newdataadr(fd, brush->mtex[a]); } +static void direct_link_script(FileData *fd, Script *script) +{ + script->id.us = 1; + script->py_draw = + script->py_event = + script->py_button = + script->py_browsercallback = + script->py_globaldict = + script->flags = NULL; +} + /* ************ READ CurveMapping *************** */ /* cuma itself has been read! */ @@ -3726,9 +3737,13 @@ static void lib_link_screen(FileData *fd, Main *main) } else if(sl->spacetype==SPACE_SCRIPT) { - SpaceScript *sc= (SpaceScript *)sl; - sc->script = NULL; + SpaceScript *scpt= (SpaceScript *)sl; + /*sc->script = NULL; - 2.45 set to null, better re-run the script */ + if (scpt->script) { + scpt->script = newlibadr(fd, sc->id.lib, scpt->script); + SCRIPT_SET_NULL(scpt->script); + } } else if(sl->spacetype==SPACE_OOPS) { SpaceOops *so= (SpaceOops *)sl; @@ -3899,9 +3914,14 @@ void lib_link_screen_restore(Main *newmain, Scene *curscene) if(st->text==NULL) st->text= newmain->text.first; } else if(sl->spacetype==SPACE_SCRIPT) { - SpaceScript *sc= (SpaceScript *)sl; - - sc->script = NULL; + SpaceScript *scpt= (SpaceScript *)sl; + + scpt->script= restore_pointer_by_name(newmain, (ID *)scpt->script, 1); + + /*sc->script = NULL; - 2.45 set to null, better re-run the script */ + if (scpt->script) { + SCRIPT_SET_NULL(scpt->script); + } } else if(sl->spacetype==SPACE_OOPS) { SpaceOops *so= (SpaceOops *)sl; @@ -4359,6 +4379,9 @@ static BHead *read_libblock(FileData *fd, Main *main, BHead *bhead, int flag, ID case ID_PA: direct_link_particlesettings(fd, (ParticleSettings*)id); break; + case ID_SCRIPT: + direct_link_script(fd, (Script*)id); + break; } /*link direct data of ID properties*/ diff --git a/source/blender/blenloader/intern/writefile.c b/source/blender/blenloader/intern/writefile.c index 6fcb5312da8..a4c91d77f5e 100644 --- a/source/blender/blenloader/intern/writefile.c +++ b/source/blender/blenloader/intern/writefile.c @@ -1955,6 +1955,18 @@ static void write_brushes(WriteData *wd, ListBase *idbase) } } +static void write_scripts(WriteData *wd, ListBase *idbase) +{ + Script *script; + + for(script=idbase->first; script; script= script->id.next) { + if(script->id.us>0 || wd->current) { + writestruct(wd, ID_SCRIPT, "Script", 1, script); + if (script->id.properties) IDP_WriteProperty(script->id.properties, wd); + } + } +} + static void write_global(WriteData *wd) { FileGlobal fg; @@ -2020,6 +2032,7 @@ static int write_file_handle(int handle, MemFile *compare, MemFile *current, int write_particlesettings(wd, &G.main->particle); write_nodetrees(wd, &G.main->nodetree); write_brushes (wd, &G.main->brush); + write_scripts (wd, &G.main->script); if(current==NULL) write_libraries(wd, G.main->next); /* no library save in undo */ diff --git a/source/blender/include/BPI_script.h b/source/blender/include/BPI_script.h deleted file mode 100644 index 80ac8b46900..00000000000 --- a/source/blender/include/BPI_script.h +++ /dev/null @@ -1,71 +0,0 @@ -/** - * include/BPI_script.h (jan-2004 ianwill) - * - * $Id$ - * - * Header for BPython's script structure. BPI: Blender Python external include - * file. - * - * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. The Blender - * Foundation also sells licenses for use in proprietary software under - * the Blender License. See http://www.blender.org/BL/ for information - * about this. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * - * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. - * All rights reserved. - * - * This is a new part of Blender. - * - * Contributor(s): Willian P. Germano. - * - * ***** END GPL/BL DUAL LICENSE BLOCK ***** - */ - -#ifndef BPI_SCRIPT_H -#define BPI_SCRIPT_H - -//#include "DNA_listBase.h" -#include "DNA_ID.h" - -typedef struct Script { - ID id; - - void *py_draw; - void *py_event; - void *py_button; - void *py_browsercallback; - void *py_globaldict; - - int flags, lastspace; - -} Script; - -/* Note: a script that registers callbacks in the script->py_* pointers - * above (or calls the file or image selectors) needs to keep its global - * dictionary until Draw.Exit() is called and the callbacks removed. - * Unsetting SCRIPT_RUNNING means the interpreter reached the end of the - * script and returned control to Blender, but we can't get rid of its - * namespace (global dictionary) while SCRIPT_GUI or SCRIPT_FILESEL is set, - * because of the callbacks. The flags and the script name are saved in - * each running script's global dictionary, under '__script__'. */ - -/* Flags */ -#define SCRIPT_RUNNING 0x01 -#define SCRIPT_GUI 0x02 -#define SCRIPT_FILESEL 0x04 - -#endif /* BPI_SCRIPT_H */ diff --git a/source/blender/makesdna/DNA_space_types.h b/source/blender/makesdna/DNA_space_types.h index 9efeffbe691..213ccee7e29 100644 --- a/source/blender/makesdna/DNA_space_types.h +++ b/source/blender/makesdna/DNA_space_types.h @@ -291,6 +291,24 @@ typedef struct SpaceText { } SpaceText; +typedef struct Script { + ID id; + + void *py_draw; + void *py_event; + void *py_button; + void *py_browsercallback; + void *py_globaldict; + + int flags, lastspace; + char scriptname[256]; /* store the script file here so we can re-run it on loading blender, if "Enable Scripts" is on */ + char scriptarg[256]; +} Script; +#define SCRIPT_SET_NULL(_script) _script->py_draw = _script->py_event = _script->py_button = _script->py_browsercallback = _script->py_globaldict = _script->flags = 0; +#define SCRIPT_RUNNING 0x01 +#define SCRIPT_GUI 0x02 +#define SCRIPT_FILESEL 0x04 + typedef struct SpaceScript { SpaceLink *next, *prev; int spacetype; diff --git a/source/blender/python/BPY_extern.h b/source/blender/python/BPY_extern.h index c3aef82751d..d7bd4f22fc6 100644 --- a/source/blender/python/BPY_extern.h +++ b/source/blender/python/BPY_extern.h @@ -44,13 +44,13 @@ struct ScriptLink; /* DNA_scriptlink_types.h */ struct ListBase; /* DNA_listBase.h */ struct SpaceText; /* DNA_space_types.h */ struct SpaceScript; /* DNA_space_types.h */ -struct Script; /* BPI_script.h */ struct ScrArea; /* DNA_screen_types.h */ struct bScreen; /* DNA_screen_types.h */ struct bConstraint; /* DNA_constraint_types.h */ struct bPythonConstraint; /* DNA_constraint_types.h */ struct bConstraintOb; /* DNA_constraint_types.h */ struct bConstraintTarget; /* DNA_constraint_types.h*/ +struct Script; /* DNA_screen_types.h */ #ifdef __cplusplus extern "C" { #endif @@ -94,6 +94,7 @@ extern "C" { int BPY_txt_do_python_Text( struct Text *text ); int BPY_menu_do_python( short menutype, int event ); void BPY_run_python_script( char *filename ); + int BPY_run_script(struct Script *script); void BPY_free_compiled_text( struct Text *text ); void BPY_clear_bad_scriptlinks( struct Text *byebye ); @@ -126,6 +127,7 @@ extern "C" { unsigned short event, short val, char ascii ); void BPY_clear_script( struct Script *script ); void BPY_free_finished_script( struct Script *script ); + void BPY_scripts_clear_pyobjects( void ); /* void BPY_Err_Handle(struct Text *text); */ /* void BPY_clear_bad_scriptlink(struct ID *id, struct Text *byebye); */ diff --git a/source/blender/python/BPY_interface.c b/source/blender/python/BPY_interface.c index 2336c343f68..d4d91e1a19a 100644 --- a/source/blender/python/BPY_interface.c +++ b/source/blender/python/BPY_interface.c @@ -57,7 +57,7 @@ #include "MEM_guardedalloc.h" #include "BPY_extern.h" #include "BPY_menus.h" -#include "BPI_script.h" +#include "DNA_space_types.h" #include "BKE_global.h" #include "BKE_main.h" #include "BKE_armature.h" @@ -661,7 +661,7 @@ int BPY_txt_do_python_Text( struct Text *text ) script->py_event = NULL; script->py_button = NULL; script->py_browsercallback = NULL; - + strncpy(script->scriptname, text->id.name+2, sizeof(script->scriptname)); gilstate = PyGILState_Ensure(); py_dict = CreateGlobalDictionary( ); @@ -768,6 +768,178 @@ void BPY_run_python_script( char *fn ) } } +int BPY_run_script(Script *script) +{ + PyObject *py_dict, *py_res, *pyarg; + Text *text = NULL; + BPy_constant *info; + int len; + char *buffer, *s; + + FILE *fp = NULL; + + PyGILState_STATE gilstate = PyGILState_Ensure(); + + if (!BLI_exists(script->scriptname)) { + /* The file dosnt exist, maybe this blend file was made on some other persons computer? */ + char fname[FILE_MAX]; + char fpath[FILE_MAX]; + char ftmp[FILE_MAX]; + + strcpy(ftmp, script->scriptname); + BLI_split_dirfile(ftmp, fpath, fname); + BLI_make_file_string("/", fpath, bpy_gethome(1), fname); + + if (BLI_exists(fpath)) { + strncpy(script->scriptname, fpath, sizeof(script->scriptname)); + } else if (U.pythondir[0]) { + BLI_make_file_string("/", fpath, U.pythondir, fname); + if (BLI_exists(fpath)) { + strncpy(script->scriptname, fpath, sizeof(script->scriptname)); + } + } + + /* cant find the file?, fallback to text block */ + if (!BLI_exists(script->scriptname)) { + for (text=G.main->text.first; text; text=text->id.next) { + if (strcmp(script->scriptname, text->id.name+2)==0) { + break; + } + } + } + } + if (text) { + Py_INCREF( Py_None ); + pyarg = Py_None; + } else { + fp = fopen( script->scriptname, "rb" ); + if( !fp ) { + printf( "Error loading script: couldn't open file %s\n", script->scriptname ); + if( G.main->script.first ) + free_libblock( &G.main->script, script ); + PyGILState_Release(gilstate); + return 0; + } + + if( script->scriptarg[0] == '\0' ) { /* no submenus */ + Py_INCREF( Py_None ); + pyarg = Py_None; + } else { + pyarg = PyString_FromString( script->scriptarg ); + } + } + + script->id.us = 1; + script->flags = SCRIPT_RUNNING; + script->py_draw = NULL; + script->py_event = NULL; + script->py_button = NULL; + script->py_browsercallback = NULL; + + py_dict = CreateGlobalDictionary( ); + + script->py_globaldict = py_dict; + + if( !setup_armature_weakrefs()){ + printf("Oops - weakref dict\n"); + if( G.main->script.first ) + free_libblock( &G.main->script, script ); + ReleaseGlobalDictionary( py_dict ); + MEM_freeN( buffer ); + PyGILState_Release(gilstate); + return 0; + } + + info = ( BPy_constant * ) PyConstant_New( ); + if( info ) { + PyConstant_Insert( info, "name", + PyString_FromString( script->id.name + 2 ) ); + PyConstant_Insert( info, "arg", pyarg ); + EXPP_dict_set_item_str( py_dict, "__script__", + ( PyObject * ) info ); + } + + if (text) { + py_res = RunPython( text, py_dict ); + } else { + /* Previously we used PyRun_File to run directly the code on a FILE + * object, but as written in the Python/C API Ref Manual, chapter 2, + * 'FILE structs for different C libraries can be different and + * incompatible'. + * So now we load the script file data to a buffer */ + + fseek( fp, 0L, SEEK_END ); + len = ftell( fp ); + fseek( fp, 0L, SEEK_SET ); + + buffer = MEM_mallocN( len + 2, "pyfilebuf" ); /* len+2 to add '\n\0' */ + len = fread( buffer, 1, len, fp ); + + buffer[len] = '\n'; /* fix syntax error in files w/o eol */ + buffer[len + 1] = '\0'; + + /* fast clean-up of dos cr/lf line endings: change '\r' to space */ + + /* we also have to check for line splitters: '\\' */ + /* to avoid possible syntax errors on dos files on win */ + /**/ + /* but first make sure we won't disturb memory below &buffer[0]: */ + if( *buffer == '\r' ) + *buffer = ' '; + + /* now handle the whole buffer */ + for( s = buffer + 1; *s != '\0'; s++ ) { + if( *s == '\r' ) { + if( *( s - 1 ) == '\\' ) { /* special case: long lines split with '\': */ + *( s - 1 ) = ' '; /* we write ' \', because '\ ' is a syntax error */ + *s = '\\'; + } else + *s = ' '; /* not a split line, just replace '\r' with ' ' */ + } + } + + fclose( fp ); + + + py_res = PyRun_String( buffer, Py_file_input, py_dict, py_dict ); + MEM_freeN( buffer ); + } + + if( !py_res ) { /* Failed execution of the script */ + + BPY_Err_Handle( script->id.name + 2 ); + ReleaseGlobalDictionary( py_dict ); + script->py_globaldict = NULL; + if( G.main->script.first ) + free_libblock( &G.main->script, script ); + error( "Python script error: check console" ); + + PyGILState_Release(gilstate); + return 0; + } else { + Py_DECREF( py_res ); + script->flags &= ~SCRIPT_RUNNING; + + if( !script->flags ) { + ReleaseGlobalDictionary( py_dict ); + script->py_globaldict = NULL; + free_libblock( &G.main->script, script ); + + /* special case: called from the menu in the Scripts window + * we have to change sc->script pointer, since it'll be freed here.*/ + if( curarea->spacetype == SPACE_SCRIPT ) { + SpaceScript *sc = curarea->spacedata.first; + sc->script = G.main->script.first; /* can be null, which is ok ... */ + /* ... meaning no other script is running right now. */ + } + + } + } + + PyGILState_Release(gilstate); + return 1; +} + /**************************************************************************** * Description: This function executes the script chosen from a menu. * Notes: It is called by the ui code in src/header_???.c when a user @@ -777,17 +949,14 @@ void BPY_run_python_script( char *fn ) *****************************************************************************/ int BPY_menu_do_python( short menutype, int event ) { - PyObject *py_dict, *py_res, *pyarg = NULL; - BPy_constant *info; + char *argstr = NULL; BPyMenu *pym; BPySubMenu *pysm; - FILE *fp = NULL; - char *buffer, *s; - char filestr[FILE_MAXDIR + FILE_MAXFILE]; char scriptname[21]; Script *script = NULL; - int len; + int ret, len; PyGILState_STATE gilstate; + char filestr[FILE_MAX]; pym = BPyMenu_GetEntry( menutype, ( short ) event ); @@ -817,7 +986,7 @@ int BPY_menu_do_python( short menutype, int event ) if( arg >= 0 ) { while( arg-- ) pysm = pysm->next; - pyarg = PyString_FromString( pysm->arg ); + argstr = pysm->arg; } else { PyGILState_Release(gilstate); return 0; @@ -825,11 +994,6 @@ int BPY_menu_do_python( short menutype, int event ) } } - if( !pyarg ) { /* no submenus */ - Py_INCREF( Py_None ); - pyarg = Py_None; - } - if( pym->dir ) { /* script is in U.pythondir */ char upythondir[FILE_MAXDIR]; @@ -850,14 +1014,6 @@ int BPY_menu_do_python( short menutype, int event ) BLI_make_file_string( "/", filestr, scriptsdir, pym->filename ); } - fp = fopen( filestr, "rb" ); - if( !fp ) { - printf( "Error loading script: couldn't open file %s\n", - filestr ); - PyGILState_Release(gilstate); - return 0; - } - BLI_strncpy(scriptname, pym->name, 21); len = strlen(scriptname) - 1; /* by convention, scripts that open the file browser or have submenus @@ -872,7 +1028,6 @@ int BPY_menu_do_python( short menutype, int event ) if( !script ) { printf( "couldn't allocate memory for Script struct!" ); - fclose( fp ); PyGILState_Release(gilstate); return 0; } @@ -917,111 +1072,12 @@ int BPY_menu_do_python( short menutype, int event ) } break; } - - script->id.us = 1; - script->flags = SCRIPT_RUNNING; - script->py_draw = NULL; - script->py_event = NULL; - script->py_button = NULL; - script->py_browsercallback = NULL; - - py_dict = CreateGlobalDictionary( ); - - script->py_globaldict = py_dict; - - info = ( BPy_constant * ) PyConstant_New( ); - if( info ) { - PyConstant_Insert( info, "name", - PyString_FromString( script->id.name + 2 ) ); - PyConstant_Insert( info, "arg", pyarg ); - EXPP_dict_set_item_str( py_dict, "__script__", - ( PyObject * ) info ); - } - - /* Previously we used PyRun_File to run directly the code on a FILE - * object, but as written in the Python/C API Ref Manual, chapter 2, - * 'FILE structs for different C libraries can be different and - * incompatible'. - * So now we load the script file data to a buffer */ - - fseek( fp, 0L, SEEK_END ); - len = ftell( fp ); - fseek( fp, 0L, SEEK_SET ); - - buffer = MEM_mallocN( len + 2, "pyfilebuf" ); /* len+2 to add '\n\0' */ - len = fread( buffer, 1, len, fp ); - - buffer[len] = '\n'; /* fix syntax error in files w/o eol */ - buffer[len + 1] = '\0'; - - /* fast clean-up of dos cr/lf line endings: change '\r' to space */ - - /* we also have to check for line splitters: '\\' */ - /* to avoid possible syntax errors on dos files on win */ - /**/ - /* but first make sure we won't disturb memory below &buffer[0]: */ - if( *buffer == '\r' ) - *buffer = ' '; - - /* now handle the whole buffer */ - for( s = buffer + 1; *s != '\0'; s++ ) { - if( *s == '\r' ) { - if( *( s - 1 ) == '\\' ) { /* special case: long lines split with '\': */ - *( s - 1 ) = ' '; /* we write ' \', because '\ ' is a syntax error */ - *s = '\\'; - } else - *s = ' '; /* not a split line, just replace '\r' with ' ' */ - } - } - - fclose( fp ); - - - if( !setup_armature_weakrefs()){ - printf("Oops - weakref dict\n"); - MEM_freeN( buffer ); - PyGILState_Release(gilstate); - return 0; - } - - /* run the string buffer */ - - py_res = PyRun_String( buffer, Py_file_input, py_dict, py_dict ); - - MEM_freeN( buffer ); - - if( !py_res ) { /* Failed execution of the script */ - - BPY_Err_Handle( script->id.name + 2 ); - ReleaseGlobalDictionary( py_dict ); - script->py_globaldict = NULL; - if( G.main->script.first ) - free_libblock( &G.main->script, script ); - error( "Python script error: check console" ); - - PyGILState_Release(gilstate); - return 0; - } else { - Py_DECREF( py_res ); - script->flags &= ~SCRIPT_RUNNING; - - if( !script->flags ) { - ReleaseGlobalDictionary( py_dict ); - script->py_globaldict = NULL; - free_libblock( &G.main->script, script ); - - /* special case: called from the menu in the Scripts window - * we have to change sc->script pointer, since it'll be freed here.*/ - if( curarea->spacetype == SPACE_SCRIPT ) { - SpaceScript *sc = curarea->spacedata.first; - sc->script = G.main->script.first; /* can be null, which is ok ... */ - /* ... meaning no other script is running right now. */ - } - - } - } - - PyGILState_Release(gilstate); + + strncpy(script->scriptname, filestr, sizeof(script->scriptname)); + if (argstr!=NULL && argstr[0] != '\0') + strncpy(script->scriptarg, argstr, sizeof(script->scriptarg)); + + ret = BPY_run_script(script); return 1; /* normal return */ } @@ -1115,7 +1171,9 @@ void BPY_clear_script( Script * script ) script->py_event = NULL; script->py_button = NULL; script->py_browsercallback = NULL; - + script->scriptname[0] = '\0'; + script->scriptarg[0] = '\0'; + dict = script->py_globaldict; if( dict ) { @@ -2889,3 +2947,17 @@ void init_ourReload( void ) d = PyModule_GetDict( m ); EXPP_dict_set_item_str( d, "reload", reload ); } + + +void BPY_scripts_clear_pyobjects( void ) +{ + Script *script; + for (script=G.main->script.first; script; script=script->id.next) { + Py_XDECREF((PyObject *)script->py_draw); + Py_XDECREF((PyObject *)script->py_event); + Py_XDECREF((PyObject *)script->py_button); + Py_XDECREF((PyObject *)script->py_browsercallback); + Py_XDECREF((PyObject *)script->py_globaldict); + SCRIPT_SET_NULL(script); + } +}
\ No newline at end of file diff --git a/source/blender/python/api2_2x/Blender.c b/source/blender/python/api2_2x/Blender.c index a93316d308f..d520dded486 100644 --- a/source/blender/python/api2_2x/Blender.c +++ b/source/blender/python/api2_2x/Blender.c @@ -54,7 +54,7 @@ struct ID; /*keep me up here */ #include "BKE_library.h" #include "BKE_main.h" #include "BIF_space.h" -#include "BPI_script.h" +#include "DNA_space_types.h" /* script struct */ #include "BSE_headerbuttons.h" #include "DNA_screen_types.h" /* for SPACE_VIEW3D */ #include "DNA_userdef_types.h" diff --git a/source/blender/python/api2_2x/Draw.c b/source/blender/python/api2_2x/Draw.c index 284a6d92ba2..a4bbeba65ac 100644 --- a/source/blender/python/api2_2x/Draw.c +++ b/source/blender/python/api2_2x/Draw.c @@ -52,7 +52,7 @@ #include "BIF_space.h" #include "BIF_interface.h" #include "BIF_toolbox.h" -#include "BPI_script.h" /* script struct */ +#include "DNA_space_types.h" /* script struct */ #include "Image.h" /* for accessing Blender.Image objects */ #include "IMB_imbuf_types.h" /* for the IB_rect define */ #include "interface.h" @@ -912,6 +912,8 @@ static PyObject *Method_Exit( PyObject * self ) /* remove our lock to the current namespace */ script->flags &= ~SCRIPT_GUI; + script->scriptname[0] = '\0'; + script->scriptarg[0] = '\0'; Py_RETURN_NONE; } diff --git a/source/blender/python/api2_2x/Window.c b/source/blender/python/api2_2x/Window.c index 7d346ed9cd9..21caab3e697 100644 --- a/source/blender/python/api2_2x/Window.c +++ b/source/blender/python/api2_2x/Window.c @@ -38,7 +38,6 @@ #include "BKE_main.h" #include "BKE_object.h" /* for during_script() and during_scriptlink() */ #include "BKE_scene.h" /* scene_find_camera() */ -#include "BPI_script.h" #include "BIF_mywindow.h" #include "BIF_imasel.h" #include "BSE_headerbuttons.h" diff --git a/source/blender/src/drawscript.c b/source/blender/src/drawscript.c index b009c3b8388..70ae4b1c94d 100644 --- a/source/blender/src/drawscript.c +++ b/source/blender/src/drawscript.c @@ -58,7 +58,6 @@ #include "BKE_global.h" #include "BKE_main.h" -#include "BPI_script.h" #include "BPY_extern.h" #include "BIF_gl.h" @@ -95,6 +94,20 @@ void drawscriptspace(ScrArea *sa, void *spacedata) script = sc->script; + /* Is this script loaded from a file and it needs running??? */ + if ( (G.f & G_DOSCRIPTLINKS) && + script->scriptname[0] != '\0' && + (script->flags == NULL && + script->py_event == NULL && + script->py_button == NULL && + script->py_draw == NULL ) + ) { + if (!BPY_run_script(script)) { + /* if this fails, script will be free'd */ + script = NULL; + } + } + if (script->py_draw) { BPY_spacescript_do_pywin_draw(sc); } @@ -114,6 +127,22 @@ void winqreadscriptspace(struct ScrArea *sa, void *spacedata, struct BWinEvent * Script *script = sc->script; if (script) { + /* Is this script loaded from a file and it needs running??? */ + if ( (G.f & G_DOSCRIPTLINKS) && + script->scriptname[0] != '\0' && + (script->flags == NULL && + script->py_event == NULL && + script->py_button == NULL && + script->py_draw == NULL ) + ) { + if (!BPY_run_script(script)) { + /* if this fails, script will be free'd */ + script = NULL; + } + } + } + + if (script) { if (script->py_event || script->py_button) BPY_spacescript_do_pywin_event(sc, event, val, ascii); diff --git a/source/blender/src/editimasel.c b/source/blender/src/editimasel.c index f3e2dcccc81..e93687fef93 100644 --- a/source/blender/src/editimasel.c +++ b/source/blender/src/editimasel.c @@ -89,8 +89,6 @@ #include "BLO_readfile.h" -#include "BPI_script.h" - #include "BSE_drawipo.h" #include "BSE_drawimasel.h" #include "BSE_edit.h" diff --git a/source/blender/src/filesel.c b/source/blender/src/filesel.c index 1262683b90a..ffd6574d781 100644 --- a/source/blender/src/filesel.c +++ b/source/blender/src/filesel.c @@ -102,7 +102,6 @@ #include "BDR_editcurve.h" #include "BDR_editobject.h" -#include "BPI_script.h" #include "BSE_filesel.h" #include "BSE_view.h" diff --git a/source/blender/src/header_script.c b/source/blender/src/header_script.c index de76a30ecdd..ba43ae2e7e5 100644 --- a/source/blender/src/header_script.c +++ b/source/blender/src/header_script.c @@ -60,7 +60,6 @@ #include "BKE_sca.h" #include "BSE_filesel.h" -#include "BPI_script.h" #include "BPY_extern.h" #include "BPY_menus.h" diff --git a/source/blender/src/headerbuttons.c b/source/blender/src/headerbuttons.c index e3c594f4ea3..d2d3a3352b7 100644 --- a/source/blender/src/headerbuttons.c +++ b/source/blender/src/headerbuttons.c @@ -148,8 +148,6 @@ #include "BIF_writeimage.h" #include "BIF_butspace.h" -#include "BPI_script.h" - #include "BSE_edit.h" #include "BSE_filesel.h" #include "BSE_headerbuttons.h" diff --git a/source/blender/src/space.c b/source/blender/src/space.c index 6b5c0e57da5..af6cfa8de9f 100644 --- a/source/blender/src/space.c +++ b/source/blender/src/space.c @@ -1031,6 +1031,7 @@ void BIF_undo(void) else { /* now also in faceselect mode */ if(U.uiflag & USER_GLOBALUNDO) { + BPY_scripts_clear_pyobjects(); BKE_undo_step(1); sound_initialize_sounds(); } |