Exemple #1
0
def render_prt_ortho(out_path,
                     folder_name,
                     subject_name,
                     shs,
                     rndr,
                     rndr_uv,
                     im_size,
                     angl_step=4,
                     n_light=1,
                     pitch=[0],
                     yaw=(0, 360)):
    """
    out_path:
        output path of the rendered images and baked textures.
    folder_name:
        input path
    subject_name:
        subject name
    shs:
        ???
    rndr:
        ???
    rndr_uv:
        ???
    im_size:
        ???
    angle_step:
        ???
    n_light:
        ???
    pitch:
        ???

    """
    cam = Camera(width=im_size, height=im_size)
    cam.ortho_ratio = 0.4 * (512 / im_size)  # what does this mean?

    ###### range of the mesh visible is specified here
    # default: -100 ~ 100
    cam.near = -100
    cam.far = 100
    ######

    cam.sanity_check()

    # set path for obj, prt
    mesh_file = os.path.join(folder_name, subject_name + '.obj')
    if not os.path.exists(mesh_file):
        print('ERROR: obj file does not exist!!', mesh_file)
        return
    else:
        print('[HERE] will use obj file %s' % mesh_file)

    prt_file = os.path.join(folder_name, 'bounce', 'bounce0.txt')
    if not os.path.exists(prt_file):
        print('ERROR: prt file does not exist!!!', prt_file)
        return
    else:
        print('[HERE] will use prt file %s' % prt_file)

    face_prt_file = os.path.join(folder_name, 'bounce', 'face.npy')

    if args.use_prt:
        if not os.path.exists(face_prt_file):
            print('ERROR: face prt file does not exist!!!', prt_file)
            return
        else:
            print('[HERE] you have specified usr_prt.')
            print('[HERE] will use face_prt file %s' % face_prt_file)

    text_file = os.path.join(folder_name, subject_name + '.jpg')
    if not os.path.exists(text_file):
        text_file = os.path.join(folder_name, subject_name + '.png')
    if not os.path.exists(text_file):
        print('ERROR: dif file does not exist!!', text_file)
        return
    else:
        print('[HERE] will use texture file %s' % text_file)

    ### path setting done

    texture_image = cv2.imread(text_file)
    texture_image = cv2.cvtColor(texture_image, cv2.COLOR_BGR2RGB)

    vertices, faces, normals, faces_normals, textures, face_textures = load_obj_mesh(
        mesh_file, with_normal=True, with_texture=True)

    # vertices are of the shape [N_v, 3]
    vmin = vertices.min(
        0
    )  # so this has shape [3], and it denotes the min coord value in 3 dimensions.
    vmax = vertices.max(0)

    # up_axis is the axis that we are looking direction at!
    # but it seems to choose only from 1 and 2, i.e. y and z
    # up_axis = 1 if (vmax-vmin).argmax() == 1 else 2
    up_axis = args.up_axis  # always set to z
    # but there remains the problem of orientation (somehow the bikes are upside down)
    # I can confirm that orientation problem is caused by the generation of rotation matrices below
    # orientation is fixed below, by adding an additional a 180-degree rotations to the z axis.

    longest_axis = (vmax - vmin).argmax()

    # what is vmed?
    # this does scaling
    # why median not mean?
    vmed = np.median(vertices, 0)
    # the up_axis is set to have median as the mean of two farthes points.
    vmed[longest_axis] = 0.5 * (vmax[longest_axis] + vmin[longest_axis])
    # this scales the up_axis to 180?
    # called 'y_scale' supposedly because renderpeople faces up
    # why 180 ???
    # BECAUSE the near-far range is -100~100, so 180 takes 90% of the range!!!
    #y_scale = 180/(vmax[up_axis] - vmin[up_axis])
    longest_scale = 180 / (vmax[longest_axis] - vmin[longest_axis])

    # are these cameras?
    # these two lines normalizes the coordinates according to vmed and y_scale, by setting two normalization matrices.
    # original scaling was done by y_scale.
    # but for bikes, need to change to longest_axis scale.
    rndr.set_norm_mat(longest_scale, vmed)
    rndr_uv.set_norm_mat(longest_scale, vmed)

    tan, bitan = compute_tangent(vertices, faces, normals, textures,
                                 face_textures)

    ###### prt loading
    # this has a potential problem if the mesh is low-poly
    # a large area would have the same texture and the output would appear broken
    # using a all-one tensor instead gets rid of the prt effects
    prt = np.loadtxt(prt_file)  # what does this do?
    print(prt.shape)  # [???, 9]
    face_prt = np.load(face_prt_file)

    prt_mean = prt.mean()
    prt_var = ((prt - prt_mean)**2).mean()
    print('prt mean = %.8f, prt var = %.8f' % (prt_mean, prt_var))
    #print(face_prt)
    #print('face_prt shape = ', face_prt.shape) # same as faces.shape
    if not args.use_prt:
        prt = np.random.randn(*(prt.shape)) * 0.01 + args.pm
    ######

    rndr.set_mesh(vertices, faces, normals, faces_normals, textures,
                  face_textures, prt, face_prt, tan, bitan)
    rndr.set_albedo(texture_image)

    rndr_uv.set_mesh(vertices, faces, normals, faces_normals, textures,
                     face_textures, prt, face_prt, tan, bitan)
    rndr_uv.set_albedo(texture_image)

    os.makedirs(os.path.join(out_path, 'GEO', 'OBJ', subject_name),
                exist_ok=True)
    os.makedirs(os.path.join(out_path, 'PARAM', subject_name), exist_ok=True)
    os.makedirs(os.path.join(out_path, 'RENDER', subject_name), exist_ok=True)
    os.makedirs(os.path.join(out_path, 'MASK', subject_name), exist_ok=True)
    os.makedirs(os.path.join(out_path, 'UV_RENDER', subject_name),
                exist_ok=True)
    os.makedirs(os.path.join(out_path, 'UV_MASK', subject_name), exist_ok=True)
    os.makedirs(os.path.join(out_path, 'UV_POS', subject_name), exist_ok=True)
    os.makedirs(os.path.join(out_path, 'UV_NORMAL', subject_name),
                exist_ok=True)

    if not os.path.exists(os.path.join(out_path, 'val.txt')):
        f = open(os.path.join(out_path, 'val.txt'), 'w')
        f.close()

    # copy obj file
    cmd = 'cp %s %s' % (mesh_file,
                        os.path.join(out_path, 'GEO', 'OBJ', subject_name))
    print(cmd)
    os.system(cmd)

    for p in pitch:
        for y in tqdm(range(yaw[0], yaw[1], angl_step)):
            # y is the angle in degrees
            # y goes through 0~360 (integers)
            R = np.matmul(make_rotate(math.radians(p), 0, 0),
                          make_rotate(0, math.radians(y), 0))
            if up_axis == 2:
                R = np.matmul(
                    R, make_rotate(math.radians(90), 0, math.radians(180)))

            rndr.rot_matrix = R
            rndr_uv.rot_matrix = R
            rndr.set_camera(cam)
            rndr_uv.set_camera(cam)

            for j in range(n_light):
                sh_id = random.randint(0, shs.shape[0] - 1)
                sh = shs[sh_id]
                sh_angle = 0.2 * np.pi * (random.random() - 0.5)
                sh = rotateSH(sh, make_rotate(0, sh_angle, 0).T)

                dic = {
                    'sh': sh,
                    'ortho_ratio': cam.ortho_ratio,
                    'scale': longest_scale,
                    'center': vmed,
                    'R': R
                }

                rndr.set_sh(sh)
                rndr.analytic = False
                rndr.use_inverse_depth = False
                rndr.display()

                out_all_f = rndr.get_color(0)
                out_mask = out_all_f[:, :, 3]
                out_all_f = cv2.cvtColor(out_all_f, cv2.COLOR_RGBA2BGR)

                np.save(
                    os.path.join(out_path, 'PARAM', subject_name,
                                 '%d_%d_%02d.npy' % (y, p, j)), dic)
                cv2.imwrite(
                    os.path.join(out_path, 'RENDER', subject_name,
                                 '%d_%d_%02d.jpg' % (y, p, j)),
                    255.0 * out_all_f)
                cv2.imwrite(
                    os.path.join(out_path, 'MASK', subject_name,
                                 '%d_%d_%02d.png' % (y, p, j)),
                    255.0 * out_mask)

                rndr_uv.set_sh(sh)
                rndr_uv.analytic = False
                rndr_uv.use_inverse_depth = False
                rndr_uv.display()

                uv_color = rndr_uv.get_color(0)
                uv_color = cv2.cvtColor(uv_color, cv2.COLOR_RGBA2BGR)
                cv2.imwrite(
                    os.path.join(out_path, 'UV_RENDER', subject_name,
                                 '%d_%d_%02d.jpg' % (y, p, j)),
                    255.0 * uv_color)

                if y == 0 and j == 0 and p == pitch[0]:
                    uv_pos = rndr_uv.get_color(1)
                    uv_mask = uv_pos[:, :, 3]
                    cv2.imwrite(
                        os.path.join(out_path, 'UV_MASK', subject_name,
                                     '00.png'), 255.0 * uv_mask)

                    data = {
                        'default': uv_pos[:, :, :3]
                    }  # default is a reserved name
                    pyexr.write(
                        os.path.join(out_path, 'UV_POS', subject_name,
                                     '00.exr'), data)

                    uv_nml = rndr_uv.get_color(2)
                    uv_nml = cv2.cvtColor(uv_nml, cv2.COLOR_RGBA2BGR)
                    cv2.imwrite(
                        os.path.join(out_path, 'UV_NORMAL', subject_name,
                                     '00.png'), 255.0 * uv_nml)
