Maya Python脚本的凹凸深度滑块

时间:2018-04-23 19:01:33

标签: python maya bump-mapping

之前我曾经问过这个Pyhton脚本,但是现在我遇到了一个新问题:我希望我的脚本的纹理滑块影响我模型的凹凸深度,但我不知道怎么做。那里有人可以帮助我吗?我在我的模型中应用了凹凸贴图纹理,我只需要帮助将下图中的滑块变为Python格式。 enter image description here

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)

1 个答案:

答案 0 :(得分:0)

有几件事情在这里受到影响:

  1. 您在按钮上设置了两个不同的命令:mc.button(label="Apply", command="applyMaterial()" command="applyTexture()")看起来您真正想要的是按钮上的applyMaterial和附加到其{的滑块上的applyTexture {1}}
  2. 如上所述,您没有跟踪着色器本身,因此您不知道在哪里设置属性
  3. 您应该避免使用命令的字符串形式,而是直接使用这些函数,原因是here
  4. 这里是gui部分的返工,如上所述:

    changeCommand

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

    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