How to add a folding button to python?

How to add a folding button to python?

2368457978
Enthusiast Enthusiast
598 Views
4 Replies
Message 1 of 5

How to add a folding button to python?

2368457978
Enthusiast
Enthusiast

Hello, everyone, my current script needs to manually input parameters before it can be executed. I want to know how to add a folding button that can be preselected? I looked for Python help documentation but couldn't find it. A button similar to my screenshot, I want to preset png exr tiff iff,67e73fdc11b5eeaa8ec3b5ae2984672.png2ac6dc0254d1de410209c001866cc4e.png


import maya.cmds as cmds import maya.mel as mel import os import shutil def get_texture_node(graph_node): mel_cmd = 'pgYetiGraph -listNodes -type "texture" %s' % str(graph_node) texture_nodes = mel.eval(mel_cmd) return texture_nodes def get_texture_path(texture, graph): mel_cmd = 'pgYetiGraph -node "%s" -param "file_name" -getParamValue %s' % (str(texture), str(graph)) path = mel.eval(mel_cmd) return path def set_texture_path(path, node, graph): mel_cmd = '''pgYetiGraph -node "{node_name}" -param "file_name" -setParamValueString "{path}" "{graph}"'''.format(node_name=str(node), path=str(path), graph=str(graph)) print(mel.eval(mel_cmd)) ################################################################################################ if cmds.window("YetiTool", exists=True): cmds.deleteUI("YetiTool") cmds.window("YetiTool", t="Yetitool", w=200, h=200, sizeable=True, mnb=True, mxb=False) cmds.scrollLayout('scrollLayout') cmds.columnLayout(adj=True) ################################## def set_yeti_texture_image(): all_yeti_nodes = cmds.ls(sl=1, s=1, dag=1) image_format = mc.textFieldGrp('imageformat', q=True, text=True) ## png exr tiff iff for node_name in all_yeti_nodes: tnodes = get_texture_node(node_name) if tnodes: for node in tnodes: texs = get_texture_path(node, node_name) tex_path = texs.split('.')[0] set_texture_path(tex_path + '.' + image_format, node, node_name) print ("Set scucessful: %s" % (tex_path + image_format)) cmds.textFieldGrp("imageformat", l='image', editable=True, cw2=(110, 110), cal=(1, 'left')) cmds.button(l='Replace image', c="set_yeti_texture_image()", bgc=(0.2, 0.4, 0.2), width=120) cmds.showWindow("YetiTool")

0 Likes
Accepted solutions (1)
599 Views
4 Replies
Replies (4)
Message 2 of 5

Kahylan
Advisor
Advisor

Hi!

 

Please when you post code use the codeblock (This symbol in your comment tool bar: </>) so we can just copy and paste the code instead of trying to guess the indentation levels and linebrakes... I could not get your script to run...

But what you are asking for isn't called a "Folding button" it is called a "Dropdown Menu"

What you want to do can be achieved like this:

import maya.cmds as cmds

window = cmds.window()
cmds.columnLayout()
cmds.optionMenu( label='Image Types')
cmds.menuItem( label='PNG' )
cmds.menuItem( label='EXR' )
cmds.menuItem( label='TIFF' )
cmds.menuItem( label='IFF' )
cmds.showWindow( window )

 

I hope it helps!

0 Likes
Message 3 of 5

2368457978
Enthusiast
Enthusiast
import maya.cmds as cmds
import maya.mel as mel
import os
import shutil

def get_texture_node(graph_node):
    mel_cmd = 'pgYetiGraph -listNodes -type "texture" %s' % str(graph_node)
    texture_nodes = mel.eval(mel_cmd)
    return texture_nodes
def get_texture_path(texture, graph):
    mel_cmd = 'pgYetiGraph -node "%s" -param "file_name" -getParamValue %s' % (str(texture), str(graph))
    path = mel.eval(mel_cmd)
    return path
def set_texture_path(path, node, graph):
    mel_cmd = '''pgYetiGraph -node "{node_name}" -param "file_name" -setParamValueString "{path}"  "{graph}"'''.format(node_name=str(node), path=str(path), graph=str(graph))
    print(mel.eval(mel_cmd)) 
        
