修obj合并bug
This commit is contained in:
parent
38db33b2c0
commit
ffcd4ef8d5
@ -111,8 +111,10 @@ class MergeObj:
|
|||||||
self.logger.info(f"开始合并OBJ模型:\n输入1: {obj1_path}\n输入2: {obj2_path}")
|
self.logger.info(f"开始合并OBJ模型:\n输入1: {obj1_path}\n输入2: {obj2_path}")
|
||||||
|
|
||||||
# 读取两个obj文件
|
# 读取两个obj文件
|
||||||
vertices1, tex_coords1, normals1, faces1, face_materials1, mtl1 = self.read_obj(obj1_path)
|
vertices1, tex_coords1, normals1, faces1, face_materials1, mtl1 = self.read_obj(
|
||||||
vertices2, tex_coords2, normals2, faces2, face_materials2, mtl2 = self.read_obj(obj2_path)
|
obj1_path)
|
||||||
|
vertices2, tex_coords2, normals2, faces2, face_materials2, mtl2 = self.read_obj(
|
||||||
|
obj2_path)
|
||||||
|
|
||||||
# 读取MTL文件内容以获取正确的材质名称
|
# 读取MTL文件内容以获取正确的材质名称
|
||||||
src_dir1 = os.path.dirname(obj1_path)
|
src_dir1 = os.path.dirname(obj1_path)
|
||||||
@ -130,21 +132,19 @@ class MergeObj:
|
|||||||
|
|
||||||
# 处理第一个模型的材质映射
|
# 处理第一个模型的材质映射
|
||||||
for old_name in materials1.keys():
|
for old_name in materials1.keys():
|
||||||
# 如果材质名称已经包含了网格ID前缀,就不再添加
|
if "grid_0_0" in obj1_path:
|
||||||
if old_name.startswith(f"material_{grid_id1[0]}_{grid_id1[1]}_"):
|
|
||||||
material_map1[old_name] = old_name
|
|
||||||
else:
|
|
||||||
material_map1[old_name] = f"material_{grid_id1[0]}_{grid_id1[1]}_{old_name}"
|
material_map1[old_name] = f"material_{grid_id1[0]}_{grid_id1[1]}_{old_name}"
|
||||||
|
else:
|
||||||
|
# 更新完一次后,之后就不用再更新了
|
||||||
|
material_map1[old_name] = old_name
|
||||||
|
|
||||||
# 处理第二个模型的材质映射
|
# 处理第二个模型的材质映射
|
||||||
for old_name in materials2.keys():
|
for old_name in materials2.keys():
|
||||||
if old_name.startswith(f"material_{grid_id2[0]}_{grid_id2[1]}_"):
|
|
||||||
material_map2[old_name] = old_name
|
|
||||||
else:
|
|
||||||
material_map2[old_name] = f"material_{grid_id2[0]}_{grid_id2[1]}_{old_name}"
|
material_map2[old_name] = f"material_{grid_id2[0]}_{grid_id2[1]}_{old_name}"
|
||||||
|
|
||||||
# 平移第二个模型的顶点
|
# 平移第二个模型的顶点
|
||||||
vertices2_translated = self.translate_vertices(vertices2, translation)
|
vertices2_translated = self.translate_vertices(
|
||||||
|
vertices2, translation)
|
||||||
|
|
||||||
# 计算偏移量
|
# 计算偏移量
|
||||||
v_offset = len(vertices1)
|
v_offset = len(vertices1)
|
||||||
@ -162,15 +162,17 @@ class MergeObj:
|
|||||||
|
|
||||||
# 更新第一个模型的材质名称
|
# 更新第一个模型的材质名称
|
||||||
for material in face_materials1:
|
for material in face_materials1:
|
||||||
all_face_materials.append(material_map1.get(material, material))
|
all_face_materials.append(material_map1.get(material))
|
||||||
|
|
||||||
# 更新第二个模型的面索引和材质名称
|
# 更新第二个模型的面索引和材质名称
|
||||||
for face, material in zip(faces2, face_materials2):
|
for face, material in zip(faces2, face_materials2):
|
||||||
new_face_v = [f + v_offset for f in face[0]]
|
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_vt = [
|
||||||
new_face_vn = [f + vn_offset for f in face[2]] if face[2] else []
|
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_faces.append((new_face_v, new_face_vt, new_face_vn))
|
||||||
all_face_materials.append(material_map2.get(material, material))
|
all_face_materials.append(material_map2.get(material))
|
||||||
|
|
||||||
# 写入合并后的obj文件,使用与MTL文件相同的名称
|
# 写入合并后的obj文件,使用与MTL文件相同的名称
|
||||||
mtl_filename = "merged_model.mtl" # 使用固定的MTL文件名
|
mtl_filename = "merged_model.mtl" # 使用固定的MTL文件名
|
||||||
@ -287,7 +289,8 @@ class MergeObj:
|
|||||||
mtl_path = os.path.join(base_dir, "odm_textured_model_geo.mtl")
|
mtl_path = os.path.join(base_dir, "odm_textured_model_geo.mtl")
|
||||||
|
|
||||||
if not os.path.exists(obj_path) or not os.path.exists(mtl_path):
|
if not os.path.exists(obj_path) or not os.path.exists(mtl_path):
|
||||||
self.logger.warning(f"网格 ({grid_id[0]},{grid_id[1]}) 的文件不存在")
|
self.logger.warning(
|
||||||
|
f"网格 ({grid_id[0]},{grid_id[1]}) 的文件不存在")
|
||||||
continue
|
continue
|
||||||
|
|
||||||
grid_files[grid_id] = {
|
grid_files[grid_id] = {
|
||||||
@ -344,14 +347,8 @@ class MergeObj:
|
|||||||
)
|
)
|
||||||
temp_files.append(temp_output) # 添加到临时文件列表
|
temp_files.append(temp_output) # 添加到临时文件列表
|
||||||
|
|
||||||
self.merge_two_objs(merged_obj, files['obj'], temp_output, translation, reference_id, grid_id)
|
self.merge_two_objs(
|
||||||
|
merged_obj, files['obj'], temp_output, translation, reference_id, grid_id)
|
||||||
# 如果上一个merged_obj是临时文件,则删除它
|
|
||||||
if merged_obj != grid_files[reference_id]['obj'] and os.path.exists(merged_obj):
|
|
||||||
try:
|
|
||||||
os.remove(merged_obj)
|
|
||||||
except Exception as e:
|
|
||||||
self.logger.warning(f"删除临时文件失败: {merged_obj}, 错误: {str(e)}")
|
|
||||||
|
|
||||||
merged_obj = temp_output
|
merged_obj = temp_output
|
||||||
|
|
||||||
@ -375,7 +372,8 @@ class MergeObj:
|
|||||||
try:
|
try:
|
||||||
os.remove(temp_file)
|
os.remove(temp_file)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.logger.warning(f"删除临时文件失败: {temp_file}, 错误: {str(e)}")
|
self.logger.warning(
|
||||||
|
f"删除临时文件失败: {temp_file}, 错误: {str(e)}")
|
||||||
|
|
||||||
self.logger.info(
|
self.logger.info(
|
||||||
f"模型合并完成,输出目录: {output_model_dir}\n"
|
f"模型合并完成,输出目录: {output_model_dir}\n"
|
||||||
|
Loading…
Reference in New Issue
Block a user