Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Maya check Cycle Errors more thoroughly where maya.cmds.cycleCheck(list=True) fails.
from maya import cmds
import contextlib
@contextlib.contextmanager
def capture_cycle_errors():
"""Capture the plugs in Cycle Error warnings.
During this context "Cycle on 'x' may not evaluate as expected"
are captured and 'x' is extracted as plugs in the yielded list.
"""
import maya.OpenMaya as om
plugs = []
def _callback(nativeMsg, messageType, data):
if messageType != om.MCommandMessage.kWarning:
# We only care about warnings
return
if not nativeMsg.startswith("Cycle on '"):
# Consider only Cycle on messages
return
plug = nativeMsg.split("'")[1]
plugs.append(plug)
id = om.MCommandMessage.addCommandOutputCallback(_callback, None)
# Make sure evaluation time cycle check is enabled
original = cmds.cycleCheck(query=True, evaluation=True)
cmds.cycleCheck(evaluation=True)
try:
yield plugs
finally:
om.MMessage.removeCallback(id)
cmds.cycleCheck(evaluation=original)
def cycle_check(nodes):
# Include history for the nodes since it makes
# the following check much more accurate to
# correctly log during the capture block
nodes = list(set(cmds.listHistory(nodes)))
with capture_cycle_errors() as plugs:
# Force the full DG evaluation
cmds.dgdirty(nodes)
cmds.dgeval(nodes)
return plugs
# Test
cmds.file(new=True, force=True)
cmds.loadPlugin("matrixNodes", quiet=True)
a = cmds.createNode("transform", name="a")
b = cmds.createNode("transform", name="b")
decompose = cmds.createNode("decomposeMatrix")
cmds.connectAttr(a + ".worldMatrix[0]", decompose + ".inputMatrix")
cmds.connectAttr(decompose + ".outputTranslate", b + ".translate")
cmds.connectAttr(b + ".worldMatrix[0]", a + ".offsetParentMatrix")
nodes = [a, b]
plugs = cycle_check(nodes)
# Our check:
print(">> cycle_check(nodes)")
print(plugs)
# THIS FINDS THE PLUG!
# maya.cmds.cycleCheck
print(">> maya.cmds.cycleCheck(all=True, list=True)")
print(cmds.cycleCheck(all=True, list=True))
# MAYA FAILS TO FIND THE PLUG WITH THIS.
@BigRoy
Copy link
Author

BigRoy commented Jan 12, 2022

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