材质与质感变化脚本(前问)

2024-05-07 00:00:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试在mayapython中创建一个脚本,通过下拉菜单更改模型的材质,通过滑块更改粗糙度参数来更改模型的纹理。我正在把它应用到我的模型上。任何帮助将不胜感激!在

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

Tags: 模型trueobjectsmclabelrammaterialassign
1条回答
网友
1楼 · 发布于 2024-05-07 00:00:56

有一些问题。在

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

相关问题 更多 >