def _load_texture_images( material_names: List[str], data_dir: str, material_properties: MaterialProperties, texture_files: TextureFiles, ) -> Tuple[MaterialProperties, TextureImages]: final_material_properties = {} texture_images = {} # Only keep the materials referenced in the obj. for material_name in material_names: if material_name in texture_files: # Load the texture image. path = os.path.join(data_dir, texture_files[material_name]) if os.path.isfile(path): image = _read_image(path, format="RGB") / 255.0 image = torch.from_numpy(image) texture_images[material_name] = image else: msg = f"Texture file does not exist: {path}" warnings.warn(msg) if material_name in material_properties: final_material_properties[material_name] = material_properties[ material_name] return final_material_properties, texture_images
def _load_texture_images( material_names: List[str], data_dir: str, material_properties: MaterialProperties, texture_files: TextureFiles, path_manager: PathManager, ) -> Tuple[MaterialProperties, TextureImages]: final_material_properties = {} texture_images = {} used_material_names = list(material_names) if not used_material_names and material_properties: if len(material_properties) > 1: raise ValueError( "Multiple materials but no usemtl declarations in the obj file" ) # No materials were specified in obj file and only one is in the # specified .mtl file, so we use it. used_material_names.append(next(iter(material_properties.keys()))) # Only keep the materials referenced in the obj. for material_name in used_material_names: if material_name in texture_files: # Load the texture image. path = os.path.join(data_dir, texture_files[material_name]) if path_manager.exists(path): image = (_read_image( path, path_manager=path_manager, format="RGB") / 255.0) image = torch.from_numpy(image) texture_images[material_name] = image else: msg = f"Texture file does not exist: {path}" warnings.warn(msg) if material_name in material_properties: final_material_properties[material_name] = material_properties[ material_name] return final_material_properties, texture_images
def load_mtl(f_mtl, material_names: List, data_dir: str, device="cpu"): """ Load texture images and material reflectivity values for ambient, diffuse and specular light (Ka, Kd, Ks, Ns). Args: f_mtl: a file like object of the material information. material_names: a list of the material names found in the .obj file. data_dir: the directory where the material texture files are located. Returns: material_colors: dict of properties for each material. If a material does not have any properties it will have an emtpy dict. { material_name_1: { "ambient_color": tensor of shape (1, 3), "diffuse_color": tensor of shape (1, 3), "specular_color": tensor of shape (1, 3), "shininess": tensor of shape (1) }, material_name_2: {}, ... } texture_images: dict of material names and texture images { material_name_1: (H, W, 3) image, ... } """ texture_files = {} material_colors = {} material_properties = {} texture_images = {} material_name = "" f_mtl, new_f = _open_file(f_mtl) lines = [line.strip() for line in f_mtl] for line in lines: if len(line.split()) != 0: if line.split()[0] == "newmtl": material_name = line.split()[1] material_colors[material_name] = {} if line.split()[0] == "map_Kd": # Texture map. texture_files[material_name] = line.split()[1] if line.split()[0] == "Kd": # RGB diffuse reflectivity kd = np.array(list(line.split()[1:4])).astype(np.float32) kd = torch.from_numpy(kd).to(device) material_colors[material_name]["diffuse_color"] = kd if line.split()[0] == "Ka": # RGB ambient reflectivity ka = np.array(list(line.split()[1:4])).astype(np.float32) ka = torch.from_numpy(ka).to(device) material_colors[material_name]["ambient_color"] = ka if line.split()[0] == "Ks": # RGB specular reflectivity ks = np.array(list(line.split()[1:4])).astype(np.float32) ks = torch.from_numpy(ks).to(device) material_colors[material_name]["specular_color"] = ks if line.split()[0] == "Ns": # Specular exponent ns = np.array(list(line.split()[1:4])).astype(np.float32) ns = torch.from_numpy(ns).to(device) material_colors[material_name]["shininess"] = ns if new_f: f_mtl.close() # Only keep the materials referenced in the obj. for name in material_names: if name in texture_files: # Load the texture image. filename = texture_files[name] filename_texture = os.path.join(data_dir, filename) if os.path.isfile(filename_texture): image = _read_image(filename_texture, format="RGB") / 255.0 image = torch.from_numpy(image) texture_images[name] = image else: msg = f"Texture file does not exist: {filename_texture}" warnings.warn(msg) if name in material_colors: material_properties[name] = material_colors[name] return material_properties, texture_images