Skip to content

Instantly share code, notes, and snippets.

@tokejepsen
Last active December 8, 2015 11:05
Show Gist options
  • Save tokejepsen/2146d3bc5e5b67be9c31 to your computer and use it in GitHub Desktop.
Save tokejepsen/2146d3bc5e5b67be9c31 to your computer and use it in GitHub Desktop.
Maya: Matte from Materials
"""
Select the geometry to make the renderlayers from.
Will find all the materials (lambert), make a renderlayer per material only showing that material.
"""
import traceback
import pymel.core
selection = pymel.core.ls(selection=True)
shader = pymel.core.shadingNode('lambert', name='no_alpha_mat', asShader=1)
shader.matteOpacity.set(0)
shader.colorR.set(0)
shader.colorG.set(0)
shader.colorB.set(0)
shader_sg = pymel.core.sets(renderable=1, noSurfaceShader=1, empty=1, name='no_alpha_mat_SG')
pymel.core.connectAttr( shader + ".outColor", shader_sg + ".surfaceShader", force=1)
materials = {}
for node in selection:
for shp in node.getShapes():
for mat in shp.listConnections(type='shadingEngine'):
if mat in materials:
materials[mat].append(node)
else:
materials[mat] = [node]
drg = pymel.core.PyNode('defaultRenderGlobals')
for sg in materials:
mat = None
try:
mat = sg.listConnections(type='lambert')[0]
except:
continue
layer = pymel.core.rendering.createRenderLayer(name=mat.name().split(':')[-1], empty=True)
layer.addMembers(selection)
layer.setCurrent()
layer.addAdjustments(drg.currentRenderer)
drg.currentRenderer.set('mayaSoftware')
layer.addAdjustments(drg.imageFormat)
drg.imageFormat.set(32)
for node in selection:
if node not in materials[sg]:
pymel.core.sets(shader_sg, edit=1, forceElement=node)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment