Skip to content

Instantly share code, notes, and snippets.

@BigRoy
Last active January 16, 2023 11:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BigRoy/3bda6741a323aef6c99f899439aeb2d0 to your computer and use it in GitHub Desktop.
Save BigRoy/3bda6741a323aef6c99f899439aeb2d0 to your computer and use it in GitHub Desktop.
Maya set shading node classification on existing nodes like `maya.cmds.shadingNode` does for creation of new nodes
from maya import cmds
def asShadingNode(node, **kwargs):
"""Set shading node classification on existing nodes.
Like `maya.cmds.shadingNode` does on creation of nodes
this command can add or remove a shading node classification.
It matches the same argument names of `maya.cmds.shadingNode`
like `asShader` and `asLight`.
Examples:
# Add asShader classification
>>> asShadingNode(node, asShader=True)
# Remove asShader classification
>>> asShadingNode(node, asShader=False)
"""
mapping = {
"asShader": ":defaultShaderList1.s",
"asTexture": ":defaultTextureList1.tx",
"asLight": ":lightList1.l",
"asPostProcess": ":postProcessList1.p",
"asUtility": ":defaultRenderUtilityList1.u",
"asRendering": ":defaultRenderingList1.r"
}
for key, value in kwargs.items():
if key not in mapping:
raise NotImplementedError("Key not supported for shading node type: {}".format(key))
dest = mapping[key]
src = node + ".msg"
if value:
cmds.connectAttr(src, dest, nextAvailable=True)
else:
cmds.disconnectAttr(src, dest, nextAvailable=True)
# Using shading node directly on creation
cmds.shadingNode("blinn", asShader=True)
# The equivalent on an existing node using this function
material = cmds.createNode("blinn")
asShadingNode(material, asShader=True)
# Or remove the classification on an existing node
asShadingNode(material, asShader=False)
@BigRoy
Copy link
Author

BigRoy commented Jan 16, 2023

The equivalent of this could actually be achieved by regularly using the maya.cmds.shadingNode command but passing it the shared=True flag. As such the above code is redundant.

However, you'll need to still pass it the node type explicitly. For example to mark it as a texture.

maya.cmds.shadingNode(node_type, shared=True, asTexture=True, name=node_name)

More details on the Shared flag in the maya documentation

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