diff options
author | Campbell Barton <ideasman42@gmail.com> | 2021-03-15 04:22:18 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2021-03-15 04:24:19 +0300 |
commit | a34b1cee5169b6b8a53b7cd0eca49b444e274ab1 (patch) | |
tree | 024edea14b9af9df952c9d10a1fe1ef27ea24ab1 /space_view3d_pie_menus | |
parent | ee2045bd9efdbf9691f4b4ae78b39873892a7dfc (diff) |
Cleanup: simplify file name incrementing logic
Diffstat (limited to 'space_view3d_pie_menus')
-rw-r--r-- | space_view3d_pie_menus/pie_save_open_menu.py | 33 |
1 files changed, 11 insertions, 22 deletions
diff --git a/space_view3d_pie_menus/pie_save_open_menu.py b/space_view3d_pie_menus/pie_save_open_menu.py index 31714699..f1bef21b 100644 --- a/space_view3d_pie_menus/pie_save_open_menu.py +++ b/space_view3d_pie_menus/pie_save_open_menu.py @@ -43,29 +43,18 @@ class PIE_MT_SaveOpen(Menu): @staticmethod def _save_as_mainfile_calc_incremental_name(): - f_path = bpy.data.filepath - b_name = bpy.path.basename(f_path) - - if b_name and b_name.find("_") != -1: - # except in cases when there is an underscore in the name like my_file.blend - try: - str_nb = b_name.rpartition("_")[-1].rpartition(".blend")[0] - int_nb = int(str(str_nb)) - new_nb = str_nb.replace(str(int_nb), str(int_nb + 1)) - output = f_path.replace(str_nb, new_nb) - - i = 1 - while os.path.isfile(output): - str_nb = b_name.rpartition("_")[-1].rpartition(".blend")[0] - i += 1 - new_nb = str_nb.replace(str(int_nb), str(int_nb + i)) - output = f_path.replace(str_nb, new_nb) - except ValueError: - output = f_path.rpartition(".blend")[0] + "_001" + ".blend" + import re + dirname, base_name = os.path.split(bpy.data.filepath) + base_name_no_ext, ext = os.path.splitext(base_name) + match = re.match(r"(.*)_([\d]+)$", base_name_no_ext) + if match: + prefix, number = match.groups() + number = int(number) + 1 else: - # no underscore in the name or saving a nameless (.blend) file - output = f_path.rpartition(".blend")[0] + "_001" + ".blend" - + prefix, number = base_name_no_ext, 1 + prefix = os.path.join(dirname, prefix) + while os.path.isfile(output := "%s_%03d%s" % (prefix, number, ext)): + number += 1 return output def draw(self, context): |