[Bf-committers] Python patch: sys.argv

Chris Want cwant at ualberta.ca
Fri Oct 15 00:44:51 CEST 2004


Hello,

I think it would be greatly useful to allow python
scripts to have access to the argument list that
blender was spawned with (via the standard variable
sys.argv). The current implementation artificially
sets this argument list to a singleton array holding
the path to the blender executable... I think this
might be so that the .blender directory can be
found there in cases where it isn't found elsewhere
(if so, that's kind of a kludgy way to do it,
but anyways...).

The attached small patch sets the python sys.argv from
blender's argv, argc. I think something for future
consideration would be to reserve a commandline
flag, such as "--py-arg var=value", and assemble
a handy python associative array that scripts can access
(maybe Blender.args?). Here is a sample usage to illustrate
what I am trying to say:

blender -P cool-text.py \
      --py-arg color=red \
      --py-arg "text=Hello world" \
      --py-arg output=foo.jpg

which would give cool-text.py script access to
Blender.args["color"] = "red",
Blender.args["test"] = "Hello world",
Blender.args["output"] = "foo.jpg"

Anyways, that's a bit of an aside, but I think it would
be a worthy addition ...

Thank you for considering this patch!

Chris
-------------- next part --------------
Index: source/blender/python/BPY_extern.h
===================================================================
RCS file: /cvsroot/bf-blender/blender/source/blender/python/BPY_extern.h,v
retrieving revision 1.18
diff -u -p -r1.18 BPY_extern.h
--- source/blender/python/BPY_extern.h	7 Oct 2004 19:25:39 -0000	1.18
+++ source/blender/python/BPY_extern.h	14 Oct 2004 22:24:34 -0000
@@ -46,7 +46,7 @@ struct _object;  // forward declaration 
 extern "C" {
 #endif
 
-	void BPY_start_python( void );
+	void BPY_start_python(int argc, char **argv);
 	void BPY_end_python( void );
 	void BPY_post_start_python( void );
 	int BPY_Err_getLinenumber( void );
Index: source/blender/python/BPY_interface.c
===================================================================
RCS file: /cvsroot/bf-blender/blender/source/blender/python/BPY_interface.c,v
retrieving revision 1.54
diff -u -p -r1.54 BPY_interface.c
--- source/blender/python/BPY_interface.c	7 Oct 2004 19:25:39 -0000	1.54
+++ source/blender/python/BPY_interface.c	14 Oct 2004 22:24:34 -0000
@@ -114,7 +114,7 @@ PyObject *blender_import( PyObject * sel
 *	          api variations.
 * Notes:	Currently only the api for 2.2x will be initialised. 
 ****************************************************************************/
-void BPY_start_python( void )
+void BPY_start_python(int argc, char **argv)
 {
 	bpy_registryDict = PyDict_New(  );	/* check comment at start of this file */
 
@@ -134,6 +134,7 @@ void BPY_start_python( void )
 	fflush( stdout );
 
 	Py_Initialize(  );
+	PySys_SetArgv(argc, argv);
 
 	init_ourImport(  );
 
@@ -265,6 +266,13 @@ void init_syspath( void )
 		printf( "Continuing happily.\n" );
 	}
 
+
+	/* Is the code below obsolete? sys.argv set in BPY_start_python() 
+	 * now ... does this cause any problems? (e.g., finding the 
+	 * .blender directory) (hos) 
+	 */
+
+#if 0
 	/* 
 	 * initialize the sys module
 	 * set sys.executable to the Blender exe 
@@ -282,6 +290,8 @@ void init_syspath( void )
 				      Py_BuildValue( "[s]", bprogname ) );
 		Py_DECREF( mod );
 	}
+#endif
+
 }
 
 /****************************************************************************
Index: source/creator/creator.c
===================================================================
RCS file: /cvsroot/bf-blender/blender/source/creator/creator.c,v
retrieving revision 1.41
diff -u -p -r1.41 creator.c
--- source/creator/creator.c	1 Aug 2004 22:28:56 -0000	1.41
+++ source/creator/creator.c	14 Oct 2004 22:24:34 -0000
@@ -440,7 +440,7 @@ int main(int argc, char **argv)
 			}
 		}
 
-		BPY_start_python();
+		BPY_start_python(argc, argv);
 		
 		/**
 		 * NOTE: sound_init_audio() *must be* after start_python,
@@ -452,7 +452,7 @@ int main(int argc, char **argv)
 
 	}
 	else {
-		BPY_start_python();
+		BPY_start_python(argc, argv);
 		
 		// (ton) Commented out. I have no idea whats thisfor... will mail around!
 		// SYS_WriteCommandLineInt(syshandle,"noaudio",1);


More information about the Bf-committers mailing list