Search code examples
pythonmaya

Maya python CHANGE UVTILE with place2dTexture


In python I want to select one object and change repeatUV .

The code work for all objects and not for one specific object

import maya.cmds as cmds
import pymel.core as pmc

def UVTILECHANGE():

    selected = cmds.ls(sl=True)

    textField_obj_name = cmds.textField( 'textField_action_name',query=True,text=True )

    textField_obj = cmds.textField( 'textField_action',query=True,text=True )
    #print textField_obj
    textField_obj2 = cmds.textField( 'textField_action2',query=True,text=True )

    if  textField_obj_name==selected[0]:  

        array = cmds.ls(type="place2dTexture")
        #SELECT ALL PLACE2DTECTURE
        #I WANT JUST FOR selected[0]
        #print len(array)
        i=0
        while (i <= len(array)):
            print 
            cmds.setAttr(str(array[i])+ ".repeatU", float(textField_obj))

            cmds.setAttr(str(array[i])+ ".repeatV", float(textField_obj2))
            i=i+1

def MakeWindow():

    if (pmc.window('flattenVertwindow', exists=True)):
        pmc.deleteUI('flattenVertwindow')

    pmc.window('flattenVertwindow', title="TEST",widthHeight=(200,100))
    pmc.columnLayout(adjustableColumn=True)
    #pmc.text(label='select axis')
    cmds.text("Choisir nom de l'objet")
    cmds.separator(h=10)
    textField00 = cmds.textField( 'textField_action_name' )
    cmds.separator(h=10)
    cmds.text("Taille des Tiles")
    cmds.separator(h=10)
    textField01 = cmds.textField( 'textField_action' )
    textField02 = cmds.textField( 'textField_action2' )
    red= pmc.button(label='Change UV TILE ', command= 'UVTILECHANGE()')

    pmc.showWindow('flattenVertwindow')


MakeWindow()  

Solution

  • Im not sure what doesn't work, Ive improved your code with comments. I hope it will help, it works here :

    import maya.cmds as cmds
    import pymel.core as pmc
    
    # if you dont know classes, you can create a dict to store all main controls
    UI_DIC = {}
    
    # for function used in ui, you need *args because maya will try to parse a default True
    # argument as last argument
    def picker(*args):
        sel = cmds.ls(sl=True)[0]
        cmds.textField(UI_DIC['textField00'], edit=True, text=sel )
    
    # use lower case for functions, you can use camel casing like maya : uvTilesChange
    # but keep the first letter lower case as uppercase for the first is used for Class
    def uv_tile_change(*args):
    
        # we parse fields with the value captured in the dictionnary
        textField_obj_name = cmds.textField(UI_DIC['textField00'], query=True, text=True )
        textField_obj = cmds.textField(UI_DIC['textField01'], query=True, text=True )
        textField_obj2 = cmds.textField(UI_DIC['textField02'], query=True, text=True )
    
        # if there is nothing in the ui, dont execute anything
        if not textField_obj_name:
            return
    
        # we are using few funcitons to find the place2dTexture of an object
        shape_mesh = cmds.ls(textField_obj_name, dag=True, type='shape', ni=True)
        if not shape_mesh:
            # if the object doesnt exists, stop here
            return
        # we look for the shading engine of the object
        hist = cmds.listHistory(shape_mesh, future=True)
        sgs = cmds.ls(hist, type="shadingEngine")
        # from the sgs, we look for the place2dTexture
        place2dTextures = [i for i in cmds.listHistory(sgs) if cmds.ls(i, type='place2dTexture')]
        # instead of a while, just use a python for loop
        for p2t in place2dTextures:
            cmds.setAttr(p2t + ".repeatU", float(textField_obj))
            cmds.setAttr(p2t + ".repeatV", float(textField_obj2))
    
    def make_window():
    
        width = 180
        height = 150
    
        if (pmc.window('flattenVertwindow', exists=True)):
            pmc.deleteUI('flattenVertwindow')
    
        # sizeable works for me
        pmc.window('flattenVertwindow', title="TEST",widthHeight=(width,height), sizeable=False)
        main_layout = pmc.columnLayout(adjustableColumn=True)
        # use the flag parent, it is more clear
        pmc.text("Choisir nom de l'objet", parent=main_layout)
        # choose either cmds or pymel, do not use both
        pmc.separator(h=10, parent=main_layout)
    
        # here is an example on how parent flag is useful
        # we create a sub layout and parent it to the main one
        # we parent all the controls to the sub layout
        picker_layout = pmc.rowLayout(nc=3, parent=main_layout, width=width, ad1=True)
        # we capture the name of the control on creation inside the dict
        UI_DIC['textField00'] = pmc.textField(parent=picker_layout, w=width)
        # take the habit to not put string in the command flag
        pmc.button(label='<', command= picker, parent=picker_layout)
    
        pmc.separator(h=10, parent=main_layout)
        pmc.text("Taille des Tiles", parent=main_layout)
        pmc.separator(h=10)
        UI_DIC['textField01'] = pmc.textField(parent=main_layout)
        UI_DIC['textField02'] = pmc.textField(parent=main_layout)
        red= pmc.button(label='Change UV TILE ', command= uv_tile_change, parent=main_layout)
    
        pmc.showWindow('flattenVertwindow')
    
    
    make_window()