BGE PyAPI can now import text (within the blend-file)

Previously this only worked with the Blender API.
- bpy_internal_import small C file that Blender scripting and the game engine use.
- Tested with blender, blenderplayer, loading files
- Needed to use a hack to override the Main struct since the game engine doesn't set G.main
- when the sandbox is set, only internal scripts can be imported.
This commit is contained in:
Campbell Barton
2009-04-05 19:37:13 +00:00
parent 3dacfbb231
commit b4e4ccf92d
9 changed files with 333 additions and 188 deletions

View File

@@ -71,6 +71,7 @@
#include "api2_2x/Registry.h"
#include "api2_2x/Pose.h"
#include "api2_2x/bpy.h" /* for the new "bpy" module */
#include "api2_2x/bpy_internal_import.h"
/*these next two are for pyconstraints*/
#include "api2_2x/IDProp.h"
@@ -164,10 +165,8 @@ static PyObject *RunPython( Text * text, PyObject * globaldict );
static PyObject *CreateGlobalDictionary( void );
static void ReleaseGlobalDictionary( PyObject * dict );
static void DoAllScriptsFromList( ListBase * list, short event );
static PyObject *importText( char *name );
static void init_ourImport( void );
static void init_ourReload( void );
static PyObject *blender_import( PyObject * self, PyObject * args, PyObject * kw);
static void BPY_Err_Handle( char *script_name );
@@ -2821,91 +2820,11 @@ static void DoAllScriptsFromList( ListBase * list, short event )
return;
}
static PyObject *importText( char *name )
{
Text *text;
char txtname[22]; /* 21+NULL */
char *buf = NULL;
int namelen = strlen( name );
if (namelen>21-3) return NULL; /* we know this cant be importable, the name is too long for blender! */
memcpy( txtname, name, namelen );
memcpy( &txtname[namelen], ".py", 4 );
for(text = G.main->text.first; text; text = text->id.next) {
if( !strcmp( txtname, text->id.name+2 ) )
break;
}
if( !text )
return NULL;
if( !text->compiled ) {
buf = txt_to_buf( text );
text->compiled = Py_CompileString( buf, text->id.name+2, Py_file_input );
MEM_freeN( buf );
if( PyErr_Occurred( ) ) {
PyErr_Print( );
BPY_free_compiled_text( text );
return NULL;
}
}
return PyImport_ExecCodeModule( name, text->compiled );
}
static PyMethodDef bimport[] = {
{"blimport", blender_import, METH_KEYWORDS, "our own import"}
};
static PyObject *blender_import( PyObject * self, PyObject * args, PyObject * kw)
{
PyObject *exception, *err, *tb;
char *name;
PyObject *globals = NULL, *locals = NULL, *fromlist = NULL;
PyObject *m;
//PyObject_Print(args, stderr, 0);
#if (PY_VERSION_HEX >= 0x02060000)
int dummy_val; /* what does this do?*/
static char *kwlist[] = {"name", "globals", "locals", "fromlist", "level", 0};
if( !PyArg_ParseTupleAndKeywords( args, kw, "s|OOOi:bimport", kwlist,
&name, &globals, &locals, &fromlist, &dummy_val) )
return NULL;
#else
static char *kwlist[] = {"name", "globals", "locals", "fromlist", 0};
if( !PyArg_ParseTupleAndKeywords( args, kw, "s|OOO:bimport", kwlist,
&name, &globals, &locals, &fromlist ) )
return NULL;
#endif
m = PyImport_ImportModuleEx( name, globals, locals, fromlist );
if( m )
return m;
else
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" );
} else {
PyErr_Restore( exception, err, tb );
}
return m;
}
static void init_ourImport( void )
{
PyObject *m, *d;
PyObject *import = PyCFunction_New( bimport, NULL );
PyObject *import = PyCFunction_New( bpy_import, NULL );
m = PyImport_AddModule( "__builtin__" );
d = PyModule_GetDict( m );
@@ -2913,100 +2832,10 @@ static void init_ourImport( void )
EXPP_dict_set_item_str( d, "__import__", import );
}
/*
* find in-memory module and recompile
*/
static PyObject *reimportText( PyObject *module )
{
Text *text;
char *txtname;
char *name;
char *buf = NULL;
/* get name, filename from the module itself */
txtname = PyModule_GetFilename( module );
name = PyModule_GetName( module );
if( !txtname || !name)
return NULL;
/* look up the text object */
text = ( Text * ) & ( G.main->text.first );
while( text ) {
if( !strcmp( txtname, text->id.name+2 ) )
break;
text = text->id.next;
}
/* uh-oh.... didn't find it */
if( !text )
return NULL;
/* if previously compiled, free the object */
/* (can't see how could be NULL, but check just in case) */
if( text->compiled ){
Py_DECREF( (PyObject *)text->compiled );
}
/* compile the buffer */
buf = txt_to_buf( text );
text->compiled = Py_CompileString( buf, text->id.name+2, Py_file_input );
MEM_freeN( buf );
/* if compile failed.... return this error */
if( PyErr_Occurred( ) ) {
PyErr_Print( );
BPY_free_compiled_text( text );
return NULL;
}
/* make into a module */
return PyImport_ExecCodeModule( name, text->compiled );
}
/*
* our reload() module, to handle reloading in-memory scripts
*/
static PyObject *blender_reload( PyObject * self, PyObject * args )
{
PyObject *exception, *err, *tb;
PyObject *module = NULL;
PyObject *newmodule = NULL;
/* check for a module arg */
if( !PyArg_ParseTuple( args, "O:breload", &module ) )
return NULL;
/* try reimporting from file */
newmodule = PyImport_ReloadModule( module );
if( newmodule )
return newmodule;
/* no file, try importing from memory */
PyErr_Fetch( &exception, &err, &tb ); /*restore for probable later use */
newmodule = reimportText( module );
if( newmodule ) { /* found module, ignore above exception */
PyErr_Clear( );
Py_XDECREF( exception );
Py_XDECREF( err );
Py_XDECREF( tb );
} else
PyErr_Restore( exception, err, tb );
return newmodule;
}
static PyMethodDef breload[] = {
{"blreload", blender_reload, METH_VARARGS, "our own reload"}
};
static void init_ourReload( void )
{
PyObject *m, *d;
PyObject *reload = PyCFunction_New( breload, NULL );
PyObject *reload = PyCFunction_New( bpy_reload, NULL );
m = PyImport_AddModule( "__builtin__" );
d = PyModule_GetDict( m );

View File

@@ -0,0 +1,230 @@
/*
* $Id: bpy_types.h 14444 2008-04-16 22:40:48Z hos $
* ***** BEGIN GPL 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
* All rights reserved.
*
* This is a new part of Blender.
*
* Contributor(s): Willian P. Germano
*
* ***** END GPL LICENSE BLOCK *****
*/
#include "bpy_internal_import.h"
#include "DNA_text_types.h"
#include "DNA_ID.h"
#include "BKE_global.h"
#include "MEM_guardedalloc.h"
#include "BKE_text.h" /* txt_to_buf */
#include "BKE_main.h"
static Main *bpy_import_main= NULL;
static void free_compiled_text(Text *text)
{
if(text->compiled) {
Py_DECREF(text->compiled);
}
text->compiled= NULL;
}
struct Main *bpy_import_main_get(void)
{
return bpy_import_main;
}
void bpy_import_main_set(struct Main *maggie)
{
bpy_import_main= maggie;
}
PyObject *importText( char *name )
{
Text *text;
char txtname[22]; /* 21+NULL */
char *buf = NULL;
int namelen = strlen( name );
Main *maggie= bpy_import_main ? bpy_import_main:G.main;
if (namelen>21-3) return NULL; /* we know this cant be importable, the name is too long for blender! */
memcpy( txtname, name, namelen );
memcpy( &txtname[namelen], ".py", 4 );
for(text = maggie->text.first; text; text = text->id.next) {
fprintf(stderr, "%s | %s\n", txtname, text->id.name+2);
if( !strcmp( txtname, text->id.name+2 ) )
break;
}
if( !text )
return NULL;
if( !text->compiled ) {
buf = txt_to_buf( text );
text->compiled = Py_CompileString( buf, text->id.name+2, Py_file_input );
MEM_freeN( buf );
if( PyErr_Occurred( ) ) {
PyErr_Print( );
free_compiled_text( text );
return NULL;
}
}
return PyImport_ExecCodeModule( name, text->compiled );
}
/*
* find in-memory module and recompile
*/
PyObject *reimportText( PyObject *module )
{
Text *text;
char *txtname;
char *name;
char *buf = NULL;
Main *maggie= bpy_import_main ? bpy_import_main:G.main;
/* get name, filename from the module itself */
txtname = PyModule_GetFilename( module );
name = PyModule_GetName( module );
if( !txtname || !name)
return NULL;
/* look up the text object */
text = ( Text * ) & ( maggie->text.first );
while( text ) {
if( !strcmp( txtname, text->id.name+2 ) )
break;
text = text->id.next;
}
/* uh-oh.... didn't find it */
if( !text )
return NULL;
/* if previously compiled, free the object */
/* (can't see how could be NULL, but check just in case) */
if( text->compiled ){
Py_DECREF( (PyObject *)text->compiled );
}
/* compile the buffer */
buf = txt_to_buf( text );
text->compiled = Py_CompileString( buf, text->id.name+2, Py_file_input );
MEM_freeN( buf );
/* if compile failed.... return this error */
if( PyErr_Occurred( ) ) {
PyErr_Print( );
free_compiled_text( text );
return NULL;
}
/* make into a module */
return PyImport_ExecCodeModule( name, text->compiled );
}
static PyObject *blender_import( PyObject * self, PyObject * args, PyObject * kw)
{
PyObject *exception, *err, *tb;
char *name;
PyObject *globals = NULL, *locals = NULL, *fromlist = NULL;
PyObject *m;
//PyObject_Print(args, stderr, 0);
#if (PY_VERSION_HEX >= 0x02060000)
int dummy_val; /* what does this do?*/
static char *kwlist[] = {"name", "globals", "locals", "fromlist", "level", 0};
if( !PyArg_ParseTupleAndKeywords( args, kw, "s|OOOi:bpy_import", kwlist,
&name, &globals, &locals, &fromlist, &dummy_val) )
return NULL;
#else
static char *kwlist[] = {"name", "globals", "locals", "fromlist", 0};
if( !PyArg_ParseTupleAndKeywords( args, kw, "s|OOO:bpy_import", kwlist,
&name, &globals, &locals, &fromlist ) )
return NULL;
#endif
m = PyImport_ImportModuleEx( name, globals, locals, fromlist );
if( m )
return m;
else
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" );
} else {
PyErr_Restore( exception, err, tb );
}
return m;
}
/*
* our reload() module, to handle reloading in-memory scripts
*/
static PyObject *blender_reload( PyObject * self, PyObject * args )
{
PyObject *exception, *err, *tb;
PyObject *module = NULL;
PyObject *newmodule = NULL;
/* check for a module arg */
if( !PyArg_ParseTuple( args, "O:bpy_reload", &module ) )
return NULL;
/* try reimporting from file */
newmodule = PyImport_ReloadModule( module );
if( newmodule )
return newmodule;
/* no file, try importing from memory */
PyErr_Fetch( &exception, &err, &tb ); /*restore for probable later use */
newmodule = reimportText( module );
if( newmodule ) { /* found module, ignore above exception */
PyErr_Clear( );
Py_XDECREF( exception );
Py_XDECREF( err );
Py_XDECREF( tb );
} else
PyErr_Restore( exception, err, tb );
return newmodule;
}
PyMethodDef bpy_import[] = { {"bpy_import", blender_import, METH_KEYWORDS, "blenders import"} };
PyMethodDef bpy_reload[] = { {"bpy_reload", blender_reload, METH_VARARGS, "blenders reload"} };

