Skip to content

Instantly share code, notes, and snippets.

@hoorayfor3d
Last active July 9, 2018 18:38
Show Gist options
  • Save hoorayfor3d/516ff28a9a979183c9de35e3bb5bd719 to your computer and use it in GitHub Desktop.
Save hoorayfor3d/516ff28a9a979183c9de35e3bb5bd719 to your computer and use it in GitHub Desktop.
import pymel.core as pmc
def fixupCyclicAnimLayers():
"""
This function finds animation layer nodes which are connected to themselves, disconnects them, and HOPEFULLY finds
the remaining unconnected animation curve, and plugs it back into the appropriate attribute.
:return: None
"""
# Get all our initial layer/curve data
animLayers = [layer for layer in pmc.ls(type=['animLayer']) if 'BaseAnimation' not in layer.name()]
animBlendNodes = pmc.ls(type=['animBlendNodeBase'])
animCurveNodes = pmc.ls(type=['animCurve'])
# Want to work on one layer at a time
for animLayer in animLayers:
for animBlend in [blendNode for blendNode in animBlendNodes if animLayer.name() in blendNode.name()]:
blendNodeShortName = animBlend.shortName().split(':')[-1].split('_{layerName}'.format(layerName=animLayer.name()))[0]
matchingCurves = [animCurve for animCurve in animCurveNodes if blendNodeShortName in animCurve.shortName()]
# Find layer nodes that are connected to layer nodes, which... shouldn't be happening.
connectedNodeAttrs = pmc.listConnections(animBlend, type='animBlendNodeBase',
plugs=True, d=True, s=False, c=True)
animBlendAttrs = []
for nodeAttr in connectedNodeAttrs:
# GTFO cyclic connections!
pmc.disconnectAttr(nodeAttr[0], nodeAttr[1])
# Need to track what we're connecting back to
animBlendAttrs.append(nodeAttr[1])
i = 0
for animCurve in matchingCurves:
connectedAttrs = pmc.listConnections(animCurve)
if len(connectedAttrs) == 0 or connectedAttrs is None:
try:
pmc.connectAttr(animCurve.output, animBlendAttrs[i])
except:
pass
i += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment