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()
This commit is contained in:
Campbell Barton
2011-02-20 23:39:29 +00:00
parent 55a0e21a03
commit c30149991c
9 changed files with 114 additions and 15 deletions

View File

@@ -127,8 +127,21 @@ if(WITH_BUILDINFO)
endif()
# message(STATUS "Configuring blender")
if(WITH_PYTHON_MODULE)
add_definitions(-DWITH_PYTHON_MODULE)
add_executable(blender ${EXETYPE} ${SRC})
# creates ./bin/bpy.so which can be imported as a python module.
add_library(blender SHARED ${SRC})
set_target_properties(
blender
PROPERTIES
PREFIX ""
OUTPUT_NAME bpy
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/
)
else()
add_executable(blender ${EXETYPE} ${SRC})
endif()
# Post build steps for bundling/packaging.

View File

@@ -154,6 +154,7 @@ static void fpe_handler(int UNUSED(sig))
}
#endif
#ifdef WITH_PYTHON_MODULE
/* handling ctrl-c event in console */
static void blender_esc(int sig)
{
@@ -170,6 +171,7 @@ static void blender_esc(int sig)
count++;
}
}
#endif
/* buildinfo can have quotes */
#ifdef BUILD_DATE
@@ -1111,12 +1113,21 @@ static void setupArguments(bContext *C, bArgs *ba, SYS_SystemHandle *syshandle)
}
int main(int argc, char **argv)
#ifdef WITH_PYTHON_MODULE
/* allow python module to call main */
#define main main_python
#endif
int main(int argc, const char **argv)
{
SYS_SystemHandle syshandle;
bContext *C= CTX_create();
bArgs *ba;
#ifdef WITH_PYTHON_MODULE
#undef main
#endif
#ifdef WITH_BINRELOC
br_init( NULL );
#endif
@@ -1192,10 +1203,13 @@ int main(int argc, char **argv)
setuid(getuid()); /* end superuser */
#endif
#ifdef WITH_PYTHON_MODULE
G.background= 1; /* python module mode ALWAYS runs in background mode (for now) */
#else
/* for all platforms, even windos has it! */
if(G.background) signal(SIGINT, blender_esc); /* ctrl c out bg render */
#endif
/* background render uses this font too */
BKE_font_register_builtin(datatoc_Bfont, datatoc_Bfont_size);
@@ -1248,6 +1262,10 @@ int main(int argc, char **argv)
BLI_argsFree(ba);
#ifdef WITH_PYTHON_MODULE
return 0; /* keep blender in background mode running */
#endif
if(G.background) {
/* actually incorrect, but works for now (ton) */
WM_exit(C);