View File

@@ -0,0 +1,48 @@
/*
* $Id: bpy_types.h 14444 2008-04-16 22:40:48Z hos $
* ***** BEGIN GPL 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
* All rights reserved.
*
* This is a new part of Blender.
*
* Contributor(s): Willian P. Germano, Campbell Barton
*
* ***** END GPL LICENSE BLOCK *****
*/
/* Note, the BGE needs to use this too, keep it minimal */
#ifndef EXPP_bpy_import_h
#define EXPP_bpy_import_h
#include <Python.h>
#include "compile.h" /* for the PyCodeObject */
#include "eval.h" /* for PyEval_EvalCode */
PyObject *importText( char *name );
PyObject *reimportText( PyObject *module );
extern PyMethodDef bpy_import[];
extern PyMethodDef bpy_reload[];
/* The game engine has its own Main struct, if this is set search this rather then G.main */
struct Main *bpy_import_main_get(void);
void bpy_import_main_set(struct Main *maggie);
#endif /* EXPP_bpy_import_h */

View File

@@ -357,7 +357,7 @@ extern "C" void StartKetsjiShell(struct ScrArea *area,
blscene);
// some python things
PyObject* dictionaryobject = initGamePythonScripting("Ketsji", psl_Lowest);
PyObject* dictionaryobject = initGamePythonScripting("Ketsji", psl_Lowest, blenderdata);
ketsjiengine->SetPythonDictionary(dictionaryobject);
initRasterizer(rasterizer, canvas);
PyObject *gameLogic = initGameLogic(ketsjiengine, startscene);
@@ -656,7 +656,7 @@ extern "C" void StartKetsjiShellSimulation(struct ScrArea *area,
blscene);
// some python things
PyObject* dictionaryobject = initGamePythonScripting("Ketsji", psl_Lowest);
PyObject* dictionaryobject = initGamePythonScripting("Ketsji", psl_Lowest, blenderdata);
ketsjiengine->SetPythonDictionary(dictionaryobject);
initRasterizer(rasterizer, canvas);
PyObject *gameLogic = initGameLogic(ketsjiengine, startscene);

