Feedback

What's your question?

By: Asked

Maya Python: find encoding for printing byte strings

I'm struggling to get (or even set) the encoding used when a byte string is printed in the maya Script Editor.

I have discovered that sys.stdout has been assigned to maya.Output. This class doesn't have an encoding attribute.

From the tests I've done, I have found that maya 2011 is using cp1252 (which is my locale encoding) et maya 2009, ascii. Unfortunately, all the tests I've done to find the encoding used returns the same information in 2009 or 2011 (cp1252).

Here is a code sample to show my research:

import sys
import maya.cmds as mc 
try: 
    print "Stdout Encoding:", sys.stdout.encoding 
except AttributeError: 
    print "No stdout encoding" 
print "Default Encoding:", sys.getdefaultencoding() 
print "Filesystem Encoding:", sys.getfilesystemencoding() 
import locale 
print "Locale:" 
print locale.getpreferredencoding() 
print locale.getlocale() 
print locale.getdefaultlocale() 
print "Maya Locale:", mc.about(codeset=True) 
print "Non-ascii char conversion:" 
uni = u"\xe9"  # "é" character 
DEFAULT_ENCODING = "cp1252" 
try: 
    encoding = sys.stdout.encoding or DEFAULT_ENCODING 
except AttributeError: 
    encoding = DEFAULT_ENCODING 
print "encoding:", encoding 
bstr = unicode(uni).encode(encoding, "replace") 
print "repr:", repr(bstr) 
print "unicode:", uni 
try: 
    print "byte string:", bstr 
except UnicodeDecodeError, err: 
    print err

In Maya 2009 I have:

Stdout Encoding: No stdout encoding 
Default Encoding: ascii 
Filesystem Encoding: mbcs 
Locale: 
cp1252 
('fr_FR', 'cp1252') 
('fr_FR', 'cp1252')
Maya Locale: 1252 
Non-ascii char conversion: 
encoding: cp1252 
repr: '\xe9' 
unicode: é 
byte string:  'ascii' codec can't decode byte 0xe9 in position 0: 
ordinal not in range(128) 

In Maya2011:

Stdout Encoding: No stdout encoding 
Default Encoding: ascii 
Filesystem Encoding: mbcs 
Locale: 
cp1252 
('fr_FR', 'cp1252') 
('fr_FR', 'cp1252') 
Maya Locale: 1252
Non-ascii char conversion: 
encoding: cp1252 
repr: '\xe9' 
unicode: é 
byte string: é

As you can see last line of the two outputs, Maya 2011 looks to use cp1252 for encoding whereas Maya 2009 use ascii. - Why Maya 2009 and Maya 2011 behave differently? - How can I get the encoding used when printing? I.e. maya.Output encoding? - Is it possible to set the encoding? (I'd rather use utf8)

(I'm using maya2009 x64 SP1 and maya2011 x64 hotfix3) Many thanks!

  • 0

bfloch

The changes should be related to Python 2.5 in Maya 2008/2009 vs. Python 2.6 in Maya 2010/2011.

I haven't tried myself but in python 2.6 there is this:

The encoding used for standard input, output, and standard error can be specified by setting the PYTHONIOENCODING environment variable before running the interpreter.

http://docs.python.org/whatsnew/2.6.html#interpreter-changes

NN comments
or Cancel