42 lines
1.4 KiB
Python
42 lines
1.4 KiB
Python
import open3d as o3d
|
|
import os
|
|
|
|
def load_ply(file_path):
|
|
"""Carrega um arquivo PLY usando open3d."""
|
|
mesh = o3d.io.read_triangle_mesh(file_path)
|
|
return mesh
|
|
|
|
def generate_mtl(mesh, mtl_path):
|
|
"""Gera um arquivo MTL a partir de uma malha."""
|
|
if not mesh.has_vertex_colors():
|
|
print("A malha não possui cores de vértices.")
|
|
return
|
|
|
|
# Agrupar cores únicas
|
|
unique_colors = {}
|
|
for i, color in enumerate(mesh.vertex_colors):
|
|
color_tuple = tuple(color)
|
|
if color_tuple not in unique_colors:
|
|
unique_colors[color_tuple] = f"material_{len(unique_colors)}"
|
|
|
|
# Escrever o arquivo MTL
|
|
with open(mtl_path, 'w') as mtl_file:
|
|
for color, material_name in unique_colors.items():
|
|
mtl_file.write(f"newmtl {material_name}\n")
|
|
mtl_file.write(f"Kd {color[0]} {color[1]} {color[2]}\n")
|
|
mtl_file.write("Ka 1.000 1.000 1.000\n")
|
|
mtl_file.write("Ks 0.000 0.000 0.000\n")
|
|
mtl_file.write("d 1.0\n")
|
|
mtl_file.write("illum 2\n")
|
|
|
|
def generate_mtl_from_ply(ply_path, mtl_path):
|
|
"""Gera um arquivo MTL a partir de um arquivo PLY."""
|
|
mesh = load_ply(ply_path)
|
|
generate_mtl(mesh, mtl_path)
|
|
|
|
# Caminhos dos arquivos
|
|
ply_file = 'montagem_pulverizador.ply'
|
|
mtl_file = 'montagem_pulverizador.mtl'
|
|
|
|
# Gerar o arquivo MTL
|
|
generate_mtl_from_ply(ply_file, mtl_file) |