Skip to content

Instantly share code, notes, and snippets.

@davidlatwe
Last active August 21, 2020 06:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidlatwe/b1b0af462eb7920849b8df1182c3061f to your computer and use it in GitHub Desktop.
Save davidlatwe/b1b0af462eb7920849b8df1182c3061f to your computer and use it in GitHub Desktop.
Maya command `sets -forceElement` failed cases
"""
Maya command `sets -forceElement` failed cases
"""
from maya import cmds
CUBE = None
MESH = None
PHONG_SG = None
def _setup():
"""Scene setup for testing
"""
global CUBE, MESH, PHONG_SG
cmds.file(new=True, force=True)
CUBE = cmds.polyCube(constructionHistory=False)[0]
MESH = cmds.listRelatives(CUBE, shapes=True)[0]
phong = cmds.shadingNode("phong", asShader=True)
PHONG_SG = cmds.createNode("shadingEngine", name="phong1SG")
cmds.connectAttr(phong + ".outColor",
PHONG_SG + ".surfaceShader",
force=True)
cmds.connectAttr(PHONG_SG + ".partition",
":renderPartition.sets",
nextAvailable=True)
cmds.setAttr(phong + ".color", 1, 0, 0)
def duplicated_set_member():
"""
pCubeShape1 phong1SG
.------------------------. .---------------------.
| | | |
| - instObjGroups[0] | .---> @ - dagSetMembers[0] |
| + objectGroups[1] @ ---*----> @ - dagSetMembers[1] |
| | | |
"""
cmds.sets(CUBE + ".f[0]", forceElement=PHONG_SG)
# Re-produce duplicated objectGroup connections
conns = cmds.listConnections(MESH,
source=False,
destination=True,
plugs=True,
connections=True,
type="shadingEngine")
for SRC, dst in zip(conns[::2], conns[1::2]):
if dst.startswith(PHONG_SG):
cmds.connectAttr(SRC, PHONG_SG + ".dagSetMembers[1]")
break
# RuntimeError raised.
cmds.sets("pCubeShape1.f[0]", forceElement="initialShadingGroup")
def disconnect_renderpartition_face():
"""Not connected to renderPartition (Face assign)
"""
# Face assign
cmds.sets(CUBE + ".f[0]", forceElement=PHONG_SG)
# Disconnect renderPartition
cmds.disconnectAttr(PHONG_SG + ".partition",
":renderPartition.sets",
nextAvailable=True)
# Shader not changed, and NO error raised !
cmds.sets("pCubeShape1.f[0]", forceElement="initialShadingGroup")
def disconnect_renderpartition_object():
"""Not connected to renderPartition (Object assign)
"""
# Object assign
cmds.sets(CUBE, forceElement=PHONG_SG)
# Disconnect renderPartition
cmds.disconnectAttr(PHONG_SG + ".partition",
":renderPartition.sets",
nextAvailable=True)
# Shader not changed, and RuntimeError raised.
cmds.sets("pCubeShape1", forceElement="initialShadingGroup")
#
# Run test
#
# Case 1
_setup() # Don't forget to reset
cmds.evalDeferred("duplicated_set_member()")
# Case 2
_setup() # Don't forget to reset
cmds.evalDeferred("disconnect_renderpartition_face()")
# Case 3
_setup() # Don't forget to reset
cmds.evalDeferred("disconnect_renderpartition_object()")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment