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 fprintf(stderr, "%s | %s\n", txtname, text->id.name+2);
74 if( !strcmp( txtname, text->id.name+2 ) )
81 if( !text->compiled ) {
82 buf = txt_to_buf( text );
83 text->compiled = Py_CompileString( buf, text->id.name+2, Py_file_input );
86 if( PyErr_Occurred( ) ) {
88 free_compiled_text( text );
93 return PyImport_ExecCodeModule( name, text->compiled );
98 * find in-memory module and recompile
101 PyObject *reimportText( PyObject *module )
107 Main *maggie= bpy_import_main ? bpy_import_main:G.main;
109 /* get name, filename from the module itself */
111 txtname = PyModule_GetFilename( module );
112 name = PyModule_GetName( module );
113 if( !txtname || !name)
116 /* look up the text object */
117 text = ( Text * ) & ( maggie->text.first );
119 if( !strcmp( txtname, text->id.name+2 ) )
121 text = text->id.next;
124 /* uh-oh.... didn't find it */
128 /* if previously compiled, free the object */
129 /* (can't see how could be NULL, but check just in case) */
130 if( text->compiled ){
131 Py_DECREF( (PyObject *)text->compiled );
134 /* compile the buffer */
135 buf = txt_to_buf( text );
136 text->compiled = Py_CompileString( buf, text->id.name+2, Py_file_input );
139 /* if compile failed.... return this error */
140 if( PyErr_Occurred( ) ) {
142 free_compiled_text( text );
146 /* make into a module */
147 return PyImport_ExecCodeModule( name, text->compiled );
151 static PyObject *blender_import( PyObject * self, PyObject * args, PyObject * kw)
153 PyObject *exception, *err, *tb;
155 PyObject *globals = NULL, *locals = NULL, *fromlist = NULL;
158 //PyObject_Print(args, stderr, 0);
159 #if (PY_VERSION_HEX >= 0x02060000)
160 int dummy_val; /* what does this do?*/
161 static char *kwlist[] = {"name", "globals", "locals", "fromlist", "level", 0};
163 if( !PyArg_ParseTupleAndKeywords( args, kw, "s|OOOi:bpy_import", kwlist,
164 &name, &globals, &locals, &fromlist, &dummy_val) )
167 static char *kwlist[] = {"name", "globals", "locals", "fromlist", 0};
169 if( !PyArg_ParseTupleAndKeywords( args, kw, "s|OOO:bpy_import", kwlist,
170 &name, &globals, &locals, &fromlist ) )
173 m = PyImport_ImportModuleEx( name, globals, locals, fromlist );
178 PyErr_Fetch( &exception, &err, &tb ); /*restore for probable later use */
180 m = importText( name );
181 if( m ) { /* found module, ignore above exception */
183 Py_XDECREF( exception );
186 printf( "imported from text buffer...\n" );
188 PyErr_Restore( exception, err, tb );
195 * our reload() module, to handle reloading in-memory scripts
198 static PyObject *blender_reload( PyObject * self, PyObject * args )
200 PyObject *exception, *err, *tb;
201 PyObject *module = NULL;
202 PyObject *newmodule = NULL;
204 /* check for a module arg */
205 if( !PyArg_ParseTuple( args, "O:bpy_reload", &module ) )
208 /* try reimporting from file */
209 newmodule = PyImport_ReloadModule( module );
213 /* no file, try importing from memory */
214 PyErr_Fetch( &exception, &err, &tb ); /*restore for probable later use */
216 newmodule = reimportText( module );
217 if( newmodule ) { /* found module, ignore above exception */
219 Py_XDECREF( exception );
223 PyErr_Restore( exception, err, tb );
228 PyMethodDef bpy_import[] = { {"bpy_import", blender_import, METH_KEYWORDS, "blenders import"} };
229 PyMethodDef bpy_reload[] = { {"bpy_reload", blender_reload, METH_VARARGS, "blenders reload"} };