Exemple #2
0
def render_prt_ortho(mesh_file,
                     prt,
                     face_prt,
                     out_path,
                     frame_start,
                     subject_name,
                     video_name,
                     shs,
                     rndr,
                     im_size=512,
                     angl_step=45,
                     n_light=1,
                     pitch=[0]):
    cam = Camera(width=im_size, height=im_size)
    cam.ortho_ratio = 0.4 * (512 / im_size)
    cam.near = -100
    cam.far = 100
    cam.sanity_check()

    vertices, faces, normals, faces_normals = load_obj_mesh(mesh_file,
                                                            with_normal=True,
                                                            with_texture=False)
    vmin = vertices.min(0)
    vmax = vertices.max(0)
    up_axis = 1 if (vmax - vmin).argmax() == 1 else 2

    vmed = np.median(vertices, 0)
    vmed[up_axis] = 0.5 * (vmax[up_axis] + vmin[up_axis])
    y_scale = 180 / (vmax[up_axis] - vmin[up_axis])

    rndr.set_norm_mat(y_scale, vmed)

    tan, bitan = compute_tangent(vertices, faces,
                                 normals)  #, textures, face_textures)
    #prt = np.loadtxt(prt_file)
    #face_prt = np.load(face_prt_file)
    rndr.set_mesh(vertices, faces, normals, faces_normals, None, None, prt,
                  face_prt, tan, bitan)

    del vertices, faces, normals, faces_normals

    os.makedirs(os.path.join(out_path, 'GEO', 'OBJ', subject_name, video_name),
                exist_ok=True)
    os.makedirs(os.path.join(out_path, 'PARAM', subject_name, video_name),
                exist_ok=True)
    os.makedirs(os.path.join(out_path, 'RENDER', subject_name, video_name),
                exist_ok=True)
    os.makedirs(os.path.join(out_path, 'MASK', subject_name, video_name),
                exist_ok=True)

    # copy obj file
    cmd = 'cp %s %s' % (mesh_file,
                        os.path.join(out_path, 'GEO', 'OBJ', subject_name,
                                     video_name))
    print(cmd)
    os.system(cmd)

    for p in pitch:
        for y in range(0, 360, angl_step):
            R = np.matmul(make_rotate(math.radians(p), 0, 0),
                          make_rotate(0, math.radians(y), 0))
            if up_axis == 2:
                R = np.matmul(R, make_rotate(math.radians(90), 0, 0))

            rndr.rot_matrix = R
            #rndr_uv.rot_matrix = R
            rndr.set_camera(cam)
            #rndr_uv.set_camera(cam)

            for j in range(n_light):
                sh_id = random.randint(0, shs.shape[0] - 1)
                sh = shs[sh_id]
                sh_angle = 0.2 * np.pi * (random.random() - 0.5)
                sh = rotateSH(sh, make_rotate(0, sh_angle, 0).T)

                dic = {
                    'sh': sh,
                    'ortho_ratio': cam.ortho_ratio,
                    'scale': y_scale,
                    'center': vmed,
                    'R': R
                }

                rndr.set_sh(sh)
                rndr.analytic = False
                rndr.use_inverse_depth = False
                rndr.display()

                out_all_f = rndr.get_color(0)
                out_mask = out_all_f[:, :, 3]
                out_all_f = cv2.cvtColor(out_all_f, cv2.COLOR_RGBA2BGR)

                np.save(
                    os.path.join(out_path, 'PARAM', subject_name, video_name,
                                 frame_start + '_%d_%d_%02d.npy' % (y, p, j)),
                    dic)
                cv2.imwrite(
                    os.path.join(out_path, 'RENDER', subject_name, video_name,
                                 frame_start + '_%d_%d_%02d.jpg' % (y, p, j)),
                    255.0 * out_all_f)
                cv2.imwrite(
                    os.path.join(out_path, 'MASK', subject_name, video_name,
                                 frame_start + '_%d_%d_%02d.png' % (y, p, j)),
                    255.0 * out_mask)
                del out_all_f, out_mask, dic
