[Solved] Can someone help me? Unicode (UTF8) to an external Editor (Notepad++)

I’ve been trying to wrap my head around this for 6 days now and have searched the net without finding a solution. So I’m throwing it out to you Blender Script Writing Experts.

I have a model that everything has been modelled entirely in a foreign language, (which of course is in UTF8, I think). The Blender Python console displays the data perfectly with international fonts turned on, however, if I try to send the data to a file that I can open in Notepad++, I get the age old error,


# my current code. I'm not a programmer but this works for me what the data fall in the cp1252 character set. 
# ------------------

import bpy
import os
import codecs
user = getpass.getuser() 
os.chdir("C:\\users\\" + str(user) + "[\\Desktop\\](file://\\Desktop\\)")
myFile = open('mesh.txt','w')
# or 
# myFile = open('mesh.txt','wb')
g = ''
for mats in bpy.data.materials:
      g = str(mats.name)
      print(g)
      myFile.write(g + '
')
myFile.close()

# ---------------------------------------------

This is the Console Output from the code above.


      
Material
9
材質1Traceback (most recent call last):
  File "<blender_console>", line 4, in <module>
  File "C:\Program Files (x86)\Steam\steamapps\common\Blender\2.78\python\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-1: character maps to <undefined>


I’ve tried other work-arounds that I’ve found on the net (like trying to write to a binary file, various other python encoding and decoding codes to work with UTF8 and UTF-8-sig input, etc), but python still errs out every time I run a script or copy or paste the code into the python console and manually run it. I get the error and nothing is produced in the outfile.

I do currently have a work around that I’m using (which is to manual run code and send output to the console, then copy and paste it into Notepad,) but I would really like to just write the UTF8 characters straight to a file from Blender that I can open in Notepad++. (especially when the output is over 255 lines deep and I lose the first bit of data output due to the window limit and have to throw character line delimiters into code in order to keep the data from scrolling off the screen.)

Any suggestions? Please!

so you are running blender INSIDE of Steam ???

and the 32 bit version also

why not just use the Microsoft Windows exe

it looks like your problem is related to using Steam and a steam problem of missing or different things

Thanks for the reply John, but alas… Same result


#~ PYTHON INTERACTIVE CONSOLE 3.5.2 (default, Dec  1 2016, 20:58:16) [MSC v.1800 64 bit (AMD64)]
#~ 
#~ Command History:     Up/Down Arrow
#~ Cursor:              Left/Right Home/End
#~ Remove:              Backspace/Delete
#~ Execute:             Enter
#~ Autocomplete:        Ctrl-Space
#~ Zoom:                Ctrl +/-, Ctrl-Wheel
#~ Builtin Modules:     bpy, bpy.data, bpy.ops, bpy.props, bpy.types, bpy.context, bpy.utils, bgl, blf, mathutils
#~ Convenience Imports: from mathutils import *; from math import *
#~ Convenience Variables: C = bpy.context, D = bpy.data
#~ 
import getpass
import bpy
import os
# get the current user and and send the created text file to the User's Desktop
user = getpass.getuser() 
os.chdir("C:\\users\\" + str(user) + "[\\Desktop\\](file://\\Desktop\\)")
myFile = open('mesh.txt','w')
g = ''
for mats in bpy.data.materials:
    g = str(mats.name)
     myFile.write(g + '
')
myfile.close()


# Output

#~ 9
#~ 
#! Traceback (most recent call last):
#!   File "<blender_console>", line 5, in <module>
#!   File "D:\Brad\PortableApps\blender-2.78c-windows64\2.78\python\lib\encodings\cp1252.py", line 19, in encode
#!     return codecs.charmap_encode(input,self.errors,encoding_table)[0]
#! UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-1: character maps to <undefined>
#!


Hmmm, I’m declaring g as a string with the g=’’ statement, hmmm I wonder

Nope, changing the statement to

for mats in bpy.data.materials:
h = mats.name
myFile.write(h + ’
')

still produces the cp1252 error

I GOT IT!!!



import sys
import getpass
import bpy
import os
user = getpass.getuser() 
os.chdir("C:\\users\\" + str(user) + "[\\Desktop\\](file://\\Desktop\\)")
myFile = open('mesh.txt','w', encoding='utf8')
for mats in bpy.data.materials:
     g = mats.name
     print(g, file=myFile)
     myFile.flush()
     
myFile.close()