#################################################  
def ObjectCreation():
    currentValue = cmds.optionMenu("image_Format", query=True, value=True)
    if currentValue == "png": 
        cmds.textField("png", e=True)
    elif currentValue == "exr":
        cmds.textField("exr", e=True)
    elif currentValue == "tif":
        cmds.textField("tif", e=True)
    elif currentValue == "iff":
        cmds.textField("iff", e=True)
    elif currentValue == "<UDIM>.png":
        cmds.textField("<UDIM>.png", e=True)
    elif currentValue == "<UDIM>.tif":
        cmds.textField("<UDIM>.tif", e=True)
    elif currentValue == "<UDIM>.exr":
        cmds.textField("<UDIM>.exr", e=True)            
    elif currentValue == "<UDIM>.iff":
        cmds.textField("<UDIM>.iff", e=True)                                      

def set_yeti_texture_image():
    all_yeti_nodes = cmds.ls(sl=1, s=1, dag=1)
    image_format = ObjectCreation()
    for node_name in all_yeti_nodes:
       
        tnodes = get_texture_node(node_name)
        if tnodes:
            for node in tnodes:
               
                texs = get_texture_path(node, node_name)
                tex_path = texs.split('.')[0]
                
                set_texture_path(tex_path + '.' + image_format, node, node_name)
                print ("Set scucessful: %s" % (tex_path + image_format))
                                
############################################
if cmds.window("YetiTool", exists=True):
    cmds.deleteUI("YetiTool")

cmds.window("YetiTool", t="YetiTool",
          w=460, h=600, sizeable=True, mnb=True, mxb=False)

cmds.scrollLayout('scrollLayout')
cmds.columnLayout(adj=True)
############################################## 
cmds.rowLayout(nc=4)

cmds.optionMenu("image_Format", w = 140, label = "Select format")
cmds.menuItem( label='png' )
cmds.menuItem( label='exr' )
cmds.menuItem( label='tif' )
cmds.menuItem( label='iff' )
cmds.menuItem( label='<UDIM>.png' )
cmds.menuItem( label='<UDIM>.tif' )
cmds.menuItem( label='<UDIM>.exr' )
cmds.menuItem( label='<UDIM>.iff' )


cmds.button(l='Replacement format', c="set_yeti_texture_image()", bgc=(0.2, 0.4, 0.2), width=100)
  
cmds.showWindow("YetiTool")                                                             
                               

Hello, I used the drop-down menu and defined parameters, but it still can't run. My script function is to replace the existing image format by selecting png, tiff and other image formats and then clicking Replace at the back. image_format = ObjectCreation (), ObjectCreation () should be the parameters I selected in the drop-down menu.35ca08729ce26f41b84982961466262.png

0 Likes
Message 4 of 5

Kahylan
Advisor
Advisor
Accepted solution

I couldn't really test it, since I've never used Yeti. Therefore I don't know how to set up a scene that I can test this in.

 

But I'm assuming the Yeti part of your code works and from the looks of your script you just need the dropdown menu to do some string formatting.

 

If you just need to get the value in the dropdown menu,you can do it like this, no additional readout function needed:

import maya.cmds as cmds
import maya.mel as mel
import os
import shutil

def get_texture_node(graph_node):
    mel_cmd = 'pgYetiGraph -listNodes -type "texture" %s' % str(graph_node)
    texture_nodes = mel.eval(mel_cmd)
    return texture_nodes
def get_texture_path(texture, graph):
    mel_cmd = 'pgYetiGraph -node "%s" -param "file_name" -getParamValue %s' % (str(texture), str(graph))
    path = mel.eval(mel_cmd)
    return path
def set_texture_path(path, node, graph):
    mel_cmd = '''pgYetiGraph -node "{node_name}" -param "file_name" -setParamValueString "{path}"  "{graph}"'''.format(node_name=str(node), path=str(path), graph=str(graph))
    print(mel.eval(mel_cmd)) 
        
#################################################  
                         

def set_yeti_texture_image():
    all_yeti_nodes = cmds.ls(sl=1, s=1, dag=1)
    image_format = cmds.optionMenu(imgFormat, query=True, value=True)

    for node_name in all_yeti_nodes:
       
        tnodes = get_texture_node(node_name)
        if tnodes:
            for node in tnodes:
               
                texs = get_texture_path(node, node_name)
                tex_path = texs.split('.')[0]
                
                set_texture_path(tex_path + '.' + image_format, node, node_name)
                print ("Set scucessful: %s" % (tex_path + image_format))
                                
