src/bvhtoolbox/convert/csv2bvh.py
def csv2bvh_string(hierarchy_file, position_file, rotation_file, scale=1.0):
    """ Compose the contents of a bvh file from 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: String that can be written to BVH file or build BvhTree from.
    :rtype: str
    """
    joint_info = get_hierarchy_data(hierarchy_file)
    
    # Get root positions.
    root_name = get_root_name(joint_info)
    pos_df, positions = get_transform_data(position_file)
    positions *= scale
    root_pos_df = [channel for channel in pos_df if root_name == channel.split('.')[0]]
    if not root_pos_df:  # Make sure root is in position data.
        raise Exception("ERROR: No position data found in {} for hierarchy's root '{}'.\n"
                        "Make sure the names match. They are case-sensitive.".format(position_file, root_name))
    root_pos_channels = [channel.split('.')[1].upper() + "position" for channel in root_pos_df]
    root_pos_indices = [pos_df.index(df) for df in root_pos_df]  # Get data indices for root position df.
    root_pos_data = positions[:, root_pos_indices]
    n_frames = len(root_pos_data)
    
    # Get joint rotations.
    rot_df, rotations = get_transform_data(rotation_file)
    # Make sure there are as many frames as in position data.
    if len(rotations) != n_frames:
        raise Exception("ERROR: Mismatch of frame count between positions and rotations!")
    rot_joint_names = df_to_joints(rot_df)
    
    # Make sure there's rotation data for each joint that is not an end site.
    non_leaf_nodes = sorted([joint for joint in joint_info if joint_info[joint]['children']])
    if rot_joint_names != non_leaf_nodes:
        missing_joint_data = set(non_leaf_nodes).difference(rot_joint_names)
        raise Exception("ERROR: No rotation data found for: {}".format(", ".join(missing_joint_data)))
    
    # Make HIERARCHY section.
    rot_channels = _df_to_channels(rot_df)
    _update_channels(joint_info, root_pos_channels, rot_channels)
    hierarchy_string = _get_hierarchy_string(joint_info)
    
    # Make MOTION section.
    # Let's just assume the degrees of freedom are in the correct order (successive) for now.
    # Ignore time column (index=0). # Todo: Don't assume.
    motion_data = np.concatenate((root_pos_data, rotations[:, 1:]), axis=1)
    frame_time = _get_frame_time(rotations[:, rot_df.index('time')])
    motion_string = _get_motion_string(n_frames, frame_time, motion_data)
    
    bvh_string = hierarchy_string + motion_string
    return bvh_string
 Compose the contents of a bvh file from 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: String that can be written to BVH file or build BvhTree from.
    :rtype: str
    
Compose the contents of a bvh file from CSV files.
Compose the contents of a bvh file from CSV files .
def csv2bvh_string(hierarchy_file, position_file, rotation_file, scale=1.0):
    joint_info = get_hierarchy_data(hierarchy_file)
    root_name = get_root_name(joint_info)
    pos_df, positions = get_transform_data(position_file)
    positions *= scale
    root_pos_df = [channel for channel in pos_df if root_name == channel.split('.')[0]]
    if not root_pos_df:  
        raise Exception("ERROR: No position data found in {} for hierarchy's root '{}'.\n"
                        "Make sure the names match. They are case-sensitive.".format(position_file, root_name))
    root_pos_channels = [channel.split('.')[1].upper() + "position" for channel in root_pos_df]
    root_pos_indices = [pos_df.index(df) for df in root_pos_df]  
    root_pos_data = positions[:, root_pos_indices]
    n_frames = len(root_pos_data)
    rot_df, rotations = get_transform_data(rotation_file)
    if len(rotations) != n_frames:
        raise Exception("ERROR: Mismatch of frame count between positions and rotations!")
    rot_joint_names = df_to_joints(rot_df)
    non_leaf_nodes = sorted([joint for joint in joint_info if joint_info[joint]['children']])
    if rot_joint_names != non_leaf_nodes:
        missing_joint_data = set(non_leaf_nodes).difference(rot_joint_names)
        raise Exception("ERROR: No rotation data found for: {}".format(", ".join(missing_joint_data)))
    rot_channels = _df_to_channels(rot_df)
    _update_channels(joint_info, root_pos_channels, rot_channels)
    hierarchy_string = _get_hierarchy_string(joint_info)
    motion_data = np.concatenate((root_pos_data, rotations[:, 1:]), axis=1)
    frame_time = _get_frame_time(rotations[:, rot_df.index('time')])
    motion_string = _get_motion_string(n_frames, frame_time, motion_data)
    bvh_string = hierarchy_string + motion_string
    return bvh_string
def csv2bvh_string ( hierarchy_file , position_file , rotation_file , scale = 1.0 ) : joint_info = get_hierarchy_data ( hierarchy_file ) root_name = get_root_name ( joint_info ) pos_df , positions = get_transform_data ( position_file ) positions *= scale root_pos_df = [ channel for channel in pos_df if root_name == channel . split ( '.' ) [ 0 ] ] if not root_pos_df : raise Exception ( "ERROR: No position data found in {} for hierarchy's root '{}'.\n" "Make sure the names match. They are case-sensitive." . format ( position_file , root_name ) ) root_pos_channels = [ channel . split ( '.' ) [ 1 ] . upper ( ) + "position" for channel in root_pos_df ] root_pos_indices = [ pos_df . index ( df ) for df in root_pos_df ] root_pos_data = positions [ : , root_pos_indices ] n_frames = len ( root_pos_data ) rot_df , rotations = get_transform_data ( rotation_file ) if len ( rotations ) != n_frames : raise Exception ( "ERROR: Mismatch of frame count between positions and rotations!" ) rot_joint_names = df_to_joints ( rot_df ) non_leaf_nodes = sorted ( [ joint for joint in joint_info if joint_info [ joint ] [ 'children' ] ] ) if rot_joint_names != non_leaf_nodes : missing_joint_data = set ( non_leaf_nodes ) . difference ( rot_joint_names ) raise Exception ( "ERROR: No rotation data found for: {}" . format ( ", " . join ( missing_joint_data ) ) ) rot_channels = _df_to_channels ( rot_df ) _update_channels ( joint_info , root_pos_channels , rot_channels ) hierarchy_string = _get_hierarchy_string ( joint_info ) motion_data = np . concatenate ( ( root_pos_data , rotations [ : , 1 : ] ) , axis = 1 ) frame_time = _get_frame_time ( rotations [ : , rot_df . index ( 'time' ) ] ) motion_string = _get_motion_string ( n_frames , frame_time , motion_data ) bvh_string = hierarchy_string + motion_string return bvh_string
Compose the contents of a bvh file from CSV files.
Compose the contents of a bvh file from CSV files .
""" Compose the contents of a bvh file from 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: String that can be written to BVH file or build BvhTree from.
    :rtype: str
    """ # Get root positions. # Make sure root is in position data. # Get data indices for root position df. # Get joint rotations. # Make sure there are as many frames as in position data. # Make sure there's rotation data for each joint that is not an end site. # Make HIERARCHY section. # Make MOTION section. # Let's just assume the degrees of freedom are in the correct order (successive) for now. # Ignore time column (index=0). # Todo: Don't assume.
String that can be written to BVH file or build BvhTree from.
String that can be written to BVH file or build BvhTree from .
str
hierarchy_file
CSV file path containing hierarchy.
CSV file path containing hierarchy .
position_file
CSV file path to positions.
CSV file path to positions .
rotation_file
CSV file path to rotations.
CSV file path to rotations .
scale
Scale factor for root position and offset values.
Scale factor for root position and offset values .