[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [17357] trunk/blender: [#17958] Windows path fix for image_edit.py script.

Campbell Barton ideasman42 at gmail.com
Fri Nov 7 16:16:30 CET 2008


Revision: 17357
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=17357
Author:   campbellbarton
Date:     2008-11-07 16:16:30 +0100 (Fri, 07 Nov 2008)

Log Message:
-----------
[#17958] Windows path fix for image_edit.py script.
Modified to work in linux too,  on my system subprocess.Popen(appstring) only works when appstring is a list.

Blenders __import__ didnt support keywords like pythons causing the subprocess module to fail for me.
added keywords to blenders c/api import to match pythons.

Modified Paths:
--------------
    trunk/blender/release/scripts/image_edit.py
    trunk/blender/source/blender/python/BPY_interface.c

Modified: trunk/blender/release/scripts/image_edit.py
===================================================================
--- trunk/blender/release/scripts/image_edit.py	2008-11-07 10:54:32 UTC (rev 17356)
+++ trunk/blender/release/scripts/image_edit.py	2008-11-07 15:16:30 UTC (rev 17357)
@@ -1,6 +1,6 @@
 #!BPY
 """
-Name: 'Edit Externaly'
+Name: 'Edit Externally'
 Blender: 242a
 Group: 'Image'
 Tooltip: 'Open in an application for editing. (hold Shift to configure)'
@@ -9,22 +9,23 @@
 __author__ = "Campbell Barton"
 __url__ = ["blender", "blenderartists.org"]
 __version__ = "1.0"
-
 __bpydoc__ = """\
 This script opens the current image in an external application for editing.
 
-Useage:
+Usage:
 Choose an image for editing in the UV/Image view.
 
-To configure the application to open the image with, hold Shift as you click on
-this menu item.
+To configure the application to open the image with, hold Shift as you
+click on this menu item.
 
-For first time users try running the default application for your operating system.
-If the application does not open you can type in the full path.
-You can choose that the last entered application will be saved as a default.
+For first time users try running the default application for your
+operating system.  If the application does not open you can type in
+the full path.  You can choose that the last entered application will
+be saved as a default.
 
-* Note, default commants for opening an image are "start" for win32 and "open" for macos.
-This will use the system default assosiated application.
+* Note, default commants for opening an image are "start" for win32
+and "open" for macos.  This will use the system default associated
+application.
 """
 
 # ***** BEGIN GPL LICENSE BLOCK *****
@@ -48,25 +49,24 @@
 # ***** END GPL LICENCE BLOCK *****
 # --------------------------------------------------------------------------
 
+import Blender
+from Blender import Image, sys, Draw, Registry
 
 try:
-	import os
+	import subprocess
 	import sys as py_sys
 	platform = py_sys.platform
 except:
-	Draw.PupMenu('Error, python not installed')
-	os=None
+	Draw.PupMenu('Error: Recent version of Python not installed.')
+	subprocess=None
 
-import Blender
-from Blender import Image, sys, Draw, Registry
-
 def edit_extern(image=None):
 	
 	if not image:
 		image = Image.GetCurrent()
 	
 	if not image: # Image is None
-		Draw.PupMenu('ERROR: You must select an active Image.')
+		Draw.PupMenu('ERROR: Please select active Image.')
 		return
 	if image.packed:
 		Draw.PupMenu('ERROR: Image is packed, unpack before editing.')
@@ -94,7 +94,10 @@
 	if new_text:
 		pupblock.append('first time, set path.')
 		if platform == 'win32':
-			appstring = 'start "" /B "%f"'
+			# Example of path to popular image editor... ;-)
+			# appstring = '"C:\\Program Files\\Adobe\\Photoshop CS\\photoshop.exe" "%f"'
+			# Have to add "cmd /c" to make sure we're using Windows shell.
+			appstring = 'cmd /c start "" /B "%f"'
 		elif platform == 'darwin':
 			appstring = 'open "%f"'
 		else:
@@ -103,7 +106,7 @@
 	appstring_but = Draw.Create(appstring)
 	save_default_but = Draw.Create(0)
 	
-	pupblock.append(('editor: ', appstring_but, 0, 48, 'Path to application, %f will be replaced with the image path.'))
+	pupblock.append(('editor: ', appstring_but, 0, 99, 'Path to application, %f will be replaced with the image path.'))
 	pupblock.append(('Set Default', save_default_but, 'Store this path in the blender registry.'))
 	
 	# Only configure if Shift is held,
@@ -118,19 +121,23 @@
 		Registry.SetKey('ExternalImageEditor', {'path':appstring}, True)
 	
 	if appstring.find('%f') == -1:
-		Draw.PupMenu('ERROR: The comment you entered did not contain the filename ("%f")')
+		Draw.PupMenu('ERROR: No filename specified! ("%f")')
 		return
 	
 	# -------------------------------
 	
+	# evil trick, temp replace spaces so we can allow spaces in filenames
+	appstring = appstring.replace(' ', '\t')
+	
 	appstring = appstring.replace('%f', imageFileName)
-	print '\tediting image with command "%s"' % appstring
-	os.system(appstring)
+	appstring = appstring.split('\t')
+	print 'Editing image with command "%s"' % appstring
+	p = subprocess.Popen(appstring)
 
 
 def main():
 	edit_extern()
 	
 
-if __name__ == '__main__' and os != None:
-	main()
\ No newline at end of file
+if __name__ == '__main__' and subprocess:
+	main()

Modified: trunk/blender/source/blender/python/BPY_interface.c
===================================================================
--- trunk/blender/source/blender/python/BPY_interface.c	2008-11-07 10:54:32 UTC (rev 17356)
+++ trunk/blender/source/blender/python/BPY_interface.c	2008-11-07 15:16:30 UTC (rev 17357)
@@ -167,7 +167,7 @@
 static PyObject *importText( char *name );
 static void init_ourImport( void );
 static void init_ourReload( void );
-static PyObject *blender_import( PyObject * self, PyObject * args );
+static PyObject *blender_import( PyObject * self, PyObject * args,  PyObject * kw);
 
 
 static void BPY_Err_Handle( char *script_name );
@@ -2849,24 +2849,28 @@
 }
 
 static PyMethodDef bimport[] = {
-	{"blimport", blender_import, METH_VARARGS, "our own import"}
+	{"blimport", blender_import, METH_KEYWORDS, "our own import"}
 };
 
-static PyObject *blender_import( PyObject * self, PyObject * args )
+static PyObject *blender_import( PyObject * self, PyObject * args,  PyObject * kw)
 {
 	PyObject *exception, *err, *tb;
 	char *name;
 	PyObject *globals = NULL, *locals = NULL, *fromlist = NULL;
 	PyObject *m;
+	
 	//PyObject_Print(args, stderr, 0);
 #if (PY_VERSION_HEX >= 0x02060000)
 	int dummy_val; /* what does this do?*/
+	static char *kwlist[] = {"name", "globals", "locals", "fromlist", "level", 0};
 	
-	if( !PyArg_ParseTuple( args, "s|OOOi:bimport",
+	if( !PyArg_ParseTupleAndKeywords( args, kw, "s|OOOi:bimport", kwlist,
 			       &name, &globals, &locals, &fromlist, &dummy_val) )
 		return NULL;
 #else
-	if( !PyArg_ParseTuple( args, "s|OOO:bimport",
+	static char *kwlist[] = {"name", "globals", "locals", "fromlist", 0};
+	
+	if( !PyArg_ParseTupleAndKeywords( args, kw, "s|OOO:bimport", kwlist,
 			       &name, &globals, &locals, &fromlist ) )
 		return NULL;
 #endif





More information about the Bf-blender-cvs mailing list