Search code examples
pythonmayapyside2

maya can't recognize unicode if it's in PySide2?


So there is a lines of code that if I run it in 'script editor' it whould work fine, but if it get the same text from PySide2 it wouldn't work.

lets say you put 'a#↑' into your input. it would print 'a' and '#' but not '↑' and instead it print 'Not recognized'. this only happen in Maya and not windows. I'm confused.

# -*- coding: utf-8 -*-
from PySide2 import QtCore, QtGui, QtWidgets


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(400, 300)
        self.pushButton = QtWidgets.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(165, 125, 75, 23))
        self.pushButton.setObjectName("pushButton")
        self.plainTextEdit = QtWidgets.QPlainTextEdit(Form)
        self.plainTextEdit.setGeometry(QtCore.QRect(30, 10, 341, 96))
        self.plainTextEdit.setInputMethodHints(QtCore.Qt.ImhNone)
        self.plainTextEdit.setObjectName("plainTextEdit")

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

        self.pushButton.clicked.connect(self.printIt)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.pushButton.setText(_translate("Form", "Test"))

    def printIt(self):
        text = self.plainTextEdit.toPlainText()
        for i in text:
            if i == "a":
                print ('a is printed')
            elif i == "#":
                print ('# is printed')
            elif i == "↑":
                print ('↑ is printed')
            else:
                print ('not recognized')


if __name__ == "__main__":
    import sys
    Form = QtWidgets.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()

but in this form it works fine:

text = 'a#↑'
for i in text:
    if i == "a":
        print ('a is printed')
    elif i == "#":
        print ('# is printed')
    elif i == "↑":
        print ('↑ is printed')
    else:
        print ('not recognized')

Solution

  • So turns out this is the "script editor" problem. Thanks to @zewt in Autodesk forum, he introduce a solution for this. If you import a file that contain the code instead of just running the code directly in script editor, then it works.

    here is the link to his solution