1 # ##### BEGIN GPL LICENSE BLOCK #####
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software Foundation,
15 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 # ##### END GPL LICENSE BLOCK #####
22 This module has a similar scope to os.path, containing utility
23 functions for dealing with paths in Blender.
31 "display_name_from_filepath",
43 def abspath(path, start=None, library=None):
45 Returns the absolute path relative to the current blend file
46 using the "//" prefix.
48 :arg start: Relative to this path,
49 when not set the current filename is used.
51 :arg library: The library this path is from. This is only included for
52 convenience, when the library is not None its path replaces *start*.
53 :type library: :class:`bpy.types.Library`
55 if path.startswith("//"):
57 start = abspath(_os.path.dirname(library.filepath))
58 return _os.path.join(_os.path.dirname(_bpy.data.filepath)
59 if start is None else start,
66 def relpath(path, start=None):
68 Returns the path relative to the current blend file using the "//" prefix.
70 :arg start: Relative to this path,
71 when not set the current filename is used.
74 if not path.startswith("//"):
76 start = _os.path.dirname(_bpy.data.filepath)
77 return "//" + _os.path.relpath(path, start)
82 def is_subdir(path, directory):
84 Returns true if *path* in a subdirectory of *directory*.
85 Both paths must be absolute.
87 from os.path import normpath, normcase
88 path = normpath(normcase(path))
89 directory = normpath(normcase(directory))
90 return path.startswith(directory)
93 def clean_name(name, replace="_"):
95 Returns a name with characters replaced that
96 may cause problems under various circumstances,
97 such as writing to a file.
98 All characters besides A-Z/a-z, 0-9 are replaced with "_"
99 or the replace argument if defined.
102 bad_chars = ("\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e"
103 "\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d"
104 "\x1e\x1f\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c"
105 "\x2e\x2f\x3a\x3b\x3c\x3d\x3e\x3f\x40\x5b\x5c\x5d\x5e\x60\x7b"
106 "\x7c\x7d\x7e\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a"
107 "\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99"
108 "\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8"
109 "\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7"
110 "\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6"
111 "\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5"
112 "\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4"
113 "\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3"
114 "\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe")
117 name = name.replace(ch, replace)
121 def display_name(name):
123 Creates a display string from name to be used menus and the user interface.
124 Capitalize the first letter in all lowercase names,
125 mixed case names are kept as is. Intended for use with
126 filenames and module names.
129 name_base = _os.path.splitext(name)[0]
131 # string replacements
132 name_base = name_base.replace("_colon_", ":")
134 name_base = name_base.replace("_", " ")
136 if name_base.islower():
137 return name_base.lower().title()
142 def display_name_from_filepath(name):
144 Returns the path stripped of directory and extension,
145 ensured to be utf8 compatible.
147 name = _os.path.splitext(basename(name))[0]
148 return name.encode("utf8", "replace").decode("utf8")
151 def resolve_ncase(path):
153 Resolve a case insensitive path on a case sensitive system,
154 returning a string with the path if found else return the original path.
159 def _ncase_path_found(path):
160 if not path or os.path.exists(path):
163 # filename may be a directory or a file
164 filename = os.path.basename(path)
165 dirpath = os.path.dirname(path)
167 suffix = path[:0] # "" but ensure byte/str match
168 if not filename: # dir ends with a slash?
169 if len(dirpath) < len(path):
170 suffix = path[:len(path) - len(dirpath)]
172 filename = os.path.basename(dirpath)
173 dirpath = os.path.dirname(dirpath)
175 if not os.path.exists(dirpath):
179 dirpath, found = _ncase_path_found(dirpath)
184 # at this point, the directory exists but not the file
186 # we are expecting 'dirpath' to be a directory, but it could be a file
187 if os.path.isdir(dirpath):
188 files = os.listdir(dirpath)
192 filename_low = filename.lower()
196 if f_iter.lower() == filename_low:
197 f_iter_nocase = f_iter
201 return os.path.join(dirpath, f_iter_nocase) + suffix, True
203 # cant find the right one, just return the path as is.
206 ncase_path, found = _ncase_path_found(path)
207 return ncase_path if found else path
210 def ensure_ext(filepath, ext, case_sensitive=False):
212 Return the path with the extension added if it is not already set.
214 :arg ext: The extension to check for.
216 :arg case_sensitive: Check for matching case when comparing extensions.
217 :type case_sensitive: bool
220 fn_base, fn_ext = os.path.splitext(filepath)
221 if fn_base and fn_ext:
222 if ((case_sensitive and ext == fn_ext) or
223 (ext.lower() == fn_ext.lower())):
230 return filepath + ext
233 def module_names(path, recursive=False):
235 Return a list of modules which can be imported from *path*.
237 :arg path: a directory to scan.
239 :arg recursive: Also return submodule names for packages.
240 :type recursive: bool
241 :return: a list of string pairs (module_name, module_file).
245 from os.path import join, isfile
249 for filename in sorted(_os.listdir(path)):
250 if filename == "modules":
251 pass # XXX, hard coded exception.
252 elif filename.endswith(".py") and filename != "__init__.py":
253 fullpath = join(path, filename)
254 modules.append((filename[0:-3], fullpath))
255 elif ("." not in filename):
256 directory = join(path, filename)
257 fullpath = join(directory, "__init__.py")
259 modules.append((filename, fullpath))
261 for mod_name, mod_path in module_names(directory, True):
262 modules.append(("%s.%s" % (filename, mod_name),
271 Equivalent to os.path.basename, but skips a "//" prefix.
273 Use for Windows compatibility.
275 return _os.path.basename(path[2:] if path[:2] in {"//", b"//"} else path)