The problem of python using (if..else) and (optionMenu)

The problem of python using (if..else) and (optionMenu)

niuxihuanming
Participant Participant
554 Views
6 Replies
Message 1 of 7

The problem of python using (if..else) and (optionMenu)

niuxihuanming
Participant
Participant

Hello, everyone, this is a code to rename and modify the shader UDIM. I have two questions now. The first question: I want to add one in set_name () If..else function, I want name_01=name_00 when textField doesn't have any input, otherwise execute rename_0. Now I've added if.else, but it doesn't seem to work, Second question, I want to modify uvTilingMode of shader file node through optionMenu now, but I don't know how to call the value of uvTilingMode with menuItem.If anyone knows please give pointers, thanks

def set_name():
        #tx
    rename_0 = cmds.textField("rename", q=True, text=True)
        
    br =cmds.optionMenu(symbol, query=True, value=True) 
        # Attribute name  
    typeall=['import','file']
         
    for i in cmds.ls(sl=1,dag=1,type='transform'):
                 #Get the object name
        name_00 = [k.split("Shape")[0] for k in cmds.ls(i)][0]
   #if.......else             
        for a in rename_0:      
            if rename_0==' ':
               a=name_00    
        else:
               a=rename_0 
 ####################                                                                                                                           
        for i1 in typeall:
            try:
                node_type_all=get_node_type(i1,i)
                name_01 = a 
                name_02=str(i1)
                name_04=br         
                for i2 in node_type_all:
                    try:
                        name=name_01+ name_04 +name_02
                        set_node_name(i2,name,i)
                    except:
                        pass
            except:
                pass
                                
def file_setup0():  
    all_files = cmds.ls(type="file", sl=True)
    for n in all_files:
        STS = cmds.optionMenu(Tiling, query=True, value=True)        
        cmds.setAttr('{0}.uvTilingMode' '{}'.format(n), STS)

cmds.window( title='maya_tool')
cmds.columnLayout()                
#####################set_name(TheSuffix)
cmds.rowLayout(nc=5)
cmds.text("name:", h=30, w=30)
cmds.textField("rename",h=30,w=110, editable=True)
cmds.optionMenu("symbol", w = 155, h=30, label = "Add underline?:")
cmds.menuItem( label='')
cmds.menuItem( label='_')
            
cmds.button("sure", w=50, h=30,c="set_name()")

cmds.setParent( '..' )
  
#######################
#####    file_setup0()
Tiling=cmds.optionMenu("Tiling", w = 364, label = "UV Tiling Mode")
cmds.menuItem( label='off' )
cmds.menuItem( label='0-based' )
cmds.menuItem( label='1-based' )
cmds.menuItem( label='UDIM' )
cmds.menuItem( label='Tiles' )
file_setup0()

cmds.showWindow()     

 

0 Likes
Accepted solutions (1)
555 Views
6 Replies
Replies (6)
Message 2 of 7

jmreinhart
Advisor
Advisor

I cleaned up your code and fixed what I think you were asking about, but your question is a bit unclear.

 

Some general advice:

  • Use more descriptive variable names. Stuff like name_00 and name_01 make it very hard to understand what your code is doing when reading it.
  • Please include your whole code or just a small section. You are calling functions that you did not include in the code you posted, which makes testing the whole code impossible. 
  • Don't indent your comments.
  • Please add some comments before sharing your code so that it's easy for people to know what you want the code to do.

 

 

 

 

def set_name(*args):
    # Get the contents of the rename textfield
    rename_text = cmds.textField("rename", q=True, text=True)
    underline = cmds.optionMenu("symbol", query=True, value=True)
         
    # For each selected object
    # WHAT DO YOU WANT THIS cmds.ls TO RETURN?
    for each_transform in cmds.ls(sl=1,dag=1,type='transform'):
        # Get the name of the transform node the shape
        # USING NAMES INSTEAD OF NODE TYPES IS GENERALLY BAD
        transform_name = [k.split("Shape")[0] for k in cmds.ls(each_transform)][0]

        # If the rename_text is empty then keep the transform name    
        result_name = ''   
        if rename_text:
           result_name = transform_name    
        else:
           result_name = rename_text 

        for j in ['import','file']:
            # ONLY USE TRY AND EXCEPTS IF YOU REALLY NEED THEM
            try:
                final_name = result_name + br + str(j)
                # MISSING FUNCTION
                for k in get_node_type(j,each_transform):
                    try:                        
                        # MISSING FUNCTION
                        set_node_name(k,final_name,each_transform)
                    except:
                        pass
            except:
                pass
                                
