[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [54448] trunk/blender/source/creator/ creator.c: Command line argument '--python' wasn' t working as documented since a long time, It wasnt checking for text-blocks.

Campbell Barton ideasman42 at gmail.com
Mon Feb 11 04:01:53 CET 2013


Revision: 54448
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=54448
Author:   campbellbarton
Date:     2013-02-11 03:01:50 +0000 (Mon, 11 Feb 2013)
Log Message:
-----------
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.

Modified Paths:
--------------
    trunk/blender/source/creator/creator.c

Modified: trunk/blender/source/creator/creator.c
===================================================================
--- trunk/blender/source/creator/creator.c	2013-02-11 02:06:19 UTC (rev 54447)
+++ trunk/blender/source/creator/creator.c	2013-02-11 03:01:50 UTC (rev 54448)
@@ -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 @@
 	printf("\n");
 
 	BLI_argsPrintArgDoc(ba, "--python");
+	BLI_argsPrintArgDoc(ba, "--python-text");
 	BLI_argsPrintArgDoc(ba, "--python-console");
 	BLI_argsPrintArgDoc(ba, "--addons");
 
@@ -1089,11 +1091,11 @@
 			_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,12 @@
 		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
@@ -1120,12 +1122,42 @@
 #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
+	(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_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 @@
 	BLI_argsAdd(ba, 4, "-s", "--frame-start", "<frame>\n\tSet start to frame <frame> (use before the -a argument)", set_start_frame, C);
 	BLI_argsAdd(ba, 4, "-e", "--frame-end", "<frame>\n\tSet end to frame <frame> (use before the -a argument)", set_end_frame, C);
 	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 (filename or Blender Text)", run_python, 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-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