[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [17684] trunk/blender/release/scripts/ bpymodules: added a function to resolve case insensitive paths in BPySys and an option to use it in BPyImage

Campbell Barton ideasman42 at gmail.com
Wed Dec 3 03:03:53 CET 2008


Revision: 17684
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=17684
Author:   campbellbarton
Date:     2008-12-03 03:03:51 +0100 (Wed, 03 Dec 2008)

Log Message:
-----------
added a function to resolve case insensitive paths in BPySys and an option to use it in BPyImage 
- Useful when loading files with saved in windows on a *nix system.

Modified Paths:
--------------
    trunk/blender/release/scripts/bpymodules/BPyImage.py
    trunk/blender/release/scripts/bpymodules/BPySys.py

Modified: trunk/blender/release/scripts/bpymodules/BPyImage.py
===================================================================
--- trunk/blender/release/scripts/bpymodules/BPyImage.py	2008-12-02 23:45:11 UTC (rev 17683)
+++ trunk/blender/release/scripts/bpymodules/BPyImage.py	2008-12-03 02:03:51 UTC (rev 17684)
@@ -79,7 +79,7 @@
 	return path + sys.sep
 
 
-def comprehensiveImageLoad(imagePath, filePath, PLACE_HOLDER= True, RECURSIVE=True, VERBOSE=False):
+def comprehensiveImageLoad(imagePath, filePath, PLACE_HOLDER= True, RECURSIVE=True, VERBOSE=False, CONVERT_CALLBACK=None):
 	'''
 	imagePath: The image filename
 		If a path precedes it, this will be searched as well.
@@ -93,13 +93,30 @@
 	
 	RECURSIVE: If True, directories will be recursivly searched.
 		Be carefull with this if you have files in your root directory because it may take a long time.
+	
+	CASE_INSENSITIVE: for non win32 systems, find the correct case for the file.
+	
+	CONVERT_CALLBACK: a function that takes an existing path and returns a new one.
+		Use this when loading image formats blender may not support, the CONVERT_CALLBACK
+		can take the path for a GIF (for example), convert it to a PNG and return the PNG's path.
+		For formats blender can read, simply return the path that is given.
 	'''
 	
+	# VERBOSE = True
+	
 	if VERBOSE: print 'img:', imagePath, 'file:', filePath
+	
+	if os == None and CASE_INSENSITIVE:
+		CASE_INSENSITIVE = True
+	
 	# When we have the file load it with this. try/except niceness.
 	def imageLoad(path):
 		#if path.endswith('\\') or path.endswith('/'):
 		#	raise 'INVALID PATH'
+		
+		if CONVERT_CALLBACK:
+			path = CONVERT_CALLBACK(path)
+		
 		try:
 			img = bpy.data.images.new(filename=path)
 			if VERBOSE: print '\t\tImage loaded "%s"' % path

Modified: trunk/blender/release/scripts/bpymodules/BPySys.py
===================================================================
--- trunk/blender/release/scripts/bpymodules/BPySys.py	2008-12-02 23:45:11 UTC (rev 17683)
+++ trunk/blender/release/scripts/bpymodules/BPySys.py	2008-12-03 02:03:51 UTC (rev 17684)
@@ -12,3 +12,63 @@
 	for ch in invalid:	name = name.replace(ch, '_')
 	return name
 
+def caseInsensitivePath(path, RET_FOUND=False):
+	'''
+	Get a case insensitive path on a case sensitive system
+	
+	RET_FOUND is for internal use only, to avoid too many calls to os.path.exists
+	# Example usage
+	getCaseInsensitivePath('/hOmE/mE/sOmEpAtH.tXt')
+	'''
+	import os # todo, what happens with no os?
+	
+	if os==None:
+		if RET_FOUND:	ret = path, True
+		else:			ret = path
+		return ret
+	
+	if path=='' or os.path.exists(path):
+		if RET_FOUND:	ret = path, True
+		else:			ret = path
+		return ret
+	
+	f = os.path.basename(path) # f may be a directory or a file
+	d = os.path.dirname(path)
+	
+	suffix = ''
+	if not f: # dir ends with a slash?
+		if len(d) < len(path):
+			suffix = path[:len(path)-len(d)]
+
+		f = os.path.basename(d)
+		d = os.path.dirname(d)
+	
+	if not os.path.exists(d):
+		d, found = caseInsensitivePath(d, True)
+		
+		if not found:
+			if RET_FOUND:	ret = path, False
+			else:			ret = path
+			return ret
+	
+	# at this point, the directory exists but not the file
+	
+	try: # we are expecting 'd' to be a directory, but it could be a file
+		files = os.listdir(d)
+	except:
+		if RET_FOUND:	ret = path, False
+		else:			ret = path
+
+	f_low = f.lower()
+	
+	try:	f_nocase = [fl for fl in files if fl.lower() == f_low][0]
+	except:	f_nocase = None
+	
+	if f_nocase:
+		if RET_FOUND:	ret = os.path.join(d, f_nocase) + suffix, True
+		else:			ret = os.path.join(d, f_nocase) + suffix
+		return ret
+	else:
+		if RET_FOUND:	ret = path, False
+		else:			ret = path
+		return ret # cant find the right one, just return the path as is.
\ No newline at end of file





More information about the Bf-blender-cvs mailing list