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
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')
-rw-r--r--source/blender/python/BPY_extern.h7
-rw-r--r--source/blender/python/BPY_interface.c77
-rw-r--r--source/blender/python/api2_2x/Blender.c32
-rw-r--r--source/blender/python/api2_2x/Blender.h15
-rw-r--r--source/blender/python/api2_2x/Draw.c5
5 files changed, 92 insertions, 44 deletions
diff --git a/source/blender/python/BPY_extern.h b/source/blender/python/BPY_extern.h
index 66fd86a57a4..608f1f2d1f0 100644
--- a/source/blender/python/BPY_extern.h
+++ b/source/blender/python/BPY_extern.h
@@ -25,17 +25,13 @@
*
* The Original Code is: source/blender/bpyton/include/BPY_extern.h
*
- * Contributor(s): Michel Selten, Willian P. Germano
+ * Contributor(s): Michel Selten, Willian P. Germano, Chris Keith
*
* ***** END GPL/BL DUAL LICENSE BLOCK *****
*/
extern char bprogname[]; /* holds a copy of argv[0], from creator.c */
-/* Global to control whether the global dictionary should be preserved or not
- * each time a script is executed by the Python Interpreter: */
-extern short EXPP_releaseGlobalDict; /* defaults to TRUE */
-
struct Text; /* defined in DNA_text_types.h */
struct ID; /* defined in DNA_ID.h */
struct ScriptLink; /* defined in DNA_scriptlink_types.h */
@@ -54,6 +50,7 @@ int BPY_Err_getLinenumber(void);
const char *BPY_Err_getFilename(void);
/* void BPY_Err_Handle(struct Text *text); */
int BPY_txt_do_python(struct SpaceText* st);
+void BPY_run_python_script(char *filename);
void BPY_free_compiled_text(struct Text* text);
/*void BPY_clear_bad_scriptlink(struct ID *id, struct Text *byebye); */
void BPY_clear_bad_scriptlinks(struct Text *byebye);
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);
diff --git a/source/blender/python/api2_2x/Blender.c b/source/blender/python/api2_2x/Blender.c
index 3f83185fc79..29c6e4fbdd1 100644
--- a/source/blender/python/api2_2x/Blender.c
+++ b/source/blender/python/api2_2x/Blender.c
@@ -29,6 +29,9 @@
* ***** END GPL/BL DUAL LICENSE BLOCK *****
*/
+//#include "BKE_utildefines.h"
+#include "BIF_usiblender.h"
+
#include "Blender.h"
/*****************************************************************************/
@@ -157,7 +160,6 @@ PyObject *Blender_Get (PyObject *self, PyObject *args)
/*****************************************************************************/
PyObject *Blender_Redraw(PyObject *self, PyObject *args)
{
-
int wintype = SPACE_VIEW3D;
if (!PyArg_ParseTuple (args, "|i", &wintype))
@@ -172,24 +174,24 @@ PyObject *Blender_Redraw(PyObject *self, PyObject *args)
/*****************************************************************************/
/* Function: Blender_ReleaseGlobalDict */
/* Python equivalent: Blender.ReleaseGlobalDict */
-/* Description: Receives an int (treated as boolean) to define */
-/* whether the global Python dictionary should be */
-/* cleared after the script is run or not. Default */
-/* is to clear (to release). To change this, call */
-/* Blender.ReleaseGlobalDict with a non-zero int */
-/* argument. If called with an empty arg list, it */
-/* doesn't change anything. */
-/* Returns the current behavior. */
+/* Description: Deprecated function. */
/*****************************************************************************/
PyObject *Blender_ReleaseGlobalDict(PyObject *self, PyObject *args)
{
- if (!PyArg_ParseTuple (args, "|i", &EXPP_releaseGlobalDict))
- {
- return EXPP_ReturnPyObjError (PyExc_TypeError,
- "expected int argument (or nothing)");
- }
+ Py_INCREF(Py_None);
+ return Py_None;
+}
+
+/*****************************************************************************/
+/* Function: Blender_Quit */
+/* Python equivalent: Blender.Quit */
+/*****************************************************************************/
+PyObject *Blender_Quit(PyObject *self)
+{
+ exit_usiblender();
- return Py_BuildValue("i", (EXPP_releaseGlobalDict?1:0));
+ Py_INCREF(Py_None);
+ return Py_None;
}
/*****************************************************************************/
diff --git a/source/blender/python/api2_2x/Blender.h b/source/blender/python/api2_2x/Blender.h
index ff72d3bbd36..f44df3e3cb0 100644
--- a/source/blender/python/api2_2x/Blender.h
+++ b/source/blender/python/api2_2x/Blender.h
@@ -50,11 +50,6 @@
/* From Window.h, used here by Blender_Redraw */
PyObject *M_Window_Redraw(PyObject *self, PyObject *args);
-/* This global variable controls whether the global Interpreter dictionary
- * should be cleared after a script is run. Default is to clear it.
- * See Blender.ReleaseGlobalDict(bool) */
-extern short EXPP_releaseGlobalDict;
-
/*****************************************************************************/
/* Python API function prototypes for the Blender module. */
/*****************************************************************************/
@@ -62,6 +57,7 @@ PyObject *Blender_Set (PyObject *self, PyObject *args);
PyObject *Blender_Get (PyObject *self, PyObject *args);
PyObject *Blender_Redraw(PyObject *self, PyObject *args);
PyObject *Blender_ReleaseGlobalDict(PyObject *self, PyObject *args);
+PyObject *Blender_Quit(PyObject *self);
/*****************************************************************************/
/* The following string definitions are used for documentation strings. */
@@ -88,10 +84,10 @@ char Blender_Get_doc[] =
char Blender_Redraw_doc[] = "() - Redraw all 3D windows";
char Blender_ReleaseGlobalDict_doc[] =
-"(int) - Define whether the global Python Interpreter dictionary\n\
- should be cleared after the script is run. Default is\n\
- to clear (non-zero int).\n\
-() - Return the current behavior as a bool value (0 is false, 1 is true)\n";
+"Deprecated, please use the Blender.Registry module solution instead.";
+
+char Blender_Quit_doc[] =
+"() - Quit Blender. Experimental, please use with caution.";
/*****************************************************************************/
/* Python method structure definition. */
@@ -100,6 +96,7 @@ struct PyMethodDef Blender_methods[] = {
{"Set", &Blender_Set, METH_VARARGS, Blender_Set_doc},
{"Get", &Blender_Get, METH_VARARGS, Blender_Get_doc},
{"Redraw", &Blender_Redraw, METH_VARARGS, Blender_Redraw_doc},
+ {"Quit", &Blender_Quit, METH_NOARGS, Blender_Quit_doc},
{"ReleaseGlobalDict", &Blender_ReleaseGlobalDict,
METH_VARARGS, Blender_ReleaseGlobalDict_doc},
{NULL, NULL}
diff --git a/source/blender/python/api2_2x/Draw.c b/source/blender/python/api2_2x/Draw.c
index 5ad4387b483..bd1494dbe51 100644
--- a/source/blender/python/api2_2x/Draw.c
+++ b/source/blender/python/api2_2x/Draw.c
@@ -68,11 +68,6 @@
#include "interface.h"
#include "mydevice.h" /*@ for all the event constants */
-
-/* declared in ../BPY_extern.h,
- * used to control global dictionary persistence: */
-extern short EXPP_releaseGlobalDict;
-
/* This one was an extern in BPY_main.h, but only opy_draw.c was using it */
int g_window_redrawn;