Search code examples
pythonfilenamesmaya

Maya - How to update file name displayed on menu bar after renaming the scene


Using this command:

cmds.file(rename = "newName.mb")

Doesn't display the new name in the menu bar on the top of the window. It still displays the old name but adding an asterisk * to show it has been changed.

The problem is this is confusing for the user. The file will be saved in a different path, but you don't know until you do it.

How can I update the name displayed on top, if rename doesn't?


Solution

  • You can use PySide to achieve this, it is natively shipped with Maya since version 2014. You can customize pretty much everything in Maya using Pyside.

    Here's the code to change the window title:

    import maya.cmds as cmds
    from PySide import QtCore, QtGui
    from maya import OpenMayaUI as omui
    from shiboken import wrapInstance
    
    def getMayaWindow():
        omui.MQtUtil.mainWindow()    
        ptr = omui.MQtUtil.mainWindow()
        return wrapInstance(long(ptr), QtGui.QWidget)
    
    newPath = r"C:\Users\pfruchet\Desktop\NewSceneName.ma"
    
    mayaWindow = getMayaWindow()
    
    print mayaWindow.windowTitle() #Prints the window title with your original scene path
    cmds.file(rename = newPath)
    print mayaWindow.windowTitle() #Prints the window title with your original scene path
    print cmds.file(query=True, sn=True) #But Prints C:/Users/pfruchet/Desktop/NewSceneName.ma ---> Scene name changed but not updated in window title
    
    mayaWindow.setWindowTitle(r"Autodesk Maya 2014: " + newPath) #here is the magic
    print mayaWindow.windowTitle() #Prints Autodesk Maya 2014: C:\Users\pfruchet\Desktop\NewSceneName.ma
    mayaWindow.setWindowModified(False)
    

    The last line allows you to show or hide the "*" after the file path.

    Output:

    Autodesk Maya 2014: C:\Users\pfruchet\Desktop\OriginalScene.ma
    Autodesk Maya 2014: C:\Users\pfruchet\Desktop\OriginalScene.ma
    C:/Users/pfruchet/Desktop/NewSceneName.ma
    Autodesk Maya 2014: C:\Users\pfruchet\Desktop\NewSceneName.ma
    

    Some references:

    Autodesk doc: Working with PySide in Maya How to destroy maya by adding hideous stylesheets.

    Pyside doc: QWidget If you want to try other stuff (you can change the window icon apparently)