def file_setup0(STS):  
    for n in cmds.ls(type="file", sl=True):
        cmds.setAttr('{0}.uvTilingMode' '{}'.format(n), STS)

def create_window():
    # Create the window
    cmds.window( title='maya_tool')
    cmds.columnLayout()                
    cmds.rowLayout(nc=5)
    cmds.text("name:", h=30, w=30)
    #
    cmds.textField("rename",h=30,w=110, editable=True)
    cmds.optionMenu("symbol", w = 155, h=30, label = "Add underline?:")
    cmds.menuItem( label='')
    cmds.menuItem( label='_')
                
    cmds.button("sure", w=50, h=30, c= set_name)
    cmds.setParent( '..' )
      
    tilingMenu =cmds.optionMenu("Tiling", w = 364, label = "UV Tiling Mode", changeCommand = file_setup0)
    cmds.menuItem( label='off' )
    cmds.menuItem( label='0-based' )
    cmds.menuItem( label='1-based' )
    cmds.menuItem( label='UDIM' )
    cmds.menuItem( label='Tiles' )
    cmds.setParent( '..' )

    cmds.showWindow()     

create_window()

 

 

0 Likes
Message 3 of 7

niuxihuanming
Participant
Participant

Thank you, the first problem is solved, and the second problem is that I want to use optionMenu to modify the UV Tiling Mode 812968b03c650097ec6faabf910ea4f.png

0 Likes
Message 4 of 7

jmreinhart
Advisor
Advisor
def file_setup0(STS):  
    for n in cmds.ls(type="file", sl=True):
        cmds.setAttr('{0}.uvTilingMode' '{}'.format(n), STS)

Well this code is getting called when you change the optionMenu, right?

Do you get an error? 

If not then the issue is probably this line

for n in cmds.ls(type="file", sl=True):

This will return the selected nodes that are of type "file". If the node is not selected it won't be affected.

 

This line also looks wrong.

cmds.setAttr('{0}.uvTilingMode' '{}'.format(n), STS)

It should be

cmds.setAttr('{}.uvTilingMode'.format(n), STS)

 

0 Likes
Message 5 of 7

niuxihuanming
Participant
Participant

still error

# Error: RuntimeError: file <maya console> line 3: setAttr: Error reading data element number 1: 2 #

My code to execute a single value is

def file_setup():  
    all_files = cmds.ls(type="file", sl=True)
    for n in all_files:
        cmds.setAttr('{0}.uvTilingMode'.format(n), 3)

Before I used button to control, but using button can only control one value, I want to control with optionMenu)

Command execution receipt 

 setAttr "file1.uvTilingMode" 2

The current code

##STS is an int value

def file_setup0(STS):  
    for n in cmds.ls(type="file", sl=True):         
        cmds.setAttr('{}.uvTilingMode'.format(n),STS)            
                    
    cmds.optionMenu( w = 364, label = "UV Tiling Mode", changeCommand = file_setup0)
    cmds.menuItem( label='off' )
    cmds.menuItem( label='0-based(Zbrush)' )
    cmds.menuItem( label='1-based(Mudbox)' )
    cmds.menuItem( label='UDIM(Mari)' )
    cmds.menuItem( label='Explicit Tiles' )
0 Likes
Message 6 of 7

jmreinhart
Advisor
Advisor
Accepted solution
def file_setup0(STS):  
    for n in cmds.ls(type="file", sl=True):
        stsInt = ['off','0-base','1-based','UDIM','Tiles'].index(STS)
        cmds.setAttr('{0}.uvTilingMode'.format(n), stsInt)

Try this as file_setup0.

0 Likes
Message 7 of 7

niuxihuanming
Participant
Participant
thank you very much
0 Likes