Py学习  »  Python

python文件、maya脚本编辑器和maya ui之间的文本编码(python 2.7、maya 2015、windows 7)

Design Runner • 4 年前 • 702 次点击  

我有一个python文件,作为脚本加载到maya脚本编辑器中。python文件当前编码为utf-8。

我需要使用and字符(或unicode中的任何其他箭头替换,例如or,我只想上下传递)。我用这些字符作为按钮的标签。剧本如下:

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()

该脚本保存为mypythonscript.py,然后使用“加载脚本”按钮加载到maya脚本编辑器中。

在执行时,我得到了一个ui窗口和按钮,但按钮的标签现在是“?”(问号)。我好像不能让玛雅显示箭头。

为了解决这个问题,我尝试了一些代码内的东西。以下是我的一些尝试:

# 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: "?"

老实说(从我的代码片段中很明显),我从来没有尝试过文本编码,对它一无所知。我不确定是否需要更改.py文件的编码,或者用utf-16或其他东西对字符串进行编码。这远远超出了我的专业领域,我很难找到帮助我理解文本和字符串编码的资源。

我查过这个: Unicode Within Maya

而这: Convert a Unicode String to a String in Python Containing Extra Symbols

但是我不能理解我读到的很多东西,我不确定它们是否与这个问题有关。

我是那种不喜欢使用我不懂的代码的人(人们如何记录这些代码?),所以我来这里是为了获得学习资源的链接和关于这个主题的一般建议,而不是为了得到一个我想要的代码片段。如果这是不可能的,我可以用图像按钮代替。但是,对于我可能使用的每个特殊字符,它们的制作效率和时间都较低。

谢谢你通读这篇文章,并提前感谢任何能给我指明正确方向的人。干杯!

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/40249
 
702 次点击  
文章 [ 1 ]  |  最新文章 4 年前
vonDoomhammer
Reply   •   1 楼
vonDoomhammer    5 年前

据我所知,土生土长的玛雅人使用 Code Page 1252 Windows Latin 1 (ANSI) 如前所述的字符集(至少在Windows上…) here ,而且在一些拉面之后,这些*看起来都像广告上说的那样工作。

我很想看到一个解释如何改变它和访问op所寻找的东西的答案,但是作为一个真正想要更多特殊字符的人的替代方案,我建议您学习pyside/qt来构建您的ui。

告诫

  1. 在制作“简单的东西”时,需要更多的样板和设置
  2. 一些mayaControl没有直接的qt实现( gradientControlNoAttr 是一个新的发现,并且是一个很好的例子)
  3. 示例是在假定用户已安装并使用 Qt.py

让我们直接进入:

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()

有很多东西要打开,但却没有得到巨大的回报,但相关的信息却生活在“创建控制”之下。

demonstration of unicode in Maya UI via PySide