[Bf-extensions-cvs] [a34b1cee] master: Cleanup: simplify file name incrementing logic
Campbell Barton
noreply at git.blender.org
Mon Mar 15 02:25:31 CET 2021
Commit: a34b1cee5169b6b8a53b7cd0eca49b444e274ab1
Author: Campbell Barton
Date: Mon Mar 15 12:22:18 2021 +1100
Branches: master
https://developer.blender.org/rBAa34b1cee5169b6b8a53b7cd0eca49b444e274ab1
Cleanup: simplify file name incrementing logic
===================================================================
M space_view3d_pie_menus/pie_save_open_menu.py
===================================================================
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):
More information about the Bf-extensions-cvs
mailing list