简体   繁体   English

使用Maya Python使用着色遮罩设置渲染层

[英]Setting render layers with shading masks with Maya Python

So I'm trying to write a script that takes the name of materials from a list, creates a render layer named after each material, then applies a white surface shader to materials that share the render layer's name and a black surface shader to objects that don't. 因此,我正在尝试编写一个脚本,该脚本从列表中获取材质的名称,创建以每种材质命名的渲染层,然后将白色表面着色器应用于共享渲染层名称的材质,并将黑色表面着色器应用于对象别。

import maya.cmds as cmds

matName = ['blue_mat', 'green_mat', 'red_mat', 'purple_mat']

cmds.shadingNode('surfaceShader',asShader=True,n='WhiteMat')
cmds.setAttr('WhiteMat.outColor', 1.0, 1.0, 1.0, type = 'double3')
cmds.shadingNode('surfaceShader',asShader=True,n='BlackMat')
cmds.setAttr('BlackMat.outColor', 0.0, 0.0, 0.0, type = 'double3')

for i in range(4):
    cmds.select(cl=True)
    cmds.select( hi=True, all=True)
    cmds.createRenderLayer(n=matName[i]+'_layer')
    cmds.hyperShade(objects=matName[i])
    cmds.hyperShade(assign='BlackMat')
    if cmds.hyperShade(objects='BlackMat'):
        cmds.hyperShade(objects=matName[i])

So far, I've gotten it to create the layers with all objects and apply the black shader to everything, but I'm a bit lost on how to apply the white shader to a material on only the layer it's named after. 到目前为止,我已经获得了使用所有对象创建图层并将黑色着色器应用于所有内容的方法,但是我对如何仅将白色着色器应用于以其命名的图层上的材质有些迷惑。 I think I can use an if statement to set it up, but I'm new to Python and still trying to get a handle on things. 我想我可以使用if语句来设置它,但是我对Python还是陌生的,并且仍然在尝试处理。

Thanks! 谢谢!

alright, got it figured out. 好吧,弄清楚了。 Just had to change a few things, and include an if else inside a range to apply the shaders in the right order. 只需更改一些内容,并在range内包含if else ,即可按正确的顺序应用着色器。

import maya.cmds as cmds

#Put in your material names here.  Make sure they have the EXACT SAME spelling, caps, 
#and name conventions as in the Hypershade.  And don't forget to put each one in ''!

matName = ['blue_mat','green_mat','red_mat','purple_mat']

cmds.shadingNode('surfaceShader',asShader=True,n='WhiteMat')
cmds.setAttr('WhiteMat.outColor', 1.0, 1.0, 1.0, type = 'double3')
cmds.shadingNode('surfaceShader',asShader=True,n='BlackMat')
cmds.setAttr('BlackMat.outColor', 0.0, 0.0, 0.0, type = 'double3')

for i in range(len(matName)):
    cmds.select(cl=True)
    cmds.select( ado=True)
    cmds.createRenderLayer(n=matName[i]+'_layer')

    cmds.hyperShade(objects=matName[i])
    cmds.editRenderLayerGlobals(crl=matName[i]+'_layer')    

    for x in range(len(matName)):
        if matName[x]!=matName[i]:
            cmds.hyperShade(objects=matName[x])
            cmds.hyperShade(assign='BlackMat')
        else:
           cmds.hyperShade(objects=matName[x])
           cmds.hyperShade(assign='WhiteMat')  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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