0

我之前询问过这个 Python 脚本,但现在我遇到了一个新问题:我想让脚本的纹理滑块影响模型的凹凸深度,但我不知道该怎么做。有没有人可以帮助我?我的模型中应用了凹凸贴图纹理,我只需要帮助将下图中的滑块转换为 Python 格式。 在此处输入图像描述

import maya.cmds as mc
if mc.window("Mat_Tex", exists=True):
    mc.deleteUI(ram)

ram = mc.window("Mat_Tex", 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")
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
texBumpDepth = mc.floatSliderGrp(label="Texture", min=-5, max=5, field=True)

def applySlider(*args):
    slidervalue = mc.floatSliderGrp(texBumpDepth, q = True, value = True)
    # here is where you want to do something with the value
    print "setting bump value to", slidervalue

    # it will probably look like
    mc.setAttr( "bump2d2" + ".bumpDepth", slidervalue)

def set_shader_bump_depth(shader, amount):
    bump2ds = mc.listConnections(shader, type = 'bump2d')
    # that's always list, so we loop over it
    for eachbump2d in bump2ds:
        print "editing", eachbump2d
        mc.setAttr(eachbump2d + ".bumpDepth", amount)

mc.floatSliderGrp(texBumpDepth, e=True, changeCommand = applySlider)

def applyMaterial(*args):
  currentValue = mc.optionMenu(matOptionMenu, query=True, value=True)
  if currentValue == "Red":
    mc.hyperShade(assign='red')
  elif currentValue == "Blue":
    mc.hyperShade(assign='blue')
  elif currentValue == "Yellow":
    mc.hyperShade(assign='yellow')
  elif currentValue == "Green":
    mc.hyperShade(assign='green')
  elif currentValue == "Orange":
    mc.hyperShade(assign='orange')
  elif currentValue == "Purple":
    mc.hyperShade(assign='purple')

# A button to apply any changes
mc.button(label="Apply", command=applyMaterial)

mc.showWindow(ram)
4

1 回答 1

0

有几件事妨碍了这里:

  1. 你在按钮上设置了两个不同的命令:mc.button(label="Apply", command="applyMaterial()" command="applyTexture()")看起来你真正想要的是applyMaterial按钮和applyTexture附加到它的滑块上changeCommand
  2. 如所写,您没有跟踪着色器本身,因此您不知道在哪里设置属性
  3. 您应该避免使用命令的字符串形式,而是直接使用函数,原因在此处列出。

这是 gui 部分的返工,设置如上:

import maya.cmds as mc
if mc.window("Mat_Tex", exists=True):
    mc.deleteUI(ram)

ram = mc.window("Mat_Tex", 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")
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
texBumpDepth = mc.intSliderGrp(label="Texture", min=-5, max=5, field=True)

def applySlider(*args):
    slidervalue = mc.intSliderGrp(texBumpDepth, q = True, value = True)
    # here is where you want to do something with the value
    print "setting bump value to", slidervalue

    # it will probably look like
    # mc.setAttr( your_bump2dnode_here + ".bumpDepth", slidervalue

mc.intSliderGrp(texBumpDepth, e=True, changeCommand = applySlider)

def applyMaterial(*args):
  currentValue = mc.optionMenu(matOptionMenu, query=True, value=True)
  if currentValue == "Red":
    mc.select('octopusModel')
    mc.hyperShade(assign='red')
  elif currentValue == "Blue":
    mc.select('octopusModel')
    mc.hyperShade(assign='blue')
  elif currentValue == "Yellow":
    mc.select('octopusModel')
    mc.hyperShade(assign='yellow')
  elif currentValue == "Green":
    mc.select('octopusModel')
    mc.hyperShade(assign='green')
  elif currentValue == "Orange":
    mc.select('octopusModel')
    mc.hyperShade(assign='orange')
  elif currentValue == "Purple":
    mc.select('octopusModel')
    mc.hyperShade(assign='purple')


# A button to apply any changes
mc.button(label="Apply", command=applyMaterial)

mc.showWindow(ram)

要实际编辑凹凸节点,您需要在拥有着色器后执行以下步骤:

1. get the connections from the shader that are `bump2d` nodes
2. use `mc.setAttr` to set the value

它看起来像这样:

def set_shader_bump_depth(shader, amount):
    bump2ds = mc.listConnections(shader, type = 'bump2d')
    # that's always list, so we loop over it
    for eachbump2d in bump2ds:
        print "editing", eachbump2d
        mc.setAttr(eachbump2d + ".bumpDepth", amount)
于 2018-04-23T20:39:50.223 回答