Fnord
Fnord

Reputation: 5875

Customize Maya's script editor font

Up to Maya 2019 I was using the following script to customize the script editor font.

from PySide2 import QtGui, QtCore, QtWidgets

def set_font(font='Courier New', size=12):
    """
    Sets the style sheet of Maya's script Editor
    """
    
    # Find the script editor widget
    app = QtWidgets.QApplication.instance()
    win = next(w for w in app.topLevelWidgets() if w.objectName()=='MayaWindow')

    # Add a custom property
    win.setProperty('maya_ui', 'scriptEditor')

    # Apply style sheet
    styleSheet = '''
    QWidget[maya_ui="scriptEditor"] QTextEdit {
      font-family: %s;
      font: normal %spx;
    }
    ''' %(font, size)

    app.setStyleSheet(styleSheet)
    

And with this I could change the script editor's font style and size uniformly across all tabs.

# this is my current favorite
set_font(font='Consolas', size=20) 

In Maya 2018 and 2019 this works fine. I have not tested 2020, but in 2022 and 2023 it executes without errors but fails to change the interface as desired.

QUESTION

What has changed since 2019 that would make this script fail. Any tip on how to make this script work would be greatly appreciated. Otherwise I'll post a solution here when I find the issue.

Upvotes: 1

Views: 921

Answers (1)

Morten
Morten

Reputation: 497

I do not have a complete answer, but the following seems to work. One notable change is that QTextEdit appears to have been replaced with QPlainTextEdit, but only replacing that does not fix it.

However, the following accounts for Maya versions before and after 2022, and seems to work.

Tested on macOS 12.4 and Maya 2022.2 where the same solution applies. It may be that a different minor version of Maya 2022 requires the old solution.

import maya.cmds as cmds
from PySide2 import QtGui, QtCore, QtWidgets


def set_font(font='Courier New', size=12):
    """
    Sets the style sheet of Maya's script Editor
    """
    
    app = QtWidgets.QApplication.instance()
    win = None
    
    for w in app.topLevelWidgets():
        if "Script Editor" in w.windowTitle():
            win = w
            break
            
    if win is None:
        return
    
    # for Maya 2022 and above
    if int(cmds.about(version=True)) >= 2022:
        style = '''
            QPlainTextEdit {
                font-family: %s;
                font: normal %dpx;
            }
            ''' % (font, size)
        
        win.setStyleSheet(style)

    # for Maya 2020 and older
    else:
        style = '''
            QWidget[maya_ui="scriptEditor"] QTextEdit {
                font-family: %s;
                font: normal %dpx;
            }
            ''' % (font, size)
        
        win.setProperty('maya_ui', 'scriptEditor')
        app.setStyleSheet(style)
    
    
set_font(font='Consolas', size=20) 

Upvotes: 3

Related Questions