Maya Python脚本的凹凸深度滑块

2024-05-06 19:27:48 发布

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

我以前问过这个pyton脚本,但是现在我遇到了一个新问题:我想让我的脚本的纹理滑块影响模型的凹凸深度,但我不知道怎么做。有人能帮我吗?我有一个凹凸贴图纹理应用在我的模型,我只需要帮助从下面的图像滑块到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)

Tags: thetotruevaluemclabelramassign
1条回答
网友
1楼 · 发布于 2024-05-06 19:27:48

有几件事阻碍了我们的发展:

  1. 你在按钮上设置了两个不同的命令:mc.button(label="Apply", command="applyMaterial()" command="applyTexture()")看起来你真正想要的是applyMaterial在按钮上,applyTexture在它的changeCommand上的滑块上
  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)

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

^{pr2}$

它看起来像这样:

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)

相关问题 更多 >