2022-02-11 09:07:11 +11:00
|
|
|
# SPDX-License-Identifier: GPL-2.0-or-later
|
2010-08-06 01:40:54 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
This module has a similar scope to os.path, containing utility
|
|
|
|
functions for dealing with paths in Blender.
|
|
|
|
"""
|
|
|
|
|
2011-07-31 03:15:37 +00:00
|
|
|
__all__ = (
|
|
|
|
"abspath",
|
|
|
|
"basename",
|
|
|
|
"clean_name",
|
|
|
|
"display_name",
|
2018-01-14 14:31:39 +11:00
|
|
|
"display_name_to_filepath",
|
2011-07-31 03:15:37 +00:00
|
|
|
"display_name_from_filepath",
|
|
|
|
"ensure_ext",
|
2013-01-04 03:47:37 +00:00
|
|
|
"extensions_image",
|
|
|
|
"extensions_movie",
|
|
|
|
"extensions_audio",
|
2011-07-31 03:15:37 +00:00
|
|
|
"is_subdir",
|
|
|
|
"module_names",
|
2016-03-29 18:22:35 +11:00
|
|
|
"native_pathsep",
|
2013-08-12 07:44:38 +00:00
|
|
|
"reduce_dirs",
|
2011-07-31 03:15:37 +00:00
|
|
|
"relpath",
|
|
|
|
"resolve_ncase",
|
2016-07-29 21:22:27 +10:00
|
|
|
)
|
2011-07-31 03:15:37 +00:00
|
|
|
|
2010-08-06 01:40:54 +00:00
|
|
|
import bpy as _bpy
|
2010-08-06 13:06:13 +00:00
|
|
|
import os as _os
|
2010-08-06 01:40:54 +00:00
|
|
|
|
2015-01-27 17:46:07 +11:00
|
|
|
from _bpy_path import (
|
2016-07-29 21:22:27 +10:00
|
|
|
extensions_audio,
|
|
|
|
extensions_movie,
|
|
|
|
extensions_image,
|
|
|
|
)
|
2010-09-07 15:17:42 +00:00
|
|
|
|
2013-01-15 23:15:32 +00:00
|
|
|
|
2013-04-05 00:30:32 +00:00
|
|
|
def _getattr_bytes(var, attr):
|
|
|
|
return var.path_resolve(attr, False).as_bytes()
|
|
|
|
|
|
|
|
|
PyAPI: use keyword only arguments
Use keyword only arguments for the following functions.
- addon_utils.module_bl_info 2nd arg `info_basis`.
- addon_utils.modules 1st `module_cache`, 2nd arg `refresh`.
- addon_utils.modules_refresh 1st arg `module_cache`.
- bl_app_template_utils.activate 1nd arg `template_id`.
- bl_app_template_utils.import_from_id 2nd arg `ignore_not_found`.
- bl_app_template_utils.import_from_path 2nd arg `ignore_not_found`.
- bl_keymap_utils.keymap_from_toolbar.generate 2nd & 3rd args `use_fallback_keys` & `use_reset`.
- bl_keymap_utils.platform_helpers.keyconfig_data_oskey_from_ctrl 2nd arg `filter_fn`.
- bl_ui_utils.bug_report_url.url_prefill_from_blender 1st arg `addon_info`.
- bmesh.types.BMFace.copy 1st & 2nd args `verts`, `edges`.
- bmesh.types.BMesh.calc_volume 1st arg `signed`.
- bmesh.types.BMesh.from_mesh 2nd..4th args `face_normals`, `use_shape_key`, `shape_key_index`.
- bmesh.types.BMesh.from_object 3rd & 4th args `cage`, `face_normals`.
- bmesh.types.BMesh.transform 2nd arg `filter`.
- bmesh.types.BMesh.update_edit_mesh 2nd & 3rd args `loop_triangles`, `destructive`.
- bmesh.types.{BMVertSeq,BMEdgeSeq,BMFaceSeq}.sort 1st & 2nd arg `key`, `reverse`.
- bmesh.utils.face_split 4th..6th args `coords`, `use_exist`, `example`.
- bpy.data.libraries.load 2nd..4th args `link`, `relative`, `assets_only`.
- bpy.data.user_map 1st..3rd args `subset`, `key_types, `value_types`.
- bpy.msgbus.subscribe_rna 5th arg `options`.
- bpy.path.abspath 2nd & 3rd args `start` & `library`.
- bpy.path.clean_name 2nd arg `replace`.
- bpy.path.ensure_ext 3rd arg `case_sensitive`.
- bpy.path.module_names 2nd arg `recursive`.
- bpy.path.relpath 2nd arg `start`.
- bpy.types.EditBone.transform 2nd & 3rd arg `scale`, `roll`.
- bpy.types.Operator.as_keywords 1st arg `ignore`.
- bpy.types.Struct.{keyframe_insert,keyframe_delete} 2nd..5th args `index`, `frame`, `group`, `options`.
- bpy.types.WindowManager.popup_menu 2nd & 3rd arg `title`, `icon`.
- bpy.types.WindowManager.popup_menu_pie 3rd & 4th arg `title`, `icon`.
- bpy.utils.app_template_paths 1st arg `subdir`.
- bpy.utils.app_template_paths 1st arg `subdir`.
- bpy.utils.blend_paths 1st..3rd args `absolute`, `packed`, `local`.
- bpy.utils.execfile 2nd arg `mod`.
- bpy.utils.keyconfig_set 2nd arg `report`.
- bpy.utils.load_scripts 1st & 2nd `reload_scripts` & `refresh_scripts`.
- bpy.utils.preset_find 3rd & 4th args `display_name`, `ext`.
- bpy.utils.resource_path 2nd & 3rd arg `major`, `minor`.
- bpy.utils.script_paths 1st..4th args `subdir`, `user_pref`, `check_all`, `use_user`.
- bpy.utils.smpte_from_frame 2nd & 3rd args `fps`, `fps_base`.
- bpy.utils.smpte_from_seconds 2nd & 3rd args `fps`, `fps_base`.
- bpy.utils.system_resource 2nd arg `subdir`.
- bpy.utils.time_from_frame 2nd & 3rd args `fps`, `fps_base`.
- bpy.utils.time_to_frame 2nd & 3rd args `fps`, `fps_base`.
- bpy.utils.units.to_string 4th..6th `precision`, `split_unit`, `compatible_unit`.
- bpy.utils.units.to_value 4th arg `str_ref_unit`.
- bpy.utils.user_resource 2nd & 3rd args `subdir`, `create`
- bpy_extras.view3d_utils.location_3d_to_region_2d 4th arg `default`.
- bpy_extras.view3d_utils.region_2d_to_origin_3d 4th arg `clamp`.
- gpu.offscreen.unbind 1st arg `restore`.
- gpu_extras.batch.batch_for_shader 4th arg `indices`.
- gpu_extras.batch.presets.draw_circle_2d 4th arg `segments`.
- gpu_extras.presets.draw_circle_2d 4th arg `segments`.
- imbuf.types.ImBuf.resize 2nd arg `resize`.
- imbuf.write 2nd arg `filepath`.
- mathutils.kdtree.KDTree.find 2nd arg `filter`.
- nodeitems_utils.NodeCategory 3rd & 4th arg `descriptions`, `items`.
- nodeitems_utils.NodeItem 2nd..4th args `label`, `settings`, `poll`.
- nodeitems_utils.NodeItemCustom 1st & 2nd arg `poll`, `draw`.
- rna_prop_ui.draw 5th arg `use_edit`.
- rna_prop_ui.rna_idprop_ui_get 2nd arg `create`.
- rna_prop_ui.rna_idprop_ui_prop_clear 3rd arg `remove`.
- rna_prop_ui.rna_idprop_ui_prop_get 3rd arg `create`.
- rna_xml.xml2rna 2nd arg `root_rna`.
- rna_xml.xml_file_write 4th arg `skip_typemap`.
2021-06-08 18:03:14 +10:00
|
|
|
def abspath(path, *, start=None, library=None):
|
2010-08-06 01:40:54 +00:00
|
|
|
"""
|
2011-07-31 03:15:37 +00:00
|
|
|
Returns the absolute path relative to the current blend file
|
|
|
|
using the "//" prefix.
|
2011-01-15 20:35:31 +00:00
|
|
|
|
2011-07-31 03:15:37 +00:00
|
|
|
:arg start: Relative to this path,
|
|
|
|
when not set the current filename is used.
|
2015-08-05 00:21:50 +10:00
|
|
|
:type start: string or bytes
|
2011-10-11 04:09:11 +00:00
|
|
|
:arg library: The library this path is from. This is only included for
|
|
|
|
convenience, when the library is not None its path replaces *start*.
|
|
|
|
:type library: :class:`bpy.types.Library`
|
2021-10-22 18:27:03 -04:00
|
|
|
:return: The absolute path.
|
|
|
|
:rtype: string
|
2010-08-06 01:40:54 +00:00
|
|
|
"""
|
2013-04-05 00:30:32 +00:00
|
|
|
if isinstance(path, bytes):
|
|
|
|
if path.startswith(b"//"):
|
|
|
|
if library:
|
2016-02-01 00:47:10 +11:00
|
|
|
start = _os.path.dirname(
|
2016-07-29 21:22:27 +10:00
|
|
|
abspath(_getattr_bytes(library, "filepath")))
|
2016-02-01 00:47:10 +11:00
|
|
|
return _os.path.join(
|
2016-07-29 21:22:27 +10:00
|
|
|
_os.path.dirname(_getattr_bytes(_bpy.data, "filepath"))
|
|
|
|
if start is None else start,
|
|
|
|
path[2:],
|
|
|
|
)
|
2013-04-05 00:30:32 +00:00
|
|
|
else:
|
|
|
|
if path.startswith("//"):
|
|
|
|
if library:
|
2016-02-01 00:47:10 +11:00
|
|
|
start = _os.path.dirname(
|
2016-07-29 21:22:27 +10:00
|
|
|
abspath(library.filepath))
|
2016-02-01 00:47:10 +11:00
|
|
|
return _os.path.join(
|
2016-07-29 21:22:27 +10:00
|
|
|
_os.path.dirname(_bpy.data.filepath)
|
|
|
|
if start is None else start,
|
|
|
|
path[2:],
|
|
|
|
)
|
2010-08-06 01:40:54 +00:00
|
|
|
|
|
|
|
return path
|
|
|
|
|
|
|
|
|
PyAPI: use keyword only arguments
Use keyword only arguments for the following functions.
- addon_utils.module_bl_info 2nd arg `info_basis`.
- addon_utils.modules 1st `module_cache`, 2nd arg `refresh`.
- addon_utils.modules_refresh 1st arg `module_cache`.
- bl_app_template_utils.activate 1nd arg `template_id`.
- bl_app_template_utils.import_from_id 2nd arg `ignore_not_found`.
- bl_app_template_utils.import_from_path 2nd arg `ignore_not_found`.
- bl_keymap_utils.keymap_from_toolbar.generate 2nd & 3rd args `use_fallback_keys` & `use_reset`.
- bl_keymap_utils.platform_helpers.keyconfig_data_oskey_from_ctrl 2nd arg `filter_fn`.
- bl_ui_utils.bug_report_url.url_prefill_from_blender 1st arg `addon_info`.
- bmesh.types.BMFace.copy 1st & 2nd args `verts`, `edges`.
- bmesh.types.BMesh.calc_volume 1st arg `signed`.
- bmesh.types.BMesh.from_mesh 2nd..4th args `face_normals`, `use_shape_key`, `shape_key_index`.
- bmesh.types.BMesh.from_object 3rd & 4th args `cage`, `face_normals`.
- bmesh.types.BMesh.transform 2nd arg `filter`.
- bmesh.types.BMesh.update_edit_mesh 2nd & 3rd args `loop_triangles`, `destructive`.
- bmesh.types.{BMVertSeq,BMEdgeSeq,BMFaceSeq}.sort 1st & 2nd arg `key`, `reverse`.
- bmesh.utils.face_split 4th..6th args `coords`, `use_exist`, `example`.
- bpy.data.libraries.load 2nd..4th args `link`, `relative`, `assets_only`.
- bpy.data.user_map 1st..3rd args `subset`, `key_types, `value_types`.
- bpy.msgbus.subscribe_rna 5th arg `options`.
- bpy.path.abspath 2nd & 3rd args `start` & `library`.
- bpy.path.clean_name 2nd arg `replace`.
- bpy.path.ensure_ext 3rd arg `case_sensitive`.
- bpy.path.module_names 2nd arg `recursive`.
- bpy.path.relpath 2nd arg `start`.
- bpy.types.EditBone.transform 2nd & 3rd arg `scale`, `roll`.
- bpy.types.Operator.as_keywords 1st arg `ignore`.
- bpy.types.Struct.{keyframe_insert,keyframe_delete} 2nd..5th args `index`, `frame`, `group`, `options`.
- bpy.types.WindowManager.popup_menu 2nd & 3rd arg `title`, `icon`.
- bpy.types.WindowManager.popup_menu_pie 3rd & 4th arg `title`, `icon`.
- bpy.utils.app_template_paths 1st arg `subdir`.
- bpy.utils.app_template_paths 1st arg `subdir`.
- bpy.utils.blend_paths 1st..3rd args `absolute`, `packed`, `local`.
- bpy.utils.execfile 2nd arg `mod`.
- bpy.utils.keyconfig_set 2nd arg `report`.
- bpy.utils.load_scripts 1st & 2nd `reload_scripts` & `refresh_scripts`.
- bpy.utils.preset_find 3rd & 4th args `display_name`, `ext`.
- bpy.utils.resource_path 2nd & 3rd arg `major`, `minor`.
- bpy.utils.script_paths 1st..4th args `subdir`, `user_pref`, `check_all`, `use_user`.
- bpy.utils.smpte_from_frame 2nd & 3rd args `fps`, `fps_base`.
- bpy.utils.smpte_from_seconds 2nd & 3rd args `fps`, `fps_base`.
- bpy.utils.system_resource 2nd arg `subdir`.
- bpy.utils.time_from_frame 2nd & 3rd args `fps`, `fps_base`.
- bpy.utils.time_to_frame 2nd & 3rd args `fps`, `fps_base`.
- bpy.utils.units.to_string 4th..6th `precision`, `split_unit`, `compatible_unit`.
- bpy.utils.units.to_value 4th arg `str_ref_unit`.
- bpy.utils.user_resource 2nd & 3rd args `subdir`, `create`
- bpy_extras.view3d_utils.location_3d_to_region_2d 4th arg `default`.
- bpy_extras.view3d_utils.region_2d_to_origin_3d 4th arg `clamp`.
- gpu.offscreen.unbind 1st arg `restore`.
- gpu_extras.batch.batch_for_shader 4th arg `indices`.
- gpu_extras.batch.presets.draw_circle_2d 4th arg `segments`.
- gpu_extras.presets.draw_circle_2d 4th arg `segments`.
- imbuf.types.ImBuf.resize 2nd arg `resize`.
- imbuf.write 2nd arg `filepath`.
- mathutils.kdtree.KDTree.find 2nd arg `filter`.
- nodeitems_utils.NodeCategory 3rd & 4th arg `descriptions`, `items`.
- nodeitems_utils.NodeItem 2nd..4th args `label`, `settings`, `poll`.
- nodeitems_utils.NodeItemCustom 1st & 2nd arg `poll`, `draw`.
- rna_prop_ui.draw 5th arg `use_edit`.
- rna_prop_ui.rna_idprop_ui_get 2nd arg `create`.
- rna_prop_ui.rna_idprop_ui_prop_clear 3rd arg `remove`.
- rna_prop_ui.rna_idprop_ui_prop_get 3rd arg `create`.
- rna_xml.xml2rna 2nd arg `root_rna`.
- rna_xml.xml_file_write 4th arg `skip_typemap`.
2021-06-08 18:03:14 +10:00
|
|
|
def relpath(path, *, start=None):
|
2010-08-06 01:40:54 +00:00
|
|
|
"""
|
|
|
|
Returns the path relative to the current blend file using the "//" prefix.
|
|
|
|
|
2015-08-05 00:21:50 +10:00
|
|
|
:arg path: An absolute path.
|
|
|
|
:type path: string or bytes
|
2011-07-31 03:15:37 +00:00
|
|
|
:arg start: Relative to this path,
|
|
|
|
when not set the current filename is used.
|
2015-08-05 00:21:50 +10:00
|
|
|
:type start: string or bytes
|
2021-10-22 18:27:03 -04:00
|
|
|
:return: The relative path.
|
|
|
|
:rtype: string
|
2010-08-06 01:40:54 +00:00
|
|
|
"""
|
2013-04-05 00:30:32 +00:00
|
|
|
if isinstance(path, bytes):
|
|
|
|
if not path.startswith(b"//"):
|
|
|
|
if start is None:
|
|
|
|
start = _os.path.dirname(_getattr_bytes(_bpy.data, "filepath"))
|
|
|
|
return b"//" + _os.path.relpath(path, start)
|
|
|
|
else:
|
|
|
|
if not path.startswith("//"):
|
|
|
|
if start is None:
|
|
|
|
start = _os.path.dirname(_bpy.data.filepath)
|
|
|
|
return "//" + _os.path.relpath(path, start)
|
2010-08-06 01:40:54 +00:00
|
|
|
|
|
|
|
return path
|
|
|
|
|
|
|
|
|
2010-10-01 05:19:40 +00:00
|
|
|
def is_subdir(path, directory):
|
|
|
|
"""
|
|
|
|
Returns true if *path* in a subdirectory of *directory*.
|
|
|
|
Both paths must be absolute.
|
2015-08-05 00:21:50 +10:00
|
|
|
|
|
|
|
:arg path: An absolute path.
|
|
|
|
:type path: string or bytes
|
2021-10-22 18:27:03 -04:00
|
|
|
:return: Whether or not the path is a subdirectory.
|
|
|
|
:rtype: boolean
|
2010-10-01 05:19:40 +00:00
|
|
|
"""
|
2016-01-13 02:58:53 +11:00
|
|
|
from os.path import normpath, normcase, sep
|
2010-10-01 05:19:40 +00:00
|
|
|
path = normpath(normcase(path))
|
|
|
|
directory = normpath(normcase(directory))
|
2015-03-26 16:29:14 +11:00
|
|
|
if len(path) > len(directory):
|
2016-01-13 02:58:53 +11:00
|
|
|
sep = sep.encode('ascii') if isinstance(directory, bytes) else sep
|
|
|
|
if path.startswith(directory.rstrip(sep) + sep):
|
|
|
|
return True
|
2015-03-26 16:29:14 +11:00
|
|
|
return False
|
2010-10-01 05:19:40 +00:00
|
|
|
|
|
|
|
|
PyAPI: use keyword only arguments
Use keyword only arguments for the following functions.
- addon_utils.module_bl_info 2nd arg `info_basis`.
- addon_utils.modules 1st `module_cache`, 2nd arg `refresh`.
- addon_utils.modules_refresh 1st arg `module_cache`.
- bl_app_template_utils.activate 1nd arg `template_id`.
- bl_app_template_utils.import_from_id 2nd arg `ignore_not_found`.
- bl_app_template_utils.import_from_path 2nd arg `ignore_not_found`.
- bl_keymap_utils.keymap_from_toolbar.generate 2nd & 3rd args `use_fallback_keys` & `use_reset`.
- bl_keymap_utils.platform_helpers.keyconfig_data_oskey_from_ctrl 2nd arg `filter_fn`.
- bl_ui_utils.bug_report_url.url_prefill_from_blender 1st arg `addon_info`.
- bmesh.types.BMFace.copy 1st & 2nd args `verts`, `edges`.
- bmesh.types.BMesh.calc_volume 1st arg `signed`.
- bmesh.types.BMesh.from_mesh 2nd..4th args `face_normals`, `use_shape_key`, `shape_key_index`.
- bmesh.types.BMesh.from_object 3rd & 4th args `cage`, `face_normals`.
- bmesh.types.BMesh.transform 2nd arg `filter`.
- bmesh.types.BMesh.update_edit_mesh 2nd & 3rd args `loop_triangles`, `destructive`.
- bmesh.types.{BMVertSeq,BMEdgeSeq,BMFaceSeq}.sort 1st & 2nd arg `key`, `reverse`.
- bmesh.utils.face_split 4th..6th args `coords`, `use_exist`, `example`.
- bpy.data.libraries.load 2nd..4th args `link`, `relative`, `assets_only`.
- bpy.data.user_map 1st..3rd args `subset`, `key_types, `value_types`.
- bpy.msgbus.subscribe_rna 5th arg `options`.
- bpy.path.abspath 2nd & 3rd args `start` & `library`.
- bpy.path.clean_name 2nd arg `replace`.
- bpy.path.ensure_ext 3rd arg `case_sensitive`.
- bpy.path.module_names 2nd arg `recursive`.
- bpy.path.relpath 2nd arg `start`.
- bpy.types.EditBone.transform 2nd & 3rd arg `scale`, `roll`.
- bpy.types.Operator.as_keywords 1st arg `ignore`.
- bpy.types.Struct.{keyframe_insert,keyframe_delete} 2nd..5th args `index`, `frame`, `group`, `options`.
- bpy.types.WindowManager.popup_menu 2nd & 3rd arg `title`, `icon`.
- bpy.types.WindowManager.popup_menu_pie 3rd & 4th arg `title`, `icon`.
- bpy.utils.app_template_paths 1st arg `subdir`.
- bpy.utils.app_template_paths 1st arg `subdir`.
- bpy.utils.blend_paths 1st..3rd args `absolute`, `packed`, `local`.
- bpy.utils.execfile 2nd arg `mod`.
- bpy.utils.keyconfig_set 2nd arg `report`.
- bpy.utils.load_scripts 1st & 2nd `reload_scripts` & `refresh_scripts`.
- bpy.utils.preset_find 3rd & 4th args `display_name`, `ext`.
- bpy.utils.resource_path 2nd & 3rd arg `major`, `minor`.
- bpy.utils.script_paths 1st..4th args `subdir`, `user_pref`, `check_all`, `use_user`.
- bpy.utils.smpte_from_frame 2nd & 3rd args `fps`, `fps_base`.
- bpy.utils.smpte_from_seconds 2nd & 3rd args `fps`, `fps_base`.
- bpy.utils.system_resource 2nd arg `subdir`.
- bpy.utils.time_from_frame 2nd & 3rd args `fps`, `fps_base`.
- bpy.utils.time_to_frame 2nd & 3rd args `fps`, `fps_base`.
- bpy.utils.units.to_string 4th..6th `precision`, `split_unit`, `compatible_unit`.
- bpy.utils.units.to_value 4th arg `str_ref_unit`.
- bpy.utils.user_resource 2nd & 3rd args `subdir`, `create`
- bpy_extras.view3d_utils.location_3d_to_region_2d 4th arg `default`.
- bpy_extras.view3d_utils.region_2d_to_origin_3d 4th arg `clamp`.
- gpu.offscreen.unbind 1st arg `restore`.
- gpu_extras.batch.batch_for_shader 4th arg `indices`.
- gpu_extras.batch.presets.draw_circle_2d 4th arg `segments`.
- gpu_extras.presets.draw_circle_2d 4th arg `segments`.
- imbuf.types.ImBuf.resize 2nd arg `resize`.
- imbuf.write 2nd arg `filepath`.
- mathutils.kdtree.KDTree.find 2nd arg `filter`.
- nodeitems_utils.NodeCategory 3rd & 4th arg `descriptions`, `items`.
- nodeitems_utils.NodeItem 2nd..4th args `label`, `settings`, `poll`.
- nodeitems_utils.NodeItemCustom 1st & 2nd arg `poll`, `draw`.
- rna_prop_ui.draw 5th arg `use_edit`.
- rna_prop_ui.rna_idprop_ui_get 2nd arg `create`.
- rna_prop_ui.rna_idprop_ui_prop_clear 3rd arg `remove`.
- rna_prop_ui.rna_idprop_ui_prop_get 3rd arg `create`.
- rna_xml.xml2rna 2nd arg `root_rna`.
- rna_xml.xml_file_write 4th arg `skip_typemap`.
2021-06-08 18:03:14 +10:00
|
|
|
def clean_name(name, *, replace="_"):
|
2010-08-06 01:40:54 +00:00
|
|
|
"""
|
2011-07-31 03:15:37 +00:00
|
|
|
Returns a name with characters replaced that
|
2013-09-18 05:20:43 +00:00
|
|
|
may cause problems under various circumstances,
|
2011-07-31 03:15:37 +00:00
|
|
|
such as writing to a file.
|
2010-08-06 01:40:54 +00:00
|
|
|
All characters besides A-Z/a-z, 0-9 are replaced with "_"
|
2015-08-05 00:21:50 +10:00
|
|
|
or the *replace* argument if defined.
|
2021-10-22 18:27:03 -04:00
|
|
|
:arg name: The path name.
|
|
|
|
:type name: string or bytes
|
|
|
|
:arg replace: The replacement for non-valid characters.
|
|
|
|
:type replace: string
|
|
|
|
:return: The cleaned name.
|
|
|
|
:rtype: string
|
2010-08-06 01:40:54 +00:00
|
|
|
"""
|
|
|
|
|
2015-06-13 19:45:53 +10:00
|
|
|
if replace != "_":
|
|
|
|
if len(replace) != 1 or ord(replace) > 255:
|
|
|
|
raise ValueError("Value must be a single ascii character")
|
|
|
|
|
|
|
|
def maketrans_init():
|
|
|
|
trans_cache = clean_name._trans_cache
|
|
|
|
trans = trans_cache.get(replace)
|
|
|
|
if trans is None:
|
|
|
|
bad_chars = (
|
|
|
|
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
|
|
|
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
|
|
|
|
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
|
|
|
|
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
|
|
|
|
0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
|
|
|
|
0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2e, 0x2f, 0x3a,
|
|
|
|
0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x5b, 0x5c,
|
|
|
|
0x5d, 0x5e, 0x60, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
|
|
|
|
0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
|
|
|
|
0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
|
|
|
|
0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
|
|
|
|
0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
|
|
|
|
0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
|
|
|
|
0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
|
|
|
|
0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
|
|
|
|
0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
|
|
|
|
0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
|
|
|
|
0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
|
|
|
|
0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
|
|
|
|
0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
|
|
|
|
0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
|
|
|
|
0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
|
|
|
|
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
|
|
|
|
0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe,
|
2016-07-29 21:22:27 +10:00
|
|
|
)
|
2015-06-13 19:45:53 +10:00
|
|
|
trans = str.maketrans({char: replace for char in bad_chars})
|
|
|
|
trans_cache[replace] = trans
|
|
|
|
return trans
|
|
|
|
|
|
|
|
trans = maketrans_init()
|
|
|
|
return name.translate(trans)
|
2018-07-03 06:27:53 +02:00
|
|
|
|
|
|
|
|
2015-06-13 19:45:53 +10:00
|
|
|
clean_name._trans_cache = {}
|
2010-08-06 01:40:54 +00:00
|
|
|
|
|
|
|
|
2012-03-21 22:29:49 +00:00
|
|
|
def _clean_utf8(name):
|
|
|
|
if type(name) == bytes:
|
|
|
|
return name.decode("utf8", "replace")
|
|
|
|
else:
|
|
|
|
return name.encode("utf8", "replace").decode("utf8")
|
|
|
|
|
|
|
|
|
2018-01-14 14:31:39 +11:00
|
|
|
_display_name_literals = {
|
|
|
|
":": "_colon_",
|
|
|
|
"+": "_plus_",
|
2021-06-04 11:50:23 +02:00
|
|
|
"/": "_slash_",
|
2018-01-14 14:31:39 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-02-03 17:32:31 -07:00
|
|
|
def display_name(name, *, has_ext=True, title_case=True):
|
2010-08-06 01:40:54 +00:00
|
|
|
"""
|
|
|
|
Creates a display string from name to be used menus and the user interface.
|
2021-02-03 17:32:31 -07:00
|
|
|
Intended for use with filenames and module names.
|
|
|
|
|
2021-10-22 18:27:03 -04:00
|
|
|
:arg name: The name to be used for displaying the user interface.
|
|
|
|
:type name: string
|
|
|
|
:arg has_ext: Remove file extension from name.
|
|
|
|
:type has_ext: boolean
|
|
|
|
:arg title_case: Convert lowercase names to title case.
|
|
|
|
:type title_case: boolean
|
|
|
|
:return: The display string.
|
|
|
|
:rtype: string
|
2010-08-06 01:40:54 +00:00
|
|
|
"""
|
2018-02-26 19:05:01 +11:00
|
|
|
|
2019-01-25 13:45:56 +11:00
|
|
|
if has_ext:
|
|
|
|
name = _os.path.splitext(basename(name))[0]
|
2018-02-26 19:05:01 +11:00
|
|
|
|
2010-08-06 01:40:54 +00:00
|
|
|
# string replacements
|
2018-01-14 14:31:39 +11:00
|
|
|
for disp_value, file_value in _display_name_literals.items():
|
|
|
|
name = name.replace(file_value, disp_value)
|
2010-08-06 01:40:54 +00:00
|
|
|
|
2017-03-24 06:48:24 +11:00
|
|
|
# strip to allow underscore prefix
|
|
|
|
# (when paths can't start with numbers for eg).
|
|
|
|
name = name.replace("_", " ").lstrip(" ")
|
2010-08-06 01:40:54 +00:00
|
|
|
|
2021-02-03 17:32:31 -07:00
|
|
|
if title_case and name.islower():
|
2012-03-21 22:29:49 +00:00
|
|
|
name = name.lower().title()
|
|
|
|
|
|
|
|
name = _clean_utf8(name)
|
|
|
|
return name
|
2010-08-06 01:40:54 +00:00
|
|
|
|
2018-07-03 06:27:53 +02:00
|
|
|
|
2018-01-14 14:31:39 +11:00
|
|
|
def display_name_to_filepath(name):
|
|
|
|
"""
|
|
|
|
Performs the reverse of display_name using literal versions of characters
|
|
|
|
which aren't supported in a filepath.
|
2021-10-22 18:27:03 -04:00
|
|
|
:arg name: The display name to convert.
|
|
|
|
:type name: string
|
|
|
|
:return: The file path.
|
|
|
|
:rtype: string
|
2018-01-14 14:31:39 +11:00
|
|
|
"""
|
|
|
|
for disp_value, file_value in _display_name_literals.items():
|
|
|
|
name = name.replace(disp_value, file_value)
|
|
|
|
return name
|
|
|
|
|
2010-08-06 01:40:54 +00:00
|
|
|
|
2011-01-03 08:28:22 +00:00
|
|
|
def display_name_from_filepath(name):
|
|
|
|
"""
|
2011-08-02 17:00:44 +00:00
|
|
|
Returns the path stripped of directory and extension,
|
2011-07-31 03:15:37 +00:00
|
|
|
ensured to be utf8 compatible.
|
2021-10-22 18:27:03 -04:00
|
|
|
:arg name: The file path to convert.
|
|
|
|
:type name: string
|
|
|
|
:return: The display name.
|
|
|
|
:rtype: string
|
2011-01-03 08:28:22 +00:00
|
|
|
"""
|
2012-03-21 22:29:49 +00:00
|
|
|
|
2018-02-26 19:05:01 +11:00
|
|
|
name = _os.path.splitext(basename(name))[0]
|
2012-03-21 22:29:49 +00:00
|
|
|
name = _clean_utf8(name)
|
|
|
|
return name
|
2011-01-03 08:28:22 +00:00
|
|
|
|
|
|
|
|
2010-08-06 01:40:54 +00:00
|
|
|
def resolve_ncase(path):
|
2010-09-07 15:17:42 +00:00
|
|
|
"""
|
|
|
|
Resolve a case insensitive path on a case sensitive system,
|
|
|
|
returning a string with the path if found else return the original path.
|
2021-10-22 18:27:03 -04:00
|
|
|
:arg path: The path name to resolve.
|
|
|
|
:type path: string
|
|
|
|
:return: The resolved path.
|
|
|
|
:rtype: string
|
2010-09-07 15:17:42 +00:00
|
|
|
"""
|
2010-08-06 01:40:54 +00:00
|
|
|
|
2010-09-07 15:17:42 +00:00
|
|
|
def _ncase_path_found(path):
|
2011-11-16 16:38:37 +00:00
|
|
|
if not path or _os.path.exists(path):
|
2010-09-07 15:17:42 +00:00
|
|
|
return path, True
|
2010-08-06 01:40:54 +00:00
|
|
|
|
2011-07-31 03:15:37 +00:00
|
|
|
# filename may be a directory or a file
|
2011-11-16 16:38:37 +00:00
|
|
|
filename = _os.path.basename(path)
|
|
|
|
dirpath = _os.path.dirname(path)
|
2010-08-06 01:40:54 +00:00
|
|
|
|
2011-05-19 06:27:39 +00:00
|
|
|
suffix = path[:0] # "" but ensure byte/str match
|
2010-09-07 15:17:42 +00:00
|
|
|
if not filename: # dir ends with a slash?
|
|
|
|
if len(dirpath) < len(path):
|
|
|
|
suffix = path[:len(path) - len(dirpath)]
|
2010-08-06 01:40:54 +00:00
|
|
|
|
2011-11-16 16:38:37 +00:00
|
|
|
filename = _os.path.basename(dirpath)
|
|
|
|
dirpath = _os.path.dirname(dirpath)
|
2010-08-06 01:40:54 +00:00
|
|
|
|
2011-11-16 16:38:37 +00:00
|
|
|
if not _os.path.exists(dirpath):
|
2011-05-06 11:27:04 +00:00
|
|
|
if dirpath == path:
|
|
|
|
return path, False
|
|
|
|
|
2010-09-07 15:17:42 +00:00
|
|
|
dirpath, found = _ncase_path_found(dirpath)
|
2010-08-06 01:40:54 +00:00
|
|
|
|
2010-09-07 15:17:42 +00:00
|
|
|
if not found:
|
|
|
|
return path, False
|
2010-08-06 01:40:54 +00:00
|
|
|
|
2010-09-07 15:17:42 +00:00
|
|
|
# at this point, the directory exists but not the file
|
2010-08-06 01:40:54 +00:00
|
|
|
|
2010-09-07 15:17:42 +00:00
|
|
|
# we are expecting 'dirpath' to be a directory, but it could be a file
|
2011-11-16 16:38:37 +00:00
|
|
|
if _os.path.isdir(dirpath):
|
2014-07-30 15:35:19 +02:00
|
|
|
try:
|
|
|
|
files = _os.listdir(dirpath)
|
2015-05-17 17:17:31 +10:00
|
|
|
except PermissionError:
|
|
|
|
# We might not have the permission to list dirpath...
|
2014-07-30 15:35:19 +02:00
|
|
|
return path, False
|
2010-09-07 15:17:42 +00:00
|
|
|
else:
|
|
|
|
return path, False
|
2010-08-06 01:40:54 +00:00
|
|
|
|
2010-09-07 15:17:42 +00:00
|
|
|
filename_low = filename.lower()
|
|
|
|
f_iter_nocase = None
|
2010-08-06 01:40:54 +00:00
|
|
|
|
2010-09-07 15:17:42 +00:00
|
|
|
for f_iter in files:
|
|
|
|
if f_iter.lower() == filename_low:
|
|
|
|
f_iter_nocase = f_iter
|
|
|
|
break
|
2010-08-06 01:40:54 +00:00
|
|
|
|
2010-09-07 15:17:42 +00:00
|
|
|
if f_iter_nocase:
|
2011-11-16 16:38:37 +00:00
|
|
|
return _os.path.join(dirpath, f_iter_nocase) + suffix, True
|
2010-09-07 15:17:42 +00:00
|
|
|
else:
|
2018-09-03 16:49:08 +02:00
|
|
|
# can't find the right one, just return the path as is.
|
2010-09-07 15:17:42 +00:00
|
|
|
return path, False
|
2010-08-06 01:40:54 +00:00
|
|
|
|
2010-09-07 15:17:42 +00:00
|
|
|
ncase_path, found = _ncase_path_found(path)
|
|
|
|
return ncase_path if found else path
|
2010-08-06 01:40:54 +00:00
|
|
|
|
|
|
|
|
PyAPI: use keyword only arguments
Use keyword only arguments for the following functions.
- addon_utils.module_bl_info 2nd arg `info_basis`.
- addon_utils.modules 1st `module_cache`, 2nd arg `refresh`.
- addon_utils.modules_refresh 1st arg `module_cache`.
- bl_app_template_utils.activate 1nd arg `template_id`.
- bl_app_template_utils.import_from_id 2nd arg `ignore_not_found`.
- bl_app_template_utils.import_from_path 2nd arg `ignore_not_found`.
- bl_keymap_utils.keymap_from_toolbar.generate 2nd & 3rd args `use_fallback_keys` & `use_reset`.
- bl_keymap_utils.platform_helpers.keyconfig_data_oskey_from_ctrl 2nd arg `filter_fn`.
- bl_ui_utils.bug_report_url.url_prefill_from_blender 1st arg `addon_info`.
- bmesh.types.BMFace.copy 1st & 2nd args `verts`, `edges`.
- bmesh.types.BMesh.calc_volume 1st arg `signed`.
- bmesh.types.BMesh.from_mesh 2nd..4th args `face_normals`, `use_shape_key`, `shape_key_index`.
- bmesh.types.BMesh.from_object 3rd & 4th args `cage`, `face_normals`.
- bmesh.types.BMesh.transform 2nd arg `filter`.
- bmesh.types.BMesh.update_edit_mesh 2nd & 3rd args `loop_triangles`, `destructive`.
- bmesh.types.{BMVertSeq,BMEdgeSeq,BMFaceSeq}.sort 1st & 2nd arg `key`, `reverse`.
- bmesh.utils.face_split 4th..6th args `coords`, `use_exist`, `example`.
- bpy.data.libraries.load 2nd..4th args `link`, `relative`, `assets_only`.
- bpy.data.user_map 1st..3rd args `subset`, `key_types, `value_types`.
- bpy.msgbus.subscribe_rna 5th arg `options`.
- bpy.path.abspath 2nd & 3rd args `start` & `library`.
- bpy.path.clean_name 2nd arg `replace`.
- bpy.path.ensure_ext 3rd arg `case_sensitive`.
- bpy.path.module_names 2nd arg `recursive`.
- bpy.path.relpath 2nd arg `start`.
- bpy.types.EditBone.transform 2nd & 3rd arg `scale`, `roll`.
- bpy.types.Operator.as_keywords 1st arg `ignore`.
- bpy.types.Struct.{keyframe_insert,keyframe_delete} 2nd..5th args `index`, `frame`, `group`, `options`.
- bpy.types.WindowManager.popup_menu 2nd & 3rd arg `title`, `icon`.
- bpy.types.WindowManager.popup_menu_pie 3rd & 4th arg `title`, `icon`.
- bpy.utils.app_template_paths 1st arg `subdir`.
- bpy.utils.app_template_paths 1st arg `subdir`.
- bpy.utils.blend_paths 1st..3rd args `absolute`, `packed`, `local`.
- bpy.utils.execfile 2nd arg `mod`.
- bpy.utils.keyconfig_set 2nd arg `report`.
- bpy.utils.load_scripts 1st & 2nd `reload_scripts` & `refresh_scripts`.
- bpy.utils.preset_find 3rd & 4th args `display_name`, `ext`.
- bpy.utils.resource_path 2nd & 3rd arg `major`, `minor`.
- bpy.utils.script_paths 1st..4th args `subdir`, `user_pref`, `check_all`, `use_user`.
- bpy.utils.smpte_from_frame 2nd & 3rd args `fps`, `fps_base`.
- bpy.utils.smpte_from_seconds 2nd & 3rd args `fps`, `fps_base`.
- bpy.utils.system_resource 2nd arg `subdir`.
- bpy.utils.time_from_frame 2nd & 3rd args `fps`, `fps_base`.
- bpy.utils.time_to_frame 2nd & 3rd args `fps`, `fps_base`.
- bpy.utils.units.to_string 4th..6th `precision`, `split_unit`, `compatible_unit`.
- bpy.utils.units.to_value 4th arg `str_ref_unit`.
- bpy.utils.user_resource 2nd & 3rd args `subdir`, `create`
- bpy_extras.view3d_utils.location_3d_to_region_2d 4th arg `default`.
- bpy_extras.view3d_utils.region_2d_to_origin_3d 4th arg `clamp`.
- gpu.offscreen.unbind 1st arg `restore`.
- gpu_extras.batch.batch_for_shader 4th arg `indices`.
- gpu_extras.batch.presets.draw_circle_2d 4th arg `segments`.
- gpu_extras.presets.draw_circle_2d 4th arg `segments`.
- imbuf.types.ImBuf.resize 2nd arg `resize`.
- imbuf.write 2nd arg `filepath`.
- mathutils.kdtree.KDTree.find 2nd arg `filter`.
- nodeitems_utils.NodeCategory 3rd & 4th arg `descriptions`, `items`.
- nodeitems_utils.NodeItem 2nd..4th args `label`, `settings`, `poll`.
- nodeitems_utils.NodeItemCustom 1st & 2nd arg `poll`, `draw`.
- rna_prop_ui.draw 5th arg `use_edit`.
- rna_prop_ui.rna_idprop_ui_get 2nd arg `create`.
- rna_prop_ui.rna_idprop_ui_prop_clear 3rd arg `remove`.
- rna_prop_ui.rna_idprop_ui_prop_get 3rd arg `create`.
- rna_xml.xml2rna 2nd arg `root_rna`.
- rna_xml.xml_file_write 4th arg `skip_typemap`.
2021-06-08 18:03:14 +10:00
|
|
|
def ensure_ext(filepath, ext, *, case_sensitive=False):
|
2010-08-06 01:40:54 +00:00
|
|
|
"""
|
2011-08-02 17:00:44 +00:00
|
|
|
Return the path with the extension added if it is not already set.
|
2010-08-06 01:40:54 +00:00
|
|
|
|
2021-10-22 18:27:03 -04:00
|
|
|
:arg filepath: The file path.
|
|
|
|
:type filepath: string
|
2015-09-03 13:09:16 +02:00
|
|
|
:arg ext: The extension to check for, can be a compound extension. Should
|
|
|
|
start with a dot, such as '.blend' or '.tar.gz'.
|
2010-08-06 01:40:54 +00:00
|
|
|
:type ext: string
|
|
|
|
:arg case_sensitive: Check for matching case when comparing extensions.
|
2021-10-22 18:27:03 -04:00
|
|
|
:type case_sensitive: boolean
|
|
|
|
:return: The file path with the given extension.
|
|
|
|
:rtype: string
|
2010-08-06 01:40:54 +00:00
|
|
|
"""
|
2011-07-31 03:15:37 +00:00
|
|
|
|
2015-09-03 21:58:11 +10:00
|
|
|
if case_sensitive:
|
|
|
|
if filepath.endswith(ext):
|
|
|
|
return filepath
|
|
|
|
else:
|
|
|
|
if filepath[-len(ext):].lower().endswith(ext.lower()):
|
|
|
|
return filepath
|
2010-08-06 01:40:54 +00:00
|
|
|
|
2015-09-03 13:09:16 +02:00
|
|
|
return filepath + ext
|
2010-09-08 04:55:37 +00:00
|
|
|
|
|
|
|
|
PyAPI: use keyword only arguments
Use keyword only arguments for the following functions.
- addon_utils.module_bl_info 2nd arg `info_basis`.
- addon_utils.modules 1st `module_cache`, 2nd arg `refresh`.
- addon_utils.modules_refresh 1st arg `module_cache`.
- bl_app_template_utils.activate 1nd arg `template_id`.
- bl_app_template_utils.import_from_id 2nd arg `ignore_not_found`.
- bl_app_template_utils.import_from_path 2nd arg `ignore_not_found`.
- bl_keymap_utils.keymap_from_toolbar.generate 2nd & 3rd args `use_fallback_keys` & `use_reset`.
- bl_keymap_utils.platform_helpers.keyconfig_data_oskey_from_ctrl 2nd arg `filter_fn`.
- bl_ui_utils.bug_report_url.url_prefill_from_blender 1st arg `addon_info`.
- bmesh.types.BMFace.copy 1st & 2nd args `verts`, `edges`.
- bmesh.types.BMesh.calc_volume 1st arg `signed`.
- bmesh.types.BMesh.from_mesh 2nd..4th args `face_normals`, `use_shape_key`, `shape_key_index`.
- bmesh.types.BMesh.from_object 3rd & 4th args `cage`, `face_normals`.
- bmesh.types.BMesh.transform 2nd arg `filter`.
- bmesh.types.BMesh.update_edit_mesh 2nd & 3rd args `loop_triangles`, `destructive`.
- bmesh.types.{BMVertSeq,BMEdgeSeq,BMFaceSeq}.sort 1st & 2nd arg `key`, `reverse`.
- bmesh.utils.face_split 4th..6th args `coords`, `use_exist`, `example`.
- bpy.data.libraries.load 2nd..4th args `link`, `relative`, `assets_only`.
- bpy.data.user_map 1st..3rd args `subset`, `key_types, `value_types`.
- bpy.msgbus.subscribe_rna 5th arg `options`.
- bpy.path.abspath 2nd & 3rd args `start` & `library`.
- bpy.path.clean_name 2nd arg `replace`.
- bpy.path.ensure_ext 3rd arg `case_sensitive`.
- bpy.path.module_names 2nd arg `recursive`.
- bpy.path.relpath 2nd arg `start`.
- bpy.types.EditBone.transform 2nd & 3rd arg `scale`, `roll`.
- bpy.types.Operator.as_keywords 1st arg `ignore`.
- bpy.types.Struct.{keyframe_insert,keyframe_delete} 2nd..5th args `index`, `frame`, `group`, `options`.
- bpy.types.WindowManager.popup_menu 2nd & 3rd arg `title`, `icon`.
- bpy.types.WindowManager.popup_menu_pie 3rd & 4th arg `title`, `icon`.
- bpy.utils.app_template_paths 1st arg `subdir`.
- bpy.utils.app_template_paths 1st arg `subdir`.
- bpy.utils.blend_paths 1st..3rd args `absolute`, `packed`, `local`.
- bpy.utils.execfile 2nd arg `mod`.
- bpy.utils.keyconfig_set 2nd arg `report`.
- bpy.utils.load_scripts 1st & 2nd `reload_scripts` & `refresh_scripts`.
- bpy.utils.preset_find 3rd & 4th args `display_name`, `ext`.
- bpy.utils.resource_path 2nd & 3rd arg `major`, `minor`.
- bpy.utils.script_paths 1st..4th args `subdir`, `user_pref`, `check_all`, `use_user`.
- bpy.utils.smpte_from_frame 2nd & 3rd args `fps`, `fps_base`.
- bpy.utils.smpte_from_seconds 2nd & 3rd args `fps`, `fps_base`.
- bpy.utils.system_resource 2nd arg `subdir`.
- bpy.utils.time_from_frame 2nd & 3rd args `fps`, `fps_base`.
- bpy.utils.time_to_frame 2nd & 3rd args `fps`, `fps_base`.
- bpy.utils.units.to_string 4th..6th `precision`, `split_unit`, `compatible_unit`.
- bpy.utils.units.to_value 4th arg `str_ref_unit`.
- bpy.utils.user_resource 2nd & 3rd args `subdir`, `create`
- bpy_extras.view3d_utils.location_3d_to_region_2d 4th arg `default`.
- bpy_extras.view3d_utils.region_2d_to_origin_3d 4th arg `clamp`.
- gpu.offscreen.unbind 1st arg `restore`.
- gpu_extras.batch.batch_for_shader 4th arg `indices`.
- gpu_extras.batch.presets.draw_circle_2d 4th arg `segments`.
- gpu_extras.presets.draw_circle_2d 4th arg `segments`.
- imbuf.types.ImBuf.resize 2nd arg `resize`.
- imbuf.write 2nd arg `filepath`.
- mathutils.kdtree.KDTree.find 2nd arg `filter`.
- nodeitems_utils.NodeCategory 3rd & 4th arg `descriptions`, `items`.
- nodeitems_utils.NodeItem 2nd..4th args `label`, `settings`, `poll`.
- nodeitems_utils.NodeItemCustom 1st & 2nd arg `poll`, `draw`.
- rna_prop_ui.draw 5th arg `use_edit`.
- rna_prop_ui.rna_idprop_ui_get 2nd arg `create`.
- rna_prop_ui.rna_idprop_ui_prop_clear 3rd arg `remove`.
- rna_prop_ui.rna_idprop_ui_prop_get 3rd arg `create`.
- rna_xml.xml2rna 2nd arg `root_rna`.
- rna_xml.xml_file_write 4th arg `skip_typemap`.
2021-06-08 18:03:14 +10:00
|
|
|
def module_names(path, *, recursive=False):
|
2010-09-08 04:55:37 +00:00
|
|
|
"""
|
|
|
|
Return a list of modules which can be imported from *path*.
|
|
|
|
|
|
|
|
:arg path: a directory to scan.
|
|
|
|
:type path: string
|
|
|
|
:arg recursive: Also return submodule names for packages.
|
|
|
|
:type recursive: bool
|
2010-09-08 07:30:20 +00:00
|
|
|
:return: a list of string pairs (module_name, module_file).
|
2021-10-22 18:27:03 -04:00
|
|
|
:rtype: list of strings
|
2010-09-08 04:55:37 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
from os.path import join, isfile
|
|
|
|
|
|
|
|
modules = []
|
|
|
|
|
|
|
|
for filename in sorted(_os.listdir(path)):
|
2010-12-11 11:52:28 +00:00
|
|
|
if filename == "modules":
|
2011-01-01 07:20:34 +00:00
|
|
|
pass # XXX, hard coded exception.
|
2010-12-11 11:52:28 +00:00
|
|
|
elif filename.endswith(".py") and filename != "__init__.py":
|
2010-09-08 07:30:20 +00:00
|
|
|
fullpath = join(path, filename)
|
|
|
|
modules.append((filename[0:-3], fullpath))
|
2019-10-29 02:06:10 +11:00
|
|
|
elif not filename.startswith("."):
|
|
|
|
# Skip hidden files since they are used by for version control.
|
2010-09-08 04:55:37 +00:00
|
|
|
directory = join(path, filename)
|
2010-09-08 07:30:20 +00:00
|
|
|
fullpath = join(directory, "__init__.py")
|
|
|
|
if isfile(fullpath):
|
|
|
|
modules.append((filename, fullpath))
|
2010-09-08 04:55:37 +00:00
|
|
|
if recursive:
|
PyAPI: use keyword only arguments
Use keyword only arguments for the following functions.
- addon_utils.module_bl_info 2nd arg `info_basis`.
- addon_utils.modules 1st `module_cache`, 2nd arg `refresh`.
- addon_utils.modules_refresh 1st arg `module_cache`.
- bl_app_template_utils.activate 1nd arg `template_id`.
- bl_app_template_utils.import_from_id 2nd arg `ignore_not_found`.
- bl_app_template_utils.import_from_path 2nd arg `ignore_not_found`.
- bl_keymap_utils.keymap_from_toolbar.generate 2nd & 3rd args `use_fallback_keys` & `use_reset`.
- bl_keymap_utils.platform_helpers.keyconfig_data_oskey_from_ctrl 2nd arg `filter_fn`.
- bl_ui_utils.bug_report_url.url_prefill_from_blender 1st arg `addon_info`.
- bmesh.types.BMFace.copy 1st & 2nd args `verts`, `edges`.
- bmesh.types.BMesh.calc_volume 1st arg `signed`.
- bmesh.types.BMesh.from_mesh 2nd..4th args `face_normals`, `use_shape_key`, `shape_key_index`.
- bmesh.types.BMesh.from_object 3rd & 4th args `cage`, `face_normals`.
- bmesh.types.BMesh.transform 2nd arg `filter`.
- bmesh.types.BMesh.update_edit_mesh 2nd & 3rd args `loop_triangles`, `destructive`.
- bmesh.types.{BMVertSeq,BMEdgeSeq,BMFaceSeq}.sort 1st & 2nd arg `key`, `reverse`.
- bmesh.utils.face_split 4th..6th args `coords`, `use_exist`, `example`.
- bpy.data.libraries.load 2nd..4th args `link`, `relative`, `assets_only`.
- bpy.data.user_map 1st..3rd args `subset`, `key_types, `value_types`.
- bpy.msgbus.subscribe_rna 5th arg `options`.
- bpy.path.abspath 2nd & 3rd args `start` & `library`.
- bpy.path.clean_name 2nd arg `replace`.
- bpy.path.ensure_ext 3rd arg `case_sensitive`.
- bpy.path.module_names 2nd arg `recursive`.
- bpy.path.relpath 2nd arg `start`.
- bpy.types.EditBone.transform 2nd & 3rd arg `scale`, `roll`.
- bpy.types.Operator.as_keywords 1st arg `ignore`.
- bpy.types.Struct.{keyframe_insert,keyframe_delete} 2nd..5th args `index`, `frame`, `group`, `options`.
- bpy.types.WindowManager.popup_menu 2nd & 3rd arg `title`, `icon`.
- bpy.types.WindowManager.popup_menu_pie 3rd & 4th arg `title`, `icon`.
- bpy.utils.app_template_paths 1st arg `subdir`.
- bpy.utils.app_template_paths 1st arg `subdir`.
- bpy.utils.blend_paths 1st..3rd args `absolute`, `packed`, `local`.
- bpy.utils.execfile 2nd arg `mod`.
- bpy.utils.keyconfig_set 2nd arg `report`.
- bpy.utils.load_scripts 1st & 2nd `reload_scripts` & `refresh_scripts`.
- bpy.utils.preset_find 3rd & 4th args `display_name`, `ext`.
- bpy.utils.resource_path 2nd & 3rd arg `major`, `minor`.
- bpy.utils.script_paths 1st..4th args `subdir`, `user_pref`, `check_all`, `use_user`.
- bpy.utils.smpte_from_frame 2nd & 3rd args `fps`, `fps_base`.
- bpy.utils.smpte_from_seconds 2nd & 3rd args `fps`, `fps_base`.
- bpy.utils.system_resource 2nd arg `subdir`.
- bpy.utils.time_from_frame 2nd & 3rd args `fps`, `fps_base`.
- bpy.utils.time_to_frame 2nd & 3rd args `fps`, `fps_base`.
- bpy.utils.units.to_string 4th..6th `precision`, `split_unit`, `compatible_unit`.
- bpy.utils.units.to_value 4th arg `str_ref_unit`.
- bpy.utils.user_resource 2nd & 3rd args `subdir`, `create`
- bpy_extras.view3d_utils.location_3d_to_region_2d 4th arg `default`.
- bpy_extras.view3d_utils.region_2d_to_origin_3d 4th arg `clamp`.
- gpu.offscreen.unbind 1st arg `restore`.
- gpu_extras.batch.batch_for_shader 4th arg `indices`.
- gpu_extras.batch.presets.draw_circle_2d 4th arg `segments`.
- gpu_extras.presets.draw_circle_2d 4th arg `segments`.
- imbuf.types.ImBuf.resize 2nd arg `resize`.
- imbuf.write 2nd arg `filepath`.
- mathutils.kdtree.KDTree.find 2nd arg `filter`.
- nodeitems_utils.NodeCategory 3rd & 4th arg `descriptions`, `items`.
- nodeitems_utils.NodeItem 2nd..4th args `label`, `settings`, `poll`.
- nodeitems_utils.NodeItemCustom 1st & 2nd arg `poll`, `draw`.
- rna_prop_ui.draw 5th arg `use_edit`.
- rna_prop_ui.rna_idprop_ui_get 2nd arg `create`.
- rna_prop_ui.rna_idprop_ui_prop_clear 3rd arg `remove`.
- rna_prop_ui.rna_idprop_ui_prop_get 3rd arg `create`.
- rna_xml.xml2rna 2nd arg `root_rna`.
- rna_xml.xml_file_write 4th arg `skip_typemap`.
2021-06-08 18:03:14 +10:00
|
|
|
for mod_name, mod_path in module_names(directory, recursive=True):
|
2011-07-31 03:15:37 +00:00
|
|
|
modules.append(("%s.%s" % (filename, mod_name),
|
2012-10-08 08:28:05 +00:00
|
|
|
mod_path,
|
|
|
|
))
|
2010-09-08 04:55:37 +00:00
|
|
|
|
|
|
|
return modules
|
2011-07-20 08:10:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
def basename(path):
|
|
|
|
"""
|
2021-05-12 20:29:32 -04:00
|
|
|
Equivalent to ``os.path.basename``, but skips a "//" prefix.
|
2011-07-20 08:10:01 +00:00
|
|
|
|
|
|
|
Use for Windows compatibility.
|
2021-10-22 18:27:03 -04:00
|
|
|
:return: The base name of the given path.
|
|
|
|
:rtype: string
|
2011-07-20 08:10:01 +00:00
|
|
|
"""
|
2011-07-25 04:00:11 +00:00
|
|
|
return _os.path.basename(path[2:] if path[:2] in {"//", b"//"} else path)
|
2013-08-12 07:44:38 +00:00
|
|
|
|
|
|
|
|
2016-03-29 18:22:35 +11:00
|
|
|
def native_pathsep(path):
|
|
|
|
"""
|
|
|
|
Replace the path separator with the systems native ``os.sep``.
|
2021-10-22 18:27:03 -04:00
|
|
|
:arg path: The path to replace.
|
|
|
|
:type path: string
|
|
|
|
:return: The path with system native separators.
|
|
|
|
:rtype: string
|
2016-03-29 18:22:35 +11:00
|
|
|
"""
|
|
|
|
if type(path) is str:
|
|
|
|
if _os.sep == "/":
|
|
|
|
return path.replace("\\", "/")
|
|
|
|
else:
|
|
|
|
if path.startswith("//"):
|
|
|
|
return "//" + path[2:].replace("/", "\\")
|
|
|
|
else:
|
|
|
|
return path.replace("/", "\\")
|
|
|
|
else: # bytes
|
|
|
|
if _os.sep == "/":
|
|
|
|
return path.replace(b"\\", b"/")
|
|
|
|
else:
|
|
|
|
if path.startswith(b"//"):
|
|
|
|
return b"//" + path[2:].replace(b"/", b"\\")
|
|
|
|
else:
|
|
|
|
return path.replace(b"/", b"\\")
|
|
|
|
|
|
|
|
|
2013-08-12 07:44:38 +00:00
|
|
|
def reduce_dirs(dirs):
|
|
|
|
"""
|
|
|
|
Given a sequence of directories, remove duplicates and
|
|
|
|
any directories nested in one of the other paths.
|
|
|
|
(Useful for recursive path searching).
|
|
|
|
|
|
|
|
:arg dirs: Sequence of directory paths.
|
2021-10-22 18:27:03 -04:00
|
|
|
:type dirs: sequence of strings
|
2013-08-12 07:44:38 +00:00
|
|
|
:return: A unique list of paths.
|
2021-10-22 18:27:03 -04:00
|
|
|
:rtype: list of strings
|
2013-08-12 07:44:38 +00:00
|
|
|
"""
|
|
|
|
dirs = list({_os.path.normpath(_os.path.abspath(d)) for d in dirs})
|
|
|
|
dirs.sort(key=lambda d: len(d))
|
2013-11-20 03:38:18 +11:00
|
|
|
for i in range(len(dirs) - 1, -1, -1):
|
2013-08-12 07:44:38 +00:00
|
|
|
for j in range(i):
|
|
|
|
print(i, j)
|
|
|
|
if len(dirs[i]) == len(dirs[j]):
|
|
|
|
break
|
|
|
|
elif is_subdir(dirs[i], dirs[j]):
|
|
|
|
del dirs[i]
|
|
|
|
break
|
|
|
|
return dirs
|