我有一个 plydata 对象plydata_resize
,它是原始 .ply 文件的调整大小版本。
我想转换plydata_resize
成myTrimesh
,这是一个修剪对象。目前,我已保存plydata_resize
为临时 .plt 文件tri_resized.ply
,然后将该文件作为 trimesh 加载。但是我不想使用保存/加载方法,因为会涉及硬盘并且速度会很慢(我有数千个 .ply 文件要以这种方式转换,所以我需要一个快速的方法)。
如何跳过将其保存为 .ply 的步骤?
import trimesh
import pyrender
import numpy as np
from plyfile import PlyData, PlyElement
file_path = "C:/Users/H42/Desktop/tri.ply" # the input file
ply_path = "C:/Users/H42/Desktop/tri_resized.ply" # the temporary file
plydata = PlyData.read(file_path)
element_vertices = plydata.elements[0] # 0 for vertices; 1 for faces
element_faces = plydata.elements[1]
vertices_values = np.array([list(vertix_values) for vertix_values in element_vertices.data]) # re-format vertices' coordinates/color as array
XYZ = vertices_values[:,0:3]*0.005 # vertices coordinates resize
RGB = vertices_values[:,3:6] # vertices color
myList = [(np.round(xyz[0],4), np.round(xyz[1],4), np.round(xyz[2],4), rgb[0], rgb[1], rgb[2]) for xyz, rgb in zip(XYZ, RGB)]
vertices_xyzrgb = np.array(myList, dtype=[('x', 'f4'), ('y', 'f4'),('z', 'f4'), ('red','u1'), ('green','u1'), ('blue','u1')])
element_vertices_resize = PlyElement.describe(vertices_xyzrgb, 'vertex')
plydata_resize = PlyData([element_vertices_resize, element_faces], text=True)
plydata_resize.write(ply_path)
myTrimesh = trimesh.load(ply_path)