+class Link:
+ def __init__(self, bone):
+ self._bone = bone
+ self.name = self._bone.name
+ self.index = None
+ self.parent_name = None
+ if self._bone.parent:
+ self.parent_name = self._bone.parent.name
+ self.parent = None
+ self.base = self._bone.head_local
+
+class Armature:
+ def __init__(self, arm):
+ self._armature = arm
+
+ self.links = [Link(b) for b in self._armature.bones]
+ for i, l in enumerate(self.links):
+ l.index = i
+
+ links_by_name = dict((l.name, l) for l in self.links)
+ for l in self.links:
+ if l.parent_name:
+ l.parent = links_by_name[l.parent_name]
+
+ def sort_links(self):
+ sorted_links = []
+ for l in self.links:
+ if l in sorted_links:
+ continue
+ if not l.parent:
+ sorted_links.append(l)
+ else:
+ chain = [l]
+ p = l.parent
+ while p and p not in sorted_links:
+ chain.append(p)
+ p = p.parent
+ sorted_links += reversed(chain)
+ self.links = sorted_links