Exemple #3
0
def render_prt_ortho(out_path,
                     folder_name,
                     subject_name,
                     tex_name,
                     shs,
                     rndr,
                     rndr_uv,
                     im_size,
                     angl_step=4,
                     n_light=1,
                     pitch=[0]):
    cam = Camera(width=im_size, height=im_size)
    cam.ortho_ratio = 0.4 * (512 / im_size)
    cam.near = -100
    cam.far = 100
    cam.sanity_check()

    # set path for obj, prt
    mesh_file = os.path.join(folder_name, subject_name + '.obj')
    if not os.path.exists(mesh_file):
        print('ERROR: obj file does not exist!!', mesh_file)
        return
    prt_file = os.path.join(folder_name, 'bounce', 'bounce0.txt')
    if not os.path.exists(prt_file):
        print('ERROR: prt file does not exist!!!', prt_file)
        return
    face_prt_file = os.path.join(folder_name, 'bounce', 'face.npy')
    if not os.path.exists(face_prt_file):
        print('ERROR: face prt file does not exist!!!', prt_file)
        return
    text_file = os.path.join(folder_name, tex_name)
    if not os.path.exists(text_file):
        print('ERROR: dif file does not exist!!', text_file)
        return

    texture_image = cv2.imread(text_file)
    texture_image = cv2.cvtColor(texture_image, cv2.COLOR_BGR2RGB)

    vertices, faces, normals, faces_normals, textures, face_textures = load_obj_mesh(
        mesh_file, with_normal=True, with_texture=True)
    vmin = vertices.min(0)
    vmax = vertices.max(0)
    up_axis = 1 if (vmax - vmin).argmax() == 1 else 2

    vmed = np.median(vertices, 0)
    vmed[up_axis] = 0.5 * (vmax[up_axis] + vmin[up_axis])
    y_scale = 180 / (vmax[up_axis] - vmin[up_axis])

    rndr.set_norm_mat(y_scale, vmed)
    rndr_uv.set_norm_mat(y_scale, vmed)

    tan, bitan = compute_tangent(vertices, faces, normals, textures,
                                 face_textures)
    prt = np.loadtxt(prt_file)
    face_prt = np.load(face_prt_file)
    rndr.set_mesh(vertices, faces, normals, faces_normals, textures,
                  face_textures, prt, face_prt, tan, bitan)
    rndr.set_albedo(texture_image)

    rndr_uv.set_mesh(vertices, faces, normals, faces_normals, textures,
                     face_textures, prt, face_prt, tan, bitan)
    rndr_uv.set_albedo(texture_image)

    os.makedirs(os.path.join(out_path, 'GEO', 'OBJ', subject_name),
                exist_ok=True)
    os.makedirs(os.path.join(out_path, 'PARAM', subject_name), exist_ok=True)
    os.makedirs(os.path.join(out_path, 'RENDER', subject_name), exist_ok=True)
    os.makedirs(os.path.join(out_path, 'MASK', subject_name), exist_ok=True)
    os.makedirs(os.path.join(out_path, 'UV_RENDER', subject_name),
                exist_ok=True)
    os.makedirs(os.path.join(out_path, 'UV_MASK', subject_name), exist_ok=True)
    os.makedirs(os.path.join(out_path, 'UV_POS', subject_name), exist_ok=True)
    os.makedirs(os.path.join(out_path, 'UV_NORMAL', subject_name),
                exist_ok=True)

    if not os.path.exists(os.path.join(out_path, 'val.txt')):
        f = open(os.path.join(out_path, 'val.txt'), 'w')
        f.close()

    # copy obj file
    cmd = 'cp %s %s' % (mesh_file,
                        os.path.join(out_path, 'GEO', 'OBJ', subject_name))
    print(cmd)
    os.system(cmd)

    for p in pitch:
        for y in tqdm(range(0, 360, angl_step)):
            R = np.matmul(make_rotate(math.radians(p), 0, 0),
                          make_rotate(0, math.radians(y), 0))
            if up_axis == 2:
                R = np.matmul(R, make_rotate(math.radians(90), 0, 0))

            rndr.rot_matrix = R
            rndr_uv.rot_matrix = R
            rndr.set_camera(cam)
            rndr_uv.set_camera(cam)

            for j in range(n_light):
                sh_id = random.randint(0, shs.shape[0] - 1)
                sh = shs[sh_id]
                sh_angle = 0.2 * np.pi * (random.random() - 0.5)
                sh = rotateSH(sh, make_rotate(0, sh_angle, 0).T)

                dic = {
                    'sh': sh,
                    'ortho_ratio': cam.ortho_ratio,
                    'scale': y_scale,
                    'center': vmed,
                    'R': R
                }

                rndr.set_sh(sh)
                rndr.analytic = False
                rndr.use_inverse_depth = False
                rndr.display()

                out_all_f = rndr.get_color(0)
                out_mask = out_all_f[:, :, 3]
                out_all_f = cv2.cvtColor(out_all_f, cv2.COLOR_RGBA2BGR)

                np.save(
                    os.path.join(out_path, 'PARAM', subject_name,
                                 '%d_%d_%02d.npy' % (y, p, j)), dic)
                cv2.imwrite(
                    os.path.join(out_path, 'RENDER', subject_name,
                                 '%d_%d_%02d.jpg' % (y, p, j)),
                    255.0 * out_all_f)
                cv2.imwrite(
                    os.path.join(out_path, 'MASK', subject_name,
                                 '%d_%d_%02d.png' % (y, p, j)),
                    255.0 * out_mask)

                rndr_uv.set_sh(sh)
                rndr_uv.analytic = False
                rndr_uv.use_inverse_depth = False
                rndr_uv.display()

                uv_color = rndr_uv.get_color(0)
                uv_color = cv2.cvtColor(uv_color, cv2.COLOR_RGBA2BGR)
                cv2.imwrite(
                    os.path.join(out_path, 'UV_RENDER', subject_name,
                                 '%d_%d_%02d.jpg' % (y, p, j)),
                    255.0 * uv_color)

                if y == 0 and j == 0 and p == pitch[0]:
                    uv_pos = rndr_uv.get_color(1)
                    uv_mask = uv_pos[:, :, 3]
                    cv2.imwrite(
                        os.path.join(out_path, 'UV_MASK', subject_name,
                                     '00.png'), 255.0 * uv_mask)

                    data = {
                        'default': uv_pos[:, :, :3]
                    }  # default is a reserved name
                    pyexr.write(
                        os.path.join(out_path, 'UV_POS', subject_name,
                                     '00.exr'), data)

                    uv_nml = rndr_uv.get_color(2)
                    uv_nml = cv2.cvtColor(uv_nml, cv2.COLOR_RGBA2BGR)
                    cv2.imwrite(
                        os.path.join(out_path, 'UV_NORMAL', subject_name,
                                     '00.png'), 255.0 * uv_nml)
