我在判断简单的脚本上遇到了麻烦,但是我在这里!问题是我对实际脚本没有真正的问题,而是Maya UI。
看来我无法避免创建多个窗口,也不能使用deleteui(" nemeofui")删除它。那使我发疯。我在线抬起头,看来我做得很好,我只是找不到答案。
在这里整个代码。
注意第15行16 16 17 18、110、208。
我知道代码不是最干净,而是它的versin 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()
这个问题有点过时,但也许答案仍然有用:问题在于窗口对象名称:" ChangeTextureFiles_V1.2"。Windows等是Maya对象,它们不允许空间或点。如果您只创建一个polycube,然后尝试将其重命名为" polycube_v1.2",则可以尝试一下。对于您的窗口,您可以改用 title 标志:
WINNAME="CTexWin"
changeTexWindow = pm.window(WINNAME,
title = "ChangeTextureFiles_v1.2",
sizeable = False,
width = 300,
height = 175,
minimizeButton = False,
maximizeButton = False
)
,如果您检查存在,只需使用获奖名称:
if pm.window(WINNAME, exists = True):
pm.deleteUI(WINNAME)