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

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWillian Padovani Germano <wpgermano@gmail.com>2003-07-03 05:42:00 +0400
committerWillian Padovani Germano <wpgermano@gmail.com>2003-07-03 05:42:00 +0400
commitdfa7a48407cd77e49204480337b7e85d49edc594 (patch)
tree4760b3276305f61c73d2f1338b23a2c17bc4c6df /source/blender/python/BPY_interface.c
parent1b726ba0805236f0253c6fc7ac3de24ba88c2cd0 (diff)
- exppython now can import modules contained in Blender Texts:
The Python import function was substituted by our own one (like done in the old bpython) to also check Blender Texts upon importing.
Diffstat (limited to 'source/blender/python/BPY_interface.c')
-rw-r--r--source/blender/python/BPY_interface.c91
1 files changed, 91 insertions, 0 deletions
diff --git a/source/blender/python/BPY_interface.c b/source/blender/python/BPY_interface.c
index 1b3ff468361..ec03f90487f 100644
--- a/source/blender/python/BPY_interface.c
+++ b/source/blender/python/BPY_interface.c
@@ -85,6 +85,9 @@ 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 */
@@ -99,6 +102,8 @@ void BPY_start_python(void)
Py_Initialize ();
+ init_ourImport ();
+
initBlenderApi2_2x ();
init_syspath();
@@ -661,3 +666,89 @@ void DoAllScriptsFromList (ListBase *list, short event)
return;
}
+
+PyObject *importText(char *name)
+{
+ Text *text;
+ char *txtname;
+ char *buf = NULL;
+ int namelen = strlen(name);
+
+ txtname = malloc(namelen+3+1);
+ if (!txtname) return NULL;
+
+ memcpy(txtname, name, namelen);
+ memcpy(&txtname[namelen], ".py", 4);
+
+ text = (Text*) &(G.main->text.first);
+
+ while(text) {
+ if (!strcmp (txtname, GetName(text)))
+ break;
+ text = text->id.next;
+ }
+
+ if (!text) {
+ free(txtname);
+ return NULL;
+ }
+
+ 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);
+ return NULL;
+ }
+ }
+
+ free(txtname);
+ return PyImport_ExecCodeModule(name, text->compiled);
+}
+
+static PyMethodDef bimport[] = {
+ { "blimport", blender_import, METH_VARARGS, "our own import"}
+};
+
+PyObject *blender_import(PyObject *self, PyObject *args)
+{
+ PyObject *exception, *err, *tb;
+ char *name;
+ PyObject *globals = NULL, *locals = NULL, *fromlist = NULL;
+ PyObject *m;
+
+ if (!PyArg_ParseTuple(args, "s|OOO:bimport",
+ &name, &globals, &locals, &fromlist))
+ return NULL;
+
+ 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;
+}
+
+void init_ourImport(void)
+{
+ PyObject *m, *d;
+ PyObject *import = PyCFunction_New(bimport, NULL);
+
+ m = PyImport_AddModule("__builtin__");
+ d = PyModule_GetDict(m);
+ PyDict_SetItemString(d, "__import__", import);
+}