3
$\begingroup$

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!

$\endgroup$
5
  • 1
    $\begingroup$ Any chance to see the code? Or at least maybe there is some minimal code example with reproducable issue? $\endgroup$ Sep 21, 2023 at 14:54
  • $\begingroup$ In this case I would rather say: let's see the icons (original files), because this sounds more like a spontaneous decoding error to me. $\endgroup$
    – quellenform
    Sep 21, 2023 at 23:05
  • $\begingroup$ thank you for the suggestion, i pasted the code above :-) $\endgroup$
    – Fox
    Sep 23, 2023 at 6:27
  • $\begingroup$ As Martynas has mentioned, you will definitely get more attention if you can provide fully functional, minimalistic, and reproducible code that we can easily copy, paste, and test. $\endgroup$ Sep 26, 2023 at 2:31
  • $\begingroup$ ...and I still think the icons themselves are the cause (and I may be wrong, of course), but please share them additionally as a ZIP. $\endgroup$
    – quellenform
    Sep 26, 2023 at 8:21

1 Answer 1

1
+100
$\begingroup$

I'm almost sure it's a Blender Bug, until a few versions ago this was rare. Even in an Alpha version of Blender 3.6 it somehow made images unusable within Blender even on subsequent startup. I never fully understood the problem, but I found a somewhat "Hacky" solution, I don't know if it will be useful to you.

In my addon I created a "Reload" button that clears all icons from memory, which basically does what your functions below do:

def reload_icon()
    # Call this function, by an Operator 
    global Icons
    remove_previews(Icons)
    Icons = get_previews_from_directory(directories.addon_icons)
    Icons = get_previews_from_directory(os.path.join(directories.addon_icons,"placeholder"), previews=Icons)

Furthermore, the 3.6 Alpha version of Blender made the icons unusable, to fix this I had to do a dirty trick by copying the icon images and deleting the old ones with this script:

def restore_images(filepath):
    # This is only if the icons persist to be transparent every time Blender is restarted.
    for file in os.listdir(filepath):
        file_path = os.path.join(filepath, file)
        temp_name = file_path + ".temp"
        shutil.copy(file_path, temp_name)
        os.remove(file)
        os.rename(temp_name, file)
    # At the end, reload the icons
    reload_icon()

I know it may sound silly, but this worked, new image files loaded correctly again. This could be a clue to the Blender problem.

It may be useful to make a detailed Bugreport at projects.blender.org

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .