- 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.
This commit is contained in:
@@ -65,4 +65,3 @@ void BPY_copy_scriptlink(struct ScriptLink *scriptlink);
|
||||
int BPY_call_importloader(char *name);
|
||||
|
||||
void init_syspath(void);
|
||||
|
||||
|
@@ -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);
|
||||
}
|
||||
|
@@ -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,
|
||||
|
Reference in New Issue
Block a user