修改合并obj代码
This commit is contained in:
parent
c8eaf997a2
commit
dfc3ad8191
@ -12,32 +12,93 @@ class MergeObj:
|
||||
self.logger = logging.getLogger('UAV_Preprocess.MergeObj')
|
||||
|
||||
def read_obj(self, file_path):
|
||||
"""读取.obj文件,返回顶点列表和面列表"""
|
||||
vertices = []
|
||||
faces = []
|
||||
"""读取.obj文件,返回顶点、纹理坐标、法线、面的列表和MTL文件名"""
|
||||
vertices = [] # v
|
||||
tex_coords = [] # vt
|
||||
normals = [] # vn
|
||||
faces = [] # f
|
||||
face_materials = [] # 每个面对应的材质名称
|
||||
mtl_file = None # mtl文件名
|
||||
current_material = None # 当前使用的材质
|
||||
|
||||
with open(file_path, 'r') as file:
|
||||
for line in file:
|
||||
parts = line.split()
|
||||
if len(parts) == 0:
|
||||
if line.startswith('#') or not line.strip():
|
||||
continue
|
||||
if parts[0] == 'v': # 顶点
|
||||
|
||||
parts = line.strip().split()
|
||||
if not parts:
|
||||
continue
|
||||
|
||||
if parts[0] == 'mtllib': # MTL文件引用
|
||||
mtl_file = parts[1]
|
||||
elif parts[0] == 'usemtl': # 材质使用
|
||||
current_material = parts[1]
|
||||
elif parts[0] == 'v': # 顶点
|
||||
vertices.append(
|
||||
[float(parts[1]), float(parts[2]), float(parts[3])])
|
||||
elif parts[0] == 'vt': # 纹理坐标
|
||||
tex_coords.append([float(parts[1]), float(parts[2])])
|
||||
elif parts[0] == 'vn': # 法线
|
||||
normals.append(
|
||||
[float(parts[1]), float(parts[2]), float(parts[3])])
|
||||
elif parts[0] == 'f': # 面
|
||||
faces.append([int(parts[1].split(
|
||||
'/')[0]), int(parts[2].split('/')[0]), int(parts[3].split('/')[0])])
|
||||
# 处理面的索引 (v/vt/vn)
|
||||
face_v = []
|
||||
face_vt = []
|
||||
face_vn = []
|
||||
for p in parts[1:]:
|
||||
indices = p.split('/')
|
||||
face_v.append(int(indices[0]))
|
||||
if len(indices) > 1 and indices[1]:
|
||||
face_vt.append(int(indices[1]))
|
||||
if len(indices) > 2:
|
||||
face_vn.append(int(indices[2]))
|
||||
faces.append((face_v, face_vt, face_vn))
|
||||
face_materials.append(current_material) # 记录这个面使用的材质
|
||||
|
||||
return vertices, faces
|
||||
return vertices, tex_coords, normals, faces, face_materials, mtl_file
|
||||
|
||||
def write_obj(self, file_path, vertices, faces):
|
||||
"""将修改后的顶点和面列表写入到.obj文件"""
|
||||
def write_obj(self, file_path, vertices, tex_coords, normals, faces, face_materials, mtl_file=None):
|
||||
"""将顶点、纹理坐标、法线和面写入到.obj文件"""
|
||||
with open(file_path, 'w') as file:
|
||||
for vertex in vertices:
|
||||
file.write(f"v {vertex[0]} {vertex[1]} {vertex[2]}\n")
|
||||
# 写入MTL文件引用
|
||||
if mtl_file:
|
||||
file.write(f"mtllib {mtl_file}\n")
|
||||
|
||||
for face in faces:
|
||||
file.write(f"f {face[0]} {face[1]} {face[2]}\n")
|
||||
# 写入顶点
|
||||
for v in vertices:
|
||||
file.write(f"v {v[0]} {v[1]} {v[2]}\n")
|
||||
|
||||
# 写入纹理坐标
|
||||
for vt in tex_coords:
|
||||
file.write(f"vt {vt[0]} {vt[1]}\n")
|
||||
|
||||
# 写入法线
|
||||
for vn in normals:
|
||||
file.write(f"vn {vn[0]} {vn[1]} {vn[2]}\n")
|
||||
|
||||
# 写入面(按材质分组)
|
||||
current_material = None
|
||||
for face, material in zip(faces, face_materials):
|
||||
# 如果材质发生变化,写入新的usemtl
|
||||
if material != current_material:
|
||||
file.write(f"usemtl {material}\n")
|
||||
current_material = material
|
||||
|
||||
face_str = "f"
|
||||
for i in range(len(face[0])):
|
||||
face_str += " "
|
||||
face_str += str(face[0][i])
|
||||
if face[1]:
|
||||
face_str += f"/{face[1][i]}"
|
||||
else:
|
||||
face_str += "/"
|
||||
if face[2]:
|
||||
face_str += f"/{face[2][i]}"
|
||||
else:
|
||||
face_str += "/"
|
||||
file.write(face_str + "\n")
|
||||
|
||||
def translate_vertices(self, vertices, translation):
|
||||
"""平移顶点"""
|
||||
@ -48,220 +109,234 @@ class MergeObj:
|
||||
try:
|
||||
self.logger.info(f"开始合并OBJ模型:\n输入1: {obj1_path}\n输入2: {obj2_path}")
|
||||
|
||||
# 检查输入文件是否存在
|
||||
if not os.path.exists(obj1_path) or not os.path.exists(obj2_path):
|
||||
raise FileNotFoundError("输入模型文件不存在")
|
||||
|
||||
# 读取两个obj文件
|
||||
vertices1, faces1 = self.read_obj(obj1_path)
|
||||
vertices2, faces2 = self.read_obj(obj2_path)
|
||||
vertices1, tex_coords1, normals1, faces1, face_materials1, mtl1 = self.read_obj(obj1_path)
|
||||
vertices2, tex_coords2, normals2, faces2, face_materials2, mtl2 = self.read_obj(obj2_path)
|
||||
|
||||
# 读取MTL文件内容以获取正确的材质名称
|
||||
src_dir1 = os.path.dirname(obj1_path)
|
||||
src_dir2 = os.path.dirname(obj2_path)
|
||||
mtl1_path = os.path.join(src_dir1, mtl1)
|
||||
mtl2_path = os.path.join(src_dir2, mtl2)
|
||||
|
||||
# 读取并更新材质内容
|
||||
materials1 = self.read_mtl(mtl1_path)
|
||||
materials2 = self.read_mtl(mtl2_path)
|
||||
|
||||
# 创建材质名称映射(使用与MTL文件相同的命名格式)
|
||||
material_map1 = {old_name: f"material_0_0_{old_name}" for old_name in materials1.keys()}
|
||||
material_map2 = {old_name: f"material_0_1_{old_name}" for old_name in materials2.keys()}
|
||||
|
||||
# 平移第二个模型的顶点
|
||||
vertices2_translated = self.translate_vertices(
|
||||
vertices2, translation)
|
||||
vertices2_translated = self.translate_vertices(vertices2, translation)
|
||||
|
||||
# 合并顶点和面
|
||||
# 计算偏移量
|
||||
v_offset = len(vertices1)
|
||||
vt_offset = len(tex_coords1)
|
||||
vn_offset = len(normals1)
|
||||
|
||||
# 合并顶点、纹理坐标和法线
|
||||
all_vertices = vertices1 + vertices2_translated
|
||||
all_faces = faces1 + \
|
||||
[[f[0] + len(vertices1), f[1] + len(vertices1),
|
||||
f[2] + len(vertices1)] for f in faces2]
|
||||
all_tex_coords = tex_coords1 + tex_coords2
|
||||
all_normals = normals1 + normals2
|
||||
|
||||
# 写入合并后的obj文件
|
||||
self.write_obj(output_path, all_vertices, all_faces)
|
||||
# 调整第二个模型的面索引和材质名称
|
||||
all_faces = faces1.copy()
|
||||
all_face_materials = []
|
||||
|
||||
# 更新第一个模型的材质名称
|
||||
for material in face_materials1:
|
||||
all_face_materials.append(material_map1.get(material, material))
|
||||
|
||||
# 更新第二个模型的面索引和材质名称
|
||||
for face, material in zip(faces2, face_materials2):
|
||||
new_face_v = [f + v_offset for f in face[0]]
|
||||
new_face_vt = [f + vt_offset for f in face[1]] if face[1] else []
|
||||
new_face_vn = [f + vn_offset for f in face[2]] if face[2] else []
|
||||
all_faces.append((new_face_v, new_face_vt, new_face_vn))
|
||||
all_face_materials.append(material_map2.get(material, material))
|
||||
|
||||
# 写入合并后的obj文件,使用与MTL文件相同的名称
|
||||
mtl_filename = "merged_model.mtl" # 使用固定的MTL文件名
|
||||
self.write_obj(output_path, all_vertices, all_tex_coords, all_normals,
|
||||
all_faces, all_face_materials, mtl_filename)
|
||||
self.logger.info(f"模型合并成功,已保存至: {output_path}")
|
||||
|
||||
except Exception as e:
|
||||
self.logger.error(f"合并OBJ模型时发生错误: {str(e)}", exc_info=True)
|
||||
raise
|
||||
|
||||
def read_mtl(self, file_path):
|
||||
"""读取.mtl文件内容"""
|
||||
with open(file_path, 'r') as file:
|
||||
return file.read()
|
||||
def read_mtl(self, mtl_path: str) -> dict:
|
||||
"""读取MTL文件内容
|
||||
Returns:
|
||||
dict: 材质名称到材质信息的映射
|
||||
"""
|
||||
materials = {}
|
||||
current_material = None
|
||||
|
||||
def copy_texture_files(self, src_dir: str, dst_dir: str, grid_id: tuple):
|
||||
with open(mtl_path, 'r') as f:
|
||||
content = f.read()
|
||||
for line in content.strip().split('\n'):
|
||||
if not line:
|
||||
continue
|
||||
|
||||
parts = line.split()
|
||||
if not parts:
|
||||
continue
|
||||
|
||||
if parts[0] == 'newmtl':
|
||||
current_material = parts[1]
|
||||
materials[current_material] = []
|
||||
elif current_material:
|
||||
materials[current_material].append(line)
|
||||
|
||||
return materials
|
||||
|
||||
def copy_and_rename_texture(self, src_dir: str, dst_dir: str, grid_id: tuple) -> dict:
|
||||
"""复制并重命名纹理文件
|
||||
Args:
|
||||
src_dir: 源纹理文件目录
|
||||
dst_dir: 目标纹理文件目录
|
||||
grid_id: 网格ID,用于重命名
|
||||
src_dir: 源纹理目录
|
||||
dst_dir: 目标纹理目录
|
||||
grid_id: 网格ID
|
||||
Returns:
|
||||
dict: 原始文件名到新文件名的映射
|
||||
"""
|
||||
# 确保目标目录存在
|
||||
texture_map = {}
|
||||
os.makedirs(dst_dir, exist_ok=True)
|
||||
|
||||
# 复制所有png文件并重命名
|
||||
for file in os.listdir(src_dir):
|
||||
if file.endswith('.png'):
|
||||
src_file = os.path.join(src_dir, file)
|
||||
# 在文件名前添加网格ID前缀
|
||||
if file.lower().endswith(('.png', '.jpg', '.jpeg')):
|
||||
# 生成新的文件名:grid_0_1_texture.png
|
||||
new_name = f"grid_{grid_id[0]}_{grid_id[1]}_{file}"
|
||||
dst_file = os.path.join(dst_dir, new_name)
|
||||
shutil.copy2(src_file, dst_file)
|
||||
src_path = os.path.join(src_dir, file)
|
||||
dst_path = os.path.join(dst_dir, new_name)
|
||||
|
||||
# 复制文件
|
||||
shutil.copy2(src_path, dst_path)
|
||||
texture_map[file] = new_name
|
||||
self.logger.debug(f"复制纹理文件: {file} -> {new_name}")
|
||||
|
||||
return dst_dir
|
||||
return texture_map
|
||||
|
||||
def update_mtl_content(self, mtl_content: str, grid_id: tuple) -> str:
|
||||
"""更新MTL文件内容,修改纹理文件路径
|
||||
def update_mtl_content(self, materials: dict, texture_map: dict, grid_id: tuple) -> dict:
|
||||
"""更新材质内容,修改材质名称和纹理路径
|
||||
Args:
|
||||
mtl_content: 原MTL文件内容
|
||||
grid_id: 网格ID,用于重命名纹理文件
|
||||
materials: 原始材质信息
|
||||
texture_map: 纹理文件映射
|
||||
grid_id: 网格ID
|
||||
Returns:
|
||||
更新后的MTL文件内容
|
||||
dict: 更新后的材质信息
|
||||
"""
|
||||
updated_materials = {}
|
||||
|
||||
for mat_name, content in materials.items():
|
||||
# 为材质名称添加网格ID前缀,与OBJ文件中的usemtl保持一致
|
||||
new_mat_name = f"material_{grid_id[0]}_{grid_id[1]}_{mat_name}"
|
||||
updated_content = []
|
||||
for line in mtl_content.split('\n'):
|
||||
if line.startswith('map_Kd'): # 纹理文件路径行
|
||||
# 获取原始文件名
|
||||
original_file = line.split()[-1]
|
||||
# 添加网格ID前缀
|
||||
new_file = f"grid_{grid_id[0]}_{grid_id[1]}_{os.path.basename(original_file)}"
|
||||
# 更新行内容
|
||||
line = f"map_Kd {new_file}"
|
||||
|
||||
for line in content:
|
||||
if line.startswith('map_'): # 更新纹理文件路径
|
||||
parts = line.split()
|
||||
old_texture = parts[-1]
|
||||
if old_texture in texture_map:
|
||||
parts[-1] = texture_map[old_texture]
|
||||
line = ' '.join(parts)
|
||||
updated_content.append(line)
|
||||
return '\n'.join(updated_content)
|
||||
|
||||
updated_materials[new_mat_name] = updated_content
|
||||
|
||||
return updated_materials
|
||||
|
||||
def merge_grid_obj(self, grid_points: Dict[tuple, pd.DataFrame], translations: Dict[tuple, tuple]):
|
||||
"""合并所有网格的OBJ模型和纹理"""
|
||||
self.logger.info("开始合并所有网格的OBJ模型")
|
||||
|
||||
if len(grid_points) < 2:
|
||||
self.logger.info("只有一个网格,无需合并")
|
||||
return
|
||||
|
||||
"""合并所有网格的OBJ模型"""
|
||||
try:
|
||||
# 创建输出目录
|
||||
output_model_dir = os.path.join(self.output_dir, "merged_model")
|
||||
os.makedirs(output_model_dir, exist_ok=True)
|
||||
|
||||
# 获取所有有效的网格OBJ文件
|
||||
grid_objs = {}
|
||||
# 获取所有有效的网格文件
|
||||
grid_files = {}
|
||||
for grid_id, points in grid_points.items():
|
||||
grid_base_dir = os.path.join(
|
||||
base_dir = os.path.join(
|
||||
self.output_dir,
|
||||
f"grid_{grid_id[0]}_{grid_id[1]}",
|
||||
"project",
|
||||
"odm_texturing"
|
||||
)
|
||||
grid_obj = os.path.join(grid_base_dir, "odm_textured_model_geo.obj")
|
||||
grid_mtl = os.path.join(grid_base_dir, "odm_textured_model_geo.mtl")
|
||||
obj_path = os.path.join(base_dir, "odm_textured_model_geo.obj")
|
||||
mtl_path = os.path.join(base_dir, "odm_textured_model_geo.mtl")
|
||||
|
||||
if not os.path.exists(grid_obj) or not os.path.exists(grid_mtl):
|
||||
self.logger.warning(
|
||||
f"网格 ({grid_id[0]},{grid_id[1]}) 的OBJ或MTL文件不存在")
|
||||
if not os.path.exists(obj_path) or not os.path.exists(mtl_path):
|
||||
self.logger.warning(f"网格 ({grid_id[0]},{grid_id[1]}) 的文件不存在")
|
||||
continue
|
||||
|
||||
grid_objs[grid_id] = {
|
||||
'obj': grid_obj,
|
||||
'mtl': grid_mtl,
|
||||
'base_dir': grid_base_dir
|
||||
grid_files[grid_id] = {
|
||||
'obj': obj_path,
|
||||
'mtl': mtl_path,
|
||||
'dir': base_dir
|
||||
}
|
||||
|
||||
if not grid_objs:
|
||||
self.logger.error("没有找到有效的OBJ文件")
|
||||
if not grid_files:
|
||||
self.logger.error("没有找到有效的文件")
|
||||
return
|
||||
|
||||
# 使用第一个网格作为参考
|
||||
reference_id = list(grid_objs.keys())[0]
|
||||
merged_obj = grid_objs[reference_id]['obj']
|
||||
|
||||
# 复制参考网格的纹理文件
|
||||
self.copy_texture_files(
|
||||
grid_objs[reference_id]['base_dir'],
|
||||
output_model_dir,
|
||||
reference_id
|
||||
)
|
||||
|
||||
# 复制并更新参考网格的MTL文件
|
||||
ref_mtl_content = self.read_mtl(grid_objs[reference_id]['mtl'])
|
||||
updated_mtl = self.update_mtl_content(ref_mtl_content, reference_id)
|
||||
|
||||
self.logger.info(
|
||||
f"使用网格 ({reference_id[0]},{reference_id[1]}) 作为参考网格")
|
||||
|
||||
# 依次合并其他网格
|
||||
for grid_id, grid_files in list(grid_objs.items())[1:]:
|
||||
# 复制当前网格的纹理文件
|
||||
self.copy_texture_files(
|
||||
grid_files['base_dir'],
|
||||
# 收集所有材质和纹理信息
|
||||
all_materials = {}
|
||||
for grid_id, files in grid_files.items():
|
||||
# 复制并重命名纹理文件
|
||||
texture_map = self.copy_and_rename_texture(
|
||||
files['dir'],
|
||||
output_model_dir,
|
||||
grid_id
|
||||
)
|
||||
|
||||
# 更新当前网格的MTL内容
|
||||
current_mtl = self.read_mtl(grid_files['mtl'])
|
||||
updated_mtl += '\n' + self.update_mtl_content(current_mtl, grid_id)
|
||||
# 读取并更新MTL内容
|
||||
materials = self.read_mtl(files['mtl'])
|
||||
updated_materials = self.update_mtl_content(
|
||||
materials,
|
||||
texture_map,
|
||||
grid_id
|
||||
)
|
||||
all_materials.update(updated_materials)
|
||||
|
||||
# 获取平移量
|
||||
# 写入合并后的MTL文件
|
||||
final_mtl = os.path.join(output_model_dir, "merged_model.mtl")
|
||||
with open(final_mtl, 'w') as f:
|
||||
for mat_name, content in all_materials.items():
|
||||
f.write(f"newmtl {mat_name}\n")
|
||||
for line in content:
|
||||
f.write(f"{line}\n")
|
||||
f.write("\n")
|
||||
|
||||
# 合并OBJ文件
|
||||
reference_id = list(grid_files.keys())[0]
|
||||
merged_obj = grid_files[reference_id]['obj']
|
||||
|
||||
for grid_id, files in list(grid_files.items())[1:]:
|
||||
translation = translations[grid_id]
|
||||
translation = (translation[0], translation[1], 0) # 添加z轴的0平移
|
||||
translation = (translation[0], translation[1], 0)
|
||||
|
||||
output_obj = os.path.join(
|
||||
output_model_dir,
|
||||
f"merged_model_{reference_id[0]}_{reference_id[1]}_{grid_id[0]}_{grid_id[1]}.obj"
|
||||
)
|
||||
|
||||
self.logger.info(
|
||||
f"合并网格 ({grid_id[0]},{grid_id[1]}):\n"
|
||||
f"平移量: x={translation[0]:.2f}m, y={translation[1]:.2f}m\n"
|
||||
f"输出: {output_obj}"
|
||||
)
|
||||
|
||||
self.merge_two_objs(merged_obj, grid_files['obj'],
|
||||
output_obj, translation)
|
||||
self.merge_two_objs(merged_obj, files['obj'], output_obj, translation)
|
||||
merged_obj = output_obj
|
||||
|
||||
# 最后的结果
|
||||
# 最终结果
|
||||
final_obj = os.path.join(output_model_dir, "merged_model.obj")
|
||||
final_mtl = os.path.join(output_model_dir, "merged_model.mtl")
|
||||
|
||||
# 保存最终的OBJ和MTL文件
|
||||
if os.path.exists(merged_obj) and merged_obj != final_obj:
|
||||
shutil.copy2(merged_obj, final_obj)
|
||||
os.remove(merged_obj)
|
||||
|
||||
# 保存合并后的MTL文件
|
||||
with open(final_mtl, 'w') as f:
|
||||
f.write(updated_mtl)
|
||||
|
||||
self.logger.info(
|
||||
f"模型合并完成,输出目录: {output_model_dir}\n"
|
||||
f"- OBJ文件: merged_model.obj\n"
|
||||
f"- MTL文件: merged_model.mtl\n"
|
||||
f"- 纹理文件: {len(os.listdir(output_model_dir)) - 2}个PNG文件"
|
||||
f"- 纹理文件: {len(os.listdir(output_model_dir)) - 2}个"
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
self.logger.error(f"OBJ模型合并过程中发生错误: {str(e)}", exc_info=True)
|
||||
self.logger.error(f"合并过程中发生错误: {str(e)}", exc_info=True)
|
||||
raise
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import sys
|
||||
sys.path.append(os.path.dirname(
|
||||
os.path.dirname(os.path.abspath(__file__))))
|
||||
from utils.logger import setup_logger
|
||||
import pandas as pd
|
||||
|
||||
# 设置输出目录和日志
|
||||
output_dir = r"G:\ODM_output\1009"
|
||||
setup_logger(output_dir)
|
||||
|
||||
# 构造测试用的grid_points字典
|
||||
grid_points = {
|
||||
(0, 0): pd.DataFrame({
|
||||
'latitude': [39.9, 39.91],
|
||||
'longitude': [116.3, 116.31],
|
||||
'altitude': [100, 101]
|
||||
}),
|
||||
(0, 1): pd.DataFrame({
|
||||
'latitude': [39.92, 39.93],
|
||||
'longitude': [116.32, 116.33],
|
||||
'altitude': [102, 103]
|
||||
})
|
||||
}
|
||||
|
||||
# 创建MergeObj实例并执行合并
|
||||
merge_obj = MergeObj(output_dir)
|
||||
merge_obj.merge_grid_obj(grid_points)
|
||||
|
@ -1,65 +0,0 @@
|
||||
def read_obj(file_path):
|
||||
"""
|
||||
读取.obj文件,返回顶点列表和面列表
|
||||
"""
|
||||
vertices = []
|
||||
faces = []
|
||||
|
||||
with open(file_path, 'r') as file:
|
||||
for line in file:
|
||||
parts = line.split()
|
||||
if len(parts) == 0:
|
||||
continue
|
||||
if parts[0] == 'v': # 顶点
|
||||
vertices.append([float(parts[1]), float(parts[2]), float(parts[3])])
|
||||
elif parts[0] == 'f': # 面
|
||||
faces.append([int(parts[1].split('/')[0]), int(parts[2].split('/')[0]), int(parts[3].split('/')[0])])
|
||||
|
||||
return vertices, faces
|
||||
|
||||
def write_obj(file_path, vertices, faces):
|
||||
"""
|
||||
将修改后的顶点和面列表写入到.obj文件
|
||||
"""
|
||||
with open(file_path, 'w') as file:
|
||||
for vertex in vertices:
|
||||
file.write(f"v {vertex[0]} {vertex[1]} {vertex[2]}\n")
|
||||
|
||||
for face in faces:
|
||||
file.write(f"f {face[0]} {face[1]} {face[2]}\n")
|
||||
|
||||
def translate_vertices(vertices, translation):
|
||||
"""
|
||||
平移顶点,translation 是一个三维向量,例如 (500, 0, 0) 会沿 X 轴平移 500 米
|
||||
"""
|
||||
return [[v[0] + translation[0], v[1] + translation[1], v[2] + translation[2]] for v in vertices]
|
||||
|
||||
def merge_objs(obj1_path, obj2_path, output_path, translation=(500, 0, 0)):
|
||||
"""
|
||||
合并两个.obj文件,并对第二个文件的顶点进行平移
|
||||
obj1_path 和 obj2_path 是输入的.obj文件路径
|
||||
output_path 是输出的合并后的.obj文件路径
|
||||
translation 是平移向量,默认为沿 X 轴平移 500 米
|
||||
"""
|
||||
# 读取第一个 obj 文件
|
||||
vertices1, faces1 = read_obj(obj1_path)
|
||||
|
||||
# 读取第二个 obj 文件
|
||||
vertices2, faces2 = read_obj(obj2_path)
|
||||
|
||||
# 平移第二个 obj 文件的顶点
|
||||
vertices2_translated = translate_vertices(vertices2, translation)
|
||||
|
||||
# 合并顶点和面
|
||||
all_vertices = vertices1 + vertices2_translated
|
||||
all_faces = faces1 + [[f[0] + len(vertices1), f[1] + len(vertices1), f[2] + len(vertices1)] for f in faces2]
|
||||
|
||||
# 写入合并后的 obj 文件
|
||||
write_obj(output_path, all_vertices, all_faces)
|
||||
print(f"合并完成,结果保存在 {output_path}")
|
||||
|
||||
# 示例调用
|
||||
obj1_path = 'model1.obj' # 第一个 .obj 文件
|
||||
obj2_path = 'model2.obj' # 第二个 .obj 文件
|
||||
output_path = 'merged_model.obj' # 输出合并后的 .obj 文件
|
||||
merge_objs(obj1_path, obj2_path, output_path, translation=(500, 0, 0))
|
245
tools/merge_two_obj_simple.py
Normal file
245
tools/merge_two_obj_simple.py
Normal file
@ -0,0 +1,245 @@
|
||||
import os
|
||||
import shutil
|
||||
|
||||
def read_obj(file_path):
|
||||
"""读取OBJ文件,返回顶点、纹理坐标、法线、面的列表和MTL文件名"""
|
||||
vertices = [] # v
|
||||
tex_coords = [] # vt
|
||||
normals = [] # vn
|
||||
faces = [] # f
|
||||
face_materials = [] # 每个面对应的材质名称
|
||||
mtl_file = None # mtl文件名
|
||||
current_material = None # 当前使用的材质
|
||||
|
||||
with open(file_path, 'r') as file:
|
||||
for line in file:
|
||||
if line.startswith('#') or not line.strip():
|
||||
continue
|
||||
|
||||
parts = line.strip().split()
|
||||
if not parts:
|
||||
continue
|
||||
|
||||
if parts[0] == 'mtllib': # MTL文件引用
|
||||
mtl_file = parts[1]
|
||||
elif parts[0] == 'usemtl': # 材质使用
|
||||
current_material = parts[1]
|
||||
elif parts[0] == 'v': # 顶点
|
||||
vertices.append([float(parts[1]), float(parts[2]), float(parts[3])])
|
||||
elif parts[0] == 'vt': # 纹理坐标
|
||||
tex_coords.append([float(parts[1]), float(parts[2])])
|
||||
elif parts[0] == 'vn': # 法线
|
||||
normals.append([float(parts[1]), float(parts[2]), float(parts[3])])
|
||||
elif parts[0] == 'f': # 面
|
||||
# 处理面的索引 (v/vt/vn)
|
||||
face_v = []
|
||||
face_vt = []
|
||||
face_vn = []
|
||||
for p in parts[1:]:
|
||||
indices = p.split('/')
|
||||
face_v.append(int(indices[0]))
|
||||
if len(indices) > 1 and indices[1]:
|
||||
face_vt.append(int(indices[1]))
|
||||
if len(indices) > 2:
|
||||
face_vn.append(int(indices[2]))
|
||||
faces.append((face_v, face_vt, face_vn))
|
||||
face_materials.append(current_material) # 记录这个面使用的材质
|
||||
|
||||
return vertices, tex_coords, normals, faces, face_materials, mtl_file
|
||||
|
||||
def read_mtl(mtl_path: str) -> tuple:
|
||||
"""读取MTL文件内容
|
||||
Returns:
|
||||
tuple: (文件内容列表, 材质名称列表)
|
||||
"""
|
||||
content = []
|
||||
material_names = []
|
||||
with open(mtl_path, 'r') as f:
|
||||
content = f.readlines()
|
||||
for line in content:
|
||||
if line.startswith('newmtl'):
|
||||
material_names.append(line.strip().split()[1])
|
||||
return content, material_names
|
||||
|
||||
def update_mtl_content(content: list, model_id: int) -> tuple:
|
||||
"""更新MTL文件内容,修改材质名称和纹理文件路径
|
||||
Returns:
|
||||
tuple: (更新后的内容, 更新后的材质名称列表)
|
||||
"""
|
||||
updated_lines = []
|
||||
updated_material_names = []
|
||||
current_material = None
|
||||
|
||||
for line in content:
|
||||
if line.startswith('newmtl'):
|
||||
# 为材质名称添加前缀
|
||||
parts = line.strip().split()
|
||||
material_name = parts[1]
|
||||
current_material = f"grid_{model_id}_{material_name}"
|
||||
updated_material_names.append(current_material)
|
||||
updated_lines.append(f"newmtl {current_material}\n")
|
||||
elif line.startswith('map_'):
|
||||
# 更新纹理文件路径
|
||||
parts = line.strip().split()
|
||||
texture_file = os.path.basename(parts[-1])
|
||||
parts[-1] = f"grid_{model_id}_{texture_file}"
|
||||
updated_lines.append(' '.join(parts) + '\n')
|
||||
else:
|
||||
updated_lines.append(line)
|
||||
|
||||
return updated_lines, updated_material_names
|
||||
|
||||
def merge_mtl_files(mtl1_path: str, mtl2_path: str, output_path: str) -> tuple:
|
||||
"""合并两个MTL文件
|
||||
Returns:
|
||||
tuple: (第一个模型的材质名称列表, 第二个模型的材质名称列表)
|
||||
"""
|
||||
# 读取两个MTL文件
|
||||
content1, materials1 = read_mtl(mtl1_path)
|
||||
content2, materials2 = read_mtl(mtl2_path)
|
||||
|
||||
# 更新两个MTL的内容
|
||||
updated_content1, updated_materials1 = update_mtl_content(content1, 0)
|
||||
updated_content2, updated_materials2 = update_mtl_content(content2, 1)
|
||||
|
||||
# 合并并写入新的MTL文件
|
||||
with open(output_path, 'w') as f:
|
||||
f.writelines(updated_content1)
|
||||
f.write('\n') # 添加分隔行
|
||||
f.writelines(updated_content2)
|
||||
|
||||
return updated_materials1, updated_materials2
|
||||
|
||||
def write_obj(file_path, vertices, tex_coords, normals, faces, face_materials, mtl_file=None):
|
||||
"""将顶点、纹理坐标、法线和面写入到OBJ文件"""
|
||||
with open(file_path, 'w') as file:
|
||||
# 写入MTL文件引用
|
||||
if mtl_file:
|
||||
file.write(f"mtllib {mtl_file}\n")
|
||||
|
||||
# 写入顶点
|
||||
for v in vertices:
|
||||
file.write(f"v {v[0]} {v[1]} {v[2]}\n")
|
||||
|
||||
# 写入纹理坐标
|
||||
for vt in tex_coords:
|
||||
file.write(f"vt {vt[0]} {vt[1]}\n")
|
||||
|
||||
# 写入法线
|
||||
for vn in normals:
|
||||
file.write(f"vn {vn[0]} {vn[1]} {vn[2]}\n")
|
||||
|
||||
# 写入面(按材质分组)
|
||||
current_material = None
|
||||
for face, material in zip(faces, face_materials):
|
||||
# 如果材质发生变化,写入新的usemtl
|
||||
if material != current_material:
|
||||
file.write(f"usemtl {material}\n")
|
||||
current_material = material
|
||||
|
||||
face_str = "f"
|
||||
for j in range(len(face[0])):
|
||||
face_str += " "
|
||||
face_str += str(face[0][j])
|
||||
if face[1]:
|
||||
face_str += f"/{face[1][j]}"
|
||||
else:
|
||||
face_str += "/"
|
||||
if face[2]:
|
||||
face_str += f"/{face[2][j]}"
|
||||
else:
|
||||
face_str += "/"
|
||||
file.write(face_str + "\n")
|
||||
|
||||
def translate_vertices(vertices, translation):
|
||||
"""平移顶点"""
|
||||
return [[v[0] + translation[0], v[1] + translation[1], v[2] + translation[2]] for v in vertices]
|
||||
|
||||
def copy_mtl_and_textures(src_dir: str, dst_dir: str, model_id: int):
|
||||
"""复制MTL文件和相关的纹理文件,并重命名避免冲突
|
||||
Args:
|
||||
src_dir: 源目录(包含MTL和纹理文件)
|
||||
dst_dir: 目标目录
|
||||
model_id: 模型ID,用于重命名
|
||||
"""
|
||||
# 复制并重命名纹理文件
|
||||
for file in os.listdir(src_dir):
|
||||
if file.lower().endswith('.png'):
|
||||
src_file = os.path.join(src_dir, file)
|
||||
new_name = f"grid_{model_id}_{file}"
|
||||
dst_file = os.path.join(dst_dir, new_name)
|
||||
shutil.copy2(src_file, dst_file)
|
||||
print(f"复制纹理文件: {file} -> {new_name}")
|
||||
|
||||
def merge_objs(obj1_path, obj2_path, output_path):
|
||||
"""合并两个OBJ文件"""
|
||||
print(f"开始合并OBJ模型:\n输入1: {obj1_path}\n输入2: {obj2_path}")
|
||||
|
||||
# 读取两个obj文件
|
||||
vertices1, tex_coords1, normals1, faces1, face_materials1, mtl1 = read_obj(obj1_path)
|
||||
vertices2, tex_coords2, normals2, faces2, face_materials2, mtl2 = read_obj(obj2_path)
|
||||
|
||||
# 固定平移量(0, 1000, 0)
|
||||
translation = (0, 1000, 0)
|
||||
|
||||
# 平移第二个模型的顶点
|
||||
vertices2_translated = translate_vertices(vertices2, translation)
|
||||
|
||||
# 计算偏移量
|
||||
v_offset = len(vertices1)
|
||||
vt_offset = len(tex_coords1)
|
||||
vn_offset = len(normals1)
|
||||
|
||||
# 合并顶点、纹理坐标和法线
|
||||
all_vertices = vertices1 + vertices2_translated
|
||||
all_tex_coords = tex_coords1 + tex_coords2
|
||||
all_normals = normals1 + normals2
|
||||
|
||||
# 调整第二个模型的面索引和材质名称
|
||||
all_faces = faces1.copy()
|
||||
all_face_materials = face_materials1.copy()
|
||||
|
||||
for face, material in zip(faces2, face_materials2):
|
||||
new_face_v = [f + v_offset for f in face[0]]
|
||||
new_face_vt = [f + vt_offset for f in face[1]] if face[1] else []
|
||||
new_face_vn = [f + vn_offset for f in face[2]] if face[2] else []
|
||||
all_faces.append((new_face_v, new_face_vt, new_face_vn))
|
||||
# 为第二个模型的材质名称添加前缀
|
||||
all_face_materials.append(f"grid_1_{material}")
|
||||
|
||||
# 为第一个模型的材质添加前缀
|
||||
all_face_materials[:len(faces1)] = [f"grid_0_{mat}" for mat in face_materials1]
|
||||
|
||||
# 创建输出子目录
|
||||
output_dir = os.path.dirname(output_path)
|
||||
os.makedirs(output_dir, exist_ok=True)
|
||||
|
||||
# 复制并重命名两个模型的纹理文件
|
||||
src_dir1 = os.path.dirname(obj1_path)
|
||||
src_dir2 = os.path.dirname(obj2_path)
|
||||
copy_mtl_and_textures(src_dir1, output_dir, 0)
|
||||
copy_mtl_and_textures(src_dir2, output_dir, 1)
|
||||
|
||||
# 合并MTL文件并获取材质名称
|
||||
src_mtl1 = os.path.join(src_dir1, mtl1)
|
||||
src_mtl2 = os.path.join(src_dir2, mtl2)
|
||||
dst_mtl = os.path.join(output_dir, "merged_model.mtl")
|
||||
merge_mtl_files(src_mtl1, src_mtl2, dst_mtl)
|
||||
|
||||
# 写入合并后的obj文件
|
||||
write_obj(output_path, all_vertices, all_tex_coords, all_normals,
|
||||
all_faces, all_face_materials, "merged_model.mtl")
|
||||
print(f"模型合并成功,已保存至: {output_path}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
# 测试参数
|
||||
obj1_path = r"G:\ODM_output\1009\grid_0_0\project\odm_texturing\odm_textured_model_geo.obj"
|
||||
obj2_path = r"G:\ODM_output\1009\grid_0_1\project\odm_texturing\odm_textured_model_geo.obj"
|
||||
output_dir = r"G:\ODM_output\1009\merge_test"
|
||||
|
||||
# 创建输出目录
|
||||
os.makedirs(output_dir, exist_ok=True)
|
||||
output_path = os.path.join(output_dir, "merged_test.obj")
|
||||
|
||||
# 执行合并
|
||||
merge_objs(obj1_path, obj2_path, output_path)
|
Loading…
Reference in New Issue
Block a user