Decoding issues for letters with diacritics


I’m trying to store a dictionary with French letters in it. My line is such:


When I start the simulation in Blender2.59, I get the error:

SyntaxError: (unicode error) 'utf8' codec can't decode byte 0xc1 in position 0: invalid start byte

Nothing on the script prior to this line is run. It appears that Blender is failing to read the text file, thus not running anything on the script at all.

I’ve found that Blender stores its text files with the encoding CP1252, not UTF8. Could this error be caused by the BGE opening its own internal text files with the wrong encoding, or am I missing an easy Python fix?

Any help/tips appreciated! Thanks!


It’s not obvious in the code snippet so I’ll paste the letter again here: Á (notice the diacritic). Decimal value 193.

Going off the assumption that the problem is with Blender reading internal texts, I’ve created a UTF-8 encoded text file with just ‘Á’ inside it. Blender was successfully able to read it, assign it to a GameLogic variable, and print it.
The problem now is getting it to display on a font map. All characters I’ve tried with accents show up as ‘Ã@’ on screen. The font map does contain the correct characters (Á + others) on it, though I’ll try different ones when I can find them.

blender uses ASCII by default, chars like yours require UTF encoded files or python will fail.

Blender has certain problems with unicode, could be a bug, or “not yet a feature”

maybe read from utf file, then convert to local codepage / ascii? Dunno if Á is a standard letter on french systems using 1 byte…

Thanks for the reply. I hope it’s not ASCII. It doesn’t have anything like the amount of symbols needed ): Do you know if there is a way to store Á in a CP1252 encoding?

well, i did a simple test file: (ANSI encoding)


placed in blender addon dir, then tried to import

SyntaxError: Non-UTF-8 code starting with ‘\xc1’ in file C:\Program Files\Blender Foundation\Blender\2.63\scripts\addons on line 1, but no encoding declared;

Then saved it as UTF-8, imports fine

Saved as ANSI again, but using:


works as well!

and finally:

ANSI, using:

# -*- coding: latin-1

works fine!!!

there was/is and addon for blender-2.6x (dont know if already for .63a…)
to insert special utf-encoding chars for the text-object-string.
Maybe you check the python-code of it to use it for the setting in your game-scripting?

Curious, has anyone tried r’raw’, b’bytes’, or u’unicode’ strings yet?

u’unicode’ has been removed for Python 3x. Instead, all strings are stored as unicode when defined like “string”. You can store ‘Á’ as bytes, but you can’t assign that to a Text object.

@CoDEmanX: It’s not Python I’m having issues with. I can manipulate and store the strings, but displaying them in Blender has been difficult. I haven’t yet figured out what format the Text objects are. The error messages tell you what you can’t do, rather than what it’s expecting.

@test-dr: Do you know what it is called?

I have made some progress solving this! You can import a text file with UTF-8 encoding into a Blender Text object using the ‘Paste File’ feature. The default Blender font doesn’t have the right characters so you’ll need to change it to something else to see your efforts in the BGE. So far I haven’t been successful assigning text to the Text object using Python, but I’ll keep trying.[‘Text’].data.body = “ÀÁ”
works fine in regular blender

if i paste file to text object having “ÀÁ”, it shows “??”

but if i type a letter, then paste “ÀÁ” into T-panel (insert text operator), works fine

Thanks for all the help! I must’ve been doing something wrong before, because the encoding script code you posted makes it all work now.

For anyone who used the search feature to find this thread, here are the steps for getting it to work in Blender:

  1. Create a ‘Text’ object. This is the kind you can type into, NOT a font map.

  2. Create a script to store/handle/assign the special letters, such as:

# -*- coding: latin-1
cont = GameLogic.getCurrentController()
own = cont.owner
own.text = 'ÀàÂâÆæ'

This script needs to have the encoding set in the top line, like as shown.

    Set the resolution on the Text object higher using a script I saw by dfelinto:

# -*- coding: latin-1
cont = GameLogic.getCurrentController()
own = cont.owner

def text_resolution(object):
	object.resolution = 3
	scaling = object.localScale[2]
	scaling *= 0.65 #value eyeballed per font used
	object.localScale = (scaling, scaling, scaling)

own.text = 'ÀàÂâÆæ'