我正在尝试在Maya Python中创建一个脚本,通过下拉菜单更改模型的材质,通过滑块更改粗糙度参数的模型纹理。我正在将它应用到我的模型中。任何帮助将不胜感激!
import maya.cmds as mc
if mc.window("ram", exists=True):
mc.deleteUI(ram)
ram = mc.window("Material and Texture", t="Material and Texture", w=300, h=300)
mc.columnLayout(adj=True)
imagePath = mc.internalVar(upd=True) + "icons/scriptlogo.jpg"
mc.image(w=300, h=200, image=imagePath)
# A dropdown menu deisnged to change material/color of octopus
mc.optionMenu(label="Material",)
myBlinn = mc.menuItem(label="Red")
myBlinn = mc.menuItem(label="Blue")
myBlinn = mc.menuItem(label="Yellow")
myBlinn = mc.menuItem(label="Green")
myBlinn = mc.menuItem(label="Orange")
myBlinn = mc.menuItem(label="Purple")
# A slider designed to alter the intensity of the octopus' texture
mc.intSliderGrp(label="Texture", min=0, max=10, field=True)
# A button to apply any changes
mc.button(label="Apply" a="applyMaterial ()")
mc.showWindow(ram)
def applyMaterial():
currentValue = mc.optionMenu("Material", query=True, value=True)
if currentValue == "Red":
mc.hyperShade(objects='lambert1')
mc.hyperShade(assign='red')
elif currentValue == "Blue":
mc.hyperShade(objects='lambert1')
mc.hyperShade(assign='blue')
elif currentValue == "Yellow":
mc.hyperShade(objects='lambert1')
mc.hyperShade(assign='yellow')
elif currentValue == "Green":
mc.hyperShade(objects='lambert1')
mc.hyperShade(assign='green')
elif currentValue == "Orange":
mc.hyperShade(objects='lambert1')
mc.hyperShade(assign='orange')
elif currentValue == "Purple":
mc.hyperShade(objects='lambert1')
mc.hyperShade(assign='purple')
答案 0 :(得分:1)
有一些问题。
1:按钮上出现语法错误。你忘了逗号。
2:按钮不存在参数“a”。使用command
代替在单击时运行您的功能。
3:您需要为optionMenu
分配一个变量才能获得其全名。您可以稍后在要查询其当前值时传递此变量。您应该将变量分配给其余的接口项。
4:没有错误检查。如果lambert1不存在或您的任何其他材料不存在,这将失败。您可以使用mc.objExists
查看他们是否在场景中。如果没有,请向用户发出错误消息,告诉他们创建它,或者让脚本自己创建材料。
如果材料在场景中,以下内容对我有效:
import maya.cmds as mc
if mc.window("ram", exists=True):
mc.deleteUI(ram)
ram = mc.window("Material and Texture", t="Material and Texture", w=300, h=300)
mc.columnLayout(adj=True)
imagePath = mc.internalVar(upd=True) + "icons/scriptlogo.jpg"
mc.image(w=300, h=200, image=imagePath)
# A dropdown menu deisnged to change material/color of octopus
matOptionMenu = mc.optionMenu(label="Material") # Need to assign a variable here to capture its full name.
myBlinn = mc.menuItem(label="Red")
myBlinn = mc.menuItem(label="Blue")
myBlinn = mc.menuItem(label="Yellow")
myBlinn = mc.menuItem(label="Green")
myBlinn = mc.menuItem(label="Orange")
myBlinn = mc.menuItem(label="Purple")
# A slider designed to alter the intensity of the octopus' texture
mc.intSliderGrp(label="Texture", min=0, max=10, field=True)
# A button to apply any changes
mc.button(label="Apply", command="applyMaterial()") # Missing comma after label, and parameter needs to be command.
mc.showWindow(ram)
def applyMaterial():
currentValue = mc.optionMenu(matOptionMenu, query=True, value=True) # Use the variable to get the value.
if currentValue == "Red":
mc.hyperShade(objects='lambert1')
mc.hyperShade(assign='red')
elif currentValue == "Blue":
mc.hyperShade(objects='lambert1')
mc.hyperShade(assign='blue')
elif currentValue == "Yellow":
mc.hyperShade(objects='lambert1')
mc.hyperShade(assign='yellow')
elif currentValue == "Green":
mc.hyperShade(objects='lambert1')
mc.hyperShade(assign='green')
elif currentValue == "Orange":
mc.hyperShade(objects='lambert1')
mc.hyperShade(assign='orange')
elif currentValue == "Purple":
mc.hyperShade(objects='lambert1')
mc.hyperShade(assign='purple')