コード例 #1
0
def bvh2xaf(bvh_filepath, dst_filepath=None, scale=1.0):
    """ Converts a BVH file into the Cal3D XAF animation file format.

    :param bvh_filepath: File path for BVH source.
    :type bvh_filepath: str
    :param dst_filepath: File path for destination Cal3D animation file (XAF).
    :type dst_filepath: str
    :param scale: Scale factor for root translation and offset values.
    :type scale: float
    :return: If the conversion was successful or not.
    :rtype: bool
    """
    with open(bvh_filepath) as file_handle:
        mocap = BvhTree(file_handle.read())

    duration = (mocap.nframes - 1) * mocap.frame_time
    joint_names = mocap.get_joints_names()
    n_tracks = len(joint_names)

    xml_root = XmlTree.Element("ANIMATION")
    xml_root.set('VERSION', '1100')
    xml_root.set('MAGIC', 'XAF')
    xml_root.set('DURATION', str(duration))
    xml_root.set('NUMTRACKS', str(n_tracks))
    comment = XmlTree.Comment('Converted from {}'.format(
        os.path.basename(bvh_filepath)))
    xml_root.append(comment)
    # Use map to compute tracks.
    tracks = list(
        map(get_track, itertools.repeat(mocap), joint_names,
            itertools.repeat(scale)))
    # Extend tracks to xml_root as children.
    xml_root.extend(tracks)
    # Add indentation.
    xml_str = prettify(xml_root)

    if not dst_filepath:
        dst_filepath = bvh_filepath[:-3] + 'xaf'
    try:
        with open(dst_filepath, 'w') as file_handle:
            file_handle.write(xml_str)
        return True
    except IOError as e:
        print("ERROR({}): Could not write to file {}.\n"
              "Make sure you have writing permissions.\n".format(
                  e.errno, dst_filepath))
        return False
コード例 #2
0
def bvh2xsf(bvh_filepath, dst_filepath=None, scale=1.0):
    """Converts a BVH file into the Cal3D XSF skeleton file format.

    :param bvh_filepath: File path for BVH source.
    :type bvh_filepath: str
    :param dst_filepath: File path for destination Cal3D skeleton file (XSF).
    :type dst_filepath: str
    :param scale: Scale factor for root translation and offset values.
    :type scale: float
    """
    try:
        with open(bvh_filepath) as file_handle:
            mocap = BvhTree(file_handle.read())
    except OSError as e:
        print("ERROR:", e)
        return False

    joints = mocap.get_joints_names(end_sites=True)

    xml_root = XmlTree.Element("SKELETON")
    xml_root.set('MAGIC', 'XSF')
    xml_root.set('VERSION', '1100')
    xml_root.set('NUMBONES', str(len(joints)))
    comment = XmlTree.Comment('Converted from {}'.format(
        os.path.basename(bvh_filepath)))
    xml_root.append(comment)
    # Use map to compute tracks.
    bones = list(
        map(get_bone_xml, itertools.repeat(mocap), joints,
            itertools.repeat(scale)))
    # Extend tracks to xml_root as children.
    xml_root.extend(bones)
    # Add indentation.
    xml_str = prettify(xml_root)

    if not dst_filepath:
        dst_filepath = bvh_filepath[:-3] + 'xsf'
    try:
        with open(dst_filepath, 'w') as file_handle:
            file_handle.write(xml_str)
    except IOError as e:
        print("ERROR({}): Could not write to file {}.\n"
              "Make sure you have writing permissions.\n".format(
                  e.errno, dst_filepath))
        return False
    return True