Book Image

Practical Maya Programming with Python

By : Robert Galanakis
Book Image

Practical Maya Programming with Python

By: Robert Galanakis

Overview of this book

Table of Contents (17 chapters)
Practical Maya Programming with Python
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Unicode strings


I can think of nothing else that has caused programmers more confusion than Unicode strings. I will not even attempt to explain the issues surrounding Unicode, but I feel it's important to bring up so when you see something like the following, you will be prepared.

>>> import pymel.core as pmc
>>> xform = pmc.polyCube()[0]
>>> myname = 'cubexform'
>>> xform.rename(myname)
>>> xform.name()
u'cubexform'

Unicode strings in Python 2 are prefixed with a u character. So how come even though we named our node using a "regular" (byte) string with no prefix, we got back a Unicode string from the xform.name() method?

Well, "regular" strings in Python 2 (the str type) support ASCII characters only. ASCII is able to represent a very limited number of characters, but all the characters of the world can be represented by the Unicode system. So if you are creating a program that needs to be localized or support more than basic English, your strings need to be Unicode. Autodesk Maya needs to be localized, of course, so all the user-facing strings are Unicode.

Fortunately the two types share the same base class (basestring) and compare equal, so cause minimal inconvenience for us most of the time.

>>> myname == xform.name()
True
>>> type(myname), type(xform.name())
(<type 'str'>, <type 'unicode'>)
>>> str.__mro__
(<type 'str'>, <type 'basestring'>, <type 'object'>)
>>> unicode.__mro__
(<type 'unicode'>, <type 'basestring'>, <type 'object'>)

Most people programming in Maya can get by without understanding Unicode or localization. If your needs get more complex and you need to build localizable tools or applications (for example, to supply translated tools to another studio), be prepared to do a significant amount more work and testing. These topics are well outside of the scope of this book, though, so we will just leave our discussion of Unicode here.