PYMEL玛雅UI疯狂

问题描述 投票:0回答:1

我在使用我认为简单的脚本时遇到了问题,但我在这里!问题是我对ACTUAL脚本没有任何实际问题,但使用Maya UI。

看来我无法避免创建多个窗口,也不能使用deleteUI(“nemeOfUI”)删除它。那让我疯狂。我抬头看了看,似乎我做得很好,我无法找到答案。

这里是整个代码。

注意第15 16 17 18,110,208行。

我知道代码不是'最干净但它的版本1.2。一旦它工作,我会做清洁!谢谢!

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()  
python mel pymel maya-api
1个回答
1
投票

这个问题有点过时了,但也许答案仍然有用:问题出在窗口对象名称:“ChangeTextureFiles_v1.2”。 Windows等是Maya对象,它们不允许空格或点。如果您只是创建一个多面体并尝试将其重命名为“polycube_v1.2”,则可以尝试使用它。对于您的窗口,您可以使用标题标志:

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

如果你检查是否存在,只需使用WINNAME:

if pm.window(WINNAME, exists = True):
    pm.deleteUI(WINNAME)
© www.soinside.com 2019 - 2024. All rights reserved.