I've developed a Blender plugin and integrated custom icons from a single folder. I'm facing a perplexing issue where, at random times, some of these icons fail to load. It's not consistent; sometimes it happens and sometimes it doesn't, and it doesn't seem to be tied to any specific icons.
Here's a brief overview of my setup:
- All icons are stored in the same plugin sub-directory, i've got +-90 icons in .png format.
- I've ensured that the paths are correctly set.
- There doesn't seem to be a correlation with any specific Blender actions or sequences.
- The issue always occure at plugin register time, closing blender & restart can fix the issues.
- Icons appear invisible when this happens, the icon exists in the bpy data, but it render invisible in the interface.
- No errors in the console.
- I'd say this bug occure 5% of the time. I rarely stumble upon it, but sometimes my users report it.
- it's impossible to tell which icons are invisible from
bpy.types.ImagePreview
api calls
Has anyone faced a similar issue or have any insights on what might be causing this sporadic behavior? Any help in troubleshooting or resolving this would be greatly appreciated.
this is how i deal with icons in my script
def get_previews_from_directory( directory, format=".png", previews=None,):
"""install previews with bpy.utils.preview, will try to search for all image file inside given directory"""
if (previews is None):
previews = bpy.utils.previews.new()
for f in os.listdir(directory):
if (f[-len(format):]==format):
icon_name = f[:-len(format)]
path = os.path.abspath(os.path.join(directory, f))
previews.load( icon_name, path, "IMAGE")
continue
return previews
Icons = {}
def get_icon_id(str_value):
global Icons
if (str_value in Icons):
return Icons[str_value].icon_id
return 0
def register():
global Icons
Icons = get_previews_from_directory(directories.addon_icons)
Icons = get_previews_from_directory(os.path.join(directories.addon_icons,"placeholder"), previews=Icons)
return None
def unregister():
global Icons
remove_previews(Icons)
return None
Thank you!