2003-02-26 19:22:24 +00:00
|
|
|
/*
|
2004-10-07 19:25:40 +00:00
|
|
|
* $Id$
|
2003-02-26 19:22:24 +00:00
|
|
|
*
|
|
|
|
* ***** 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,
|
2004-04-25 14:43:21 +00:00
|
|
|
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
2003-02-26 19:22:24 +00:00
|
|
|
*
|
|
|
|
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This is a new part of Blender.
|
|
|
|
*
|
BPython:
- tentative fix for scripts with CR/LF endings and split lines:
in 2.32, the ac3d and vrml2 exporters, for example, had lines
split with '\\\\' and so gave syntax errors when executed on Win
platforms, because the scripts bundled with Win binaries had dos
line endings.
- Chris Keith has written code to execute Python scripts from the
command-line, with '-P ' switch: "blender -P filename":
a Blender.Quit function was also added, so Blender can quit after
running the script (end the script with Blender.Quit()), but there's
still work to be done in this part, including adding more functions,
to load / save .blend files and to run scripts. More testing and
discussions are necessary.
Thanks Chris, for both your contributions and your patience, since I
wasn't available to check / commit this for a while.
2004-03-31 04:18:39 +00:00
|
|
|
* Contributor(s): Michel Selten, Willian P. Germano, Stephen Swaney,
|
|
|
|
* Chris Keith
|
2003-02-26 19:22:24 +00:00
|
|
|
*
|
|
|
|
* ***** END GPL/BL DUAL LICENSE BLOCK *****
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
2003-05-13 01:54:28 +00:00
|
|
|
|
2003-02-26 19:22:24 +00:00
|
|
|
#include <Python.h>
|
2004-10-07 19:25:40 +00:00
|
|
|
#include "compile.h" /* for the PyCodeObject */
|
|
|
|
#include "eval.h" /* for PyEval_EvalCode */
|
2003-05-13 01:54:28 +00:00
|
|
|
|
2003-02-26 19:22:24 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2003-03-18 20:21:26 +00:00
|
|
|
#include <MEM_guardedalloc.h>
|
2004-10-07 19:25:40 +00:00
|
|
|
#include <BLI_blenlib.h> /* for BLI_last_slash() */
|
2003-06-28 07:38:21 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
#include <BIF_interface.h> /* for pupmenu() */
|
2003-12-15 18:18:09 +00:00
|
|
|
#include <BIF_space.h>
|
|
|
|
#include <BIF_screen.h>
|
2004-01-15 03:46:47 +00:00
|
|
|
#include <BIF_toolbox.h>
|
2003-03-19 18:57:23 +00:00
|
|
|
#include <BKE_global.h>
|
2003-12-14 01:18:09 +00:00
|
|
|
#include <BKE_library.h>
|
2003-03-19 18:57:23 +00:00
|
|
|
#include <BKE_main.h>
|
2004-10-07 19:25:40 +00:00
|
|
|
#include <BKE_object.h> /* during_scriptlink() */
|
2003-02-26 19:22:24 +00:00
|
|
|
#include <BKE_text.h>
|
2004-01-14 21:36:10 +00:00
|
|
|
#include <BKE_utildefines.h>
|
2004-01-21 04:38:03 +00:00
|
|
|
#include <BPI_script.h>
|
|
|
|
|
2003-03-19 18:57:23 +00:00
|
|
|
#include <DNA_camera_types.h>
|
2003-02-26 19:22:24 +00:00
|
|
|
#include <DNA_ID.h>
|
2003-03-19 18:57:23 +00:00
|
|
|
#include <DNA_lamp_types.h>
|
|
|
|
#include <DNA_material_types.h>
|
|
|
|
#include <DNA_object_types.h>
|
|
|
|
#include <DNA_scene_types.h>
|
2003-12-14 01:18:09 +00:00
|
|
|
#include <DNA_screen_types.h>
|
2003-03-08 18:10:20 +00:00
|
|
|
#include <DNA_scriptlink_types.h>
|
2003-02-26 19:22:24 +00:00
|
|
|
#include <DNA_space_types.h>
|
|
|
|
#include <DNA_text_types.h>
|
2003-03-19 18:57:23 +00:00
|
|
|
#include <DNA_world_types.h>
|
2004-10-07 19:25:40 +00:00
|
|
|
#include <DNA_userdef_types.h> /* for U.pythondir */
|
2003-06-28 07:38:21 +00:00
|
|
|
|
2003-03-19 18:57:23 +00:00
|
|
|
#include "BPY_extern.h"
|
2004-01-14 21:36:10 +00:00
|
|
|
#include "BPY_menus.h"
|
2003-05-08 03:13:17 +00:00
|
|
|
#include "api2_2x/EXPP_interface.h"
|
2003-12-14 01:18:09 +00:00
|
|
|
#include "api2_2x/constant.h"
|
2004-10-07 19:25:40 +00:00
|
|
|
|
2004-05-04 14:27:41 +00:00
|
|
|
#include "api2_2x/modules.h"
|
2003-02-26 19:22:24 +00:00
|
|
|
|
2003-09-03 04:13:08 +00:00
|
|
|
/* bpy_registryDict is declared in api2_2x/Registry.h and defined
|
2004-10-07 19:25:40 +00:00
|
|
|
* in api2_2x/Registry.c
|
|
|
|
* This Python dictionary will be used to store data that scripts
|
2003-09-03 04:13:08 +00:00
|
|
|
* choose to preserve after they are executed, so user changes can be
|
2004-10-07 19:25:40 +00:00
|
|
|
* restored next time the script is used. Check the Blender.Registry module.
|
|
|
|
*/
|
|
|
|
#include "api2_2x/Registry.h"
|
2003-05-13 01:54:28 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
/*************************************************************************
|
|
|
|
* Structure definitions
|
|
|
|
**************************************************************************/
|
2003-02-26 19:22:24 +00:00
|
|
|
#define FILENAME_LENGTH 24
|
|
|
|
typedef struct _ScriptError {
|
2004-04-25 14:43:21 +00:00
|
|
|
char filename[FILENAME_LENGTH];
|
|
|
|
int lineno;
|
2003-02-26 19:22:24 +00:00
|
|
|
} ScriptError;
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
/****************************************************************************
|
|
|
|
* Global variables
|
|
|
|
****************************************************************************/
|
2003-02-26 19:22:24 +00:00
|
|
|
ScriptError g_script_error;
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* Function prototypes
|
|
|
|
***************************************************************************/
|
|
|
|
PyObject *RunPython( Text * text, PyObject * globaldict );
|
|
|
|
char *GetName( Text * text );
|
|
|
|
PyObject *CreateGlobalDictionary( void );
|
|
|
|
void ReleaseGlobalDictionary( PyObject * dict );
|
|
|
|
void DoAllScriptsFromList( ListBase * list, short event );
|
|
|
|
PyObject *importText( char *name );
|
|
|
|
void init_ourImport( void );
|
|
|
|
PyObject *blender_import( PyObject * self, PyObject * args );
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Description: This function will initialise Python and all the implemented
|
|
|
|
* api variations.
|
|
|
|
* Notes: Currently only the api for 2.2x will be initialised.
|
|
|
|
****************************************************************************/
|
|
|
|
void BPY_start_python( void )
|
2003-02-26 19:22:24 +00:00
|
|
|
{
|
2004-10-07 19:25:40 +00:00
|
|
|
bpy_registryDict = PyDict_New( ); /* check comment at start of this file */
|
2003-09-03 04:13:08 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
if( !bpy_registryDict )
|
|
|
|
printf( "Error: Couldn't create the Registry Python Dictionary!" );
|
2003-09-03 04:13:08 +00:00
|
|
|
|
2003-02-26 19:22:24 +00:00
|
|
|
/* TODO: Shouldn't "blender" be replaced by PACKAGE ?? (config.h) */
|
2004-10-07 19:25:40 +00:00
|
|
|
Py_SetProgramName( "blender" );
|
2003-02-26 19:22:24 +00:00
|
|
|
|
2004-10-01 05:28:14 +00:00
|
|
|
/*
|
|
|
|
* Py_Initialize() will attempt to import the site module and
|
|
|
|
* print an error if not found. See init_syspath() for the
|
|
|
|
* rest of our init msgs.
|
|
|
|
*/
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
printf( "Checking for Python install...\n" );
|
2004-10-01 05:28:14 +00:00
|
|
|
fflush( stdout );
|
2003-02-26 19:22:24 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
Py_Initialize( );
|
|
|
|
|
|
|
|
init_ourImport( );
|
2003-07-03 01:42:00 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
initBlenderApi2_2x( );
|
2003-02-26 19:22:24 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
init_syspath( );
|
2003-06-28 07:38:21 +00:00
|
|
|
|
2004-04-25 14:43:21 +00:00
|
|
|
return;
|
2003-02-26 19:22:24 +00:00
|
|
|
}
|
|
|
|
|
2003-03-08 18:10:20 +00:00
|
|
|
/*****************************************************************************/
|
2004-10-07 19:25:40 +00:00
|
|
|
/* Description: This function will terminate the Python interpreter */
|
2003-03-08 18:10:20 +00:00
|
|
|
/*****************************************************************************/
|
2004-10-07 19:25:40 +00:00
|
|
|
void BPY_end_python( void )
|
2003-02-26 19:22:24 +00:00
|
|
|
{
|
2004-10-07 19:25:40 +00:00
|
|
|
if( bpy_registryDict ) {
|
|
|
|
Py_DECREF( bpy_registryDict );
|
2004-04-25 14:43:21 +00:00
|
|
|
bpy_registryDict = NULL;
|
|
|
|
}
|
2003-09-03 04:13:08 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
Py_Finalize( );
|
2004-01-14 21:36:10 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
BPyMenu_RemoveAllEntries( ); /* freeing bpymenu mem */
|
2004-01-14 21:36:10 +00:00
|
|
|
|
2004-04-25 14:43:21 +00:00
|
|
|
/* a script might've opened a .blend file but didn't close it, so: */
|
2004-10-07 19:25:40 +00:00
|
|
|
EXPP_Library_Close( );
|
2004-04-25 14:43:21 +00:00
|
|
|
|
|
|
|
return;
|
2003-02-26 19:22:24 +00:00
|
|
|
}
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
void syspath_append( char *dirname )
|
2003-06-28 07:38:21 +00:00
|
|
|
{
|
2004-04-25 14:43:21 +00:00
|
|
|
PyObject *mod_sys, *dict, *path, *dir;
|
2003-06-28 07:38:21 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
PyErr_Clear( );
|
2003-06-28 07:38:21 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
dir = Py_BuildValue( "s", dirname );
|
2004-01-23 19:24:45 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
mod_sys = PyImport_ImportModule( "sys" ); /* new ref */
|
|
|
|
dict = PyModule_GetDict( mod_sys ); /* borrowed ref */
|
|
|
|
path = PyDict_GetItemString( dict, "path" ); /* borrowed ref */
|
2003-06-28 07:38:21 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
if( !PyList_Check( path ) )
|
|
|
|
return;
|
2003-06-28 07:38:21 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
PyList_Append( path, dir );
|
2003-06-28 07:38:21 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
if( PyErr_Occurred( ) )
|
|
|
|
Py_FatalError( "could not build sys.path" );
|
2003-06-28 07:38:21 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
Py_DECREF( mod_sys );
|
2003-06-28 07:38:21 +00:00
|
|
|
}
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
void init_syspath( void )
|
2003-06-28 07:38:21 +00:00
|
|
|
{
|
2004-04-25 14:43:21 +00:00
|
|
|
PyObject *path;
|
|
|
|
PyObject *mod, *d;
|
|
|
|
PyObject *p;
|
|
|
|
char *c, *progname;
|
2004-10-07 19:25:40 +00:00
|
|
|
char execdir[FILE_MAXDIR + FILE_MAXFILE]; /*defines from DNA_space_types.h */
|
2003-06-28 07:38:21 +00:00
|
|
|
|
2004-04-25 14:43:21 +00:00
|
|
|
int n;
|
2003-06-28 07:38:21 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
path = Py_BuildValue( "s", bprogname );
|
2003-06-28 07:38:21 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
mod = PyImport_ImportModule( "Blender.sys" );
|
2003-06-28 07:38:21 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
if( mod ) {
|
|
|
|
d = PyModule_GetDict( mod );
|
|
|
|
PyDict_SetItemString( d, "progname", path );
|
|
|
|
Py_DECREF( mod );
|
|
|
|
} else
|
|
|
|
printf( "Warning: could not set Blender.sys.progname\n" );
|
2003-06-28 07:38:21 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
progname = BLI_last_slash( bprogname ); /* looks for the last dir separator */
|
2003-06-28 07:38:21 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
c = Py_GetPath( ); /* get python system path */
|
|
|
|
PySys_SetPath( c ); /* initialize */
|
2003-06-28 07:38:21 +00:00
|
|
|
|
2004-04-25 14:43:21 +00:00
|
|
|
n = progname - bprogname;
|
2004-10-07 19:25:40 +00:00
|
|
|
if( n > 0 ) {
|
|
|
|
strncpy( execdir, bprogname, n );
|
|
|
|
if( execdir[n - 1] == '.' )
|
|
|
|
n--; /*fix for when run as ./blender */
|
2004-04-25 14:43:21 +00:00
|
|
|
execdir[n] = '\0';
|
2003-06-28 07:38:21 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
syspath_append( execdir ); /* append to module search path */
|
2003-06-28 07:38:21 +00:00
|
|
|
|
2004-04-25 14:43:21 +00:00
|
|
|
/* set Blender.sys.progname */
|
2004-10-07 19:25:40 +00:00
|
|
|
} else
|
|
|
|
printf( "Warning: could not determine argv[0] path\n" );
|
2003-06-28 07:38:21 +00:00
|
|
|
|
2004-04-25 14:43:21 +00:00
|
|
|
/*
|
|
|
|
* bring in the site module so we can add
|
|
|
|
* site-package dirs to sys.path
|
|
|
|
*/
|
2003-10-19 06:26:32 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
mod = PyImport_ImportModule( "site" ); /* new ref */
|
2003-09-18 00:54:43 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
if( mod ) {
|
|
|
|
PyObject *item;
|
2004-04-25 14:43:21 +00:00
|
|
|
int size = 0;
|
|
|
|
int index;
|
2003-09-18 00:54:43 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
printf( "Installed Python found!\n" );
|
2004-10-01 05:28:14 +00:00
|
|
|
|
2004-04-25 14:43:21 +00:00
|
|
|
/* get the value of 'sitedirs' from the module */
|
2003-10-19 06:26:32 +00:00
|
|
|
|
2004-04-25 14:43:21 +00:00
|
|
|
/* the ref man says GetDict() never fails!!! */
|
2004-10-07 19:25:40 +00:00
|
|
|
d = PyModule_GetDict( mod ); /* borrowed ref */
|
|
|
|
p = PyDict_GetItemString( d, "sitedirs" ); /* borrowed ref */
|
2003-09-18 00:54:43 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
if( p ) { /* we got our string */
|
2004-04-25 14:43:21 +00:00
|
|
|
/* append each item in sitedirs list to path */
|
2004-10-07 19:25:40 +00:00
|
|
|
size = PyList_Size( p );
|
2003-09-18 00:54:43 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
for( index = 0; index < size; index++ ) {
|
|
|
|
item = PySequence_GetItem( p, index ); /* new ref */
|
|
|
|
if( item )
|
|
|
|
syspath_append( PyString_AsString
|
|
|
|
( item ) );
|
2004-04-25 14:43:21 +00:00
|
|
|
}
|
|
|
|
}
|
2004-10-07 19:25:40 +00:00
|
|
|
Py_DECREF( mod );
|
|
|
|
} else { /* import 'site' failed */
|
|
|
|
PyErr_Clear( );
|
|
|
|
printf( "No installed Python found.\n" );
|
|
|
|
printf( "Only built-in modules are available. Some scripts may not run.\n" );
|
|
|
|
printf( "Continuing happily.\n" );
|
2004-04-25 14:43:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* initialize the sys module
|
|
|
|
* set sys.executable to the Blender exe
|
|
|
|
* set argv[0] to the Blender exe
|
|
|
|
*/
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
mod = PyImport_ImportModule( "sys" ); /* new ref */
|
2004-04-25 14:43:21 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
if( mod ) {
|
|
|
|
d = PyModule_GetDict( mod ); /* borrowed ref */
|
|
|
|
PyDict_SetItemString( d, "executable",
|
|
|
|
Py_BuildValue( "s", bprogname ) );
|
2004-04-25 14:43:21 +00:00
|
|
|
/* in the future this can be extended to have more argv's if needed: */
|
2004-10-07 19:25:40 +00:00
|
|
|
PyDict_SetItemString( d, "argv",
|
|
|
|
Py_BuildValue( "[s]", bprogname ) );
|
|
|
|
Py_DECREF( mod );
|
2004-04-25 14:43:21 +00:00
|
|
|
}
|
2003-06-28 07:38:21 +00:00
|
|
|
}
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
/****************************************************************************
|
|
|
|
* Description: This function finishes Python initialization in Blender.
|
|
|
|
|
|
|
|
Because U.pythondir (user defined dir for scripts) isn't
|
|
|
|
initialized when BPY_start_Python needs to be executed, we
|
|
|
|
postpone adding U.pythondir to sys.path and also BPyMenus
|
|
|
|
(mechanism to register scripts in Blender menus) for when
|
|
|
|
that dir info is available.
|
|
|
|
****************************************************************************/
|
|
|
|
void BPY_post_start_python( void )
|
2003-06-28 10:35:14 +00:00
|
|
|
{
|
2004-06-10 03:27:46 +00:00
|
|
|
PyObject *result, *dict;
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
if( U.pythondir && U.pythondir[0] != '\0' )
|
|
|
|
syspath_append( U.pythondir ); /* append to module search path */
|
2004-01-23 19:24:45 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
BPyMenu_Init( 0 ); /* get dynamic menus (registered scripts) data */
|
2004-06-10 03:27:46 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
dict = PyDict_New( );
|
2004-06-10 03:27:46 +00:00
|
|
|
|
|
|
|
/* here we check if the user has (some of) the expected modules */
|
2004-10-07 19:25:40 +00:00
|
|
|
if( dict ) {
|
2004-06-10 03:27:46 +00:00
|
|
|
char *s = "import chunk, gzip, math, os, struct, string";
|
2004-10-07 19:25:40 +00:00
|
|
|
result = PyRun_String( s, Py_eval_input, dict, dict );
|
|
|
|
if( !result ) {
|
|
|
|
PyErr_Clear( );
|
2004-06-10 03:27:46 +00:00
|
|
|
/*XXX print msg about this, point to readme.html */
|
2004-10-07 19:25:40 +00:00
|
|
|
} else
|
|
|
|
Py_DECREF( result );
|
|
|
|
Py_DECREF( dict );
|
2004-06-10 03:27:46 +00:00
|
|
|
}
|
2003-06-28 10:35:14 +00:00
|
|
|
}
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
/****************************************************************************
|
|
|
|
* Description: This function will return the linenumber on which an error
|
|
|
|
* has occurred in the Python script.
|
|
|
|
****************************************************************************/
|
|
|
|
int BPY_Err_getLinenumber( void )
|
2003-02-26 19:22:24 +00:00
|
|
|
{
|
2004-04-25 14:43:21 +00:00
|
|
|
return g_script_error.lineno;
|
2003-02-26 19:22:24 +00:00
|
|
|
}
|
|
|
|
|
2003-03-08 18:10:20 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
/* Description: This function will return the filename of the python script. */
|
|
|
|
/*****************************************************************************/
|
2004-10-07 19:25:40 +00:00
|
|
|
const char *BPY_Err_getFilename( void )
|
2003-02-26 19:22:24 +00:00
|
|
|
{
|
2004-04-25 14:43:21 +00:00
|
|
|
return g_script_error.filename;
|
2003-05-13 01:54:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2004-10-07 19:25:40 +00:00
|
|
|
/* Description: Return PyString filename from a traceback object */
|
2003-05-13 01:54:28 +00:00
|
|
|
/*****************************************************************************/
|
2004-10-07 19:25:40 +00:00
|
|
|
PyObject *traceback_getFilename( PyObject * tb )
|
2003-05-13 01:54:28 +00:00
|
|
|
{
|
2004-04-25 14:43:21 +00:00
|
|
|
PyObject *v;
|
2003-05-13 01:54:28 +00:00
|
|
|
|
|
|
|
/* co_filename is in f_code, which is in tb_frame, which is in tb */
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
v = PyObject_GetAttrString( tb, "tb_frame" );
|
|
|
|
Py_XDECREF( v );
|
|
|
|
v = PyObject_GetAttrString( v, "f_code" );
|
|
|
|
Py_XDECREF( v );
|
|
|
|
v = PyObject_GetAttrString( v, "co_filename" );
|
2003-05-13 01:54:28 +00:00
|
|
|
|
2004-04-25 14:43:21 +00:00
|
|
|
return v;
|
2003-05-13 01:54:28 +00:00
|
|
|
}
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
/****************************************************************************
|
|
|
|
* Description: Blender Python error handler. This catches the error and
|
|
|
|
* stores filename and line number in a global
|
|
|
|
*****************************************************************************/
|
|
|
|
void BPY_Err_Handle( char *script_name )
|
2003-05-13 01:54:28 +00:00
|
|
|
{
|
2004-04-25 14:43:21 +00:00
|
|
|
PyObject *exception, *err, *tb, *v;
|
2003-05-13 01:54:28 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
if( !script_name ) {
|
|
|
|
printf( "Error: script has NULL name\n" );
|
2004-01-14 21:36:10 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
PyErr_Fetch( &exception, &err, &tb );
|
2004-04-25 14:43:21 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
if( !exception && !tb ) {
|
|
|
|
printf( "FATAL: spurious exception\n" );
|
2004-04-25 14:43:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
strcpy( g_script_error.filename, script_name );
|
2004-04-25 14:43:21 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
if( exception
|
|
|
|
&& PyErr_GivenExceptionMatches( exception, PyExc_SyntaxError ) ) {
|
2004-04-25 14:43:21 +00:00
|
|
|
/* no traceback available when SyntaxError */
|
2004-10-07 19:25:40 +00:00
|
|
|
PyErr_Restore( exception, err, tb ); /* takes away reference! */
|
|
|
|
PyErr_Print( );
|
|
|
|
v = PyObject_GetAttrString( err, "lineno" );
|
|
|
|
if( v ) {
|
|
|
|
g_script_error.lineno = PyInt_AsLong( v );
|
|
|
|
Py_DECREF( v );
|
2004-05-05 03:22:22 +00:00
|
|
|
} else {
|
|
|
|
g_script_error.lineno = -1;
|
|
|
|
}
|
2004-04-25 14:43:21 +00:00
|
|
|
/* this avoids an abort in Python 2.3's garbage collecting: */
|
2004-10-07 19:25:40 +00:00
|
|
|
PyErr_Clear( );
|
2004-04-25 14:43:21 +00:00
|
|
|
return;
|
|
|
|
} else {
|
2004-10-07 19:25:40 +00:00
|
|
|
PyErr_NormalizeException( &exception, &err, &tb );
|
|
|
|
PyErr_Restore( exception, err, tb ); /* takes away reference! */
|
|
|
|
PyErr_Print( );
|
|
|
|
tb = PySys_GetObject( "last_traceback" );
|
2004-04-25 14:43:21 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
if( !tb ) {
|
|
|
|
printf( "\nCan't get traceback\n" );
|
2004-04-25 14:43:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
Py_INCREF( tb );
|
2003-05-13 01:54:28 +00:00
|
|
|
|
|
|
|
/* From old bpython BPY_main.c:
|
|
|
|
* 'check traceback objects and look for last traceback in the
|
2004-04-25 14:43:21 +00:00
|
|
|
* same text file. This is used to jump to the line of where the
|
|
|
|
* error occured. "If the error occured in another text file or module,
|
2004-10-07 19:25:40 +00:00
|
|
|
* the last frame in the current file is adressed."'
|
|
|
|
*/
|
2004-04-25 14:43:21 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
while( 1 ) {
|
|
|
|
v = PyObject_GetAttrString( tb, "tb_next" );
|
2004-04-25 14:43:21 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
if( v == Py_None
|
|
|
|
||
|
|
|
|
strcmp( PyString_AsString
|
|
|
|
( traceback_getFilename( v ) ),
|
|
|
|
script_name ) ) {
|
2004-04-25 14:43:21 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
Py_DECREF( tb );
|
2004-04-25 14:43:21 +00:00
|
|
|
tb = v;
|
|
|
|
}
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
v = PyObject_GetAttrString( tb, "tb_lineno" );
|
|
|
|
g_script_error.lineno = PyInt_AsLong( v );
|
|
|
|
Py_XDECREF( v );
|
|
|
|
v = traceback_getFilename( tb );
|
|
|
|
strncpy( g_script_error.filename, PyString_AsString( v ),
|
|
|
|
FILENAME_LENGTH );
|
|
|
|
Py_XDECREF( v );
|
|
|
|
Py_DECREF( tb );
|
2004-04-25 14:43:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
2003-02-26 19:22:24 +00:00
|
|
|
}
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
/****************************************************************************
|
|
|
|
* Description: This function executes the script passed by st.
|
|
|
|
* Notes: It is called by blender/src/drawtext.c when a Blender user
|
|
|
|
* presses ALT+PKEY in the script's text window.
|
|
|
|
*****************************************************************************/
|
|
|
|
int BPY_txt_do_python_Text( struct Text *text )
|
2003-02-26 19:22:24 +00:00
|
|
|
{
|
2004-04-25 14:43:21 +00:00
|
|
|
PyObject *py_dict, *py_result;
|
2004-01-14 21:36:10 +00:00
|
|
|
BPy_constant *info;
|
New scripts:
- hotkeys, obdatacopier and renameobjectbyblock, all from Jean-Michel Soler (jms);
- bevel_center by Loic Berthe, suggested for inclusion by jms;
- doc_browser, by Daniel Dunbar (Zr)
Thanks to them for the new contributions!
(I included doc_browser at 'Misc' because only users interested in script writing would actually use it, but it could also be under 'Help'. Opinions?)
BPython related:
- Added scriptlink methods to object, lamp, camera and world.
- Object: added object.makeTrack and object.clearTrack (old track method).
- sys: made sys.exists(path) return 0 for not found; 1 for file, 2 for dir and -1 for neither.
- doc updates and fixes.
- made ONLOAD event work. G.f's SCENESCRIPT bit was being zeroed in set_app_data.
- Blender: updated functions Load and Save to support the builtin importers and exporters besides .blend (dxf, videoscape, vrml 1.0, stl, ...)
- Draw: added mouse wheel events.
- Scene: added scene.play to play back animations (like ALT+A and SHIFT+ALT+A). Makes a good counter, too, when the 'win' attribute is set to a space that doesn't "animate".
The scene.play() addition and the fix to ONLOAD scriptlinks is part of the work for a Blender demo mode. It already works, but I'll still add support for Radiosity calculations and fix a thing in main(): it executes onload scripts too early (BIF_Init), giving funny results in alt+a animations and renderings when firing up Blender. Loading after the program is up has no such problems. When I finish I'll post examples of demo mode scripts.
2004-07-03 05:17:04 +00:00
|
|
|
char textname[24];
|
2003-12-14 01:18:09 +00:00
|
|
|
Script *script = G.main->script.first;
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
if( !text )
|
|
|
|
return 0;
|
2003-12-14 01:18:09 +00:00
|
|
|
|
|
|
|
/* check if this text is already running */
|
2004-10-07 19:25:40 +00:00
|
|
|
while( script ) {
|
|
|
|
if( !strcmp( script->id.name + 2, text->id.name + 2 ) ) {
|
|
|
|
/* if this text is already a running script,
|
|
|
|
* just move to it: */
|
2003-12-15 18:18:09 +00:00
|
|
|
SpaceScript *sc;
|
2004-10-07 19:25:40 +00:00
|
|
|
newspace( curarea, SPACE_SCRIPT );
|
2003-12-15 18:18:09 +00:00
|
|
|
sc = curarea->spacedata.first;
|
|
|
|
sc->script = script;
|
2003-12-14 01:18:09 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
script = script->id.next;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a new script structure and initialize it: */
|
2004-10-07 19:25:40 +00:00
|
|
|
script = alloc_libblock( &G.main->script, ID_SCRIPT, GetName( text ) );
|
2003-12-14 01:18:09 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
if( !script ) {
|
|
|
|
printf( "couldn't allocate memory for Script struct!" );
|
2003-12-14 01:18:09 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
New scripts:
- hotkeys, obdatacopier and renameobjectbyblock, all from Jean-Michel Soler (jms);
- bevel_center by Loic Berthe, suggested for inclusion by jms;
- doc_browser, by Daniel Dunbar (Zr)
Thanks to them for the new contributions!
(I included doc_browser at 'Misc' because only users interested in script writing would actually use it, but it could also be under 'Help'. Opinions?)
BPython related:
- Added scriptlink methods to object, lamp, camera and world.
- Object: added object.makeTrack and object.clearTrack (old track method).
- sys: made sys.exists(path) return 0 for not found; 1 for file, 2 for dir and -1 for neither.
- doc updates and fixes.
- made ONLOAD event work. G.f's SCENESCRIPT bit was being zeroed in set_app_data.
- Blender: updated functions Load and Save to support the builtin importers and exporters besides .blend (dxf, videoscape, vrml 1.0, stl, ...)
- Draw: added mouse wheel events.
- Scene: added scene.play to play back animations (like ALT+A and SHIFT+ALT+A). Makes a good counter, too, when the 'win' attribute is set to a space that doesn't "animate".
The scene.play() addition and the fix to ONLOAD scriptlinks is part of the work for a Blender demo mode. It already works, but I'll still add support for Radiosity calculations and fix a thing in main(): it executes onload scripts too early (BIF_Init), giving funny results in alt+a animations and renderings when firing up Blender. Loading after the program is up has no such problems. When I finish I'll post examples of demo mode scripts.
2004-07-03 05:17:04 +00:00
|
|
|
/* if in the script Blender.Load(blendfile) is not the last command,
|
|
|
|
* an error after it will call BPY_Err_Handle below, but the text struct
|
2004-10-07 19:25:40 +00:00
|
|
|
* will have been deallocated already, so we need to copy its name here.
|
|
|
|
*/
|
|
|
|
BLI_strncpy( textname, GetName( text ),
|
|
|
|
strlen( GetName( text ) ) + 1 );
|
New scripts:
- hotkeys, obdatacopier and renameobjectbyblock, all from Jean-Michel Soler (jms);
- bevel_center by Loic Berthe, suggested for inclusion by jms;
- doc_browser, by Daniel Dunbar (Zr)
Thanks to them for the new contributions!
(I included doc_browser at 'Misc' because only users interested in script writing would actually use it, but it could also be under 'Help'. Opinions?)
BPython related:
- Added scriptlink methods to object, lamp, camera and world.
- Object: added object.makeTrack and object.clearTrack (old track method).
- sys: made sys.exists(path) return 0 for not found; 1 for file, 2 for dir and -1 for neither.
- doc updates and fixes.
- made ONLOAD event work. G.f's SCENESCRIPT bit was being zeroed in set_app_data.
- Blender: updated functions Load and Save to support the builtin importers and exporters besides .blend (dxf, videoscape, vrml 1.0, stl, ...)
- Draw: added mouse wheel events.
- Scene: added scene.play to play back animations (like ALT+A and SHIFT+ALT+A). Makes a good counter, too, when the 'win' attribute is set to a space that doesn't "animate".
The scene.play() addition and the fix to ONLOAD scriptlinks is part of the work for a Blender demo mode. It already works, but I'll still add support for Radiosity calculations and fix a thing in main(): it executes onload scripts too early (BIF_Init), giving funny results in alt+a animations and renderings when firing up Blender. Loading after the program is up has no such problems. When I finish I'll post examples of demo mode scripts.
2004-07-03 05:17:04 +00:00
|
|
|
|
2003-12-14 01:18:09 +00:00
|
|
|
script->id.us = 1;
|
|
|
|
script->flags = SCRIPT_RUNNING;
|
|
|
|
script->py_draw = NULL;
|
|
|
|
script->py_event = NULL;
|
|
|
|
script->py_button = NULL;
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
py_dict = CreateGlobalDictionary( );
|
2003-12-14 01:18:09 +00:00
|
|
|
|
|
|
|
script->py_globaldict = py_dict;
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
info = ( BPy_constant * ) M_constant_New( );
|
|
|
|
if( info ) {
|
|
|
|
constant_insert( info, "name",
|
|
|
|
PyString_FromString( script->id.name + 2 ) );
|
|
|
|
Py_INCREF( Py_None );
|
|
|
|
constant_insert( info, "arg", Py_None );
|
|
|
|
PyDict_SetItemString( py_dict, "__script__",
|
|
|
|
( PyObject * ) info );
|
2003-12-14 01:18:09 +00:00
|
|
|
}
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
py_result = RunPython( text, py_dict ); /* Run the script */
|
2003-05-13 01:54:28 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
if( !py_result ) { /* Failed execution of the script */
|
2003-05-13 01:54:28 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
BPY_Err_Handle( textname );
|
|
|
|
ReleaseGlobalDictionary( py_dict );
|
2004-05-05 03:22:22 +00:00
|
|
|
script->py_globaldict = NULL;
|
2004-10-07 19:25:40 +00:00
|
|
|
if( G.main->script.first )
|
|
|
|
free_libblock( &G.main->script, script );
|
2004-01-14 21:36:10 +00:00
|
|
|
|
2004-04-25 14:43:21 +00:00
|
|
|
return 0;
|
2004-10-07 19:25:40 +00:00
|
|
|
} else {
|
|
|
|
Py_DECREF( py_result );
|
|
|
|
script->flags &= ~SCRIPT_RUNNING;
|
|
|
|
if( !script->flags ) {
|
|
|
|
ReleaseGlobalDictionary( py_dict );
|
2004-01-14 21:36:10 +00:00
|
|
|
script->py_globaldict = NULL;
|
2004-10-07 19:25:40 +00:00
|
|
|
free_libblock( &G.main->script, script );
|
2004-01-14 21:36:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
return 1; /* normal return */
|
2004-01-14 21:36:10 +00:00
|
|
|
}
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
/****************************************************************************
|
|
|
|
* Description: The original function of this name has been refactored
|
|
|
|
* into BPY_txt_do_python_Text. That version is needed for the command
|
|
|
|
* line support for Python. This is here to keep the interface the
|
|
|
|
* same and reduce code changes elsewhere.
|
|
|
|
****************************************************************************/
|
|
|
|
int BPY_txt_do_python( struct SpaceText *st )
|
BPython:
- tentative fix for scripts with CR/LF endings and split lines:
in 2.32, the ac3d and vrml2 exporters, for example, had lines
split with '\\\\' and so gave syntax errors when executed on Win
platforms, because the scripts bundled with Win binaries had dos
line endings.
- Chris Keith has written code to execute Python scripts from the
command-line, with '-P ' switch: "blender -P filename":
a Blender.Quit function was also added, so Blender can quit after
running the script (end the script with Blender.Quit()), but there's
still work to be done in this part, including adding more functions,
to load / save .blend files and to run scripts. More testing and
discussions are necessary.
Thanks Chris, for both your contributions and your patience, since I
wasn't available to check / commit this for a while.
2004-03-31 04:18:39 +00:00
|
|
|
{
|
2004-10-07 19:25:40 +00:00
|
|
|
return BPY_txt_do_python_Text( st->text );
|
BPython:
- tentative fix for scripts with CR/LF endings and split lines:
in 2.32, the ac3d and vrml2 exporters, for example, had lines
split with '\\\\' and so gave syntax errors when executed on Win
platforms, because the scripts bundled with Win binaries had dos
line endings.
- Chris Keith has written code to execute Python scripts from the
command-line, with '-P ' switch: "blender -P filename":
a Blender.Quit function was also added, so Blender can quit after
running the script (end the script with Blender.Quit()), but there's
still work to be done in this part, including adding more functions,
to load / save .blend files and to run scripts. More testing and
discussions are necessary.
Thanks Chris, for both your contributions and your patience, since I
wasn't available to check / commit this for a while.
2004-03-31 04:18:39 +00:00
|
|
|
}
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
/****************************************************************************
|
|
|
|
* Description: Called from command line to run a Python script
|
|
|
|
* automatically.
|
|
|
|
****************************************************************************/
|
|
|
|
void BPY_run_python_script( char *fn )
|
BPython:
- tentative fix for scripts with CR/LF endings and split lines:
in 2.32, the ac3d and vrml2 exporters, for example, had lines
split with '\\\\' and so gave syntax errors when executed on Win
platforms, because the scripts bundled with Win binaries had dos
line endings.
- Chris Keith has written code to execute Python scripts from the
command-line, with '-P ' switch: "blender -P filename":
a Blender.Quit function was also added, so Blender can quit after
running the script (end the script with Blender.Quit()), but there's
still work to be done in this part, including adding more functions,
to load / save .blend files and to run scripts. More testing and
discussions are necessary.
Thanks Chris, for both your contributions and your patience, since I
wasn't available to check / commit this for a while.
2004-03-31 04:18:39 +00:00
|
|
|
{
|
2004-10-07 19:25:40 +00:00
|
|
|
Text *text = NULL;
|
New scripts:
- hotkeys, obdatacopier and renameobjectbyblock, all from Jean-Michel Soler (jms);
- bevel_center by Loic Berthe, suggested for inclusion by jms;
- doc_browser, by Daniel Dunbar (Zr)
Thanks to them for the new contributions!
(I included doc_browser at 'Misc' because only users interested in script writing would actually use it, but it could also be under 'Help'. Opinions?)
BPython related:
- Added scriptlink methods to object, lamp, camera and world.
- Object: added object.makeTrack and object.clearTrack (old track method).
- sys: made sys.exists(path) return 0 for not found; 1 for file, 2 for dir and -1 for neither.
- doc updates and fixes.
- made ONLOAD event work. G.f's SCENESCRIPT bit was being zeroed in set_app_data.
- Blender: updated functions Load and Save to support the builtin importers and exporters besides .blend (dxf, videoscape, vrml 1.0, stl, ...)
- Draw: added mouse wheel events.
- Scene: added scene.play to play back animations (like ALT+A and SHIFT+ALT+A). Makes a good counter, too, when the 'win' attribute is set to a space that doesn't "animate".
The scene.play() addition and the fix to ONLOAD scriptlinks is part of the work for a Blender demo mode. It already works, but I'll still add support for Radiosity calculations and fix a thing in main(): it executes onload scripts too early (BIF_Init), giving funny results in alt+a animations and renderings when firing up Blender. Loading after the program is up has no such problems. When I finish I'll post examples of demo mode scripts.
2004-07-03 05:17:04 +00:00
|
|
|
int is_blenText = 0;
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
if( !BLI_exists( fn ) ) { /* if there's no such filename ... */
|
|
|
|
text = G.main->text.first; /* try an already existing Blender Text */
|
|
|
|
while( text ) {
|
|
|
|
if( !strcmp( fn, text->id.name + 2 ) )
|
|
|
|
break;
|
New scripts:
- hotkeys, obdatacopier and renameobjectbyblock, all from Jean-Michel Soler (jms);
- bevel_center by Loic Berthe, suggested for inclusion by jms;
- doc_browser, by Daniel Dunbar (Zr)
Thanks to them for the new contributions!
(I included doc_browser at 'Misc' because only users interested in script writing would actually use it, but it could also be under 'Help'. Opinions?)
BPython related:
- Added scriptlink methods to object, lamp, camera and world.
- Object: added object.makeTrack and object.clearTrack (old track method).
- sys: made sys.exists(path) return 0 for not found; 1 for file, 2 for dir and -1 for neither.
- doc updates and fixes.
- made ONLOAD event work. G.f's SCENESCRIPT bit was being zeroed in set_app_data.
- Blender: updated functions Load and Save to support the builtin importers and exporters besides .blend (dxf, videoscape, vrml 1.0, stl, ...)
- Draw: added mouse wheel events.
- Scene: added scene.play to play back animations (like ALT+A and SHIFT+ALT+A). Makes a good counter, too, when the 'win' attribute is set to a space that doesn't "animate".
The scene.play() addition and the fix to ONLOAD scriptlinks is part of the work for a Blender demo mode. It already works, but I'll still add support for Radiosity calculations and fix a thing in main(): it executes onload scripts too early (BIF_Init), giving funny results in alt+a animations and renderings when firing up Blender. Loading after the program is up has no such problems. When I finish I'll post examples of demo mode scripts.
2004-07-03 05:17:04 +00:00
|
|
|
text = text->id.next;
|
|
|
|
}
|
BPython:
- tentative fix for scripts with CR/LF endings and split lines:
in 2.32, the ac3d and vrml2 exporters, for example, had lines
split with '\\\\' and so gave syntax errors when executed on Win
platforms, because the scripts bundled with Win binaries had dos
line endings.
- Chris Keith has written code to execute Python scripts from the
command-line, with '-P ' switch: "blender -P filename":
a Blender.Quit function was also added, so Blender can quit after
running the script (end the script with Blender.Quit()), but there's
still work to be done in this part, including adding more functions,
to load / save .blend files and to run scripts. More testing and
discussions are necessary.
Thanks Chris, for both your contributions and your patience, since I
wasn't available to check / commit this for a while.
2004-03-31 04:18:39 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
if( !text ) {
|
|
|
|
printf( "\nError: no such file or Blender text -- %s.\n", fn );
|
New scripts:
- hotkeys, obdatacopier and renameobjectbyblock, all from Jean-Michel Soler (jms);
- bevel_center by Loic Berthe, suggested for inclusion by jms;
- doc_browser, by Daniel Dunbar (Zr)
Thanks to them for the new contributions!
(I included doc_browser at 'Misc' because only users interested in script writing would actually use it, but it could also be under 'Help'. Opinions?)
BPython related:
- Added scriptlink methods to object, lamp, camera and world.
- Object: added object.makeTrack and object.clearTrack (old track method).
- sys: made sys.exists(path) return 0 for not found; 1 for file, 2 for dir and -1 for neither.
- doc updates and fixes.
- made ONLOAD event work. G.f's SCENESCRIPT bit was being zeroed in set_app_data.
- Blender: updated functions Load and Save to support the builtin importers and exporters besides .blend (dxf, videoscape, vrml 1.0, stl, ...)
- Draw: added mouse wheel events.
- Scene: added scene.play to play back animations (like ALT+A and SHIFT+ALT+A). Makes a good counter, too, when the 'win' attribute is set to a space that doesn't "animate".
The scene.play() addition and the fix to ONLOAD scriptlinks is part of the work for a Blender demo mode. It already works, but I'll still add support for Radiosity calculations and fix a thing in main(): it executes onload scripts too early (BIF_Init), giving funny results in alt+a animations and renderings when firing up Blender. Loading after the program is up has no such problems. When I finish I'll post examples of demo mode scripts.
2004-07-03 05:17:04 +00:00
|
|
|
return;
|
2004-10-07 19:25:40 +00:00
|
|
|
} else
|
|
|
|
is_blenText = 1; /* fn is already a Blender Text */
|
BPython:
- tentative fix for scripts with CR/LF endings and split lines:
in 2.32, the ac3d and vrml2 exporters, for example, had lines
split with '\\\\' and so gave syntax errors when executed on Win
platforms, because the scripts bundled with Win binaries had dos
line endings.
- Chris Keith has written code to execute Python scripts from the
command-line, with '-P ' switch: "blender -P filename":
a Blender.Quit function was also added, so Blender can quit after
running the script (end the script with Blender.Quit()), but there's
still work to be done in this part, including adding more functions,
to load / save .blend files and to run scripts. More testing and
discussions are necessary.
Thanks Chris, for both your contributions and your patience, since I
wasn't available to check / commit this for a while.
2004-03-31 04:18:39 +00:00
|
|
|
}
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
if( !is_blenText )
|
|
|
|
text = add_text( fn );
|
|
|
|
if( text == NULL ) {
|
|
|
|
printf( "Error in BPY_run_python_script: couldn't create Blender text " "from %s\n", fn );
|
New scripts:
- hotkeys, obdatacopier and renameobjectbyblock, all from Jean-Michel Soler (jms);
- bevel_center by Loic Berthe, suggested for inclusion by jms;
- doc_browser, by Daniel Dunbar (Zr)
Thanks to them for the new contributions!
(I included doc_browser at 'Misc' because only users interested in script writing would actually use it, but it could also be under 'Help'. Opinions?)
BPython related:
- Added scriptlink methods to object, lamp, camera and world.
- Object: added object.makeTrack and object.clearTrack (old track method).
- sys: made sys.exists(path) return 0 for not found; 1 for file, 2 for dir and -1 for neither.
- doc updates and fixes.
- made ONLOAD event work. G.f's SCENESCRIPT bit was being zeroed in set_app_data.
- Blender: updated functions Load and Save to support the builtin importers and exporters besides .blend (dxf, videoscape, vrml 1.0, stl, ...)
- Draw: added mouse wheel events.
- Scene: added scene.play to play back animations (like ALT+A and SHIFT+ALT+A). Makes a good counter, too, when the 'win' attribute is set to a space that doesn't "animate".
The scene.play() addition and the fix to ONLOAD scriptlinks is part of the work for a Blender demo mode. It already works, but I'll still add support for Radiosity calculations and fix a thing in main(): it executes onload scripts too early (BIF_Init), giving funny results in alt+a animations and renderings when firing up Blender. Loading after the program is up has no such problems. When I finish I'll post examples of demo mode scripts.
2004-07-03 05:17:04 +00:00
|
|
|
// Chris: On Windows if I continue I just get a segmentation
|
BPython:
- tentative fix for scripts with CR/LF endings and split lines:
in 2.32, the ac3d and vrml2 exporters, for example, had lines
split with '\\\\' and so gave syntax errors when executed on Win
platforms, because the scripts bundled with Win binaries had dos
line endings.
- Chris Keith has written code to execute Python scripts from the
command-line, with '-P ' switch: "blender -P filename":
a Blender.Quit function was also added, so Blender can quit after
running the script (end the script with Blender.Quit()), but there's
still work to be done in this part, including adding more functions,
to load / save .blend files and to run scripts. More testing and
discussions are necessary.
Thanks Chris, for both your contributions and your patience, since I
wasn't available to check / commit this for a while.
2004-03-31 04:18:39 +00:00
|
|
|
// violation. To get a baseline file I exit here.
|
2004-10-07 19:25:40 +00:00
|
|
|
exit( 2 );
|
2004-04-25 14:43:21 +00:00
|
|
|
}
|
BPython:
- tentative fix for scripts with CR/LF endings and split lines:
in 2.32, the ac3d and vrml2 exporters, for example, had lines
split with '\\\\' and so gave syntax errors when executed on Win
platforms, because the scripts bundled with Win binaries had dos
line endings.
- Chris Keith has written code to execute Python scripts from the
command-line, with '-P ' switch: "blender -P filename":
a Blender.Quit function was also added, so Blender can quit after
running the script (end the script with Blender.Quit()), but there's
still work to be done in this part, including adding more functions,
to load / save .blend files and to run scripts. More testing and
discussions are necessary.
Thanks Chris, for both your contributions and your patience, since I
wasn't available to check / commit this for a while.
2004-03-31 04:18:39 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
if( BPY_txt_do_python_Text( text ) != 1 ) {
|
New scripts:
- hotkeys, obdatacopier and renameobjectbyblock, all from Jean-Michel Soler (jms);
- bevel_center by Loic Berthe, suggested for inclusion by jms;
- doc_browser, by Daniel Dunbar (Zr)
Thanks to them for the new contributions!
(I included doc_browser at 'Misc' because only users interested in script writing would actually use it, but it could also be under 'Help'. Opinions?)
BPython related:
- Added scriptlink methods to object, lamp, camera and world.
- Object: added object.makeTrack and object.clearTrack (old track method).
- sys: made sys.exists(path) return 0 for not found; 1 for file, 2 for dir and -1 for neither.
- doc updates and fixes.
- made ONLOAD event work. G.f's SCENESCRIPT bit was being zeroed in set_app_data.
- Blender: updated functions Load and Save to support the builtin importers and exporters besides .blend (dxf, videoscape, vrml 1.0, stl, ...)
- Draw: added mouse wheel events.
- Scene: added scene.play to play back animations (like ALT+A and SHIFT+ALT+A). Makes a good counter, too, when the 'win' attribute is set to a space that doesn't "animate".
The scene.play() addition and the fix to ONLOAD scriptlinks is part of the work for a Blender demo mode. It already works, but I'll still add support for Radiosity calculations and fix a thing in main(): it executes onload scripts too early (BIF_Init), giving funny results in alt+a animations and renderings when firing up Blender. Loading after the program is up has no such problems. When I finish I'll post examples of demo mode scripts.
2004-07-03 05:17:04 +00:00
|
|
|
printf( "\nError executing Python script from command-line:\n"
|
2004-10-07 19:25:40 +00:00
|
|
|
"%s (at line %d).\n", fn, BPY_Err_getLinenumber( ) );
|
BPython:
- tentative fix for scripts with CR/LF endings and split lines:
in 2.32, the ac3d and vrml2 exporters, for example, had lines
split with '\\\\' and so gave syntax errors when executed on Win
platforms, because the scripts bundled with Win binaries had dos
line endings.
- Chris Keith has written code to execute Python scripts from the
command-line, with '-P ' switch: "blender -P filename":
a Blender.Quit function was also added, so Blender can quit after
running the script (end the script with Blender.Quit()), but there's
still work to be done in this part, including adding more functions,
to load / save .blend files and to run scripts. More testing and
discussions are necessary.
Thanks Chris, for both your contributions and your patience, since I
wasn't available to check / commit this for a while.
2004-03-31 04:18:39 +00:00
|
|
|
}
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
if( !is_blenText )
|
|
|
|
free_libblock( &G.main->text, text );
|
BPython:
- tentative fix for scripts with CR/LF endings and split lines:
in 2.32, the ac3d and vrml2 exporters, for example, had lines
split with '\\\\' and so gave syntax errors when executed on Win
platforms, because the scripts bundled with Win binaries had dos
line endings.
- Chris Keith has written code to execute Python scripts from the
command-line, with '-P ' switch: "blender -P filename":
a Blender.Quit function was also added, so Blender can quit after
running the script (end the script with Blender.Quit()), but there's
still work to be done in this part, including adding more functions,
to load / save .blend files and to run scripts. More testing and
discussions are necessary.
Thanks Chris, for both your contributions and your patience, since I
wasn't available to check / commit this for a while.
2004-03-31 04:18:39 +00:00
|
|
|
}
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
/****************************************************************************
|
|
|
|
* 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
|
|
|
|
* clicks on a menu entry that refers to a script.
|
|
|
|
* Scripts are searched in the BPyMenuTable, using the given
|
|
|
|
* menutype and event values to know which one was chosen.
|
|
|
|
*****************************************************************************/
|
|
|
|
int BPY_menu_do_python( short menutype, int event )
|
2004-01-14 21:36:10 +00:00
|
|
|
{
|
2004-04-25 14:43:21 +00:00
|
|
|
PyObject *py_dict, *py_res, *pyarg = NULL;
|
2004-01-14 21:36:10 +00:00
|
|
|
BPy_constant *info;
|
|
|
|
BPyMenu *pym;
|
|
|
|
BPySubMenu *pysm;
|
|
|
|
FILE *fp = NULL;
|
2004-01-27 09:30:29 +00:00
|
|
|
char *buffer, *s;
|
2004-10-07 19:25:40 +00:00
|
|
|
char filestr[FILE_MAXDIR + FILE_MAXFILE];
|
2004-01-20 04:57:47 +00:00
|
|
|
char dirname[FILE_MAXDIR];
|
2004-01-14 21:36:10 +00:00
|
|
|
Script *script = G.main->script.first;
|
2004-01-18 15:08:02 +00:00
|
|
|
int len;
|
2004-01-14 21:36:10 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
pym = BPyMenu_GetEntry( menutype, ( short ) event );
|
2004-01-14 21:36:10 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
if( !pym )
|
|
|
|
return 0;
|
2004-08-04 06:16:46 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
if( pym->version > G.version )
|
|
|
|
notice( "Version mismatch: script was written for Blender %d. "
|
|
|
|
"It may fail with yours: %d.", pym->version,
|
|
|
|
G.version );
|
2004-08-04 06:16:46 +00:00
|
|
|
|
2004-01-14 21:36:10 +00:00
|
|
|
/* if there are submenus, let the user choose one from a pupmenu that we
|
|
|
|
* create here.*/
|
|
|
|
pysm = pym->submenus;
|
2004-10-07 19:25:40 +00:00
|
|
|
if( pysm ) {
|
|
|
|
char *pupstr;
|
2004-01-14 21:36:10 +00:00
|
|
|
int arg;
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
pupstr = BPyMenu_CreatePupmenuStr( pym, menutype );
|
2004-01-14 21:36:10 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
if( pupstr ) {
|
|
|
|
arg = pupmenu( pupstr );
|
|
|
|
MEM_freeN( pupstr );
|
2004-01-14 21:36:10 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
if( arg >= 0 ) {
|
|
|
|
while( arg-- )
|
|
|
|
pysm = pysm->next;
|
|
|
|
pyarg = PyString_FromString( pysm->arg );
|
|
|
|
} else
|
|
|
|
return 0;
|
2004-01-14 21:36:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
if( !pyarg ) { /* no submenus */
|
|
|
|
Py_INCREF( Py_None );
|
2004-01-14 21:36:10 +00:00
|
|
|
pyarg = Py_None;
|
|
|
|
}
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
if( pym->dir ) /* script is in U.pythondir */
|
|
|
|
BLI_make_file_string( "/", filestr, U.pythondir,
|
|
|
|
pym->filename );
|
|
|
|
else { /* script is in ~/.blender/scripts/ */
|
|
|
|
BLI_make_file_string( "/", dirname, bpy_gethome( ),
|
|
|
|
"scripts" );
|
|
|
|
BLI_make_file_string( "/", filestr, dirname, pym->filename );
|
2004-01-20 04:57:47 +00:00
|
|
|
}
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
fp = fopen( filestr, "rb" );
|
|
|
|
if( !fp ) {
|
|
|
|
printf( "Error loading script: couldn't open file %s\n",
|
|
|
|
filestr );
|
2004-01-14 21:36:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a new script structure and initialize it: */
|
2004-10-07 19:25:40 +00:00
|
|
|
script = alloc_libblock( &G.main->script, ID_SCRIPT, pym->name );
|
2004-01-14 21:36:10 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
if( !script ) {
|
|
|
|
printf( "couldn't allocate memory for Script struct!" );
|
|
|
|
fclose( fp );
|
2004-01-14 21:36:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-06-10 03:27:46 +00:00
|
|
|
/* let's find a proper area for an eventual script gui:
|
2004-06-11 09:17:49 +00:00
|
|
|
* (still experimenting here, need definition on which win
|
|
|
|
* each group will be put to code this properly) */
|
2004-10-07 19:25:40 +00:00
|
|
|
switch ( menutype ) {
|
2004-06-10 03:27:46 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
case PYMENU_IMPORT: /* first 3 were handled in header_info.c */
|
|
|
|
case PYMENU_EXPORT:
|
|
|
|
case PYMENU_HELP:
|
|
|
|
case PYMENU_WIZARDS:
|
|
|
|
break;
|
2004-06-11 09:17:49 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
default:
|
|
|
|
if( curarea->spacetype != SPACE_SCRIPT ) {
|
|
|
|
ScrArea *sa = NULL;
|
2004-06-10 03:27:46 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
sa = find_biggest_area_of_type( SPACE_BUTS );
|
|
|
|
if( sa ) {
|
|
|
|
if( ( 1.5 * sa->winx ) < sa->winy )
|
|
|
|
sa = NULL; /* too narrow? */
|
|
|
|
}
|
2004-06-10 03:27:46 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
if( !sa )
|
|
|
|
sa = find_biggest_area_of_type( SPACE_SCRIPT );
|
|
|
|
if( !sa )
|
|
|
|
sa = find_biggest_area_of_type( SPACE_TEXT );
|
|
|
|
if( !sa )
|
|
|
|
sa = find_biggest_area_of_type( SPACE_IMAGE ); /* group UV */
|
|
|
|
if( !sa )
|
|
|
|
sa = find_biggest_area_of_type( SPACE_VIEW3D );
|
2004-06-10 03:27:46 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
if( !sa )
|
|
|
|
sa = find_biggest_area( );
|
2004-06-10 03:27:46 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
areawinset( sa->win );
|
|
|
|
}
|
|
|
|
break;
|
2004-06-10 03:27:46 +00:00
|
|
|
}
|
|
|
|
|
2004-01-14 21:36:10 +00:00
|
|
|
script->id.us = 1;
|
|
|
|
script->flags = SCRIPT_RUNNING;
|
|
|
|
script->py_draw = NULL;
|
|
|
|
script->py_event = NULL;
|
|
|
|
script->py_button = NULL;
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
py_dict = CreateGlobalDictionary( );
|
2004-01-14 21:36:10 +00:00
|
|
|
|
|
|
|
script->py_globaldict = py_dict;
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
info = ( BPy_constant * ) M_constant_New( );
|
|
|
|
if( info ) {
|
|
|
|
constant_insert( info, "name",
|
|
|
|
PyString_FromString( script->id.name + 2 ) );
|
|
|
|
constant_insert( info, "arg", pyarg );
|
|
|
|
PyDict_SetItemString( py_dict, "__script__",
|
|
|
|
( PyObject * ) info );
|
2004-01-14 21:36:10 +00:00
|
|
|
}
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
/* 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'.
|
2004-01-18 15:08:02 +00:00
|
|
|
* So now we load the script file data to a buffer */
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
fseek( fp, 0L, SEEK_END );
|
|
|
|
len = ftell( fp );
|
|
|
|
fseek( fp, 0L, SEEK_SET );
|
2004-01-18 15:08:02 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
buffer = MEM_mallocN( len + 2, "pyfilebuf" ); /* len+2 to add '\n\0' */
|
|
|
|
len = fread( buffer, 1, len, fp );
|
2004-01-18 15:08:02 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
buffer[len] = '\n'; /* fix syntax error in files w/o eol */
|
|
|
|
buffer[len + 1] = '\0';
|
2004-01-14 21:36:10 +00:00
|
|
|
|
BPython:
- tentative fix for scripts with CR/LF endings and split lines:
in 2.32, the ac3d and vrml2 exporters, for example, had lines
split with '\\\\' and so gave syntax errors when executed on Win
platforms, because the scripts bundled with Win binaries had dos
line endings.
- Chris Keith has written code to execute Python scripts from the
command-line, with '-P ' switch: "blender -P filename":
a Blender.Quit function was also added, so Blender can quit after
running the script (end the script with Blender.Quit()), but there's
still work to be done in this part, including adding more functions,
to load / save .blend files and to run scripts. More testing and
discussions are necessary.
Thanks Chris, for both your contributions and your patience, since I
wasn't available to check / commit this for a while.
2004-03-31 04:18:39 +00:00
|
|
|
/* 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 */
|
2004-10-07 19:25:40 +00:00
|
|
|
/**/
|
|
|
|
/* but first make sure we won't disturb memory below &buffer[0]: */
|
|
|
|
if( *buffer == '\r' )
|
|
|
|
*buffer = ' ';
|
BPython:
- tentative fix for scripts with CR/LF endings and split lines:
in 2.32, the ac3d and vrml2 exporters, for example, had lines
split with '\\\\' and so gave syntax errors when executed on Win
platforms, because the scripts bundled with Win binaries had dos
line endings.
- Chris Keith has written code to execute Python scripts from the
command-line, with '-P ' switch: "blender -P filename":
a Blender.Quit function was also added, so Blender can quit after
running the script (end the script with Blender.Quit()), but there's
still work to be done in this part, including adding more functions,
to load / save .blend files and to run scripts. More testing and
discussions are necessary.
Thanks Chris, for both your contributions and your patience, since I
wasn't available to check / commit this for a while.
2004-03-31 04:18:39 +00:00
|
|
|
|
|
|
|
/* now handle the whole buffer */
|
2004-10-07 19:25:40 +00:00
|
|
|
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 */
|
BPython:
- tentative fix for scripts with CR/LF endings and split lines:
in 2.32, the ac3d and vrml2 exporters, for example, had lines
split with '\\\\' and so gave syntax errors when executed on Win
platforms, because the scripts bundled with Win binaries had dos
line endings.
- Chris Keith has written code to execute Python scripts from the
command-line, with '-P ' switch: "blender -P filename":
a Blender.Quit function was also added, so Blender can quit after
running the script (end the script with Blender.Quit()), but there's
still work to be done in this part, including adding more functions,
to load / save .blend files and to run scripts. More testing and
discussions are necessary.
Thanks Chris, for both your contributions and your patience, since I
wasn't available to check / commit this for a while.
2004-03-31 04:18:39 +00:00
|
|
|
*s = '\\';
|
2004-10-07 19:25:40 +00:00
|
|
|
} else
|
|
|
|
*s = ' '; /* not a split line, just replace '\r' with ' ' */
|
BPython:
- tentative fix for scripts with CR/LF endings and split lines:
in 2.32, the ac3d and vrml2 exporters, for example, had lines
split with '\\\\' and so gave syntax errors when executed on Win
platforms, because the scripts bundled with Win binaries had dos
line endings.
- Chris Keith has written code to execute Python scripts from the
command-line, with '-P ' switch: "blender -P filename":
a Blender.Quit function was also added, so Blender can quit after
running the script (end the script with Blender.Quit()), but there's
still work to be done in this part, including adding more functions,
to load / save .blend files and to run scripts. More testing and
discussions are necessary.
Thanks Chris, for both your contributions and your patience, since I
wasn't available to check / commit this for a while.
2004-03-31 04:18:39 +00:00
|
|
|
}
|
|
|
|
}
|
2004-01-27 09:30:29 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
fclose( fp );
|
2004-01-14 21:36:10 +00:00
|
|
|
|
2004-01-18 15:08:02 +00:00
|
|
|
/* run the string buffer */
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
py_res = PyRun_String( buffer, Py_file_input, py_dict, py_dict );
|
2004-01-18 15:08:02 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
MEM_freeN( buffer );
|
2004-01-18 15:08:02 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
if( !py_res ) { /* Failed execution of the script */
|
2004-01-14 21:36:10 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
BPY_Err_Handle( script->id.name + 2 );
|
|
|
|
PyErr_Print( );
|
|
|
|
ReleaseGlobalDictionary( py_dict );
|
|
|
|
if( G.main->script.first )
|
|
|
|
free_libblock( &G.main->script, script );
|
|
|
|
error( "Python script error: check console" );
|
2003-05-13 01:54:28 +00:00
|
|
|
|
2004-04-25 14:43:21 +00:00
|
|
|
return 0;
|
2004-10-07 19:25:40 +00:00
|
|
|
} else {
|
|
|
|
Py_DECREF( py_res );
|
|
|
|
script->flags &= ~SCRIPT_RUNNING;
|
BPython: cleaning some bug tracker entries:
(excuse me for doing all in a single commit, but they are tiny
fixes and it's bpython, that dark corner ...)
#1025 - FileSelector SEGV on dynamic callback Category:
Can't reproduce with current cvs, I'd say recent changes to fix
another crash related to FileSelector in gui-less scripts solved this
one, too.
#1028 - Reserved button event number:
Menu choices generate two events, one extra related to the menu
itself, with value=4. Made bpython ignore this extra event.
#1068 - FileSelector No file extension support:
As Ton wrote there, Blender itself doesn't support this yet. But the
requester also wanted Window.File/ImageSelector to accept a pathname. Done. Also updated doc.
#959 - Segfault on background rendering:
This happened in bg mode (blender -b filename -a, for example) when
a script with the line "Blender.Redraw()" was linked to FRAMECHANGED events. As reported in the bug page, it was because curarea is NULL in bg mode. Made Redraw() check for this and not call functions that expected curarea in Redraw, like one to swap buffers.
#1072 - Blender.Redraw() Segfault:
Good catch : ). Scripts called from the scripts win that called
Blender.Redraw() or Blender.Window.Redraw() would crash Blender because of a dirty pointer in Spacescript->script. Fixed.
2004-04-11 04:41:33 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
if( !script->flags ) {
|
|
|
|
ReleaseGlobalDictionary( py_dict );
|
2003-12-14 01:18:09 +00:00
|
|
|
script->py_globaldict = NULL;
|
2004-10-07 19:25:40 +00:00
|
|
|
free_libblock( &G.main->script, script );
|
BPython: cleaning some bug tracker entries:
(excuse me for doing all in a single commit, but they are tiny
fixes and it's bpython, that dark corner ...)
#1025 - FileSelector SEGV on dynamic callback Category:
Can't reproduce with current cvs, I'd say recent changes to fix
another crash related to FileSelector in gui-less scripts solved this
one, too.
#1028 - Reserved button event number:
Menu choices generate two events, one extra related to the menu
itself, with value=4. Made bpython ignore this extra event.
#1068 - FileSelector No file extension support:
As Ton wrote there, Blender itself doesn't support this yet. But the
requester also wanted Window.File/ImageSelector to accept a pathname. Done. Also updated doc.
#959 - Segfault on background rendering:
This happened in bg mode (blender -b filename -a, for example) when
a script with the line "Blender.Redraw()" was linked to FRAMECHANGED events. As reported in the bug page, it was because curarea is NULL in bg mode. Made Redraw() check for this and not call functions that expected curarea in Redraw, like one to swap buffers.
#1072 - Blender.Redraw() Segfault:
Good catch : ). Scripts called from the scripts win that called
Blender.Redraw() or Blender.Window.Redraw() would crash Blender because of a dirty pointer in Spacescript->script. Fixed.
2004-04-11 04:41:33 +00:00
|
|
|
|
|
|
|
/* special case: called from the menu in the Scripts window
|
|
|
|
* we have to change sc->script pointer, since it'll be freed here.*/
|
2004-10-07 19:25:40 +00:00
|
|
|
if( curarea->spacetype == SPACE_SCRIPT ) {
|
BPython: cleaning some bug tracker entries:
(excuse me for doing all in a single commit, but they are tiny
fixes and it's bpython, that dark corner ...)
#1025 - FileSelector SEGV on dynamic callback Category:
Can't reproduce with current cvs, I'd say recent changes to fix
another crash related to FileSelector in gui-less scripts solved this
one, too.
#1028 - Reserved button event number:
Menu choices generate two events, one extra related to the menu
itself, with value=4. Made bpython ignore this extra event.
#1068 - FileSelector No file extension support:
As Ton wrote there, Blender itself doesn't support this yet. But the
requester also wanted Window.File/ImageSelector to accept a pathname. Done. Also updated doc.
#959 - Segfault on background rendering:
This happened in bg mode (blender -b filename -a, for example) when
a script with the line "Blender.Redraw()" was linked to FRAMECHANGED events. As reported in the bug page, it was because curarea is NULL in bg mode. Made Redraw() check for this and not call functions that expected curarea in Redraw, like one to swap buffers.
#1072 - Blender.Redraw() Segfault:
Good catch : ). Scripts called from the scripts win that called
Blender.Redraw() or Blender.Window.Redraw() would crash Blender because of a dirty pointer in Spacescript->script. Fixed.
2004-04-11 04:41:33 +00:00
|
|
|
SpaceScript *sc = curarea->spacedata.first;
|
2004-10-07 19:25:40 +00:00
|
|
|
sc->script = G.main->script.first; /* can be null, which is ok ... */
|
BPython: cleaning some bug tracker entries:
(excuse me for doing all in a single commit, but they are tiny
fixes and it's bpython, that dark corner ...)
#1025 - FileSelector SEGV on dynamic callback Category:
Can't reproduce with current cvs, I'd say recent changes to fix
another crash related to FileSelector in gui-less scripts solved this
one, too.
#1028 - Reserved button event number:
Menu choices generate two events, one extra related to the menu
itself, with value=4. Made bpython ignore this extra event.
#1068 - FileSelector No file extension support:
As Ton wrote there, Blender itself doesn't support this yet. But the
requester also wanted Window.File/ImageSelector to accept a pathname. Done. Also updated doc.
#959 - Segfault on background rendering:
This happened in bg mode (blender -b filename -a, for example) when
a script with the line "Blender.Redraw()" was linked to FRAMECHANGED events. As reported in the bug page, it was because curarea is NULL in bg mode. Made Redraw() check for this and not call functions that expected curarea in Redraw, like one to swap buffers.
#1072 - Blender.Redraw() Segfault:
Good catch : ). Scripts called from the scripts win that called
Blender.Redraw() or Blender.Window.Redraw() would crash Blender because of a dirty pointer in Spacescript->script. Fixed.
2004-04-11 04:41:33 +00:00
|
|
|
/* ... meaning no other script is running right now. */
|
|
|
|
}
|
2004-10-07 19:25:40 +00:00
|
|
|
|
2003-12-14 01:18:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
return 1; /* normal return */
|
2003-02-26 19:22:24 +00:00
|
|
|
}
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
* Description:
|
|
|
|
* Notes:
|
|
|
|
*****************************************************************************/
|
|
|
|
void BPY_free_compiled_text( struct Text *text )
|
2003-02-26 19:22:24 +00:00
|
|
|
{
|
2004-10-07 19:25:40 +00:00
|
|
|
if( !text->compiled )
|
|
|
|
return;
|
|
|
|
Py_DECREF( ( PyObject * ) text->compiled );
|
2004-04-25 14:43:21 +00:00
|
|
|
text->compiled = NULL;
|
2003-05-13 01:54:28 +00:00
|
|
|
|
2004-04-25 14:43:21 +00:00
|
|
|
return;
|
2003-02-26 19:22:24 +00:00
|
|
|
}
|
2003-12-15 18:18:09 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
* Description: This function frees a finished (flags == 0) script.
|
|
|
|
*****************************************************************************/
|
|
|
|
void BPY_free_finished_script( Script * script )
|
2003-12-15 18:18:09 +00:00
|
|
|
{
|
2004-10-07 19:25:40 +00:00
|
|
|
if( !script )
|
|
|
|
return;
|
2003-12-15 18:18:09 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
if( PyErr_Occurred( ) ) { /* if script ended after filesel */
|
|
|
|
PyErr_Print( ); /* eventual errors are handled now */
|
|
|
|
error( "Python script error: check console" );
|
2004-01-28 19:16:50 +00:00
|
|
|
}
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
free_libblock( &G.main->script, script );
|
2003-12-15 18:18:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
void unlink_script( Script * script )
|
|
|
|
{ /* copied from unlink_text in drawtext.c */
|
2003-12-15 18:18:09 +00:00
|
|
|
bScreen *scr;
|
|
|
|
ScrArea *area;
|
|
|
|
SpaceLink *sl;
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
for( scr = G.main->screen.first; scr; scr = scr->id.next ) {
|
|
|
|
for( area = scr->areabase.first; area; area = area->next ) {
|
|
|
|
for( sl = area->spacedata.first; sl; sl = sl->next ) {
|
|
|
|
if( sl->spacetype == SPACE_SCRIPT ) {
|
|
|
|
SpaceScript *sc = ( SpaceScript * ) sl;
|
2003-12-15 18:18:09 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
if( sc->script == script ) {
|
|
|
|
sc->script = NULL;
|
2003-12-15 18:18:09 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
if( sc ==
|
|
|
|
area->spacedata.first ) {
|
|
|
|
scrarea_queue_redraw
|
|
|
|
( area );
|
2003-12-15 18:18:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
void BPY_clear_script( Script * script )
|
2003-12-15 18:18:09 +00:00
|
|
|
{
|
|
|
|
PyObject *dict;
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
if( !script )
|
|
|
|
return;
|
2003-12-15 18:18:09 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
Py_XDECREF( ( PyObject * ) script->py_draw );
|
|
|
|
Py_XDECREF( ( PyObject * ) script->py_event );
|
|
|
|
Py_XDECREF( ( PyObject * ) script->py_button );
|
2003-12-15 18:18:09 +00:00
|
|
|
|
|
|
|
dict = script->py_globaldict;
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
if( dict ) {
|
|
|
|
PyDict_Clear( dict );
|
|
|
|
Py_DECREF( dict ); /* Release dictionary. */
|
2003-12-15 18:18:09 +00:00
|
|
|
script->py_globaldict = NULL;
|
|
|
|
}
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
unlink_script( script );
|
2003-12-15 18:18:09 +00:00
|
|
|
}
|
|
|
|
|
2003-05-13 01:54:28 +00:00
|
|
|
/*****************************************************************************/
|
2004-10-07 19:25:40 +00:00
|
|
|
/* ScriptLinks */
|
2003-05-13 01:54:28 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
2003-03-08 18:10:20 +00:00
|
|
|
/*****************************************************************************/
|
2004-10-07 19:25:40 +00:00
|
|
|
/* Description: */
|
|
|
|
/* Notes: Not implemented yet */
|
2003-03-08 18:10:20 +00:00
|
|
|
/*****************************************************************************/
|
2004-10-07 19:25:40 +00:00
|
|
|
void BPY_clear_bad_scriptlinks( struct Text *byebye )
|
2003-02-26 19:22:24 +00:00
|
|
|
{
|
2003-05-13 01:54:28 +00:00
|
|
|
/*
|
2004-04-25 14:43:21 +00:00
|
|
|
BPY_clear_bad_scriptlist(getObjectList(), byebye);
|
|
|
|
BPY_clear_bad_scriptlist(getLampList(), byebye);
|
|
|
|
BPY_clear_bad_scriptlist(getCameraList(), byebye);
|
|
|
|
BPY_clear_bad_scriptlist(getMaterialList(), byebye);
|
|
|
|
BPY_clear_bad_scriptlist(getWorldList(), byebye);
|
|
|
|
BPY_clear_bad_scriptlink(&scene_getCurrent()->id, byebye);
|
|
|
|
|
|
|
|
allqueue(REDRAWBUTSSCRIPT, 0);
|
2003-05-13 01:54:28 +00:00
|
|
|
*/
|
2004-04-25 14:43:21 +00:00
|
|
|
return;
|
2003-02-26 19:22:24 +00:00
|
|
|
}
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
* Description: Loop through all scripts of a list of object types, and
|
|
|
|
* execute these scripts.
|
|
|
|
* For the scene, only the current active scene the scripts are
|
|
|
|
* executed (if any).
|
|
|
|
*****************************************************************************/
|
|
|
|
void BPY_do_all_scripts( short event )
|
2003-02-26 19:22:24 +00:00
|
|
|
{
|
2004-10-07 19:25:40 +00:00
|
|
|
DoAllScriptsFromList( &( G.main->object ), event );
|
|
|
|
DoAllScriptsFromList( &( G.main->lamp ), event );
|
|
|
|
DoAllScriptsFromList( &( G.main->camera ), event );
|
|
|
|
DoAllScriptsFromList( &( G.main->mat ), event );
|
|
|
|
DoAllScriptsFromList( &( G.main->world ), event );
|
2003-03-19 18:57:23 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
BPY_do_pyscript( &( G.scene->id ), event );
|
2003-03-19 18:57:23 +00:00
|
|
|
|
2004-04-25 14:43:21 +00:00
|
|
|
return;
|
2003-02-26 19:22:24 +00:00
|
|
|
}
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
* Description: Execute a Python script when an event occurs. The following
|
|
|
|
* events are possible: frame changed, load script and redraw.
|
|
|
|
* Only events happening to one of the following object types
|
|
|
|
* are handled: Object, Lamp, Camera, Material, World and
|
|
|
|
* Scene.
|
|
|
|
*****************************************************************************/
|
2004-05-04 14:27:41 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
static ScriptLink *ID_getScriptlink( ID * id )
|
2004-05-04 14:27:41 +00:00
|
|
|
{
|
2004-10-07 19:25:40 +00:00
|
|
|
switch ( MAKE_ID2( id->name[0], id->name[1] ) ) {
|
2004-05-04 14:27:41 +00:00
|
|
|
case ID_OB:
|
2004-10-07 19:25:40 +00:00
|
|
|
return &( ( Object * ) id )->scriptlink;
|
2004-05-04 14:27:41 +00:00
|
|
|
case ID_LA:
|
2004-10-07 19:25:40 +00:00
|
|
|
return &( ( Lamp * ) id )->scriptlink;
|
2004-05-04 14:27:41 +00:00
|
|
|
case ID_CA:
|
2004-10-07 19:25:40 +00:00
|
|
|
return &( ( Camera * ) id )->scriptlink;
|
2004-05-04 14:27:41 +00:00
|
|
|
case ID_MA:
|
2004-10-07 19:25:40 +00:00
|
|
|
return &( ( Material * ) id )->scriptlink;
|
2004-05-04 14:27:41 +00:00
|
|
|
case ID_WO:
|
2004-10-07 19:25:40 +00:00
|
|
|
return &( ( World * ) id )->scriptlink;
|
2004-05-04 14:27:41 +00:00
|
|
|
case ID_SCE:
|
2004-10-07 19:25:40 +00:00
|
|
|
return &( ( Scene * ) id )->scriptlink;
|
2004-05-04 14:27:41 +00:00
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
static PyObject *ID_asPyObject( ID * id )
|
2003-02-26 19:22:24 +00:00
|
|
|
{
|
2004-10-07 19:25:40 +00:00
|
|
|
switch ( MAKE_ID2( id->name[0], id->name[1] ) ) {
|
2004-05-04 14:27:41 +00:00
|
|
|
case ID_OB:
|
2004-10-07 19:25:40 +00:00
|
|
|
return Object_CreatePyObject( ( Object * ) id );
|
2004-05-04 14:27:41 +00:00
|
|
|
case ID_LA:
|
2004-10-07 19:25:40 +00:00
|
|
|
return Lamp_CreatePyObject( ( Lamp * ) id );
|
2004-05-04 14:27:41 +00:00
|
|
|
case ID_CA:
|
2004-10-07 19:25:40 +00:00
|
|
|
return Camera_CreatePyObject( ( Camera * ) id );
|
2004-05-04 14:27:41 +00:00
|
|
|
case ID_MA:
|
2004-10-07 19:25:40 +00:00
|
|
|
return Material_CreatePyObject( ( Material * ) id );
|
2004-05-04 14:27:41 +00:00
|
|
|
case ID_WO:
|
2004-10-07 19:25:40 +00:00
|
|
|
return World_CreatePyObject( ( World * ) id );
|
2004-05-04 14:27:41 +00:00
|
|
|
case ID_SCE:
|
2004-10-07 19:25:40 +00:00
|
|
|
return Scene_CreatePyObject( ( Scene * ) id );
|
2004-05-04 14:27:41 +00:00
|
|
|
default:
|
2004-10-07 19:25:40 +00:00
|
|
|
Py_INCREF( Py_None );
|
2004-07-16 03:08:43 +00:00
|
|
|
return Py_None;
|
2004-05-04 14:27:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
int BPY_has_onload_script( void )
|
2004-07-20 08:16:46 +00:00
|
|
|
{
|
|
|
|
ScriptLink *slink = &G.scene->scriptlink;
|
|
|
|
int i;
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
if( !slink || !slink->totscript )
|
|
|
|
return 0;
|
2004-07-20 08:16:46 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
for( i = 0; i < slink->totscript; i++ ) {
|
|
|
|
if( ( slink->flag[i] == SCRIPT_ONLOAD )
|
|
|
|
&& ( slink->scripts[i] != NULL ) )
|
|
|
|
return 1;
|
2004-07-20 08:16:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
void BPY_do_pyscript( ID * id, short event )
|
2004-05-04 14:27:41 +00:00
|
|
|
{
|
2004-10-07 19:25:40 +00:00
|
|
|
ScriptLink *scriptlink = ID_getScriptlink( id );
|
2004-05-04 14:27:41 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
if( scriptlink && scriptlink->totscript ) {
|
2004-05-04 14:27:41 +00:00
|
|
|
PyObject *dict;
|
|
|
|
PyObject *ret;
|
2004-10-07 19:25:40 +00:00
|
|
|
int index, during_slink = during_scriptlink( );
|
2004-07-16 03:08:43 +00:00
|
|
|
|
|
|
|
/* invalid scriptlinks (new .blend was just loaded), return */
|
2004-10-07 19:25:40 +00:00
|
|
|
if( during_slink < 0 )
|
|
|
|
return;
|
2004-05-04 14:27:41 +00:00
|
|
|
|
2004-07-16 03:08:43 +00:00
|
|
|
/* tell we're running a scriptlink. The sum also tells if this script
|
|
|
|
* is running nested inside another. Blender.Load needs this info to
|
|
|
|
* avoid trouble with invalid slink pointers. */
|
|
|
|
during_slink++;
|
2004-10-07 19:25:40 +00:00
|
|
|
disable_where_scriptlink( during_slink );
|
2004-07-16 03:08:43 +00:00
|
|
|
|
|
|
|
/* set globals in Blender module to identify scriptlink */
|
2004-10-07 19:25:40 +00:00
|
|
|
Py_INCREF( Py_True );
|
|
|
|
PyDict_SetItemString( g_blenderdict, "bylink", Py_True );
|
|
|
|
PyDict_SetItemString( g_blenderdict, "link",
|
|
|
|
ID_asPyObject( id ) );
|
|
|
|
PyDict_SetItemString( g_blenderdict, "event",
|
|
|
|
PyString_FromString( event_to_name
|
|
|
|
( event ) ) );
|
|
|
|
|
|
|
|
for( index = 0; index < scriptlink->totscript; index++ ) {
|
|
|
|
if( ( scriptlink->flag[index] == event ) &&
|
|
|
|
( scriptlink->scripts[index] != NULL ) ) {
|
|
|
|
dict = CreateGlobalDictionary( );
|
|
|
|
ret = RunPython( ( Text * ) scriptlink->
|
|
|
|
scripts[index], dict );
|
|
|
|
ReleaseGlobalDictionary( dict );
|
|
|
|
|
|
|
|
if( !ret ) {
|
2004-04-25 14:43:21 +00:00
|
|
|
/* Failed execution of the script */
|
2004-10-07 19:25:40 +00:00
|
|
|
BPY_Err_Handle( scriptlink->
|
|
|
|
scripts[index]->name +
|
|
|
|
2 );
|
2004-07-16 03:08:43 +00:00
|
|
|
//BPY_end_python ();
|
|
|
|
//BPY_start_python ();
|
2004-05-04 14:27:41 +00:00
|
|
|
} else {
|
2004-10-07 19:25:40 +00:00
|
|
|
Py_DECREF( ret );
|
2004-05-04 14:27:41 +00:00
|
|
|
}
|
2004-07-16 03:08:43 +00:00
|
|
|
/* If a scriptlink has just loaded a new .blend file, the
|
|
|
|
* scriptlink pointer became invalid (see api2_2x/Blender.c),
|
|
|
|
* so we stop here. */
|
2004-10-07 19:25:40 +00:00
|
|
|
if( during_scriptlink( ) == -1 ) {
|
2004-07-16 03:08:43 +00:00
|
|
|
during_slink = 1;
|
|
|
|
break;
|
|
|
|
}
|
2004-04-25 14:43:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
disable_where_scriptlink( during_slink - 1 );
|
|
|
|
|
|
|
|
/* cleanup bylink flag and clear link so PyObject
|
|
|
|
* can be released
|
|
|
|
*/
|
|
|
|
Py_INCREF( Py_False );
|
|
|
|
PyDict_SetItemString( g_blenderdict, "bylink", Py_False );
|
|
|
|
Py_INCREF( Py_None );
|
|
|
|
PyDict_SetItemString( g_blenderdict, "link", Py_None );
|
|
|
|
PyDict_SetItemString( g_blenderdict, "event",
|
|
|
|
PyString_FromString( "" ) );
|
2004-05-04 14:27:41 +00:00
|
|
|
}
|
2003-02-26 19:22:24 +00:00
|
|
|
}
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
* Description:
|
|
|
|
* Notes:
|
|
|
|
*****************************************************************************/
|
|
|
|
void BPY_free_scriptlink( struct ScriptLink *slink )
|
2003-02-26 19:22:24 +00:00
|
|
|
{
|
2004-10-07 19:25:40 +00:00
|
|
|
if( slink->totscript ) {
|
|
|
|
if( slink->flag )
|
|
|
|
MEM_freeN( slink->flag );
|
|
|
|
if( slink->scripts )
|
|
|
|
MEM_freeN( slink->scripts );
|
2004-04-25 14:43:21 +00:00
|
|
|
}
|
2003-05-13 01:54:28 +00:00
|
|
|
|
2004-04-25 14:43:21 +00:00
|
|
|
return;
|
2003-02-26 19:22:24 +00:00
|
|
|
}
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
* Description:
|
|
|
|
* Notes:
|
|
|
|
*****************************************************************************/
|
|
|
|
void BPY_copy_scriptlink( struct ScriptLink *scriptlink )
|
2003-02-26 19:22:24 +00:00
|
|
|
{
|
2004-04-25 14:43:21 +00:00
|
|
|
void *tmp;
|
2003-05-13 01:54:28 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
if( scriptlink->totscript ) {
|
2003-05-13 01:54:28 +00:00
|
|
|
|
2004-04-25 14:43:21 +00:00
|
|
|
tmp = scriptlink->scripts;
|
|
|
|
scriptlink->scripts =
|
2004-10-07 19:25:40 +00:00
|
|
|
MEM_mallocN( sizeof( ID * ) * scriptlink->totscript,
|
|
|
|
"scriptlistL" );
|
|
|
|
memcpy( scriptlink->scripts, tmp,
|
|
|
|
sizeof( ID * ) * scriptlink->totscript );
|
2003-05-13 01:54:28 +00:00
|
|
|
|
2004-04-25 14:43:21 +00:00
|
|
|
tmp = scriptlink->flag;
|
|
|
|
scriptlink->flag =
|
2004-10-07 19:25:40 +00:00
|
|
|
MEM_mallocN( sizeof( short ) * scriptlink->totscript,
|
|
|
|
"scriptlistF" );
|
|
|
|
memcpy( scriptlink->flag, tmp,
|
|
|
|
sizeof( short ) * scriptlink->totscript );
|
2004-04-25 14:43:21 +00:00
|
|
|
}
|
2003-05-13 01:54:28 +00:00
|
|
|
|
2004-04-25 14:43:21 +00:00
|
|
|
return;
|
2003-02-26 19:22:24 +00:00
|
|
|
}
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
/****************************************************************************
|
|
|
|
* Description:
|
|
|
|
* Notes: Not implemented yet
|
|
|
|
*****************************************************************************/
|
|
|
|
int BPY_call_importloader( char *name )
|
|
|
|
{ /* XXX Should this function go away from Blender? */
|
|
|
|
printf( "In BPY_call_importloader(name=%s)\n", name );
|
|
|
|
return ( 0 );
|
2003-02-26 19:22:24 +00:00
|
|
|
}
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
* Private functions
|
|
|
|
*****************************************************************************/
|
2003-02-26 19:22:24 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
* Description: This function executes the python script passed by text.
|
|
|
|
* The Python dictionary containing global variables needs to
|
|
|
|
* be passed in globaldict.
|
|
|
|
*****************************************************************************/
|
|
|
|
PyObject *RunPython( Text * text, PyObject * globaldict )
|
2003-02-26 19:22:24 +00:00
|
|
|
{
|
2004-04-25 14:43:21 +00:00
|
|
|
char *buf = NULL;
|
2003-05-13 01:54:28 +00:00
|
|
|
|
|
|
|
/* The script text is compiled to Python bytecode and saved at text->compiled
|
|
|
|
* to speed-up execution if the user executes the script multiple times */
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
if( !text->compiled ) { /* if it wasn't already compiled, do it now */
|
|
|
|
buf = txt_to_buf( text );
|
2003-05-13 01:54:28 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
text->compiled =
|
|
|
|
Py_CompileString( buf, GetName( text ),
|
|
|
|
Py_file_input );
|
2003-05-13 01:54:28 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
MEM_freeN( buf );
|
2003-05-13 01:54:28 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
if( PyErr_Occurred( ) ) {
|
|
|
|
BPY_free_compiled_text( text );
|
2004-04-25 14:43:21 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2003-05-13 01:54:28 +00:00
|
|
|
|
2004-04-25 14:43:21 +00:00
|
|
|
}
|
2003-06-09 20:07:43 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
return PyEval_EvalCode( text->compiled, globaldict, globaldict );
|
2003-02-26 19:22:24 +00:00
|
|
|
}
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
* Description: This function returns the value of the name field of the
|
|
|
|
* given Text struct.
|
|
|
|
*****************************************************************************/
|
|
|
|
char *GetName( Text * text )
|
2003-02-26 19:22:24 +00:00
|
|
|
{
|
2004-10-07 19:25:40 +00:00
|
|
|
return ( text->id.name + 2 );
|
2003-02-26 19:22:24 +00:00
|
|
|
}
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
* Description: This function creates a new Python dictionary object.
|
|
|
|
*****************************************************************************/
|
|
|
|
PyObject *CreateGlobalDictionary( void )
|
2003-03-18 20:21:26 +00:00
|
|
|
{
|
2004-10-07 19:25:40 +00:00
|
|
|
PyObject *dict = PyDict_New( );
|
2003-05-13 01:54:28 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
PyDict_SetItemString( dict, "__builtins__", PyEval_GetBuiltins( ) );
|
|
|
|
PyDict_SetItemString( dict, "__name__",
|
|
|
|
PyString_FromString( "__main__" ) );
|
2003-03-18 20:21:26 +00:00
|
|
|
|
2004-04-25 14:43:21 +00:00
|
|
|
return dict;
|
2003-03-18 20:21:26 +00:00
|
|
|
}
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
* Description: This function deletes a given Python dictionary object.
|
|
|
|
*****************************************************************************/
|
|
|
|
void ReleaseGlobalDictionary( PyObject * dict )
|
2003-03-18 20:21:26 +00:00
|
|
|
{
|
2004-10-07 19:25:40 +00:00
|
|
|
PyDict_Clear( dict );
|
|
|
|
Py_DECREF( dict ); /* Release dictionary. */
|
2003-05-13 01:54:28 +00:00
|
|
|
|
2004-04-25 14:43:21 +00:00
|
|
|
return;
|
2003-03-18 20:21:26 +00:00
|
|
|
}
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* Description: This function runs all scripts (if any) present in the
|
|
|
|
* list argument. The event by which the function has been
|
|
|
|
* called, is passed in the event argument.
|
|
|
|
*****************************************************************************/
|
|
|
|
void DoAllScriptsFromList( ListBase * list, short event )
|
2003-03-19 18:57:23 +00:00
|
|
|
{
|
2004-04-25 14:43:21 +00:00
|
|
|
ID *id;
|
2003-03-19 18:57:23 +00:00
|
|
|
|
2004-04-25 14:43:21 +00:00
|
|
|
id = list->first;
|
2003-03-19 18:57:23 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
while( id != NULL ) {
|
|
|
|
BPY_do_pyscript( id, event );
|
2004-04-25 14:43:21 +00:00
|
|
|
id = id->next;
|
|
|
|
}
|
2003-05-13 01:54:28 +00:00
|
|
|
|
2004-04-25 14:43:21 +00:00
|
|
|
return;
|
2003-03-19 18:57:23 +00:00
|
|
|
}
|
2003-07-03 01:42:00 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
PyObject *importText( char *name )
|
2003-07-03 01:42:00 +00:00
|
|
|
{
|
2004-04-25 14:43:21 +00:00
|
|
|
Text *text;
|
|
|
|
char *txtname;
|
|
|
|
char *buf = NULL;
|
2004-10-07 19:25:40 +00:00
|
|
|
int namelen = strlen( name );
|
2004-04-25 14:43:21 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
txtname = malloc( namelen + 3 + 1 );
|
|
|
|
if( !txtname )
|
|
|
|
return NULL;
|
2004-04-25 14:43:21 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
memcpy( txtname, name, namelen );
|
|
|
|
memcpy( &txtname[namelen], ".py", 4 );
|
2004-04-25 14:43:21 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
text = ( Text * ) & ( G.main->text.first );
|
2004-04-25 14:43:21 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
while( text ) {
|
|
|
|
if( !strcmp( txtname, GetName( text ) ) )
|
2004-04-25 14:43:21 +00:00
|
|
|
break;
|
|
|
|
text = text->id.next;
|
|
|
|
}
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
if( !text ) {
|
|
|
|
free( txtname );
|
2004-04-25 14:43:21 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
if( !text->compiled ) {
|
|
|
|
buf = txt_to_buf( text );
|
|
|
|
text->compiled =
|
|
|
|
Py_CompileString( buf, GetName( text ),
|
|
|
|
Py_file_input );
|
|
|
|
MEM_freeN( buf );
|
|
|
|
|
|
|
|
if( PyErr_Occurred( ) ) {
|
|
|
|
PyErr_Print( );
|
|
|
|
BPY_free_compiled_text( text );
|
|
|
|
free( txtname );
|
2004-04-25 14:43:21 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
free( txtname );
|
|
|
|
return PyImport_ExecCodeModule( name, text->compiled );
|
2003-07-03 01:42:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyMethodDef bimport[] = {
|
2004-10-07 19:25:40 +00:00
|
|
|
{"blimport", blender_import, METH_VARARGS, "our own import"}
|
2003-07-03 01:42:00 +00:00
|
|
|
};
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
PyObject *blender_import( PyObject * self, PyObject * args )
|
2003-07-03 01:42:00 +00:00
|
|
|
{
|
2004-04-25 14:43:21 +00:00
|
|
|
PyObject *exception, *err, *tb;
|
|
|
|
char *name;
|
|
|
|
PyObject *globals = NULL, *locals = NULL, *fromlist = NULL;
|
|
|
|
PyObject *m;
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
if( !PyArg_ParseTuple( args, "s|OOO:bimport",
|
|
|
|
&name, &globals, &locals, &fromlist ) )
|
|
|
|
return NULL;
|
2004-04-25 14:43:21 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
m = PyImport_ImportModuleEx( name, globals, locals, fromlist );
|
2004-04-25 14:43:21 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
if( m )
|
2004-04-25 14:43:21 +00:00
|
|
|
return m;
|
|
|
|
else
|
2004-10-07 19:25:40 +00:00
|
|
|
PyErr_Fetch( &exception, &err, &tb ); /*restore for probable later use */
|
|
|
|
|
|
|
|
m = importText( name );
|
|
|
|
if( m ) { /* found module, ignore above exception */
|
|
|
|
PyErr_Clear( );
|
|
|
|
Py_XDECREF( exception );
|
|
|
|
Py_XDECREF( err );
|
|
|
|
Py_XDECREF( tb );
|
|
|
|
printf( "imported from text buffer...\n" );
|
2004-04-25 14:43:21 +00:00
|
|
|
} else {
|
2004-10-07 19:25:40 +00:00
|
|
|
PyErr_Restore( exception, err, tb );
|
2004-04-25 14:43:21 +00:00
|
|
|
}
|
|
|
|
return m;
|
2003-07-03 01:42:00 +00:00
|
|
|
}
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
void init_ourImport( void )
|
2003-07-03 01:42:00 +00:00
|
|
|
{
|
2004-04-25 14:43:21 +00:00
|
|
|
PyObject *m, *d;
|
2004-10-07 19:25:40 +00:00
|
|
|
PyObject *import = PyCFunction_New( bimport, NULL );
|
2003-07-03 01:42:00 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
m = PyImport_AddModule( "__builtin__" );
|
|
|
|
d = PyModule_GetDict( m );
|
|
|
|
PyDict_SetItemString( d, "__import__", import );
|
2003-07-03 01:42:00 +00:00
|
|
|
}
|
2004-05-22 20:25:22 +00:00
|
|
|
|
|
|
|
/* this makes sure BLI_gethome() returns a path with '.blender' appended
|
|
|
|
* Besides, this function now either returns userhome/.blender (if it exists)
|
2004-10-07 19:25:40 +00:00
|
|
|
* or blenderInstallDir/.blender/ otherwise
|
|
|
|
*/
|
|
|
|
char *bpy_gethome( )
|
2004-05-22 20:25:22 +00:00
|
|
|
{
|
|
|
|
static char homedir[FILE_MAXDIR];
|
|
|
|
char bprogdir[FILE_MAXDIR];
|
|
|
|
char *s;
|
|
|
|
int i;
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
if( homedir[0] != '\0' )
|
|
|
|
return homedir; /* no need to search twice */
|
2004-05-22 20:25:22 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
s = BLI_gethome( );
|
2004-05-22 20:25:22 +00:00
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
if( strstr( s, ".blender" ) )
|
|
|
|
PyOS_snprintf( homedir, FILE_MAXDIR, s );
|
|
|
|
else
|
|
|
|
BLI_make_file_string( "/", homedir, s, ".blender/" );
|
2004-05-22 20:25:22 +00:00
|
|
|
|
|
|
|
/* if userhome/.blender/ exists, return it */
|
2004-10-07 19:25:40 +00:00
|
|
|
if( BLI_exists( homedir ) )
|
|
|
|
return homedir;
|
2004-05-22 20:25:22 +00:00
|
|
|
|
|
|
|
/* otherwise, use argv[0] (bprogname) to get .blender/ in
|
|
|
|
* Blender's installation dir */
|
2004-10-07 19:25:40 +00:00
|
|
|
s = BLI_last_slash( bprogname );
|
2004-05-22 20:25:22 +00:00
|
|
|
|
|
|
|
i = s - bprogname + 1;
|
|
|
|
|
2004-10-07 19:25:40 +00:00
|
|
|
PyOS_snprintf( bprogdir, i, bprogname );
|
|
|
|
BLI_make_file_string( "/", homedir, bprogdir, ".blender/" );
|
2004-05-22 20:25:22 +00:00
|
|
|
|
|
|
|
return homedir;
|
|
|
|
}
|