From dd0522242ae6e8ba3ca5c56e82592539fb74a9e1 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Thu, 2 Jun 2011 15:21:47 +0000 Subject: [PATCH] addons now show expanded list again (since Brecht's commit now makes it fast) also add utility function for getting cleaned, unique names from python: bpy_extras.io_utils.unique_name(...) --- .../scripts/modules/bpy_extras/io_utils.py | 41 +++++++++++++++++++ .../scripts/startup/bl_ui/space_userpref.py | 2 +- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/release/scripts/modules/bpy_extras/io_utils.py b/release/scripts/modules/bpy_extras/io_utils.py index c444fd618a8..12c2d809132 100644 --- a/release/scripts/modules/bpy_extras/io_utils.py +++ b/release/scripts/modules/bpy_extras/io_utils.py @@ -29,6 +29,7 @@ __all__ = ( "path_reference", "path_reference_copy", "path_reference_mode", + "unique_name" ) import bpy @@ -298,3 +299,43 @@ def path_reference_copy(copy_set, report=print): os.makedirs(dir_to) shutil.copy(file_src, file_dst) + + +def unique_name(key, name, name_dict, name_max=-1, clean_func=None): + """ + Helper function for storing unique names which may have special characters + stripped and restricted to a maximum length. + + :arg key: unique item this name belongs to, name_dict[key] will be reused + when available. + This can be the object, mesh, material, etc instance its self. + :type key: any hashable object assosiated with the *name*. + :arg name: The name used to create a unique value in *name_dict*. + :type name: string + :arg name_dict: This is used to cache namespace to ensure no collisions + occur, this should be an empty dict initially and only modified by this + function. + :type name_dict: dict + :arg clean_func: Function to call on *name* before creating a unique value. + :type clean_func: function + """ + name_new = name_dict.get(key) + if name_new is None: + count = 1 + name_dict_values = name_dict.values() + name_new = name_new_orig = name if clean_func is None else clean_func(name) + + if name_max == -1: + while name_new in name_dict_values: + name_new = "%s.%03d" % (name_new_orig, count) + count += 1 + else: + name_new = name_new[:name_max] + while name_new in name_dict_values: + count_str = "%03d" % count + name_new = "%.*s.%s" % (name_max - (len(count_str) + 1), name_new_orig, count_str) + count += 1 + + name_dict[key] = name_new + + return name_new diff --git a/release/scripts/startup/bl_ui/space_userpref.py b/release/scripts/startup/bl_ui/space_userpref.py index e34755ae72e..f018785a925 100644 --- a/release/scripts/startup/bl_ui/space_userpref.py +++ b/release/scripts/startup/bl_ui/space_userpref.py @@ -890,7 +890,7 @@ class USERPREF_PT_addons(bpy.types.Panel): col = split.column() col.prop(context.window_manager, "addon_search", text="", icon='VIEWZOOM') col.label(text="Categories") - col.prop(context.window_manager, "addon_filter", text="") # , expand=True, too slow with dynamic enum. + col.prop(context.window_manager, "addon_filter", expand=True) col.label(text="Supported Level") col.prop(context.window_manager, "addon_support", expand=True)