Last active
January 16, 2023 11:07
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The equivalent of this could actually be achieved by regularly using the
maya.cmds.shadingNode
command but passing it theshared=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.
More details on the Shared flag in the maya documentation