一半君的总结纸

听话只听一半君

#115 如何放大Script Editor的字体,并保存为默认设置?

maya script editor的字体大小似乎没有地方保存设置,虽然你可以滚轮放大缩小,但是每开个新tab都要滚好麻烦,而且如果重启maya,而你又有很多tab的话,他们又都恢复很小的字体了,看的眼睛好累

2016开始有了dpi设置,假如你是hidpi的笔记本屏幕,你可以在那把整个软件里的所有字体放大,但是如果你只想放大script editor里的字体,或者全局放大的程度不合意,或是没有权限手动修改/usr/autodesk/maya2015-x64/resources/MayaStrings(比如是2015或以前版本的Maya,在公司没权限改),可以像下面这样乱搞

试了几个方法,各有优缺点,但首先第一步都是要找到script editor上半部分对应的QTextEdit的instance,下半部分同理

from PyQt4 import QtGui,QtCore
from maya import cmds
import maya.OpenMayaUI as apiUI
import sip

ptr = apiUI.MQtUtil.mainWindow()
mwin=sip.wrapinstance(long(ptr), QtCore.QObject)

cmdReporters = cmds.lsUI(type='cmdScrollFieldReporter')
cmdReporter = mwin.findChild(QtGui.QTextEdit, cmdReporters[0])

# 同理 下半截是cmdScrollFieldExecuter,当然如果有多个tab下面返回的是个list
cmdExecuters = cmds.lsUI(type='cmdScrollFieldExecuter')
  • 首先试试zoomIn
    # 这里不知道为何先要zoomIn到10,不然会变超小
    cmdReporter.zoomIn(10)
    cmdReporter.zoomIn(2)
    

    这样虽然大了,但是发现不知为何,ctrl+鼠标滚轮缩放就失效了

  • 其次试试设置字体
    font=cmdReporter.font()
    # default is 12
    font.setPixelSize(16)
    cmdReporter.setFont(font)
    

    一样 虽然大了,但是缩放没了

  • 最后试试stylesheet
    # 把 stylesheet 只用到script editor 上也会让滚轮不能用了
    ptr = apiUI.MQtUtil.findControl('scriptEditorPanel1Window')
    win = sip.wrapinstance(long(ptr), QtCore.QObject)
    
    styleSheet = '''
    QTextEdit {
      font-family: "Courier New";
      font: normal %spx;
    }
    ''' % '16'
    
    win.setStyleSheet(styleSheet)
    

    所以干脆设到整个maya上,这样是能缩放,但是这样就会不止script editor里的QTextEditor里的字变大,其他所有凡是出现QTextEdit的地方通通都变大了

    app = QtGui.QApplication.instance()
    app.setStyleSheet(styleSheet)
    

    改进一下,我只想script editor里面的QTextEdit变大,所以我在上面的script editor对应的QWidget上加个property,然后在stylesheet里说,只有这个QWidget下层的所有QTextEdit,字才变大

    win.setProperty('maya_ui','scriptEditor')
    
    styleSheet = '''
    QWidget[maya_ui="scriptEditor"] QTextEdit {
      font-family: "Courier New";
      font: normal %spx;
    }
    ''' % '16'
    
    app.setStyleSheet(styleSheet)
    

    这样就达到了既只改变了script editor里面的QTextEdit的字体大小,又不影响其他部分的QTextEdit,缩放也能正常运作的目的

如果想一启动maya就有上面的效果,你可以把他们放在userSetup.py里,但是有个问题,才启动的时候在你手动去点script editor的按钮之前,script editor window应该是不存在的,你要如何给他加上自定义的property呢,有个办法是把maya ui上右下角的开script editor的按钮换掉,像Mack Stone的cmdReporterHighlighter.py那样,找到按钮ui的名字,然后把他的-c换掉,例子见这里

你可以在userSetup.py里放上

import maya.utils as mu
import script_editor_font as SE
mu.executeDeferred(SE.launchFromCmdWndIcon)

然后

from PyQt4 import QtGui, QtCore
from maya import cmds, mel
import maya.OpenMayaUI as apiUI
import sip


def launchFromCmdWndIcon():
    '''launch from maya command line script editor icon.'''
    def cmdWnd(arg=None):
        cmds.ScriptEditor()
        set_script_editor_font()

    # get command line formLayout
    gCommandLineForm = mel.eval('$tempVar = $gCommandLineForm')
    commandLineForm = cmds.formLayout(gCommandLineForm, q=1, ca=1)[0]
    # get cmdWndIcon button
    cmdWndIcon = cmds.formLayout(commandLineForm, q=1, ca=1)[-1]
    # change the command of the button
    cmds.symbolButton(cmdWndIcon, e=1, c=cmdWnd)

    # change the main manu item command
    menuName = 'wmScriptEditor'
    if cmds.menuItem(menuName, q=1, ex=1):
        cmds.menuItem(menuName, e=1, c=cmdWnd)


def getMayaWindowWidget():
    'another of finding main maya window'
    ptr = apiUI.MQtUtil.mainWindow()
    mwin = sip.wrapinstance(long(ptr), QtCore.QObject)
    return mwin


def set_script_editor_font():
    'set stylesheet on all qtextedit inside script editor window'
    # find script editor qwidget
    ptr = apiUI.MQtUtil.findControl('scriptEditorPanel1Window')
    win = sip.wrapinstance(long(ptr), QtCore.QObject)

    # add a custom property to it
    win.setProperty('maya_ui', 'scriptEditor')

    # apply stylesheet to any qtextedit inside script editor qwidget
    # you can use any cool fonts here
    styleSheet = '''
    QWidget[maya_ui="scriptEditor"] QTextEdit {
      font-family: "Courier New";
      font: normal %spx;
    }
    ''' % '16'

    app = QtGui.QApplication.instance()
    app.setStyleSheet(styleSheet)

ps:
此外你可以做个界面,让用户可以自己选字体大小,然后用optionvar存在userPrefs.mel里

查看当前使用的字体们

about -fontInfo

script_editor_font

Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: