src/bvhtoolbox/manipulate/renamejoints.py
def rename_joints(source_path, names_map, destination_path=None):
    """Rename joints in a BVH file using a dictionary and save the file.
    
    :param source_path: file path to bvh file.
    :type source_path: str
    :param names_map: Dictionary with old_name: new_name pairs.
    :type names_map: dict
    :param destination_path: Destination file path. If None BVH file is overwritten.
    :type destination_path: str
    :return: Whether writing the changed file was successful or not.
    :rtype: bool
    """
    file_type = os.path.splitext(source_path)[1].lower()
    if file_type != ".bvh":
        print("ERROR: File extension BVH expected for: {}".format(source_path))
        return False

    try:
        with open(source_path, mode='r') as file_handle:
            lines = file_handle.readlines()
    except FileNotFoundError:
        print("ERROR: file {} not found".format(source_path))
        return False

    prog = re.compile("(?<=JOINT|ROOT ).*$")
    
    for idx, line in enumerate(lines):
        if "MOTION\n" in line:
            break
        match = prog.search(line)
        if match:
            joint = match.group().lstrip()
            if joint in names_map:
                lines[idx] = line.replace(joint, names_map[joint])
    
    # If no destination file is specified, overwrite input file.
    if not destination_path:
        destination_path = source_path
    try:
        with open(destination_path, mode='w') as file_handle:
            file_handle.writelines(lines)
    except OSError:
        print("ERROR: Can't write to destination {}".format(destination_path))
        return False
    # If we reached this point, everything went well.
    return True
Rename joints in a BVH file using a dictionary and save the file.
    
    :param source_path: file path to bvh file.
    :type source_path: str
    :param names_map: Dictionary with old_name: new_name pairs.
    :type names_map: dict
    :param destination_path: Destination file path. If None BVH file is overwritten.
    :type destination_path: str
    :return: Whether writing the changed file was successful or not.
    :rtype: bool
    
Rename joints in a BVH file using a dictionary and save the file.
Rename joints in a BVH file using a dictionary and save the file .
def rename_joints(source_path, names_map, destination_path=None):
    file_type = os.path.splitext(source_path)[1].lower()
    if file_type != ".bvh":
        print("ERROR: File extension BVH expected for: {}".format(source_path))
        return False
    try:
        with open(source_path, mode='r') as file_handle:
            lines = file_handle.readlines()
    except FileNotFoundError:
        print("ERROR: file {} not found".format(source_path))
        return False
    prog = re.compile("(?<=JOINT|ROOT ).*$")
    for idx, line in enumerate(lines):
        if "MOTION\n" in line:
            break
        match = prog.search(line)
        if match:
            joint = match.group().lstrip()
            if joint in names_map:
                lines[idx] = line.replace(joint, names_map[joint])
    if not destination_path:
        destination_path = source_path
    try:
        with open(destination_path, mode='w') as file_handle:
            file_handle.writelines(lines)
    except OSError:
        print("ERROR: Can't write to destination {}".format(destination_path))
        return False
    return True
def rename_joints ( source_path , names_map , destination_path = None ) : file_type = os . path . splitext ( source_path ) [ 1 ] . lower ( ) if file_type != ".bvh" : print ( "ERROR: File extension BVH expected for: {}" . format ( source_path ) ) return False try : with open ( source_path , mode = 'r' ) as file_handle : lines = file_handle . readlines ( ) except FileNotFoundError : print ( "ERROR: file {} not found" . format ( source_path ) ) return False prog = re . compile ( "(?<=JOINT|ROOT ).*$" ) for idx , line in enumerate ( lines ) : if "MOTION\n" in line : break match = prog . search ( line ) if match : joint = match . group ( ) . lstrip ( ) if joint in names_map : lines [ idx ] = line . replace ( joint , names_map [ joint ] ) if not destination_path : destination_path = source_path try : with open ( destination_path , mode = 'w' ) as file_handle : file_handle . writelines ( lines ) except OSError : print ( "ERROR: Can't write to destination {}" . format ( destination_path ) ) return False return True
Rename joints in a BVH file using a dictionary and save the file.
Rename joints in a BVH file using a dictionary and save the file .
"""Rename joints in a BVH file using a dictionary and save the file.
    
    :param source_path: file path to bvh file.
    :type source_path: str
    :param names_map: Dictionary with old_name: new_name pairs.
    :type names_map: dict
    :param destination_path: Destination file path. If None BVH file is overwritten.
    :type destination_path: str
    :return: Whether writing the changed file was successful or not.
    :rtype: bool
    """ # If no destination file is specified, overwrite input file. # If we reached this point, everything went well.
Whether writing the changed file was successful or not.
Whether writing the changed file was successful or not .
bool
source_path
file path to bvh file.
file path to bvh file .
names_map
Dictionary with old_name: new_name pairs.
Dictionary with old_name : new_name pairs .
destination_path
Destination file path. If None BVH file is overwritten.
Destination file path . If None BVH file is overwritten .