2 * $Id: bpy_types.h 14444 2008-04-16 22:40:48Z hos $
3 * ***** BEGIN GPL LICENSE BLOCK *****
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20 * All rights reserved.
22 * This is a new part of Blender.
24 * Contributor(s): Willian P. Germano
26 * ***** END GPL LICENSE BLOCK *****
29 #include "bpy_internal_import.h"
30 #include "DNA_text_types.h"
33 #include "BKE_global.h"
34 #include "MEM_guardedalloc.h"
35 #include "BKE_text.h" /* txt_to_buf */
38 static Main *bpy_import_main= NULL;
40 static void free_compiled_text(Text *text)
43 Py_DECREF(( PyObject * )text->compiled);
48 struct Main *bpy_import_main_get(void)
50 return bpy_import_main;
53 void bpy_import_main_set(struct Main *maggie)
55 bpy_import_main= maggie;
59 PyObject *importText( char *name )
62 char txtname[22]; /* 21+NULL */
64 int namelen = strlen( name );
65 Main *maggie= bpy_import_main ? bpy_import_main:G.main;
67 if (namelen>21-3) return NULL; /* we know this cant be importable, the name is too long for blender! */
69 memcpy( txtname, name, namelen );
70 memcpy( &txtname[namelen], ".py", 4 );
72 for(text = maggie->text.first; text; text = text->id.next) {
73 if( !strcmp( txtname, text->id.name+2 ) )
80 if( !text->compiled ) {
81 buf = txt_to_buf( text );
82 text->compiled = Py_CompileString( buf, text->id.name+2, Py_file_input );
85 if( PyErr_Occurred( ) ) {
87 free_compiled_text( text );
92 return PyImport_ExecCodeModule( name, text->compiled );
97 * find in-memory module and recompile
100 PyObject *reimportText( PyObject *module )
106 Main *maggie= bpy_import_main ? bpy_import_main:G.main;
108 /* get name, filename from the module itself */
110 txtname = PyModule_GetFilename( module );
111 name = PyModule_GetName( module );
112 if( !txtname || !name)
115 /* look up the text object */
116 text = ( Text * ) & ( maggie->text.first );
118 if( !strcmp( txtname, text->id.name+2 ) )
120 text = text->id.next;
123 /* uh-oh.... didn't find it */
127 /* if previously compiled, free the object */
128 /* (can't see how could be NULL, but check just in case) */
129 if( text->compiled ){
130 Py_DECREF( (PyObject *)text->compiled );
133 /* compile the buffer */
134 buf = txt_to_buf( text );
135 text->compiled = Py_CompileString( buf, text->id.name+2, Py_file_input );
138 /* if compile failed.... return this error */
139 if( PyErr_Occurred( ) ) {
141 free_compiled_text( text );
145 /* make into a module */
146 return PyImport_ExecCodeModule( name, text->compiled );
150 static PyObject *blender_import( PyObject * self, PyObject * args, PyObject * kw)
152 PyObject *exception, *err, *tb;
154 PyObject *globals = NULL, *locals = NULL, *fromlist = NULL;
157 //PyObject_Print(args, stderr, 0);
158 #if (PY_VERSION_HEX >= 0x02060000)
159 int dummy_val; /* what does this do?*/
160 static char *kwlist[] = {"name", "globals", "locals", "fromlist", "level", 0};
162 if( !PyArg_ParseTupleAndKeywords( args, kw, "s|OOOi:bpy_import", kwlist,
163 &name, &globals, &locals, &fromlist, &dummy_val) )
166 static char *kwlist[] = {"name", "globals", "locals", "fromlist", 0};
168 if( !PyArg_ParseTupleAndKeywords( args, kw, "s|OOO:bpy_import", kwlist,
169 &name, &globals, &locals, &fromlist ) )
172 m = PyImport_ImportModuleEx( name, globals, locals, fromlist );
177 PyErr_Fetch( &exception, &err, &tb ); /*restore for probable later use */
179 m = importText( name );
180 if( m ) { /* found module, ignore above exception */
182 Py_XDECREF( exception );
185 printf( "imported from text buffer...\n" );
187 PyErr_Restore( exception, err, tb );
194 * our reload() module, to handle reloading in-memory scripts
197 static PyObject *blender_reload( PyObject * self, PyObject * args )
199 PyObject *exception, *err, *tb;
200 PyObject *module = NULL;
201 PyObject *newmodule = NULL;
203 /* check for a module arg */
204 if( !PyArg_ParseTuple( args, "O:bpy_reload", &module ) )
207 /* try reimporting from file */
208 newmodule = PyImport_ReloadModule( module );
212 /* no file, try importing from memory */
213 PyErr_Fetch( &exception, &err, &tb ); /*restore for probable later use */
215 newmodule = reimportText( module );
216 if( newmodule ) { /* found module, ignore above exception */
218 Py_XDECREF( exception );
222 PyErr_Restore( exception, err, tb );
227 PyMethodDef bpy_import[] = { {"bpy_import", blender_import, METH_KEYWORDS, "blenders import"} };
228 PyMethodDef bpy_reload[] = { {"bpy_reload", blender_reload, METH_VARARGS, "blenders reload"} };