0

I've got a python file that I'm loading as a script into the maya script editor. The python file is currently encoded as UTF-8.

I have the need to use the ↑ and ↓ characters (or any other arrow substitutes within Unicode, such as ➘ or ➚, I just want to convey up and down). I'm using the characters as the label of a button. Here's the script:

import Maya.cmds as cmds

def initInterface():
    cmds.window("mywin")
    cmds.rowColumnLayout("my_rcl", nc=1)
    cmds.button(label=u'\↑')
    cmds.button(label=u'\↓')
    cmds.showWindow("mywin")

initInterface()

The script is saved as myPythonScript.py and is then loaded into the Maya script editor using the load script button.

On execution, I get a UI window and buttons as expected, but the labels for the buttons are now "?" (question marks). I can't seem to get Maya to display the arrows.

To solve this, I've tried a couple of in-code things. Here are a few of my attempts:

# Attempt 1
upArrow = u'\↑'
upArrowEncoded = upArrow.encode("utf-8")
cmds.button(label=upArrowEncoded)
# Result: "?"

# Attempt 2
upArrow = u'\U+2B06'
cmds.button(label=upArrow)
# Result: "?B06"

# Attempt 3
upArrow = u'\U+2B06'
upArrowEncoded = upArrow.encode("utf-8")
cmds.button(label=upArrowEncoded)
# Result: "?"

To be honest (and is likely to be apparent from my code snippets) I've never experimented with text encoding and know next to nothing about it. I'm not sure if I need to change the encoding of my .py file, or encode the string with UTF-16 or something. This is way outside of my area of expertise and I'm having a hard time finding resources to help me understand text and string encoding.

I did check out this: Unicode Within Maya

And this: Convert a Unicode String to a String in Python Containing Extra Symbols

But I wasn't able to understand a lot of what I read, and I'm not sure if they relate to this issue or not.

I'm the type of person who doesn't enjoy using code I don't understand (how do people even document that?), so I'm here to ask for links to learning resources and for general advice on the subject, moreso than for a code snippet that does what I want. If it turns out this is not possible, I can use image buttons instead. But they are less efficient and time consuming to produce for each special character I may use.

Thank you for reading through this, and thank you in advance to anyone who can point me in the right direction here. Cheers!

Design Runner
  • 47
  • 1
  • 8

1 Answers1

0

As far as I can tell, the native MayaUi uses/has access to the Code Page 1252 Windows Latin 1 (ANSI) character set (at least on Windows...) as mentioned here, and after some noodling these *all appear to work as advertised.

I'd be curious to see an answer that explained how to change that and access what OP is looking for, but as an alternative to anyone that really truly wants more special characters, may I suggest learning PySide / Qt for building your UI.

Caveats

  1. A lot more boilerplate and setup when it comes to making 'something simple'
  2. Several mayaControls do not have direct Qt implementations (gradientControlNoAttr being a recent discovery, and case in point)
  3. Example is written under the assumption that user has installed and uses Qt.py

Lets dive right in:

import maya.cmds as cmds
import maya.OpenMayaUI as omui
from Qt import QtCore, QtGui
from Qt.QtWidgets import *
from shiboken import wrapInstance

def maya_main_window():
    main_window_ptr = omui.MQtUtil.mainWindow()
    return wrapInstance(long(main_window_ptr), QWidget)

class TestUi(QDialog):
    def __init__(self, parent=maya_main_window()):
        super(TestUi, self).__init__(parent)
        self.setAttribute(QtCore.Qt.WA_DeleteOnClose)

    def create(self):
        self.setWindowTitle("TestUi : Unicode")
        self.setWindowFlags(QtCore.Qt.Tool)

        self.create_controls()
        self.create_layout()
        self.create_connections()

    def create_controls(self):
        """
        Create the widgets for the dialog.
        """
        # using "Python source code" unicode values
        # ie: https://www.fileformat.info/info/unicode/char/2191/index.htm
        self.up_button = QPushButton(u'\u2191')
        self.down_button = QPushButton(u'\u2193')
        self.left_button = QPushButton(u'\u2190')
        self.right_button = QPushButton(u'\u2192')

    def create_layout(self):
        """
        Create the layouts & add widgets
        """
        main_layout = QVBoxLayout()
        main_layout.setContentsMargins(6, 6, 6, 6)

        main_layout.addWidget(self.up_button)
        main_layout.addWidget(self.down_button)
        main_layout.addWidget(self.left_button)
        main_layout.addWidget(self.right_button)

        main_layout.addStretch()
        self.setLayout(main_layout)


    def create_connections(self):
        """
        Create the signal/slot connections
        """
        self.up_button.clicked.connect(self.on_button_pressed)
        self.down_button.clicked.connect(self.on_button_pressed)
        self.left_button.clicked.connect(self.on_button_pressed)
        self.right_button.clicked.connect(self.on_button_pressed)

    def on_button_pressed(self):
        print "Button Pressed"

def LaunchUI():
    if __name__ == "__main__":
        # Development workaround for PySide winEvent error (Maya 2014)
        # Make sure the UI is deleted before recreating
        try:
            test_ui.deleteLater()
            test_ui.close()
        except:
            pass
         # Create minimal UI object
        test_ui = TestUi()
        # Delete the UI if errors occur to avoid causing winEvent
        # and event errors (in Maya 2014)
        try:
            test_ui.create()
            test_ui.show()
        except:
            test_ui.deleteLater()
            traceback.print_exc()
LaunchUI()

There's an awful lot to unpack there for not a terribly huge payoff, but the relevant piece of information is living under "create_controls".

demonstration of unicode in Maya UI via PySide