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:
authorCampbell Barton <ideasman42@gmail.com>2011-02-21 02:39:29 +0300
committerCampbell Barton <ideasman42@gmail.com>2011-02-21 02:39:29 +0300
commitc30149991c9417106577e2d96112b16433910215 (patch)
tree9b686c141ddf2c9346ebe0beb3b3cfc3aaacf73f /source/blender/python
parent55a0e21a03e88e5489dd6f53a91b6f3a6f770d9a (diff)
Experimental option to build blender as a python module, rather then blender embedding python.
CMake build option WITH_PYTHON_MODULE, will build ./bin/bpy.so This allows 'bpy' to be imported from python or other applications/IDE's which embed python, eg: python -c "import bpy ; bpy.ops.render.render(write_still=True)" This runs in background mode and has similar restrictions to running a script: blender --background --python test.py TODO: - install to site-packages with blender scripts - add support for imp.reload()
Diffstat (limited to 'source/blender/python')
-rw-r--r--source/blender/python/intern/CMakeLists.txt4
-rw-r--r--source/blender/python/intern/bpy.c10
-rw-r--r--source/blender/python/intern/bpy.h2
-rw-r--r--source/blender/python/intern/bpy_interface.c64
4 files changed, 72 insertions, 8 deletions
diff --git a/source/blender/python/intern/CMakeLists.txt b/source/blender/python/intern/CMakeLists.txt
index 5d1a086bc75..b73c9949ada 100644
--- a/source/blender/python/intern/CMakeLists.txt
+++ b/source/blender/python/intern/CMakeLists.txt
@@ -67,4 +67,8 @@ if(WITH_BUILDINFO)
add_definitions(-DBUILD_DATE)
endif()
+if(WITH_PYTHON_MODULE)
+ add_definitions(-DWITH_PYTHON_MODULE)
+endif()
+
blender_add_lib(bf_python "${SRC}" "${INC}")
diff --git a/source/blender/python/intern/bpy.c b/source/blender/python/intern/bpy.c
index 8076515b94b..02073f368e8 100644
--- a/source/blender/python/intern/bpy.c
+++ b/source/blender/python/intern/bpy.c
@@ -53,6 +53,8 @@
#include "AUD_PyInit.h"
+PyObject *bpy_package_py= NULL;
+
static char bpy_script_paths_doc[] =
".. function:: script_paths()\n"
"\n"
@@ -161,7 +163,7 @@ static PyMethodDef meth_bpy_script_paths = {"script_paths", (PyCFunction)bpy_scr
static PyMethodDef meth_bpy_blend_paths = {"blend_paths", (PyCFunction)bpy_blend_paths, METH_VARARGS|METH_KEYWORDS, bpy_blend_paths_doc};
static PyMethodDef meth_bpy_user_resource = {"user_resource", (PyCFunction)bpy_user_resource, METH_VARARGS|METH_KEYWORDS, NULL};
-static void bpy_import_test(const char *modname)
+static PyObject *bpy_import_test(const char *modname)
{
PyObject *mod= PyImport_ImportModuleLevel((char *)modname, NULL, NULL, NULL, 0);
if(mod) {
@@ -170,7 +172,9 @@ static void bpy_import_test(const char *modname)
else {
PyErr_Print();
PyErr_Clear();
- }
+ }
+
+ return mod;
}
/*****************************************************************************
@@ -235,5 +239,5 @@ void BPy_init_modules( void )
PyModule_AddObject(mod, meth_bpy_unregister_class.ml_name, (PyObject *)PyCFunction_New(&meth_bpy_unregister_class, NULL));
/* add our own modules dir, this is a python package */
- bpy_import_test("bpy");
+ bpy_package_py= bpy_import_test("bpy");
}
diff --git a/source/blender/python/intern/bpy.h b/source/blender/python/intern/bpy.h
index 76eef6ea4b9..90f21cb07b1 100644
--- a/source/blender/python/intern/bpy.h
+++ b/source/blender/python/intern/bpy.h
@@ -22,4 +22,4 @@
* ***** END GPL LICENSE BLOCK ***** */
void BPy_init_modules( void );
-
+extern PyObject *bpy_package_py;
diff --git a/source/blender/python/intern/bpy_interface.c b/source/blender/python/intern/bpy_interface.c
index 4ae1b41bb89..d1773e5fd7a 100644
--- a/source/blender/python/intern/bpy_interface.c
+++ b/source/blender/python/intern/bpy_interface.c
@@ -157,6 +157,7 @@ void BPY_modules_update(bContext *C)
}
/* must be called before Py_Initialize */
+#ifndef WITH_PYTHON_MODULE
static void bpy_python_start_path(void)
{
char *py_path_bundle= BLI_get_folder(BLENDER_PYTHON, NULL);
@@ -195,8 +196,7 @@ static void bpy_python_start_path(void)
// printf("found python (wchar_t) '%ls'\n", py_path_bundle_wchar);
}
}
-
-
+#endif
void BPY_context_set(bContext *C)
{
@@ -219,8 +219,9 @@ static struct _inittab bpy_internal_modules[]= {
/* call BPY_context_set first */
void BPY_python_start(int argc, const char **argv)
{
+#ifndef WITH_PYTHON_MODULE
PyThreadState *py_tstate = NULL;
-
+
/* not essential but nice to set our name */
static wchar_t bprogname_wchar[FILE_MAXDIR+FILE_MAXFILE]; /* python holds a reference */
utf8towchar(bprogname_wchar, bprogname);
@@ -252,8 +253,13 @@ void BPY_python_start(int argc, const char **argv)
/* Initialize thread support (also acquires lock) */
PyEval_InitThreads();
+#else
+ (void)argc;
+ (void)argv;
-
+ PyImport_ExtendInittab(bpy_internal_modules);
+#endif
+
/* bpy.* and lets us import it */
BPy_init_modules();
@@ -281,8 +287,10 @@ void BPY_python_start(int argc, const char **argv)
pyrna_alloc_types();
+#ifndef WITH_PYTHON_MODULE
py_tstate = PyGILState_GetThisThreadState();
PyEval_ReleaseThread(py_tstate);
+#endif
}
void BPY_python_end(void)
@@ -659,3 +667,51 @@ int BPY_context_member_get(bContext *C, const char *member, bContextDataResult *
return done;
}
+
+#ifdef WITH_PYTHON_MODULE
+/* TODO, reloading the module isnt functional at the moment. */
+
+extern int main_python(int argc, const char **argv);
+static struct PyModuleDef bpy_proxy_def = {
+ PyModuleDef_HEAD_INIT,
+ "bpy", /* m_name */
+ NULL, /* m_doc */
+ 0, /* m_size */
+ NULL, /* m_methods */
+ NULL, /* m_reload */
+ NULL, /* m_traverse */
+ NULL, /* m_clear */
+ NULL, /* m_free */
+};
+
+PyMODINIT_FUNC
+PyInit_bpy(void)
+{
+ int argc= 0;
+ const char *argv[]={NULL};
+
+ main_python(argc, argv);
+
+ /* initialized in BPy_init_modules() */
+ if(bpy_package_py) {
+ /* Problem:
+ * 1) this init function is expected to have a private member defined - 'md_def'
+ * but this is only set for C defined modules (not py packages)
+ * so we cant return 'bpy_package_py' as is.
+ *
+ * 2) there is a 'bpy' C module for python to load which is basically all of blender,
+ * and there is scripts/bpy/__init__.py,
+ * we may end up having to rename this module so there is no naming conflict here eg:
+ * 'from blender import bpy' */
+ PyObject *bpy_proxy= PyModule_Create(&bpy_proxy_def);
+ PyDict_Update(PyModule_GetDict(bpy_proxy), PyModule_GetDict(bpy_package_py));
+ return bpy_proxy;
+ }
+ else {
+ PyErr_SetString(PyExc_RuntimeError, "could not import internal bpy package");
+ return NULL;
+ }
+
+
+}
+#endif