【问题标题】:Bump Depth slider for Maya Python scriptMaya Python 脚本的凹凸深度滑块
【发布时间】:2018-04-23 19:01:33
【问题描述】:

我之前询问过这个 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)

【问题讨论】:

    标签: python maya bump-mapping


    【解决方案1】:

    这里有几件事阻碍:

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

    这里是对 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)
    

    【讨论】:

    • 你需要确保你知道滑块值和着色器名称——也许你应该在 applyMaterial 中进行赋值时将着色器名称保存到一个变量中,以便它使用它
    • 我已经在下拉菜单中为每个选项设置了材料。只是当我更改颜色时滑块不起作用
    • 您需要告诉 applySlider 函数分配了哪种材质,或者确保 applySlider 获取当前选择,找到它的材质,然后执行我在 set_shader_bump 函数中概述的操作
    • 您是否可以给我看一个物理示例,因为我似乎没有遵循。如果您想看一下,我已经编辑了原始脚本。
    猜你喜欢
    • 2019-07-28
    • 1970-01-01
    • 2020-06-15
    • 2011-10-05
    • 1970-01-01
    • 1970-01-01
    • 2016-10-06
    • 1970-01-01
    • 2019-09-24
    相关资源
    最近更新 更多