Display Unicode characters from a text file



  • On 29/12/2015 at 13:35, xxxxxxxx wrote:

    My Plugin will be used in more programs than C4D so I'm loading my strings in from a .txt file.
    When the .txt file is saved with endoding = 'ANSI' it works fine for any languages that use characters in that set.

    However, if someone wants to display other characters, say Japanese, then the this won't work.
    I tried saving the text file with 'UTF-8' and 'Unicode', but I can't get them to display properly on a button. Here is a line for a button with the text stored in a dictionary named 'gl_str_dict':

    self.AddButton(LOAD_DATA, flags=c4d.BFH_LEFT  | c4d.BFH_SCALEFIT, initw=self.four_column_button_width, inith = self.button_height, name=gl_str_dict["BUTTON_OPEN_FILE"])               
    

    My text file is format:

    BUTTON_OPEN_FILE:Open File
    

    My text file is read in like this:

    with open(file_path) as myfile:
        raw_list = "".join(line.replace('/', os.sep) for line in myfile).split('\n')
      
    gl_str_dict = {x.split(':')[0]: x.split(':')[1] for x in raw_list}
    

    How do you make this work with unicode characters?
    Can you display Japanese and other unicode characters?

    Thanks



  • On 30/12/2015 at 08:49, xxxxxxxx wrote:

    Hi,

    This isn't related to how Cinema 4D interprets unicode strings as you read them using only Python functions. See Python string class decode() and encode() methods.
    For example run the following lines in the console:

    [OK] utf8_string = u"Åland (Finland)".encode('utf8')
    [OK] utf8_string
    '\xc3\x83\xc2\x85land (Finland)'
    [OK] print utf8_string
    Ã…land (Finland)
    


  • On 31/12/2015 at 02:10, xxxxxxxx wrote:

    Yannick,

    Okay, I saved the file with 'utf-8' encoding and this displayed correctly on a button in C4D: 'Ã…land'

    However, the Japanese characters in the same file don't; they display like this: '\u00E3\u0081\u00AB'
    Is this because I need to have a C4D font selected that can display Japanese characters?



  • On 31/12/2015 at 04:53, xxxxxxxx wrote:

    Originally posted by xxxxxxxx

    However, the Japanese characters in the same file don't; they display like this: '\u00E3\u0081\u00AB'
    Is this because I need to have a C4D font selected that can display Japanese characters?

    Have you tried to actually output the string with the Japanese character with print()?
    But yes, you should definitely select a special font for the Cinema UI in the preferences. For example Meiryo or Meiryo UI.
    I don't know how this should be setup to work but as this isn't SDK related you should contact the MAXON support or search/post your question in user forums.



  • On 31/12/2015 at 16:34, xxxxxxxx wrote:

    Yes, I loaded the file, as shown above, with the following Japanese word for hello: 'こんにちは'
    I then printed it to the console in C4D and it printed correctly with Japanese characters.
    It also displayed on a button correctly with the font 'Meiryo'.

    Thanks


Log in to reply