Search code examples
pythontexturesdropdownmaya

Material and Texture Changing Script (asked before)


I am trying to create a script in Maya Python to change the material of a model through a dropdown menu and the texture of a model through a slider that will change the roughness parameters. I am having toruble applying it to my model. Any help would be greatly appreciated!

import maya.cmds as mc
if mc.window("ram", exists=True):
    mc.deleteUI(ram)

ram = mc.window("Material and Texture", 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
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
mc.intSliderGrp(label="Texture", min=0, max=10, field=True)

# A button to apply any changes
mc.button(label="Apply" a="applyMaterial ()")

mc.showWindow(ram)

def applyMaterial():
   currentValue = mc.optionMenu("Material", query=True, value=True)
   if currentValue == "Red":
       mc.hyperShade(objects='lambert1')
       mc.hyperShade(assign='red')
   elif currentValue == "Blue":
       mc.hyperShade(objects='lambert1')
       mc.hyperShade(assign='blue')
   elif currentValue == "Yellow":
       mc.hyperShade(objects='lambert1')
       mc.hyperShade(assign='yellow')
   elif currentValue == "Green":
       mc.hyperShade(objects='lambert1')
       mc.hyperShade(assign='green')
   elif currentValue == "Orange":
       mc.hyperShade(objects='lambert1')
       mc.hyperShade(assign='orange')
   elif currentValue == "Purple":
       mc.hyperShade(objects='lambert1')
       mc.hyperShade(assign='purple')

Solution

  • There's a few problems.

    1: Syntax error on your button. You forgot a comma.

    2: The parameter 'a' doesn't exist for the button. Use command instead to run your function when it's clicked.

    3: You need to assign a variable to your optionMenu in order to get its full name. You can pass this variable later when you want to query its current value. You should assign variables to the rest of your interface items.

    4: There's no error checking. This will fail if lambert1 doesn't exist or any of your other materials don't exist. You could use mc.objExists to see if they're in the scene. If not, throw an error message to the user telling them to create it, or have your script create the material itself.

    Provided the materials are in the scene, the following is working ok for me:

    import maya.cmds as mc
    if mc.window("ram", exists=True):
        mc.deleteUI(ram)
    
    ram = mc.window("Material and Texture", 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") # Need to assign a variable here to capture its full name.
    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
    mc.intSliderGrp(label="Texture", min=0, max=10, field=True)
    
    # A button to apply any changes
    mc.button(label="Apply", command="applyMaterial()") # Missing comma after label, and parameter needs to be command.
    
    mc.showWindow(ram)
    
    def applyMaterial():
       currentValue = mc.optionMenu(matOptionMenu, query=True, value=True) # Use the variable to get the value.
       if currentValue == "Red":
           mc.hyperShade(objects='lambert1')
           mc.hyperShade(assign='red')
       elif currentValue == "Blue":
           mc.hyperShade(objects='lambert1')
           mc.hyperShade(assign='blue')
       elif currentValue == "Yellow":
           mc.hyperShade(objects='lambert1')
           mc.hyperShade(assign='yellow')
       elif currentValue == "Green":
           mc.hyperShade(objects='lambert1')
           mc.hyperShade(assign='green')
       elif currentValue == "Orange":
           mc.hyperShade(objects='lambert1')
           mc.hyperShade(assign='orange')
       elif currentValue == "Purple":
           mc.hyperShade(objects='lambert1')
           mc.hyperShade(assign='purple')