From 20139348785dcd23ebe00db25f5e5726a23c685f Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Mon, 11 Feb 2013 03:01:50 +0000 Subject: Command line argument '--python' wasn't working as documented since a long time, It wasnt checking for text-blocks. Rather then be ambiguous, add --python-text argument for executing text-blocks. --- source/creator/creator.c | 45 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 6 deletions(-) (limited to 'source/creator') diff --git a/source/creator/creator.c b/source/creator/creator.c index fcfec947d9f..7ab5d9ff2bd 100644 --- a/source/creator/creator.c +++ b/source/creator/creator.c @@ -88,6 +88,7 @@ #include "BKE_depsgraph.h" /* for DAG_on_visible_update */ #include "BKE_font.h" #include "BKE_global.h" +#include "BKE_library.h" #include "BKE_main.h" #include "BKE_material.h" #include "BKE_packedFile.h" @@ -302,6 +303,7 @@ static int print_help(int UNUSED(argc), const char **UNUSED(argv), void *data) printf("\n"); BLI_argsPrintArgDoc(ba, "--python"); + BLI_argsPrintArgDoc(ba, "--python-text"); BLI_argsPrintArgDoc(ba, "--python-console"); BLI_argsPrintArgDoc(ba, "--addons"); @@ -1089,11 +1091,11 @@ static int set_skip_frame(int argc, const char **argv, void *data) _cmd; \ } \ CTX_data_scene_set(C, prevscene); \ - } \ + } (void)0 \ #endif /* WITH_PYTHON */ -static int run_python(int argc, const char **argv, void *data) +static int run_python_file(int argc, const char **argv, void *data) { #ifdef WITH_PYTHON bContext *C = data; @@ -1105,12 +1107,42 @@ static int run_python(int argc, const char **argv, void *data) BLI_strncpy(filename, argv[1], sizeof(filename)); BLI_path_cwd(filename); - BPY_CTX_SETUP(BPY_filepath_exec(C, filename, NULL)) + BPY_CTX_SETUP(BPY_filepath_exec(C, filename, NULL)); return 1; } else { - printf("\nError: you must specify a Python script after '-P / --python'.\n"); + printf("\nError: you must specify a filepath after '%s'.\n", argv[0]); + return 0; + } +#else + (void)argc; (void)argv; (void)data; /* unused */ + printf("This blender was built without python support\n"); + return 0; +#endif /* WITH_PYTHON */ +} + +static int run_python_text(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) { + /* Make the path absolute because its needed for relative linked blends to be found */ + struct Text *text = (struct Text *)BKE_libblock_find_name(ID_TXT, argv[1]); + + if (text) { + BPY_CTX_SETUP(BPY_text_exec(C, text, NULL, false)); + return 1; + } + else { + printf("\nError: text block not found %s.\n", argv[1]); + return 1; + } + } + else { + printf("\nError: you must specify a text block after '%s'.\n", argv[0]); return 0; } #else @@ -1125,7 +1157,7 @@ static int run_python_console(int UNUSED(argc), const char **argv, void *data) #ifdef WITH_PYTHON bContext *C = data; - BPY_CTX_SETUP(BPY_string_exec(C, "__import__('code').interact()")) + BPY_CTX_SETUP(BPY_string_exec(C, "__import__('code').interact()")); return 0; #else @@ -1353,7 +1385,8 @@ static void setupArguments(bContext *C, bArgs *ba, SYS_SystemHandle *syshandle) BLI_argsAdd(ba, 4, "-s", "--frame-start", "\n\tSet start to frame (use before the -a argument)", set_start_frame, C); BLI_argsAdd(ba, 4, "-e", "--frame-end", "\n\tSet end to frame (use before the -a argument)", set_end_frame, C); 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 (filename or Blender Text)", run_python, 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-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); -- cgit v1.2.3