Spaces:
Running
Running
| import numpy | |
| from PIL import Image | |
| from .core import Vec4d | |
| class Model: | |
| def __init__(self, filename, texture_filename): | |
| """ | |
| https://en.wikipedia.org/wiki/Wavefront_.obj_file#Vertex_normal_indices | |
| """ | |
| self.vertices = [] | |
| self.uv_vertices = [] | |
| self.uv_indices = [] | |
| self.indices = [] | |
| texture = Image.open(texture_filename) | |
| self.texture_array = numpy.array(texture) | |
| self.texture_width, self.texture_height = texture.size | |
| with open(filename) as f: | |
| for line in f: | |
| if line.startswith("v "): | |
| x, y, z = [float(d) for d in line.strip("v").strip().split(" ")] | |
| self.vertices.append(Vec4d(x, y, z, 1)) | |
| elif line.startswith("vt "): | |
| u, v = [float(d) for d in line.strip("vt").strip().split(" ")] | |
| self.uv_vertices.append([u, v]) | |
| elif line.startswith("f "): | |
| facet = [d.split("/") for d in line.strip("f").strip().split(" ")] | |
| self.indices.append([int(d[0]) for d in facet]) | |
| self.uv_indices.append([int(d[1]) for d in facet]) | |