Skip to content

Instantly share code, notes, and snippets.

@AndresMWeber
Last active February 9, 2017 20:36
Show Gist options
  • Save AndresMWeber/7abad6945c2e24f1809f6eb596674273 to your computer and use it in GitHub Desktop.
Save AndresMWeber/7abad6945c2e24f1809f6eb596674273 to your computer and use it in GitHub Desktop.
Compares two joints hierarchies in Maya and returns the differences between the two hierarchies as a keyed dictionary
import pymel.core as pm
import re
from pprint import pprint
def compare_joint_hierarchies():
report = {}
top_group_a, top_group_b = pm.ls(sl=True)[:2]
report['top_group_a'] = str(top_group_a.name())
report['top_group_b'] = str(top_group_b.name())
joints_a = joint_hierarchy_status(top_group_a)
joints_b = joint_hierarchy_status(top_group_b)
report['top_group_a_joints'] = 'Number of joints %d' % len([convert_to_shortname(joint) for joint in joints_a])
report['top_group_b_joints'] = 'Number of joints %d' % len([convert_to_shortname(joint) for joint in joints_b])
bigger, smaller = (max(joints_a, joints_b), min(joints_a, joints_b))
report['smaller_top_group'] = report['top_group_a'] if smaller == joints_a else report['top_group_b']
report['bigger_top_group'] = report['top_group_a'] if bigger == joints_a else report['top_group_b']
difference_smaller = list()
for joint_a in smaller:
for joint_b in bigger:
if convert_to_shortname(joint_a) in convert_to_shortname(joint_b):
bigger.remove(joint_b)
break
else:
difference_smaller.append(joint_a)
report['zJoints missing from bigger'] = [convert_to_shortname(joint) for joint in bigger]
report['zjoints missing from smaller'] = [convert_to_shortname(joint) for joint in difference_smaller]
print ('\n\n---------------------------HIERARCHY REPORT BELOW---------------------------\n\n')
return report
def joint_hierarchy_status(top_group):
joints = top_group.listRelatives(ad=True, type='joint')
if isinstance(top_group, pm.nt.Joint):
joints.append(top_group)
joints.sort(key=natural_keys)
return joints
def natural_keys(text):
'''
alist.sort(key=natural_keys) sorts in human order
http://nedbatchelder.com/blog/200712/human_sorting.html
(See Toothy's implementation in the comments)
'''
return [ int(c) for c in [f for f in re.split('(\d+)', text.name()) if f.isdigit()] ]
def convert_to_shortname(node):
return str(node.split(':')[-1].split('|')[-1])
pprint(compare_joint_hierarchies())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment