def get_bone_child(self, _bone): if Is.posebone(_bone): bone = _bone.bone if _bone.child: child = _bone.child.bone else: # No bone connected, so try to find a substitute child = self.get_closest_child(bone) else: bone = _bone child = None if len(bone.children) == 1: child = bone.children[0] elif bone.children: for bone_child in bone.children: if bone_child.use_connect: if (child is not None): child_dist = Get.distance(child.tail, bone.tail) active_dist = Get.distance(bone_child.tail, bone.tail) if (child_dist == active_dist): # both children tails same distance from bone # Just sort alphabetically child = sorted( (b.name, b) for b in [child, bone_child])[0][1] continue elif (child_dist < active_dist): # Previous is closes continue child = bone_child if child is None: # No bone connected, so try to find a substitute child = self.get_closest_child(bone) return (bone, child)
def get_closest_child(bone): if len(bone.children) == 1: return bone.children[0] elif bone.children: # Multiple children, None connected; search by distance children = dict() for bone_child in bone.children: dist = Get.distance(bone_child.head, bone.tail) if dist not in children: children[dist] = list() children[dist].append(bone_child) closest = children[sorted(children)[0]] if len(closest) == 1: return closest[0] else: # Multiple children same distance from bone; check tail distanxce children = dict() for bone_child in closest: dist = Get.distance(bone_child.tail, bone.tail) if dist not in children: children[dist] = list() children[dist].append(bone_child) closest = children[sorted(children)[0]] if len(closest) == 1: return closest[0] else: # All children tails same distance from bone # Just sort alphabetically return sorted((b.name, b) for b in closest)[0][1]