View File

@@ -681,7 +681,7 @@ bool GPG_Application::startEngine(void)
// some python things
PyObject* dictionaryobject = initGamePlayerPythonScripting("Ketsji", psl_Lowest);
PyObject* dictionaryobject = initGamePlayerPythonScripting("Ketsji", psl_Lowest, m_maggie);
m_ketsjiengine->SetPythonDictionary(dictionaryobject);
initRasterizer(m_rasterizer, m_canvas);
PyObject *gameLogic = initGameLogic(m_ketsjiengine, startscene);

View File

@@ -35,6 +35,7 @@ SET(SRC
../../../source/blender/python/api2_2x/point.c
../../../source/blender/python/api2_2x/quat.c
../../../source/blender/python/api2_2x/vector.c
../../../source/blender/python/api2_2x/bpy_internal_import.c
)
SET(INC

View File

@@ -77,6 +77,7 @@
extern "C" {
#include "Mathutils.h" // Blender.Mathutils module copied here so the blenderlayer can use.
#include "bpy_internal_import.h" /* from the blender python api, but we want to import text too! */
}
#include "marshal.h" /* python header for loading/saving dicts */
@@ -1107,11 +1108,6 @@ PyObject *KXpy_open(PyObject *self, PyObject *args) {
return NULL;
}
PyObject *KXpy_reload(PyObject *self, PyObject *args) {
PyErr_SetString(PyExc_RuntimeError, "Sandbox: reload() function disabled!\nGame Scripts should not use this function.");
return NULL;
}
PyObject *KXpy_file(PyObject *self, PyObject *args) {
PyErr_SetString(PyExc_RuntimeError, "Sandbox: file() function disabled!\nGame Scripts should not use this function.");
return NULL;
@@ -1163,12 +1159,40 @@ PyObject *KXpy_import(PyObject *self, PyObject *args)
return PyImport_ImportModuleEx(name, globals, locals, fromlist);
}
/* Import blender texts as python modules */
m= importText(name);
if (m)
return m;
PyErr_Format(PyExc_ImportError,
"Import of external Module %.20s not allowed.", name);
return NULL;
}
PyObject *KXpy_reload(PyObject *self, PyObject *args) {
/* Used to be sandboxed, bettet to allow importing of internal text only */
#if 0
PyErr_SetString(PyExc_RuntimeError, "Sandbox: reload() function disabled!\nGame Scripts should not use this function.");
return NULL;
#endif
PyObject *module = NULL;
PyObject *newmodule = NULL;
/* check for a module arg */
if( !PyArg_ParseTuple( args, "O:bpy_reload", &module ) )
return NULL;
newmodule= reimportText( module );
if (newmodule==NULL)
PyErr_SetString(PyExc_ImportError, "failed to reload from blenders internal text");
return newmodule;
}
/* override python file type functions */
#if 0
static int
@@ -1241,6 +1265,9 @@ void setSandbox(TPythonSecurityLevel level)
}
*/
default:
/* Allow importing internal text, from bpy_internal_import.py */
PyDict_SetItemString(d, "reload", PyCFunction_New(bpy_reload, NULL));
PyDict_SetItemString(d, "__import__", PyCFunction_New(bpy_import, NULL));
break;
}
}
@@ -1248,7 +1275,7 @@ void setSandbox(TPythonSecurityLevel level)
/**
* Python is not initialised.
*/
PyObject* initGamePlayerPythonScripting(const STR_String& progname, TPythonSecurityLevel level)
PyObject* initGamePlayerPythonScripting(const STR_String& progname, TPythonSecurityLevel level, Main *maggie)
{
STR_String pname = progname;
Py_SetProgramName(pname.Ptr());
@@ -1261,6 +1288,8 @@ PyObject* initGamePlayerPythonScripting(const STR_String& progname, TPythonSecur
setSandbox(level);
initPyTypes();
bpy_import_main_set(maggie);
PyObject* moduleobj = PyImport_AddModule("__main__");
return PyModule_GetDict(moduleobj);
}
@@ -1268,12 +1297,13 @@ PyObject* initGamePlayerPythonScripting(const STR_String& progname, TPythonSecur
void exitGamePlayerPythonScripting()
{
Py_Finalize();
bpy_import_main_set(NULL);
}
/**
* Python is already initialized.
*/
PyObject* initGamePythonScripting(const STR_String& progname, TPythonSecurityLevel level)
PyObject* initGamePythonScripting(const STR_String& progname, TPythonSecurityLevel level, Main *maggie)
{
STR_String pname = progname;
Py_SetProgramName(pname.Ptr());
@@ -1283,6 +1313,8 @@ PyObject* initGamePythonScripting(const STR_String& progname, TPythonSecurityLev
setSandbox(level);
initPyTypes();
bpy_import_main_set(maggie);
PyObject* moduleobj = PyImport_AddModule("__main__");
return PyModule_GetDict(moduleobj);
}
@@ -1291,6 +1323,7 @@ PyObject* initGamePythonScripting(const STR_String& progname, TPythonSecurityLev
void exitGamePythonScripting()
{
bpy_import_main_set(NULL);
}

View File

@@ -43,11 +43,11 @@ extern bool gUseVisibilityTemp;
PyObject* initGameLogic(class KX_KetsjiEngine *engine, class KX_Scene* ketsjiscene);
PyObject* initGameKeys();
PyObject* initRasterizer(class RAS_IRasterizer* rasty,class RAS_ICanvas* canvas);
PyObject* initGamePlayerPythonScripting(const STR_String& progname, TPythonSecurityLevel level);
PyObject* initGamePlayerPythonScripting(const STR_String& progname, TPythonSecurityLevel level, struct Main *maggie);
PyObject* initMathutils();
PyObject* initVideoTexture(void);
void exitGamePlayerPythonScripting();
PyObject* initGamePythonScripting(const STR_String& progname, TPythonSecurityLevel level);
PyObject* initGamePythonScripting(const STR_String& progname, TPythonSecurityLevel level, struct Main *maggie);
void exitGamePythonScripting();
void setGamePythonPath(char *path);

View File

@@ -18,7 +18,11 @@ sources.extend([\
'#source/blender/python/api2_2x/vector.c',\
])
incs = '. #source/blender/python/api2_2x' # Only for Mathutils! - no other deps
sources.extend([\
'#source/blender/python/api2_2x/bpy_internal_import.c'
])
incs = '. #source/blender/python/api2_2x' # Only for Mathutils! and bpy_internal_import.h, be very careful
incs += ' #source/kernel/gen_system #intern/string #intern/guardedalloc'
incs += ' #source/gameengine/Rasterizer/RAS_OpenGLRasterizer #intern/bmfont'