Bump Depth slider for Maya Python script

461 views Asked by At

I have asked about this Python script before, but now i have become faced with a new question: I want to have the texture slider of my script affect the bump depth of my model, but I am not sure how to do so. Is there anyone out there that can help me? I have a bump map texture applied in my model, I just need help getting the slider from the image below into Python format. 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

There are 1 answers

4
theodox On

A couple of things are getting in the way here:

  1. You're setting two different commands on the button: mc.button(label="Apply", command="applyMaterial()" command="applyTexture()") It looks like what you really want is applyMaterial on the button and applyTexture on the slider attached to its changeCommand
  2. As written, you are not tracking the shader itself so you don't know where to set the properties
  3. You should avoid using the string form of the command, and instead use the functions directly, for reasons laid out here.

Here's a rework of the gui portion that sets things up as above:

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)

To actually edit the bump node you'll need to follow these steps once you have the shader:

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

It will look something like this:

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)