Search code examples
pythonuser-interfacemayamel

Changing background color (bgc) of windows in Maya?


I worked somewhere in the past where since we had multiple sessions of Maya open, the background color could be randomly changed so when you switched from a session quickly it was easy to sort out which window belonged to what Maya session.

And so far, I can change the bgc of the main UI by using:

window -e bgc 0.5 0.5 0.5 $gMainWindow;

After searching for other global variables, I found $AllWindows, $CommandWindow, among others since the docs state that 'bgc' is a windows only flag. I can not get any of the colors to change on any window besides the $gCommandWindow, which popped up and I do not recall seeing it before.

I'm hoping to at least change the Script Editor window in addition to MainWindow if anyone knows whether it's possible or not? It is not mission critical, but now I'm interested in seeing if it can be done.

Thanks!


Solution

  • Since Maya's interface is using Qt, you can use the power of PySide to tweak any widget you want. Usually the only tricky part is actually finding the proper widget to modify.

    Here's how you can tweak the Script Editor to give it a yellow border:

    import shiboken2
    from maya import cmds
    from maya import OpenMayaUI
    from PySide2 import QtWidgets
    
    
    panels = cmds.getPanel(scriptType="scriptEditorPanel")  # Get all script editor panel names.
    
    if panels:  # Make sure one actually exists!
        script_editor_ptr = OpenMayaUI.MQtUtil.findControl(panels[0])  # Grab its pointer with its internal name.
        script_editor = shiboken2.wrapInstance(long(script_editor_ptr), QtWidgets.QWidget)  # Convert pointer to a QtWidgets.QWidget
        editor_win = script_editor.parent().parent().parent().parent()  # Not very pretty but found that this was the best object to color with. Needed to traverse up its parents.
        editor_win.setObjectName("scriptEditorFramePanel")  # This object originally had no internal name, so let's set one.
        editor_win.setStyleSheet("#scriptEditorFramePanel {border: 3px solid rgb(150, 150, 45);}")  # Set its styleSheet with its internal name so that it doesn't effect any of its children.
    

    OpenMayaUI.MQtUtil gives you the awesome ability to find any control by name, so as long as you know the name of the widget you want to modify, you can find it (tough part is finding it sometimes!). In this case I had to traverse up a few parents to find one that worked best to outline the whole window. You can fool around with this and color, let's say, only the text area. And since this is PySide's style sheets you can do whatever your heart desires, like effect the background color, the thickness of the outline, and so on.

    Since we're only effecting the style sheet this also won't save with the preferences and will revert to what it was on a new session.

    Hope that helps.

    Example