简体   繁体   中英

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...

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")

So I end up using

import maya.cmds as cmds

shaderlist=[]

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.connectAttr(shader + ".outColor", sg + ".surfaceShader")
    shaderlist.append(shader)



for shader in shaderlist[0::3]:
    cmds.setAttr(shader+'.outColor', 1.0,0,0)

for shader in shaderlist[1::3]:
    cmds.setAttr(shader+'.outColor', 0,1,0)

for shader in shaderlist[2::3]:
    cmds.setAttr(shader+'.outColor', 0,0,1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM