Search code examples
pythonrenderingmaya

Maya Python, Connect to 2 lists


So guys, this is question about Maya, python.

Can I create a loop and set shader A color to R shader B color to G and shaderC to B

for i in range(5):
    shader = cmds.shadingNode ('surfaceShader', name=('mm'+str(i)), asShader=True)
    cmds.sets(renderable=True, noSurfaceShader=True, empty=True, name=('mmSG'+str(i)))
    cmds.setAttr(shader +'.outColor', 1,0,0)

this is what I have so far, I am stucking in every 3 steps...


Solution

  • You're almost there - you just need to connect the shader's outColor to the surfaceShader attribute on the shading group:

    import maya.cmds as cmds
    
    for i in range(5):
        shader = cmds.shadingNode ('surfaceShader', name=('mm'+str(i)), asShader=True)
        sg = cmds.sets(renderable=True, noSurfaceShader=True, empty=True, name=('mmSG'+str(i)))
        cmds.setAttr(shader +'.outColor', 1,0,0)
        cmds.connectAttr(shader + ".outColor", sg + ".surfaceShader")
    

    edit

    I misread OP's intention. If the idea is to make a set of shaders with specified colors, you'd want to do something like this:

      colors = {'red': (1,0,0), 'blue': (0,1,0), 'green':(0,0,1)} 
    
      for name, color in colors.items():
          shader = cmds.shadingNode ('surfaceShader', name=(name), asShader=True)
          sg = cmds.sets(renderable=True, noSurfaceShader=True, empty=True, name=(name + "SG")
          cmds.setAttr(shader +'.outColor', color[0], color[1], color[2])
          cmds.connectAttr(shader + ".outColor", sg + ".surfaceShader")