Exemple #4
0
def render_prt_ortho(
    out_path,
    folder_name,
    type,
    subject_name,
    normalized,
    shs,
    rndr,
    rndr_uv,
    im_size,
    angl_step=4,
    n_light=1,
    pitch=[0],
):
    cam = Camera(width=im_size, height=im_size)
    cam.ortho_ratio = 0.4 * (512 / im_size)
    cam.near = -100
    cam.far = 100
    cam.sanity_check()

    # set path for obj, prt
    if type == "rp":
        mesh_file = os.path.join(folder_name, subject_name + "_100k.obj")
        text_file = os.path.join(folder_name, "tex",
                                 subject_name + "_dif_2k.jpg")
    elif type == "twindom":
        mesh_file = os.path.join(folder_name, subject_name + ".obj")
        for file in os.listdir(folder_name):
            if ".png" in file or ".jpg" in file:
                text_name = file
                break
        text_file = os.path.join(folder_name, text_name)
    elif type == "nba":
        # all files has 0_person.obj
        mesh_file = os.path.join(folder_name, "0_person.obj")
        # temporary text file path
        text_file = os.path.join(folder_name, "..", "..", "textures/shirt.png")

    if not os.path.exists(mesh_file):
        print("ERROR: obj file does not exist!!", mesh_file)
        return
    prt_file = os.path.join(folder_name, "bounce", "bounce0.txt")
    if not os.path.exists(prt_file):
        print("ERROR: prt file does not exist!!!", prt_file)
        return
    face_prt_file = os.path.join(folder_name, "bounce", "face.npy")
    if not os.path.exists(face_prt_file):
        print("ERROR: face prt file does not exist!!!", prt_file)
        return

    if not os.path.exists(text_file):
        print("ERROR: dif file does not exist!!", text_file)
        return

    texture_image = cv2.imread(text_file)
    texture_image = cv2.cvtColor(texture_image, cv2.COLOR_BGR2RGB)

    # nba dataset load from mtl file
    if type == "nba":
        (vertices, faces, normals, faces_normals, textures, face_textures,
         face_data_mat, face_norm_data_mat, face_textures_data_mat,
         mtl_data) = load_obj_mesh_mtl(mesh_file)
    else:
        (
            vertices,
            faces,
            normals,
            faces_normals,
            textures,
            face_textures,
        ) = load_obj_mesh(mesh_file, with_normal=True, with_texture=True)

    vmin = vertices.min(0)
    vmax = vertices.max(0)
    up_axis = 1 if (vmax - vmin).argmax() == 1 else 2

    # Calculate median and scale for normalization
    vmed = np.median(vertices, 0)
    vmed[up_axis] = 0.5 * (vmax[up_axis] + vmin[up_axis])
    y_scale = 180 / (vmax[up_axis] - vmin[up_axis])

    # set the scaling for the subject
    rndr.set_norm_mat(y_scale, vmed)
    rndr_uv.set_norm_mat(y_scale, vmed)

    tan, bitan = compute_tangent(vertices, faces, normals, textures,
                                 face_textures)
    prt = np.loadtxt(prt_file)
    # Loads the triangles meshes from trimesh since order is not preserved
    face_prt = np.load(face_prt_file)

    # scale and translation for nba
    scale_data = {}
    translation_data = {}

    if type == "nba":
        rndr.set_mesh_mtl(vertices=vertices,
                          faces=face_data_mat,
                          norms=normals,
                          faces_nml=face_norm_data_mat,
                          uvs=textures,
                          faces_uvs=face_textures_data_mat,
                          tans=tan,
                          bitans=bitan,
                          prt=prt,
                          face_prt=face_prt)
        # set texture and their name
        for key in mtl_data:
            text_file = os.path.join(folder_name, mtl_data[key]['map_Kd'])
            texture_image = cv2.imread(text_file)
            texture_image = cv2.cvtColor(texture_image, cv2.COLOR_BGR2RGB)
            rndr.set_albedo(texture_image=texture_image, mat_name=key)
            cnt = 1
            # e.g. '../../textures/head.png'
            # there's multiple shoes in nba dataset
            material_name = mtl_data[key]['map_Kd'].split("/")[-1]
            if 'shoe' not in mtl_data[key]['map_Kd']:
                translation_data[key] = TRANSLATION_MAPPING[material_name]
            else:
                translation_data[key] = TRANSLATION_MAPPING[material_name +
                                                            f'_{cnt}']
                cnt += 1
            scale_data[key] = SCALE_MAPPING[material_name]
    else:
        rndr.set_mesh(
            vertices,
            faces,
            normals,
            faces_normals,
            textures,
            face_textures,
            prt,
            face_prt,
            tan,
            bitan,
        )
        rndr.set_albedo(texture_image)

    if type == "nba":
        rndr_uv.set_mesh_mtl(vertices=vertices,
                             faces=face_data_mat,
                             norms=normals,
                             faces_nml=face_norm_data_mat,
                             uvs=textures,
                             faces_uvs=face_textures_data_mat,
                             tans=tan,
                             bitans=bitan,
                             prt=prt,
                             face_prt=face_prt,
                             tex_offset=translation_data,
                             scale=scale_data)
        # set texture and their name
        for key in mtl_data:
            text_file = os.path.join(folder_name, mtl_data[key]['map_Kd'])
            texture_image = cv2.imread(text_file)
            texture_image = cv2.cvtColor(texture_image, cv2.COLOR_BGR2RGB)
            rndr_uv.set_albedo(texture_image=texture_image, mat_name=key)
    else:
        rndr_uv.set_mesh(
            vertices,
            faces,
            normals,
            faces_normals,
            textures,
            face_textures,
            prt,
            face_prt,
            tan,
            bitan,
        )
        rndr_uv.set_albedo(texture_image)

    os.makedirs(os.path.join(out_path, "GEO", "OBJ", subject_name),
                exist_ok=True)
    os.makedirs(os.path.join(out_path, "PARAM", subject_name), exist_ok=True)
    os.makedirs(os.path.join(out_path, "RENDER", subject_name), exist_ok=True)
    os.makedirs(os.path.join(out_path, "MASK", subject_name), exist_ok=True)
    os.makedirs(os.path.join(out_path, "UV_RENDER", subject_name),
                exist_ok=True)
    os.makedirs(os.path.join(out_path, "UV_MASK", subject_name), exist_ok=True)
    os.makedirs(os.path.join(out_path, "UV_POS", subject_name), exist_ok=True)
    os.makedirs(os.path.join(out_path, "UV_NORMAL", subject_name),
                exist_ok=True)

    if not os.path.exists(os.path.join(out_path, "val.txt")):
        f = open(os.path.join(out_path, "val.txt"), "w")
        f.close()

    if not normalized:
        # copy obj file
        cmd = "cp %s %s" % (mesh_file,
                            os.path.join(out_path, "GEO", "OBJ", subject_name))
        os.system(cmd)
    else:  # don't use this
        obj_name = mesh_file.split("/")[-1]
        mesh = trimesh.load(mesh_file,
                            process=False,
                            force="mesh",
                            skip_materials=True)
        mesh = center_normalize_mesh(mesh)

        with open(os.path.join(out_path, "GEO", "OBJ", subject_name, obj_name),
                  "w") as f:
            mesh.export(f, "obj")

    if type == "nba":
        # rename 0_person.obj to subject name
        src_path = os.path.join(out_path, "GEO", "OBJ", subject_name,
                                "0_person.obj")
        dest_path = src_path.replace("0_person", subject_name)
        shutil.copyfile(src_path, dest_path)

    for p in pitch:
        for y in tqdm(range(0, 360, angl_step)):
            # rotation matrix for the subject
            R = np.matmul(make_rotate(math.radians(p), 0, 0),
                          make_rotate(0, math.radians(y), 0))
            if up_axis == 2:
                R = np.matmul(R, make_rotate(math.radians(90), 0, 0))

            rndr.rot_matrix = R
            rndr_uv.rot_matrix = R
            rndr.set_camera(cam)
            rndr_uv.set_camera(cam)

            for j in range(n_light):
                sh_id = random.randint(0, shs.shape[0] - 1)
                sh = shs[sh_id]
                sh_angle = 0.2 * np.pi * (random.random() - 0.5)
                sh = rotateSH(sh, make_rotate(0, sh_angle, 0).T)

                dic = {
                    "sh": sh,
                    "ortho_ratio": cam.ortho_ratio,
                    "scale": y_scale,
                    "center": vmed,
                    "R": R,
                }

                rndr.set_sh(sh)
                rndr.analytic = True
                rndr.use_inverse_depth = False
                rndr.display()

                out_all_f = rndr.get_color(0)
                out_mask = out_all_f[:, :, 3]
                out_all_f = cv2.cvtColor(out_all_f, cv2.COLOR_RGBA2BGR)

                np.save(
                    os.path.join(out_path, "PARAM", subject_name,
                                 "%d_%d_%02d.npy" % (y, p, j)),
                    dic,
                )
                cv2.imwrite(
                    os.path.join(out_path, "RENDER", subject_name,
                                 "%d_%d_%02d.jpg" % (y, p, j)),
                    255.0 * out_all_f,
                )
                cv2.imwrite(
                    os.path.join(out_path, "MASK", subject_name,
                                 "%d_%d_%02d.png" % (y, p, j)),
                    255.0 * out_mask,
                )

                # Draw the UV mapping here
                rndr_uv.set_sh(sh)
                rndr_uv.analytic = True
                rndr_uv.use_inverse_depth = False
                rndr_uv.display()

                uv_color = rndr_uv.get_color(0)
                uv_color = cv2.cvtColor(uv_color, cv2.COLOR_RGBA2BGR)
                cv2.imwrite(
                    os.path.join(
                        out_path,
                        "UV_RENDER",
                        subject_name,
                        "%d_%d_%02d.jpg" % (y, p, j),
                    ),
                    255.0 * uv_color,
                )

                if y == 0 and j == 0 and p == pitch[0]:
                    uv_pos = rndr_uv.get_color(1)
                    uv_mask = uv_pos[:, :, 3]
                    cv2.imwrite(
                        os.path.join(out_path, "UV_MASK", subject_name,
                                     "00.png"),
                        255.0 * uv_mask,
                    )

                    # default is a reserved name
                    data = {"default": uv_pos[:, :, :3]}
                    pyexr.write(
                        os.path.join(out_path, "UV_POS", subject_name,
                                     "00.exr"), data)

                    uv_nml = rndr_uv.get_color(2)
                    uv_nml = cv2.cvtColor(uv_nml, cv2.COLOR_RGBA2BGR)
                    cv2.imwrite(
                        os.path.join(out_path, "UV_NORMAL", subject_name,
                                     "00.png"),
                        255.0 * uv_nml,
                    )