Python API: use cached translation tables

bpy.path.clean_name() and AddPresetBase.as_filename() were doing
inefficient search-replace of individual characters.

Use cached replacement table instead.
This commit is contained in:
2015-06-13 19:45:53 +10:00
parent 02a496c61c
commit 60ddaf045a
2 changed files with 54 additions and 19 deletions

View File

@@ -53,9 +53,20 @@ class AddPresetBase:
@staticmethod
def as_filename(name): # could reuse for other presets
for char in " !@#$%^&*(){}:\";'[]<>,.\\/?":
name = name.replace(char, '_')
return name.lower().strip()
# lazy init maketrans
def maketrans_init():
cls = AddPresetBase
attr = "_as_filename_trans"
trans = getattr(cls, attr, None)
if trans is None:
trans = str.maketrans({char: "_" for char in " !@#$%^&*(){}:\";'[]<>,.\\/?"})
setattr(cls, attr, trans)
return trans
trans = maketrans_init()
return name.lower().strip().translate(trans)
def execute(self, context):
import os