0

I have asked about this Pyhton 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)
Jake
  • 23
  • 3

1 Answers1

0

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)
theodox
  • 11,352
  • 3
  • 20
  • 35
  • You need to make sure that you know both the slider value and the shader name -- probably you should save the shader name into a variable when you do the assignment in `applyMaterial` so it uses that – theodox Apr 23 '18 at 23:26
  • I already have material set up for each option in the dropdown menu. Its just when I change the color the slider does not work – Jake Apr 23 '18 at 23:37
  • You need to either tell the applySlider function which material is assigned, or make sure that applySlider gets the current selection, finds its material, and then does what I've outlined in the set_shader_bump function – theodox Apr 26 '18 at 17:18
  • Is it possible you can show me a physical example because I don't seem to follow. I have my script in the original edited if you want to take a look. – Jake May 01 '18 at 03:56