Skip to content

Instantly share code, notes, and snippets.

@mfessenden
Last active February 26, 2024 03:14
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save mfessenden/71e2166c16fbcf7e8cf8 to your computer and use it in GitHub Desktop.
Save mfessenden/71e2166c16fbcf7e8cf8 to your computer and use it in GitHub Desktop.
Maya command to create a lambert shader & shading group node named for the object
import maya.cmds as mc
def applyMaterial(node):
if mc.objExists(node):
shd = mc.shadingNode('lambert', name="%s_lambert" % node, asShader=True)
shdSG = mc.sets(name='%sSG' % shd, empty=True, renderable=True, noSurfaceShader=True)
mc.connectAttr('%s.outColor' % shd, '%s.surfaceShader' % shdSG)
mc.sets(node, e=True, forceElement=shdSG)
applyMaterial("pSphere1")
@BigRoy
Copy link

BigRoy commented Feb 17, 2021

For anyone else who came here through google search in a lazy moment and just wanted the code quickly create a material with the shading engine here's the code for solely the creation without assigning or checking whether material exists.

from maya import cmds

def create_shader(name, node_type="lambert"):
    material = cmds.shadingNode(node_type, name=name, asShader=True)
    sg = cmds.sets(name="%sSG" % name, empty=True, renderable=True, noSurfaceShader=True)
    cmds.connectAttr("%s.outColor" % material, "%s.surfaceShader" % sg)
    return material, sg

And thus for example to create the shader, change the color and then assign it:

color = [1, 0, 0]
meshes = cmds.ls(selection=True, dag=True, type="mesh", noIntermediate=True)
mtl, sg = create_shader("helloworld_MTL")
cmds.setAttr(mtl + ".color", color[0], color[1], color[2], type="double3")
cmds.sets(meshes, forceElement=sg)

Just putting that here so that on my next google search I can be even lazier!

@jomaro110
Copy link

Many thanks

@saliwillient
Copy link

Many thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment