Search code examples
pythonmelpymelmaya-api

PYMEL Maya UI crazyness


I'm having trouble with a script i judged simple, yet here I am! The thing is I have no real problem with the ACTUAL script, but with the Maya UI.

It seems I can't avoid the creation of multiple windows, nor delete it using deleteUI("nemeOfUI"). thats driving me crazy. I looked up on line and it seems I'm doing it alright and I just can't finde the answer.

Here the whole code.

Attention to lines 15 16 17 18, 110, 208.

I know the code isnt' the cleanest but its versin 1.2. Once it is working I'll do the cleaning! Thank you!

import pymel.core as pm
from functools import partial
import os.path

try:
    pm.deleteUI(changeTexWindow)
except:
    print''

global sizeChoice

def drawUi():


    if (pm.window("ChangeTextureFiles_v1.2", query = True, exists = True)):
        pm.deleteUI("ChangeTextureFiles_v1.2")

    changeTexWindow = pm.window("ChangeTextureFiles_v1.2",
                            sizeable = False,
                            width = 300,
                            height = 175,
                            minimizeButton = False,
                            maximizeButton = False
                            )

    uiTabs = pm.tabLayout(width = 300,
                      height = 175, 
                      parent = changeTexWindow
                     )

    uiColumn1 = pm.columnLayout("Change Texture Files",
                            rowSpacing = 5,
                            parent = changeTexWindow,
                            width = 300 
                           )

    uiColumn2 = pm.columnLayout("Help",
                            rowSpacing = 5,
                            parent = changeTexWindow,
                            width = 300
                           )                          

    uiRowExtra = pm.rowLayout(numberOfColumns = 1, parent = uiColumn1, h = 2)

    uiRow0 = pm.rowLayout(numberOfColumns = 3, parent = uiColumn1, h = 15)

    pm.separator(style = "none", height = 10, width = 2, horizontal = False)

    pm.text("Change texture...")

    uiRow1 = pm.rowLayout(numberOfColumns = 3, parent = uiColumn1, h = 15)
    uiRow2 = pm.rowLayout(numberOfColumns = 3, parent = uiColumn1, h = 15)
    uiRow3 = pm.rowLayout(numberOfColumns = 3, parent = uiColumn1, h = 15)

    options = pm.radioCollection(parent = uiRow1)

    opt1 = pm.radioButton(parent = uiRow1, label = "From all objects in the scene", data=1)
    opt2 = pm.radioButton(parent = uiRow2, label = "From only selected objects", data=2)
    opt3 = pm.radioButton(parent = uiRow3, label = "From all objects in scene but selected", data=3)
    options = cmds.radioCollection( options, edit=True, select=opt1 )

    uiRow4 = pm.rowLayout(numberOfColumns = 3, parent = uiColumn1)

    uiRow5 = pm.rowLayout(numberOfColumns = 2, parent = uiColumn1)

    pm.text('Type the size of texture you want:', parent = uiRow5, annotation = "Enter values as '1,2 3...'. Check Help tab. ", width = 215)
    sizeChoice = pm.textField(parent = uiRow5, width = 60, annotation = "Enter values as '1,2 3...'. Check Help tab. ")

    uiRow6 = pm.rowLayout(numberOfColumns = 3, parent = uiColumn1)

    allB = pm.button(label = "Apply", width = 115, command = partial(passValue, options, sizeChoice, 0))
    selB = pm.button(label = "Apply and close", width = 115, command = partial(passValue, options, sizeChoice, 1))
    otherB = pm.button(label = "Cancel", width = 54, command = "closeUi()")


    helpTxt0 = pm.text("",parent= uiColumn2, height = 2)
    helpTxt1 = pm.text("Created by Mendel Reis at", parent= uiColumn2, width = 300, height = 12)
    helpTxt2 = pm.text("FACULDADE MELIES", parent= uiColumn2, width = 300,height = 12)
    helpTxt3 = pm.text("www.melies.com.br", parent= uiColumn2, width = 300, height = 12)
    helpTxt01 = pm.text(" ",parent= uiColumn2, height = 5)
    helpTxt4 = pm.text("HOW TO USE:", parent= uiColumn2, width = 300, height = 12)
    helpTxt5 = pm.text("Will change files thru naming convention:", parent= uiColumn2, width = 300, height = 12)
    helpTxt6 = pm.text("file_name.SIZE.extension", parent= uiColumn2, width = 300, height = 12)
    helpTxt7 = pm.text("Input the SIZE as you wish.", parent= uiColumn2, width = 300, height = 12)
    helpTxt8 = pm.text("Only use . (DOT) to isolate the SIZE tag.", parent= uiColumn2, width = 300, height = 12)

    pm.showWindow(changeTexWindow)


def passValue(options, sizeChoice, closeAfter, *args):
radioCol = cmds.radioCollection(options, query=True, sl=True)
selOption = cmds.radioButton(radioCol, query=True, data=True)
newSize = pm.textField(sizeChoice, query = True, tx = True)
print selOption   
print newSize


    if (selOption == 1):
        changeAll(newSize)
    elif (selOption == 2): 
        changeSelected(newSize)
    elif (selOption == 3):
        changeAllBut(newSize)

    if (closeAfter): 
        try:
            del sizeChoice
        except:
            print''
        pm.deleteUI("ChangeTextureFiles_v1.2")


def changeAll(newSize):

    allTex = pm.ls(type = "file") 
    notFound = [] #array for texture files without a size change match

    for tex in allTex: #get info from texture file to be changed

        path = tex.getAttr("fileTextureName")  #get the full path of texture file   
        name = path.split("/")[-1].split('.')[0]  #get the name of the file
        size = path.split("/")[-1].split('.')[-2] #get the user specified, thru naming convention, texture file size info
        extension = path.split("/")[-1].split('.')[-1] #get the texture file extension

        if (size != newSize):  #check if texture file needs to be changed
            old = name + "." + size + "." + extension  #concatenate the old name
            new = name + "." + newSize + "." + extension #concatenate the new name
            newTex = path.replace(old, new, 1) #create string for new fileTextureName
            if (os.path.isfile(newTex)):  #check if requered file exists
                tex.setAttr("fileTextureName", newTex)  #change the textureFileName
            else:
                notFound.append(name+'.' + extension)  #create a log with failed attempts 


def changeSelected(newSize):

    objs = pm.selected()

    for item in objs:
        a = pm.listRelatives(item)[0]
        b = pm.listConnections(a, type = "shadingEngine")
        sgInfo = pm.listConnections(b, type='materialInfo')
        fileNode = pm.listConnections(sgInfo[0], type='file')
        textureFile = pm.getAttr(fileNode[0].fileTextureName)

        name = textureFile.split("/")[-1].split('.')[0]  #get the name of the file
        print "NAME", name
        size = textureFile.split("/")[-1].split('.')[-2] #get the user specified, thru naming convention, texture file size info
        print "SIZE", size
        extension = textureFile.split("/")[-1].split('.')[-1] #get the texture file extension
        print "EXTENSION", extension

        if (size != newSize):  #check if texture file needs to be changed
            old = name + "." + size + "." + extension  #concatenate the old name
            new = name + "." + newSize + "." + extension #concatenate the new name
            newTex = textureFile.replace(old, new, 1) #create string for new fileTextureName
            if (os.path.isfile(newTex)):  #check if requered file exists
                fileNode[0].setAttr("fileTextureName", newTex)  #change the textureFileName
            else:
                print "Did not find a texture to replace. Check naming convention: enter size as '1K', '2K'..."  


def changeAllBut(newSize):

    allObjs = pm.ls(type = "mesh")

    selection = pm.selected()
    selObjs = []

    for item in selection:
        a = pm.listRelatives(item)[0]
        selObjs.append(a) 

    for item in allObjs:
        if item in selObjs:
            allObjs.remove(item)


    for item in allObjs:
            a = item 
            b = pm.listConnections(a, type = "shadingEngine")
            sgInfo = pm.listConnections(b, type='materialInfo')
            fileNode = pm.listConnections(sgInfo[0], type='file')
            textureFile = pm.getAttr(fileNode[0].fileTextureName)

            name = textureFile.split("/")[-1].split('.')[0]  #get the name of the file
            print "NAME", name
            size = textureFile.split("/")[-1].split('.')[-2] #get the user specified, thru naming convention, texture file size info
            print "SIZE", size
            extension = textureFile.split("/")[-1].split('.')[-1] #get the texture file extension
            print "EXTENSION", extension

            if (size != newSize):  #check if texture file needs to be changed
                old = name + "." + size + "." + extension  #concatenate the old name
                new = name + "." + newSize + "." + extension #concatenate the new name
                newTex = textureFile.replace(old, new, 1) #create string for new fileTextureName
                if (os.path.isfile(newTex)):  #check if requered file exists
                    fileNode[0].setAttr("fileTextureName", newTex)  #change the textureFileName
                else:
                    print "Did not find a texture to replace. Check naming convention: enter size as '1K', '2K'..."               


def closeUi():
    try:
        del sizeChoice
    except:
        print''
    pm.deleteUI("ChangeTextureFiles_v1.2")


 drawUi()  

Solution

  • This question is a bit dated, but maybe an answer is still helpful: The problem lies in the window objects name:"ChangeTextureFiles_v1.2". Windows etc. are Maya objects and they to not allow spaces or points. You can try it if you simply create a polycube and try to rename it to "polycube_v1.2". For your window you can use the title flag instead:

    WINNAME="CTexWin"
    changeTexWindow = pm.window(WINNAME,
                                title = "ChangeTextureFiles_v1.2",
                                sizeable = False,
                                width = 300,
                                height = 175,
                                minimizeButton = False,
                                maximizeButton = False
                                )
    

    And if you check for existence just use the WINNAME:

    if pm.window(WINNAME, exists = True):
        pm.deleteUI(WINNAME)