diff options
author | Campbell Barton <ideasman42@gmail.com> | 2008-12-03 05:03:51 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2008-12-03 05:03:51 +0300 |
commit | 2acd1064929a3851f39720a76f12eb3a0c279f80 (patch) | |
tree | 5ed5cc6a9f5f829c68f2313009066107dabdeee0 /release/scripts/bpymodules | |
parent | 6fbeed135509885ea1d81de8344b9db2181b17e1 (diff) |
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.
Diffstat (limited to 'release/scripts/bpymodules')
-rw-r--r-- | release/scripts/bpymodules/BPyImage.py | 19 | ||||
-rw-r--r-- | release/scripts/bpymodules/BPySys.py | 60 |
2 files changed, 78 insertions, 1 deletions
diff --git a/release/scripts/bpymodules/BPyImage.py b/release/scripts/bpymodules/BPyImage.py index 2c342ddec39..504e4ee29ba 100644 --- a/release/scripts/bpymodules/BPyImage.py +++ b/release/scripts/bpymodules/BPyImage.py @@ -79,7 +79,7 @@ def addSlash(path): 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 @@ def comprehensiveImageLoad(imagePath, filePath, PLACE_HOLDER= True, RECURSIVE=Tr 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 diff --git a/release/scripts/bpymodules/BPySys.py b/release/scripts/bpymodules/BPySys.py index 594264fad84..a2d2120ebff 100644 --- a/release/scripts/bpymodules/BPySys.py +++ b/release/scripts/bpymodules/BPySys.py @@ -12,3 +12,63 @@ def cleanName(name): 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 |