Skip to content

Instantly share code, notes, and snippets.

@zewt
Created August 19, 2020 06:14
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zewt/73bd5f83927f3ccf20c32cf0440120a4 to your computer and use it in GitHub Desktop.
Save zewt/73bd5f83927f3ccf20c32cf0440120a4 to your computer and use it in GitHub Desktop.
Reset bind position to joint position
# Public domain
from pymel import core as pm
import re
def resetJointOnCluster(joint, skinClusterPreMatrixPlug):
inverseMatrix = joint.attr('worldInverseMatrix').get()
curMatrix = skinClusterPreMatrixPlug.get()
if curMatrix is not None:
different = any(abs(a-b) > 0.00001 for a, b in zip(inverseMatrix, curMatrix))
if not different:
return False
skinClusterPreMatrixPlug.set(inverseMatrix, type='matrix')
return True
def resetSkinnedJoints():
"""Reset skin deformation for selected skinClusters."""
selection = pm.ls(sl=True) or []
if not selection:
print 'Select one or more skinClusters or joints.'
return
skinClustersToReset = []
jointsReset = []
for node in selection:
if isinstance(node, pm.nodetypes.Joint):
# If a joint is selected, reset the joint on all skinClusters that it's attached to.
skinClustersOnJoint = pm.listConnections(node.attr('worldMatrix'), type='skinCluster', p=True) or []
for skinClusterMatrixConnection in skinClustersOnJoint:
skinCluster = skinClusterMatrixConnection.node()
idx = skinClusterMatrixConnection.index()
if resetJointOnCluster(node, skinCluster.attr('bindPreMatrix').elementByLogicalIndex(idx)):
jointsReset.append(node)
skinClustersToReset.append(skinCluster)
continue
if isinstance(node, pm.nodetypes.SkinCluster):
# Reset all joints attached to this skinCluster.
print 'Resetting joints on skinCluster %s' % node
for idx in node.attr('matrix').get(mi=True):
joints = pm.listConnections(node.attr('matrix').elementByLogicalIndex(idx), type='joint')
if not joints:
print 'No joints attached to %s' % node
continue
joint = joints[0]
if resetJointOnCluster(joint, node.attr('bindPreMatrix').elementByLogicalIndex(idx)):
jointsReset.append(joint)
skinClustersToReset.append(node)
else:
print 'Selected node %s isn\'t a skinCluster or a joint' % (node)
continue
print 'Reset %i joints: %s' % (len(jointsReset), ', '.join(node.nodeName() for node in jointsReset))
resetSkinnedJoints()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment