EnumProperty and string encoding

Hi there,
I occured strange behaviour with EnumProperty and it’s encoding with non English characters.
In addon UI each time I want to check the list it doesn’t read it properly.
Error: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf0 in position 0: invalid continuation byte
In console it prints proper values, so I know I feed it with proper data. A funny thing starts to happen when I try to decode el string with .decode('utf-8'). It raise an error in console that string doesn’t have decode func, and of course pleny more errors. But in UI everything works like a charm. Encoding, proper path imports, etc…

I’m thinking it might be connected with python implementation of EnumProperty, but maybe someone can lead me to some clean sollution that doesn’t involve raising any errors?

Code part:

def categories(self, context):
    categories = []
    nr = 0
    pref = context.preferences.addons[__name__].preferences
    lib_path = bpy.path.abspath(pref.lib_path)
    for el in sorted(os.listdir(lib_path)):
        p = os.path.join(lib_path, el)
        if os.path.isdir(p) and not el.startswith('.'):
            nr += 1
            categories.append((el, el, '', nr))
    categories.insert(0, ('.', '.', '', 0))
    # print(categories)
    if len(categories) > 1:
        return categories
        bpy.context.scene['asset_manager']['cat'] = 0
        return [('empty', '', '', 0), ]

# Drop Down Menu
class SimpleAssetManager(bpy.types.PropertyGroup):
    cat: EnumProperty(
        description="Select a Category",

It looks like you are not keeping a reference to the returned string.
Save them somewhere, e.g. in a global variable, before returning them from the categories() function.

See the warning here: https://docs.blender.org/api/master/bpy.props.html#bpy.props.EnumProperty

There is a known bug with using a callback, Python must keep a reference to the strings returned or Blender will misbehave or even crash.

1 Like

Thank you, did exactly that a moment ago. I thought it was “dirty fix” :).