Material 和纹理更改脚本(之前问过)

Material and Texture Changing Script (asked before)

我正在尝试在 Maya 中创建脚本 Python 以通过下拉菜单更改模型的 material 并通过将更改粗糙度参数的滑块更改模型的纹理。我很难将它应用到我的模型中。任何帮助将不胜感激!

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')

有一些问题。

1:您的按钮有语法错误。你忘了一个逗号。

2:按钮的参数'a'不存在。单击 command 而不是 运行 您的函数。

3:您需要为您的 optionMenu 分配一个变量以获得其全名。当你想查询它的当前值时,你可以稍后传递这个变量。您应该将变量分配给其余的界面项。

4: 没有错误检查。如果 lambert1 不存在或您的任何其他 material 不存在,这将失败。您可以使用 mc.objExists 查看他们是否在场景中。如果没有,则向用户抛出一条错误消息,告诉他们创建它,或者让您的脚本自己创建 material。

假设场景中有 material,以下对我来说工作正常:

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')