Skip to content

Instantly share code, notes, and snippets.

@BigRoy
Created January 12, 2022 00:36
Show Gist options
  • Save BigRoy/3f1825c429a0a9e1c8c922912c46011e to your computer and use it in GitHub Desktop.
Save BigRoy/3f1825c429a0a9e1c8c922912c46011e to your computer and use it in GitHub Desktop.
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