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.
This commit is contained in:
Campbell Barton 2015-06-11 16:57:31 +10:00
parent 087c82e392
commit 958c20872a
3 changed files with 30 additions and 2 deletions

View File

@ -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() */

View File

@ -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)
{

View File

@ -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", "<frames>\n\tSet number of frames to step forward after each rendered frame", set_skip_frame, C);
BLI_argsAdd(ba, 4, "-P", "--python", "<filename>\n\tRun the given Python script file", run_python_file, C);
BLI_argsAdd(ba, 4, NULL, "--python-text", "<name>\n\tRun the given Python script text block", run_python_text, C);
BLI_argsAdd(ba, 4, NULL, "--python-expr", "<expression>\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);