'utf8' codec can't decode byte 0xdc in position 0: invalid continuation byte

Hello,
I have an operator looking like this:

<b>class </b><b>QuickRemoveOperator</b>(bpy.types.Operator):    
<i>"""Removes all scenes created via this add-on during this blender session"""
</i>    bl_label = "Quick remove"
    bl_idname = 'scene.wireframe_and_clay_quick_remove'

    <b>def </b>__init__(self):
        self.start = time.time()

    <b>def </b><b>execute</b>(self, context):
        <b>for </b>scene <b>in </b>list(w_var.created_scenes):
            <b>if </b>scene.name <b>in </b>bpy.data.scenes:  # <i>TODO: This line creates decoding error sometimes.
</i>                bpy.data.scenes.remove(scene)
                w_var.created_scenes.remove(scene)

        self.report({'INFO'}, "Remove done in {} seconds!".format(round(time.time() - self.start, 2)))
        <b>return </b>{'FINISHED'}

Usually it works just as it’s supposed to, but seemingly random it fails and reports an error on the line where the TODO tag is seen above. The error says: “‘utf8’ codec can’t decode byte 0xdc in position 0: invalid continuation byte”. I don’t know why this happens, I can’t reproduce the error. Sometimes (very rare) it happens, and when it does I can’t get past it no matter how many times I try to use the operator. Maybe I should do that check in another way like:

if scene in list(bpy.data.scenes):

Any suggestions? Is this a common error here?

Why are you doing “if scene.name in bpy.data.scenes”? You’re essentially comparing a string to a “bpy_types.Scene” type.
I think what you mean is either “if scene in bpy.data.scenes” or “if bpy.data.scenes.find(scene.name) != -1”

Where does w_var come from? If it is read from a file, it may contain non-utf8 characters.

Sorry I wan’t clear about that, it’s another file containing various variables I use across multiple files. This variable in particular is assigned to a python set containing blender scene objects. This operator shall remove all the scenes stored in this variable from the blender session, and then also from this variable.

I guess a scene object might contain non-utf8 characters sometimes, but again this operator works 99% of the time I use it. Does blender encode the scene name differently depending on something in the scene? Should I maybe do this check in another way? If I encounter this error once again, I will try to log some and find out more about the problem. Last time it occurred I left the scene open and put the computer to sleep as I went to school, but when I came home and woke it up blender crashed…

You should definitely leave out the call to list() and simply do:

“Scene” in bpy.data.scenes

Do you edit your script file in an external text editor? If so, make sure it saves the file without BOM.

The first thing I tried to do was “scene in bpy.data.scenes” but it raises an error.

TypeError: bpy_prop_collection.__contains__: expected a string or a tuple of strings

That’s why I suggested to first converted it to a list, because that works. The collection class seems to be a subclass of pythons dict class, with in this case the scene names mapped to the scene objects. However, I think you probably know better about this than I do so I might have misunderstood you.

I do edit the script in an external text editor (PyCharm), what problems can saving the file with BOM cause?

What does your “scene” contain then?!

BOMs can cause encoding issues if the target application expects an UTF-8 encoded file without BOM (JSON for instance is always assumed to be UTF-8 without the signature).

The “scene” variable is assigned to a blender scene object, its type is “bpy.types.Scene”. (what I previously meant with: )

This variable in particular is assigned to a python set containing blender scene objects.
Thus it’s not a string.

Thanks for the explanation about BOM. BTW, if blender crashes leaving this error message in the console: “Error: EXCEPTION_ACCESS_VIOLATION”, what does that exactly mean? Could it be a problem with my add-on or strictly a blender bug?

Ah, then do:

scene.name in bpy.data.scenes

It might not be safe if linked scenes (is that possible?) got identical names.
http://www.blender.org/api/blender_python_api_2_75_release/info_gotcha.html#library-collisions
http://www.blender.org/api/blender_python_api_2_75_release/info_gotcha.html#data-names

Such crashes are easily caused by own scripts. You should try if it happens without your addon, then you’ll know I guess. What exactly did you do before the crash (or rather the addon)? Could be some use-after-free, often happens with bmesh module.

http://www.blender.org/api/blender_python_api_2_75_release/info_gotcha.html#help-my-script-crashes-blender
http://www.blender.org/api/blender_python_api_2_75_release/info_gotcha.html#edit-mode-memory-access
http://www.blender.org/api/blender_python_api_2_75_release/info_gotcha.html#removing-data

scene.name in bpy.data.scenes
is what I currently do, what I do when I get the error, read the first post… :wink: I don’ t think I have ever had two scenes with identical names, though I will check those links.

I’ve got an operator that loads a config file for the add-on, after the 5th time I use this operator in a blender scene it crashes. Same with another operator that saves a config file. As it always is the 5th time, it feels like something is stacking… Currently there is only one specific .blend file that I can reproduce this crash on. It has also happened that blender crashes right after the main operator has finished, but it’s pretty rare. I don’t use the bmesh module but I’ll look into those links.

Strange, what is the exact scene name that causes the error?

What is the encoding of the config files?

The scene name that has caused the error had been ‘wireframe’, but it’s pretty irrelevant in this case as I almost always use that scene name and 99 % of the time it works to remove. Feels like sometimes, blender does something to the name (encoding wise I guess) and then I get this error when I use my remove operator. However, as I don’t know why this occur I can’t reproduce the error and debug further… :frowning:

The config files are created using the configparser module (https://docs.python.org/3/library/configparser.html), they use UTF-8 encoding.