[Bf-blender-cvs] [958c208] master: Add argument --python-expr to pass Python directly

Campbell Barton noreply at git.blender.org
Thu Jun 11 13:54:27 CEST 2015


Commit: 958c20872a662808be8353b33904f1e30c2d0fcf
Author: Campbell Barton
Date:   Thu Jun 11 16:57:31 2015 +1000
Branches: master
https://developer.blender.org/rB958c20872a662808be8353b33904f1e30c2d0fcf

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.

===================================================================

M	source/blender/python/BPY_extern.h
M	source/blender/python/intern/bpy_interface.c
M	source/creator/creator.c

===================================================================

diff --git a/source/blender/python/BPY_extern.h b/source/blender/python/BPY_extern.h
index a5cf0b9..815beeb 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 4be6304..2c893c3 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 2a96988..237dcec 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", "<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);




More information about the Bf-blender-cvs mailing list