############################################
if cmds.window("YetiTool", exists=True):
    cmds.deleteUI("YetiTool")

cmds.window("YetiTool", t="YetiTool",
          w=460, h=600, sizeable=True, mnb=True, mxb=False)

cmds.scrollLayout('scrollLayout')
cmds.columnLayout(adj=True)
############################################## 
cmds.rowLayout(nc=4)

imgFormat = cmds.optionMenu("image_Format", w = 140, label = "Select format")
cmds.menuItem( label='png' )
cmds.menuItem( label='exr' )
cmds.menuItem( label='tif' )
cmds.menuItem( label='iff' )
cmds.menuItem( label='<UDIM>.png' )
cmds.menuItem( label='<UDIM>.tif' )
cmds.menuItem( label='<UDIM>.exr' )
cmds.menuItem( label='<UDIM>.iff' )


cmds.button(l='Replacement format', c="set_yeti_texture_image()", bgc=(0.2, 0.4, 0.2), width=100)

  
cmds.showWindow("YetiTool") 

 

In your code, you for some reason referenced a textfield in your function "ObjectCreation" but you did not have any textfields in your UI. Also the way the textfield command works in edit mode is:

cmds.textField("Name of Textfield", tx = "Text you want to insert", e = True)

So your script was looking for the textfield with the name "png" (or whatever imageformat your dropdown menu had selected) instead of inserting the imageformat into a textfield. So if for some reason while running you also wanted to store your imageformat parameter in a textfield (I don't quite see the point since the dropdown Menu already is a clear indicator)

You would have to first create a textfield in your UI and then store the value to it correctly.

Something like this:


import maya.cmds as cmds
import maya.mel as mel
import os
import shutil

def get_texture_node(graph_node):
    mel_cmd = 'pgYetiGraph -listNodes -type "texture" %s' % str(graph_node)
    texture_nodes = mel.eval(mel_cmd)
    return texture_nodes
def get_texture_path(texture, graph):
    mel_cmd = 'pgYetiGraph -node "%s" -param "file_name" -getParamValue %s' % (str(texture), str(graph))
    path = mel.eval(mel_cmd)
    return path
def set_texture_path(path, node, graph):
    mel_cmd = '''pgYetiGraph -node "{node_name}" -param "file_name" -setParamValueString "{path}"  "{graph}"'''.format(node_name=str(node), path=str(path), graph=str(graph))
    print(mel.eval(mel_cmd)) 
        
#################################################  
def ObjectCreation():
    currentValue = cmds.optionMenu("image_Format", query=True, value=True)
    cmds.textField(txtField, tx = currentValue, e=True)
    return currentValue                          

def set_yeti_texture_image():
    all_yeti_nodes = cmds.ls(sl=1, s=1, dag=1)
    image_format = ObjectCreation()
    for node_name in all_yeti_nodes:
       
        tnodes = get_texture_node(node_name)
        if tnodes:
            for node in tnodes:
               
                texs = get_texture_path(node, node_name)
                tex_path = texs.split('.')[0]
                
                set_texture_path(tex_path + '.' + image_format, node, node_name)
                print ("Set scucessful: %s" % (tex_path + image_format))
                                
############################################
if cmds.window("YetiTool", exists=True):
    cmds.deleteUI("YetiTool")

cmds.window("YetiTool", t="YetiTool",
          w=460, h=600, sizeable=True, mnb=True, mxb=False)

cmds.scrollLayout('scrollLayout')
cmds.columnLayout(adj=True)
############################################## 
cmds.rowLayout(nc=4)

cmds.optionMenu("image_Format", w = 140, label = "Select format")
cmds.menuItem( label='png' )
cmds.menuItem( label='exr' )
cmds.menuItem( label='tif' )
cmds.menuItem( label='iff' )
cmds.menuItem( label='<UDIM>.png' )
cmds.menuItem( label='<UDIM>.tif' )
cmds.menuItem( label='<UDIM>.exr' )
cmds.menuItem( label='<UDIM>.iff' )


cmds.button(l='Replacement format', c="set_yeti_texture_image()", bgc=(0.2, 0.4, 0.2), width=100)
txtField = cmds.textField()
  
cmds.showWindow("YetiTool")  

 

I hope it helps!

0 Likes
Message 5 of 5

2368457978
Enthusiast
Enthusiast
Thank you. The first method is what I need. Thank you very much.
0 Likes