From 958c20872a662808be8353b33904f1e30c2d0fcf Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Thu, 11 Jun 2015 16:57:31 +1000 Subject: [PATCH] Add argument --python-expr to pass Python directly This works like Python's -c argument, handy to be able to avoid writing small scripts to disk. --- source/blender/python/BPY_extern.h | 1 + source/blender/python/intern/bpy_interface.c | 8 +++++-- source/creator/creator.c | 23 ++++++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/source/blender/python/BPY_extern.h b/source/blender/python/BPY_extern.h index a5cf0b94c62..815beebb159 100644 --- a/source/blender/python/BPY_extern.h +++ b/source/blender/python/BPY_extern.h @@ -86,6 +86,7 @@ void BPY_driver_reset(void); float BPY_driver_exec(struct ChannelDriver *driver, const float evaltime); int BPY_button_exec(struct bContext *C, const char *expr, double *value, const bool verbose); +int BPY_string_exec_ex(struct bContext *C, const char *expr, bool use_eval); int BPY_string_exec(struct bContext *C, const char *expr); void BPY_DECREF(void *pyob_ptr); /* Py_DECREF() */ diff --git a/source/blender/python/intern/bpy_interface.c b/source/blender/python/intern/bpy_interface.c index 4be63042c1e..2c893c37bd6 100644 --- a/source/blender/python/intern/bpy_interface.c +++ b/source/blender/python/intern/bpy_interface.c @@ -607,7 +607,7 @@ int BPY_button_exec(bContext *C, const char *expr, double *value, const bool ver return error_ret; } -int BPY_string_exec(bContext *C, const char *expr) +int BPY_string_exec_ex(bContext *C, const char *expr, bool use_eval) { PyGILState_STATE gilstate; PyObject *main_mod = NULL; @@ -630,7 +630,7 @@ int BPY_string_exec(bContext *C, const char *expr) bmain_back = bpy_import_main_get(); bpy_import_main_set(CTX_data_main(C)); - retval = PyRun_String(expr, Py_eval_input, py_dict, py_dict); + retval = PyRun_String(expr, use_eval ? Py_eval_input : Py_file_input, py_dict, py_dict); bpy_import_main_set(bmain_back); @@ -650,6 +650,10 @@ int BPY_string_exec(bContext *C, const char *expr) return error_ret; } +int BPY_string_exec(bContext *C, const char *expr) +{ + return BPY_string_exec_ex(C, expr, true); +} void BPY_modules_load_user(bContext *C) { diff --git a/source/creator/creator.c b/source/creator/creator.c index 2a96988a0bd..237dcec4b26 100644 --- a/source/creator/creator.c +++ b/source/creator/creator.c @@ -297,6 +297,7 @@ static int print_help(int UNUSED(argc), const char **UNUSED(argv), void *data) BLI_argsPrintArgDoc(ba, "--python"); BLI_argsPrintArgDoc(ba, "--python-text"); + BLI_argsPrintArgDoc(ba, "--python-expr"); BLI_argsPrintArgDoc(ba, "--python-console"); BLI_argsPrintArgDoc(ba, "--addons"); @@ -1283,6 +1284,27 @@ static int run_python_text(int argc, const char **argv, void *data) #endif /* WITH_PYTHON */ } +static int run_python_expr(int argc, const char **argv, void *data) +{ +#ifdef WITH_PYTHON + bContext *C = data; + + /* workaround for scripts not getting a bpy.context.scene, causes internal errors elsewhere */ + if (argc > 1) { + BPY_CTX_SETUP(BPY_string_exec_ex(C, argv[1], false)); + return 1; + } + else { + printf("\nError: you must specify a Python expression after '%s'.\n", argv[0]); + return 0; + } +#else + UNUSED_VARS(argc, argv, data); + printf("This blender was built without python support\n"); + return 0; +#endif /* WITH_PYTHON */ +} + static int run_python_console(int UNUSED(argc), const char **argv, void *data) { #ifdef WITH_PYTHON @@ -1549,6 +1571,7 @@ static void setupArguments(bContext *C, bArgs *ba, SYS_SystemHandle *syshandle) BLI_argsAdd(ba, 4, "-j", "--frame-jump", "\n\tSet number of frames to step forward after each rendered frame", set_skip_frame, C); BLI_argsAdd(ba, 4, "-P", "--python", "\n\tRun the given Python script file", run_python_file, C); BLI_argsAdd(ba, 4, NULL, "--python-text", "\n\tRun the given Python script text block", run_python_text, C); + BLI_argsAdd(ba, 4, NULL, "--python-expr", "\n\tRun the given expression as a Python script", run_python_expr, C); BLI_argsAdd(ba, 4, NULL, "--python-console", "\n\tRun blender with an interactive console", run_python_console, C); BLI_argsAdd(ba, 4, NULL, "--addons", "\n\tComma separated list of addons (no spaces)", set_addons, C);