Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCampbell Barton <ideasman42@gmail.com>2008-03-07 00:25:15 +0300
committerCampbell Barton <ideasman42@gmail.com>2008-03-07 00:25:15 +0300
commit08f306c81cb8015db59fe63ad1ef2c977751652e (patch)
tree94f656dbf98fba2bf3d47547d34c06131d0a88cf /source/blender/python
parent3796abf092c9de4d43131dbce05102b126e14896 (diff)
Made python scripts save and load in the blend file so you can have the same scripts running when you open a blend file.
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.
Diffstat (limited to 'source/blender/python')
-rw-r--r--source/blender/python/BPY_extern.h4
-rw-r--r--source/blender/python/BPY_interface.c330
-rw-r--r--source/blender/python/api2_2x/Blender.c2
-rw-r--r--source/blender/python/api2_2x/Draw.c4
-rw-r--r--source/blender/python/api2_2x/Window.c1
5 files changed, 208 insertions, 133 deletions
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"