-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuildingObj.py
More file actions
51 lines (44 loc) · 2.07 KB
/
BuildingObj.py
File metadata and controls
51 lines (44 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import os
from img_class import TextureImage
class BuildingObj:
def __init__(self, obj_path, mtl_path, temp_path, output_path):
self.obj_path = obj_path
self.mtl_path = mtl_path
self.texture_list: list[TextureImage] = []
self.temp_path = temp_path
self.output_path = output_path
print("Loading image from:", obj_path)
print(f"obj path : {obj_path}")
print(f"mtl path : {mtl_path}")
print(f"temp path : {temp_path}")
print(f"output path : {output_path}")
self.load_texture()
def load_texture(self):
if self.mtl_path is None:
print("No MTL file provided, skipping texture loading.")
return
# Ensure the mtl path is absolute to resolve relative paths correctly.
mtl_file_path = os.path.abspath(self.mtl_path)
mtl_dir = os.path.dirname(mtl_file_path)
print(f"Reading MTL file from: {mtl_file_path}")
try:
with open(mtl_file_path) as f:
lines = f.readlines()
for line in lines:
line = line.strip()
if line.startswith("map_Kd"):
# The texture path is relative to the MTL file.
relative_texture_path = line.split(" ", 1)[1]
# Construct the absolute path for the texture.
texture_path = os.path.join(mtl_dir, relative_texture_path)
# Normalize the path to handle separators like \ or /
texture_path = os.path.normpath(texture_path)
print(f"Found texture: {texture_path}")
img = TextureImage(texture_path)
img.building_obj = self
self.texture_list.append(img)
print(f"{len(self.texture_list)} textures were loaded.")
except FileNotFoundError:
print(f"Error: Could not find MTL file at {mtl_file_path}")
except Exception as e:
print(f"An error occurred while loading textures: {e}")