diff options
author | Willian Padovani Germano <wpgermano@gmail.com> | 2003-07-03 05:42:00 +0400 |
---|---|---|
committer | Willian Padovani Germano <wpgermano@gmail.com> | 2003-07-03 05:42:00 +0400 |
commit | dfa7a48407cd77e49204480337b7e85d49edc594 (patch) | |
tree | 4760b3276305f61c73d2f1338b23a2c17bc4c6df | |
parent | 1b726ba0805236f0253c6fc7ac3de24ba88c2cd0 (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.
-rw-r--r-- | source/blender/python/BPY_extern.h | 1 | ||||
-rw-r--r-- | source/blender/python/BPY_interface.c | 91 | ||||
-rw-r--r-- | source/blender/python/api2_2x/Lamp.c | 2 |
3 files changed, 91 insertions, 3 deletions
diff --git a/source/blender/python/BPY_extern.h b/source/blender/python/BPY_extern.h index ccf0457aa21..794d8f3b637 100644 --- a/source/blender/python/BPY_extern.h +++ b/source/blender/python/BPY_extern.h @@ -65,4 +65,3 @@ void BPY_copy_scriptlink(struct ScriptLink *scriptlink); int BPY_call_importloader(char *name); void init_syspath(void); - 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); +} diff --git a/source/blender/python/api2_2x/Lamp.c b/source/blender/python/api2_2x/Lamp.c index ad023352644..a5abfb5ebd3 100644 --- a/source/blender/python/api2_2x/Lamp.c +++ b/source/blender/python/api2_2x/Lamp.c @@ -72,8 +72,6 @@ static PyObject *M_Lamp_New(PyObject *self, PyObject *args, PyObject *keywords) Lamp *bl_lamp; /* for actual Lamp Data we create in Blender */ char buf[21]; - printf ("In Lamp_New()\n"); - if (!PyArg_ParseTupleAndKeywords(args, keywords, "|ss", kwlist, &type_str, &name_str)) return (EXPP_ReturnPyObjError (PyExc_AttributeError, |