Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWillian Padovani Germano <wpgermano@gmail.com>2004-03-31 08:18:39 +0400
committerWillian Padovani Germano <wpgermano@gmail.com>2004-03-31 08:18:39 +0400
commitfa0196b8f920c6662cf7bc1d74161febb36787cb (patch)
tree73d9b4be486aaccd67aafc7c73f9be1be52ffedd /source/blender/python/BPY_interface.c
parent2b27a909f022ba568a7404d5283f70f8a569ff0e (diff)
BPython:
- tentative fix for scripts with CR/LF endings and split lines: in 2.32, the ac3d and vrml2 exporters, for example, had lines split with '\\\\' and so gave syntax errors when executed on Win platforms, because the scripts bundled with Win binaries had dos line endings. - Chris Keith has written code to execute Python scripts from the command-line, with '-P ' switch: "blender -P filename": a Blender.Quit function was also added, so Blender can quit after running the script (end the script with Blender.Quit()), but there's still work to be done in this part, including adding more functions, to load / save .blend files and to run scripts. More testing and discussions are necessary. Thanks Chris, for both your contributions and your patience, since I wasn't available to check / commit this for a while.
Diffstat (limited to 'source/blender/python/BPY_interface.c')
-rw-r--r--source/blender/python/BPY_interface.c77
1 files changed, 67 insertions, 10 deletions
diff --git a/source/blender/python/BPY_interface.c b/source/blender/python/BPY_interface.c
index 2e9cbd7b3f2..5bc64d20c22 100644
--- a/source/blender/python/BPY_interface.c
+++ b/source/blender/python/BPY_interface.c
@@ -24,7 +24,8 @@
*
* This is a new part of Blender.
*
- * Contributor(s): Michel Selten, Willian P. Germano, Stephen Swaney
+ * Contributor(s): Michel Selten, Willian P. Germano, Stephen Swaney,
+ * Chris Keith
*
* ***** END GPL/BL DUAL LICENSE BLOCK *****
*/
@@ -89,7 +90,6 @@ typedef struct _ScriptError {
/* Global variables */
/*****************************************************************************/
ScriptError g_script_error;
-short EXPP_releaseGlobalDict = 1;
/*****************************************************************************/
/* Function prototypes */
@@ -388,17 +388,17 @@ void BPY_Err_Handle(char *script_name)
/* Notes: It is called by blender/src/drawtext.c when a Blender user */
/* presses ALT+PKEY in the script's text window. */
/*****************************************************************************/
-int BPY_txt_do_python(struct SpaceText* st)
+int BPY_txt_do_python_Text(struct Text* text)
{
PyObject *py_dict, *py_result;
BPy_constant *info;
Script *script = G.main->script.first;
- if (!st->text) return 0;
+ if (!text) return 0;
/* check if this text is already running */
while (script) {
- if (!strcmp(script->id.name+2, st->text->id.name+2)) {
+ if (!strcmp(script->id.name+2, text->id.name+2)) {
/* if this text is already a running script, just move to it: */
SpaceScript *sc;
newspace(curarea, SPACE_SCRIPT);
@@ -410,7 +410,7 @@ int BPY_txt_do_python(struct SpaceText* st)
}
/* Create a new script structure and initialize it: */
- script = alloc_libblock(&G.main->script, ID_SCRIPT, GetName(st->text));
+ script = alloc_libblock(&G.main->script, ID_SCRIPT, GetName(text));
if (!script) {
printf("couldn't allocate memory for Script struct!");
@@ -437,11 +437,11 @@ int BPY_txt_do_python(struct SpaceText* st)
clearScriptLinks ();
- py_result = RunPython (st->text, py_dict); /* Run the script */
+ py_result = RunPython (text, py_dict); /* Run the script */
if (!py_result) { /* Failed execution of the script */
- BPY_Err_Handle(GetName(st->text));
+ BPY_Err_Handle(GetName(text));
ReleaseGlobalDictionary(py_dict);
free_libblock(&G.main->script, script);
//BPY_end_python();
@@ -463,6 +463,47 @@ int BPY_txt_do_python(struct SpaceText* st)
}
/*****************************************************************************/
+/* Description: The original function of this name has been refactored */
+/* into BPY_txt_do_python_Text. That version is needed for the command */
+/* line support for Python. This is here to keep the interface the */
+/* same and reduce code changes elsewhere. */
+/*****************************************************************************/
+int BPY_txt_do_python(struct SpaceText* st)
+{
+ return BPY_txt_do_python_Text(st->text);
+}
+
+/*****************************************************************************/
+/* Description: Called from command line to run a Python script
+* automatically. */
+/*****************************************************************************/
+void BPY_run_python_script(char *fn)
+{
+ Text *text;
+
+ if ( !BLI_exists(fn) ) {
+ printf("\nError: no such file -- %s.\n", fn);
+ return;
+ }
+
+ text = add_text(fn);
+ if (text == NULL) {
+ printf("Error in BPY_run_python_script: couldn't create Blender text "
+ "from %s\n", fn);
+ // On Windows if I continue I just get a segmentation
+ // violation. To get a baseline file I exit here.
+ exit(2);
+ }
+
+ if (BPY_txt_do_python_Text(text) != 1) {
+ printf( "\nError executing Python script:\n"
+ "%s (at line %d)\n", fn, BPY_Err_getLinenumber());
+ }
+
+ free_libblock(&G.main->text, text);
+}
+
+/*****************************************************************************/
/* Description: This function executes the script chosen from a menu. */
/* Notes: It is called by the ui code in src/header_???.c when a user */
/* clicks on a menu entry that refers to a script. */
@@ -572,8 +613,24 @@ int BPY_menu_do_python(short menutype, int event)
buffer[len] = '\0';
- /* fast clean-up of dos lines */
- for (s = buffer; *s != '\0'; s++) if (*s == '\r') *s = ' ';
+ /* fast clean-up of dos cr/lf line endings: change '\r' to space */
+
+ /* we also have to check for line splitters: '\\' */
+ /* to avoid possible syntax errors on dos files on win */
+ /**/
+ /* but first make sure we won't disturb memory below &buffer[0]: */
+ if (*buffer == '\r') *buffer = ' ';
+
+ /* now handle the whole buffer */
+ for (s = buffer + 1; *s != '\0'; s++) {
+ if (*s == '\r') {
+ if (*(s-1) == '\\') { /* special case: long lines split with '\': */
+ *(s-1) = ' '; /* we write ' \', because '\ ' is a syntax error */
+ *s = '\\';
+ }
+ else *s = ' '; /* not a split line, just replace '\r' with ' ' */
+ }
+ }
fclose(fp);