in tensorflow_graphics/projects/points_to_3Dobjects/utils/plot.py [0:0]
def obj_read_for_gl(filename, texture_size=(32, 32)):
"""Read vertex and part information from OBJ file."""
if texture_size:
print(texture_size)
with gfile.Open(filename, 'r') as f:
content = f.readlines()
vertices = []
texture_coords = []
vertex_normals = []
group_name = None
material_name = None
faces = []
faces_tex = []
faces_normals = []
face_groups = []
material_ids = []
for i in range(len(content)):
line = content[i]
parts = re.split(r'\s+', line)
# if parts[0] == 'mtllib':
# material_file = parts[1]
# Vertex information -----------------------------------------------------
if parts[0] == 'v':
vertices.append([float(v) for v in parts[1:4]])
if parts[0] == 'vt':
texture_coords.append([float(v) for v in parts[1:4]])
if parts[0] == 'vn':
vertex_normals.append([float(v) for v in parts[1:4]])
if parts[0] == 'g':
group_name = parts[1]
if parts[0] == 'usemtl':
material_name = parts[1]
# Face information ------------------------------------------------------
if parts[0] == 'f':
vertex_index, tex_index, normal_index = 0, 0, 0
current_face, current_face_tex, current_face_norm = [], [], []
for j in range(1, 4):
face_info = parts[j]
if face_info.count('/') == 2:
vertex_index, tex_index, normal_index = face_info.split('/')
if not tex_index:
tex_index = 0
elif face_info.count('/') == 1:
vertex_index, tex_index = face_info.split('/')
elif face_info.count('/') == 0:
vertex_index = face_info
current_face.append(int(vertex_index)-1)
current_face_tex.append(int(tex_index)-1)
current_face_norm.append(int(normal_index)-1)
faces.append(current_face)
faces_tex.append(current_face_tex)
faces_normals.append(current_face_norm)
face_groups.append(group_name)
material_ids.append(material_name)
vertices = np.array(vertices)
texture_coords = np.array(texture_coords)
vertex_normals = np.array(vertex_normals)
has_tex_coord, has_normals = True, True
if texture_coords.shape[0] == 0:
has_tex_coord = False
if vertex_normals.shape[0] == 0:
has_normals = False
faces = np.array(faces)
faces_tex = np.array(faces_tex)
faces_normals = np.array(faces_normals)
n_faces = faces.shape[0]
vertex_positions = np.zeros((n_faces, 3, 3), dtype=np.float32)
tex_coords = np.zeros((n_faces, 3, 2), dtype=np.float32)
normals = np.zeros((n_faces, 3, 3), dtype=np.float32)
for i in range(n_faces):
for j in range(3):
vertex_positions[i, j, :] = vertices[faces[i, j], :]
if has_tex_coord:
tex_coords[i, j, :] = texture_coords[faces_tex[i, j], :2]
if has_normals:
normals[i, j, :] = vertex_normals[faces_normals[i, j], :]
# Material info --------------------------------------------------------------
return vertex_positions, \
tex_coords, \
normals, \
material_ids, \
vertices, \
faces