我试图为maya创建一个脚本,它基本上将快速选择集作为列表中的索引。
我已经使用已存在的按钮存储和加载信息,但我希望用户能够在默认选择集数量不足时生成新按钮。
我目前有一个生成新按钮的按钮。如果只生成一个按钮,它可以正常工作。
我的第一个问题:如果您生成第二个按钮,则第一个生成的按钮会使用与第二个生成按钮相同的列表索引。
e.g。我创建了一个新按钮(按钮4)。它可以毫无问题地存储和加载选择。 我创建了另一个新按钮(按钮5)。现在按钮4将存储和加载,就像它是按钮5一样,按钮5本身也是如此。
我的第二个问题:如果您已经存储了选择,则无法创建新按钮。
到目前为止我的代码是:
import maya.cmds as mc
favsWindowName = 'favsWindow'
numButtons = 4
def favsWindowUI():
if mc.window(favsWindowName, exists=True):
mc.deleteUI(favsWindowName, wnd=True)
mc.window(favsWindowName, title="Favourites", resizeToFitChildren=True, bgc=(0.20, 0.50, 0.50), s=True)
mc.rowColumnLayout(nr=1)
mc.button("newSet", label="New Selection Set", c=("newButton()"))
mc.rowColumnLayout(nr=2)
mc.button("Load1", label="Load Slot 1", w=200, c=("Load(1)"))
mc.button("Sel1", label="Select Slot 1", w=200, c=("Sel(1)"))
mc.button("Load2", label="Load Slot 2", w=200, c=("Load(2)"))
mc.button("Sel2", label="Select Slot 2", w=200, c=("Sel(2)"))
mc.button("Load3", label="Load Slot 3", w=200, c=("Load(3)"))
mc.button("Sel3", label="Select Slot 3", w=200, c=("Sel(3)"))
mc.showWindow()
selList = []
def Load(favNum):
try:
# if a selection has already been loaded for this button, replace it.
selList[favNum-1] = mc.ls(sl=True)
except IndexError:
try:
#if the previous index exists
if selList[favNum-2] > 0:
# if a selection has not yet been loaded for this button, create it.
selList.append(mc.ls(sl=True))
except IndexError:
# if the previous index doesn't exist 'cause this is the first entry
if favNum == 1:
selList.append(mc.ls(sl=True))
else:
#if the previous index doesn't exist, raise an error.
mc.error("Load the previous selection first!")
def Sel(favNum):
try:
# if a selection has been loaded for this button, select it.
mc.select(selList[favNum-1], r=True)
except IndexError:
# if no selection has been loaded for this button, raise an error.
mc.error("No selection loaded.")
def newButton():
#generate a new button set using the next available index.
global numButtons
mc.button("Load"+str(numButtons), label="Load Slot "+str(numButtons), w=200, c=("Load(numButtons-1)"))
mc.button("Sel"+str(numButtons), label="Select Slot "+str(numButtons), w=200, c=("Sel(numButtons-1)"))
numButtons += 1
favsWindowUI()
我也不确定为什么使用生成的按钮我需要在newButton函数中使用Load(numButtons-1)
而不是Load(numButtons)
...但它似乎可以解决问题。
答案 0 :(得分:0)
如果有人有类似的问题,我会发布我们(我和学校的一些朋友)想出来的解决方案。
使用partial
解决了使用相同索引的所有新生成按钮的问题。
另一个问题(加载选择后无法生成新按钮)是由于没有设置新按钮的父级。我们给了rowColumnLayout
一个名称,并使用setParent
maya命令将其设置为父级。
要在上下文中查看,请参阅下面的代码。
import maya.cmds as mc
from functools import partial
favsWindowName = 'favsWindow'
def favsWindowUI():
if mc.window(favsWindowName, exists=True):
mc.deleteUI(favsWindowName, wnd=True)
mc.window(favsWindowName, title="Favourites", resizeToFitChildren=True, bgc=(0.20, 0.50, 0.50), s=True)
mc.rowColumnLayout(nr=2)
mc.textFieldGrp('btnName', tx='new button name')
mc.button("newSet", label="New selection Set", c=(newButton))
mc.rowColumnLayout('selectionLayout', nc=2)
mc.showWindow()
selList = {}
def load(favNum, *args):
# Load the selection into the dictionary entry
selList[favNum] = mc.ls(sl=True)
def sel(favNum, *args):
try:
# if a selection has been loaded for this button, select it.
mc.select(selList[favNum], r=True)
except IndexError:
# if no selection has been loaded for this button, raise an error.
mc.error("No selection loaded.")
def newButton(*args):
buttonName = mc.textFieldGrp('btnName', q=True, tx=True)
mc.setParent('selectionLayout')
if buttonName != 'new button name':
if mc.button("load"+str(buttonName), exists=True, q=True):
mc.error("A selection set named %s already exists." % buttonName)
mc.button("load"+str(buttonName), label="Load Slot "+str(buttonName), w=200, c=(partial(load,buttonName)))
mc.button("sel"+str(buttonName), label="Select Slot "+str(buttonName), w=200, c=(partial(sel,buttonName)))
mc.textFieldGrp('btnName', tx='new button name', e=True)
else:
mc.error("Rename the button first.")
favsWindowUI()