Made python scripts save and load in the blend file so you can have the same scripts...
authorCampbell Barton <ideasman42@gmail.com>
Thu, 6 Mar 2008 21:25:15 +0000 (21:25 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Thu, 6 Mar 2008 21:25:15 +0000 (21:25 +0000)
Also scripts will re-run on undo rather then closing.

This is done by saving and loading the name of the script or textblock of the 'Script' datablock, connected to the ScriptSpace. This way when there is a name but the script dosnt run.
Blender runs the script or text block if available.

20 files changed:
source/blender/blenkernel/bad_level_call_stubs/stubs.c
source/blender/blenkernel/intern/library.c
source/blender/blenkernel/intern/script.c
source/blender/blenlib/BLI_blenlib.h
source/blender/blenlib/intern/util.c
source/blender/blenloader/intern/readfile.c
source/blender/blenloader/intern/writefile.c
source/blender/include/BPI_script.h [deleted file]
source/blender/makesdna/DNA_space_types.h
source/blender/python/BPY_extern.h
source/blender/python/BPY_interface.c
source/blender/python/api2_2x/Blender.c
source/blender/python/api2_2x/Draw.c
source/blender/python/api2_2x/Window.c
source/blender/src/drawscript.c
source/blender/src/editimasel.c
source/blender/src/filesel.c
source/blender/src/header_script.c
source/blender/src/headerbuttons.c
source/blender/src/space.c

index 5e827ca766bf8191aa66ff029e003b41887492fd..1da46149eac59ad41f838fc541e7e3658069af2a 100644 (file)
@@ -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"
index e81d3bac655b993afb764d56b3290be0573906b4..4cd7e1c6d38e66d7f0e74e4c5c8699fb51b0d031 100644 (file)
 #include "BKE_brush.h"
 #include "BKE_idprop.h"
 
-#include "BPI_script.h"
+#include "DNA_space_types.h"
 
 #define MAX_IDPUP              60      /* was 24 */
 
index b99c2c514410289976edc2b142a7a6eaa2fb90f6..099fedf460af34cc748af46b7b0bc2652d72156c 100644 (file)
@@ -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 */
index 551b673233390ee2bb2219a86460bbb87494dc97..e8c73398635aaa77c4429dc27fc8ce7bb2614db5 100644 (file)
@@ -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);
index 4610c887476e1140e725055845157b1d5cf542ac..51691499ba8f6dd81a240fa3fe148940b9ee0a22 100644 (file)
@@ -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
index ecc11d080b5e54d2025dd3021e87151d414e789b..a48ca83c38126b8428b940d66762f856b31f8d5a 100644 (file)
@@ -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*/
index 6fcb5312da8509333a32af000f0a99cfda786b1b..a4c91d77f5e76305aac4a2e2cf0cfccb697155cc 100644 (file)
@@ -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 (file)
index 80ac8b4..0000000
+++ /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 */
index 9efeffbe691ffb06993e394304b5488b26ee85f9..213ccee7e296f8ee9ce0a586d66b691b021b00bd 100644 (file)
@@ -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;
index c3aef82751d2a3f9a344acb76655b9e31585cb9a..d7bd4f22fc6ccd990f0c2d332a9f792b57738472 100644 (file)
@@ -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); */
index 2336c343f6875ca43c90d8cc664ec49ebafd1d5b..d4d91e1a19ae13542d7f57c5dbcaec46e00a7f98 100644 (file)
@@ -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
index a93316d308f473517e9e074921ce5f15ca5d1d56..d520dded4861db3f8668612f509204413d62ec35 100644 (file)
@@ -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"
index 284a6d92ba2a95e9179175aa1b009b5e61cf80d6..a4bbeba65ac31be3e7a0d25b2f63db7ad83fa8d7 100644 (file)
@@ -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;
 }
index 7d346ed9cd9942063860389457ce2d9633396e72..21caab3e6978e600e8defe631bbea273f1c6b822 100644 (file)
@@ -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"
index b009c3b838837c1827ee76dfe0195d97ff2e5325..70ae4b1c94d5d165e391933b253de1f74301942e 100644 (file)
@@ -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);
        }
@@ -113,6 +126,22 @@ void winqreadscriptspace(struct ScrArea *sa, void *spacedata, struct BWinEvent *
        SpaceScript *sc = curarea->spacedata.first;
        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);
index f3e2dcccc8114a71cfd9b58054bc6c5455bb7fff..e93687fef93106a7deac2e1f5fdc712718555f5b 100644 (file)
@@ -89,8 +89,6 @@
 
 #include "BLO_readfile.h"
 
-#include "BPI_script.h"
-
 #include "BSE_drawipo.h"
 #include "BSE_drawimasel.h"
 #include "BSE_edit.h"
index 1262683b90a8fe129899c5be61d2259edff3e040..ffd6574d78128055c74af9fbeab212bc124c4ef1 100644 (file)
 #include "BDR_editcurve.h"
 #include "BDR_editobject.h"
 
-#include "BPI_script.h"
 #include "BSE_filesel.h"
 #include "BSE_view.h"
 
index de76a30ecdd51491c21078b9d8b56bb38d276548..ba43ae2e7e5fd4cec5abde08f1707579787df510 100644 (file)
@@ -60,7 +60,6 @@
 #include "BKE_sca.h"
 #include "BSE_filesel.h"
 
-#include "BPI_script.h"
 #include "BPY_extern.h"
 #include "BPY_menus.h"
 
index e3c594f4ea3c1056a32f56c04c39990225bcf37e..d2d3a3352b7ae870fe7bbd8f51f0ac60e33ed604 100644 (file)
 #include "BIF_writeimage.h"
 #include "BIF_butspace.h"
 
-#include "BPI_script.h"
-
 #include "BSE_edit.h"
 #include "BSE_filesel.h"
 #include "BSE_headerbuttons.h"
index 6b5c0e57da5fb210ba539baea60eaa2ef80eae5d..af6cfa8de9fe76da0582a842168765f83ede9565 100644 (file)
@@ -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();
                        }