コード例 #1
0
def bvhfile_offset_angles(src_filepath, angle_offsets, dst_filepath=None):
    """
    :param src_filepath: File path for BVH source.
    :type src_filepath: str
    :param angle_offsets: A dictionary containing joint names as keys and euler angles in degrees as values.
    The order of the euler angles needs to be the same as the channel order for this joint in the BVH file.
    If not all 3 rotation channels are present for a joint, the missing channels will get appended.
    :type angle_offsets: dict
    :param dst_filepath: File path for destination BVH file.
    :type dst_filepath: str
    :return: If reading and writing the BVH files was successful or not.
    :rtype: bool
    """
    if not angle_offsets:
        print("WARNING: No rotation offsets. Aborting processing of file",
              src_filepath)
        return False

    try:
        with open(src_filepath) as file_handle:
            mocap = BvhTree(file_handle.read())
    except OSError as e:
        print("ERROR:", e)
        return False

    add_angle_offsets(mocap, angle_offsets)

    if not dst_filepath:
        dst_filepath = src_filepath
    res = mocap.write_file(dst_filepath)
    return res
コード例 #2
0
def bvh2egg(bvh_filepath, dst_filepath=None, scale=1.0):
    """ Converts a BVH file into the Panda3D egg animation file format.
    
    :param bvh_filepath: File path for BVH source.
    :type bvh_filepath: str
    :param dst_filepath: File path for destination Panda3D Egg file.
    :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())
    
    # Even though the BVH is Y-up, because the skeleton got rotated by 90 degrees around X, Z is now up.
    coords_up = '<CoordinateSystem> { Z-up }\n'
    comment = '<Comment> {{ Converted from {0} }}\n'.format(os.path.basename(bvh_filepath))
    init_table_str = '<Table> {\n  <Bundle> Armature {\n    <Table> "<skeleton>" {\n'
    
    egg_str = coords_up + comment + init_table_str + get_egg_anim_tables(mocap, scale=scale)
    egg_str = close_tables(egg_str)
    
    if not dst_filepath:
        dst_filepath = bvh_filepath[:-3] + 'egg'
    try:
        with open(dst_filepath, 'w') as file_handle:
            file_handle.write(egg_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
コード例 #3
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
コード例 #4
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
コード例 #5
0
def bvh2csv(bvh_filepath,
            dst_dirpath=None,
            scale=1.0,
            export_rotation=True,
            export_position=True,
            export_hierarchy=True,
            end_sites=True):
    """Converts a BVH file into CSV file format.

    :param bvh_filepath: File path for BVH source.
    :type bvh_filepath: str
    :param dst_dirpath: Folder path for destination CSV files.
    :type dst_dirpath: str
    :param scale: Scale factor for root position and offset values.
    :type scale: float
    :param export_rotation: Output rotation CSV file.
    :type export_rotation: bool
    :param export_position: Output world space position CSV file.
    :type export_position: bool
    :param export_hierarchy: Output hierarchy CSV file.
    :type export_hierarchy: bool
    :param end_sites: Include BVH End Sites in position CSV.
    :type end_sites: bool
    :return: If the conversion was successful or not.
    :rtype: bool
    """
    try:
        with open(bvh_filepath) as file_handle:
            mocap = BvhTree(file_handle.read())
    except IOError as e:
        print("ERROR {}: Could not open file".format(e.errno), bvh_filepath)
        return False

    # Assume everything works and only set False on error.
    pos_success = True
    rot_success = True
    hierarchy_success = True
    if not dst_dirpath:
        dst_filepath = os.path.join(os.path.dirname(bvh_filepath),
                                    os.path.basename(bvh_filepath)[:-4])
    else:
        # If the specified path doesn't yet exist, create it.
        if not os.path.exists(dst_dirpath):
            os.mkdir(dst_dirpath)
        dst_filepath = os.path.join(dst_dirpath,
                                    os.path.basename(bvh_filepath)[:-4])
    if export_position:
        pos_success = write_joint_positions(mocap, dst_filepath + '_pos.csv',
                                            scale, end_sites)
    if export_rotation:
        rot_success = write_joint_rotations(mocap, dst_filepath + '_rot.csv')
    if export_hierarchy:
        hierarchy_success = write_joint_hierarchy(
            mocap, dst_filepath + '_hierarchy.csv', scale)

    n_succeeded = sum([pos_success, rot_success, hierarchy_success])
    return bool(n_succeeded)
コード例 #6
0
def get_bvh_tree(file_path):
    """Parse a BVH file and return it as Bvh object.
    
    :param file_path: file path to BVH file.
    :type file_path: str
    :return: BVH tree
    :rtype: bvhtree.BvhTree
    """
    with open(file_path) as file_handle:
        bvh_tree = BvhTree(file_handle.read())
    return bvh_tree
コード例 #7
0
def csv2bvhtree(hierarchy_file, position_file, rotation_file, scale=1.0):
    """ Get a BvhTree instance from input CSV files.

    :param hierarchy_file: CSV file path containing hierarchy. Columns: joint, parent, offset.x, offset.y, offset.z
    :type hierarchy_file: str
    :param position_file: CSV file path to positions.
    :type position_file: str
    :param rotation_file:CSV file path to rotations.
    :type rotation_file: str
    :param scale: Scale factor for root position and offset values.
    :type scale: float
    :return: BVHTree instance.
    :rtype: bvh.BvhTree
    """
    data = csv2bvh_string(hierarchy_file, position_file, rotation_file, scale)
    bvh_tree = BvhTree(data)
    return bvh_tree
コード例 #8
0

def convert_quaternion_to_bullet(q: Quaternion):
    return [q.x, q.y, q.z, q.w]


def convert_bvh_to_quaternion(bvh_q) -> Quaternion:
    return Quaternion(w=bvh_q[0], x=bvh_q[1], y=bvh_q[2], z=bvh_q[3])


def convert_bvh_to_bullet(bvh_q):
    return [bvh_q[1], bvh_q[2], bvh_q[3], bvh_q[0]]


with open(path) as file_handle:
    mocap = BvhTree(file_handle.read())
axes = 'rzxz'

leftLegFrames = get_quaternions(mocap, 'mixamorig:LeftLeg', axes)
leftFootFrames = get_quaternions(mocap, 'mixamorig:LeftFoot', axes)
leftToeBaseFrames = get_quaternions(mocap, 'mixamorig:LeftToeBase', axes)
rightUpLegFrames = get_quaternions(mocap, 'mixamorig:RightUpLeg', axes)
rightLegFrames = get_quaternions(mocap, 'mixamorig:RightLeg', axes)
rightFootFrames = get_quaternions(mocap, 'mixamorig:RightFoot', axes)
rightToeBaseFrames = get_quaternions(mocap, 'mixamorig:RightToeBase', axes)
spineFrames = get_quaternions(mocap, 'mixamorig:Spine', axes)
spine1Frames = get_quaternions(mocap, 'mixamorig:Spine1', axes)
spine2Frames = get_quaternions(mocap, 'mixamorig:Spine2', axes)
leftShoulderFrames = get_quaternions(mocap, 'mixamorig:LeftShoulder', axes)
leftArmFrames = get_quaternions(mocap, 'mixamorig:LeftArm', axes)
leftHandFrames = get_quaternions(mocap, 'mixamorig:LeftHand', axes)