Adding texture (.mtl) to obj
Hello,
Thanks for your library. I use it for work with .obj. Already it rotation, scaling properly, but i want add to it texture. How i can do it? I have next methods for get .obj an .mtl. But how to display it, don't know
def create_mesh_from_obj(obj_path): with open(obj_path, 'r') as f: lines = f.readlines()
verts = []
faces = []
for line in lines:
if line.startswith('v '):
vertex = line.split()[1:]
vertex = list(map(float, vertex))
verts.append(vertex)
elif line.startswith('f '):
face = line.split()[1:]
face_verts = []
for i in range(1, len(face) - 1):
idx1 = face[0].split('/')
idx2 = face[i].split('/')
idx3 = face[i + 1].split('/')
vertex_idx1 = int(idx1[0]) - 1
vertex_idx2 = int(idx2[0]) - 1
vertex_idx3 = int(idx3[0]) - 1
face_verts.append((vertex_idx1, vertex_idx2, vertex_idx3))
faces.extend(face_verts)
verts = np.array(verts, dtype=np.float32)
faces = np.array(faces, dtype=np.int32)
verts, faces = torch.from_numpy(verts), torch.from_numpy(faces)
print("verts %s " % verts)
print("faces %s " % faces)
return verts, faces
def load_mtl(mtl_path): materials = {} with open(mtl_path, 'r') as file: lines = file.readlines()
for line in lines:
components = line.strip().split()
if not components:
continue
if components[0] == 'newmtl':
material_name = components[1]
materials[material_name] = {}
elif components[0] in ['Ka', 'Kd', 'Ks']:
# Ka, Kd, and Ks are the ambient, diffuse, and specular color coefficients
# They are each followed by three floats representing red, green, and blue
assert len(components[1:]) == 3
materials[material_name][components[0]] = list(map(float, components[1:]))
return materials
class ROMP(nn.Module): def init(self, romp_settings): super(ROMP, self).init() self.settings = romp_settings self.tdevice = determine_device(self.settings.GPU) self.build_model() self.initilization() self.verts, self.faces = create_mesh_from_obj(obj_path=self.settings.obj_path) self.materials = load_mtl(mtl_path=self.settings.mtl_path)
parser.add_argument('--obj_path', type=str, default=osp.join(osp.expanduser("~"), '.romp', 'cube3d.obj'), help='The path of the SMPL .obj model file')
parser.add_argument('--mtl_path', type=str, default=osp.join(osp.expanduser("~"), '.romp', 'mtl_path.mtl'), help='The path of the SMPL .mtl texture file')