Search code examples
pythonmaya

maya python ramp and texture thumbnail views in gui


I am working on a python script for Maya and would like to use the small texture thumbnails Maya displays for ramp textures, but in my script's GUI. Is there a way to utilize those images, or would I have to generate new thumbnails on the fly, as my script runs, and manage those images?

enter image description here


Solution

  • https://help.autodesk.com/cloudhelp/2018/CHS/Maya-Tech-Docs/CommandsPython/show.html?swatchDisplayPort.html&cat=Windows

    From the documentation above, I think you can use this command :

    cmds.window()
    cmds.columnLayout('r')
    myShader = 'ramp2'
    cmds.swatchDisplayPort('slPP', wh=(256, 256), sn=myShader)
    cmds.showWindow()
    

    If you are using PySide or PyQt, it is a bit tricky, you may have to use mayaAPI to find the pointer of this command.

    from PyQt5 import QtWidgets, QtGui, QtCore
    from sip import wrapinstance
    import maya.cmds as cmds
    import maya.OpenMayaUI as omui
    
    def mayaToQT( name ):
        # Maya -> QWidget
        ptr = omui.MQtUtil.findControl( name )
        if ptr is None:         ptr = omui.MQtUtil.findLayout( name )
        if ptr is None:         ptr = omui.MQtUtil.findMenuItem( name )
        if ptr is not None:     return wrapinstance( long( ptr ), QtWidgets.QWidget )
    
    myShader = 'ramp2'
    control = cmds.swatchDisplayPort('slPP', wh=(256, 256), sn=myShader)
    
    swatchQT_ramp2 = mayaToQT(control)
    

    and then add to your pyside/pyqt this object converted back into your ui !