Add new points in .PLY file

Viewed 35

i'm trying to add new line in my .ply files using PlyData:

from plyfile import PlyData, PlyElement
import numpy

with open(filepath, 'rb') as f:
    plydata = PlyData.read(f)
    vertex = numpy.array([([0, 1, 2], 255, 255, 255), ([0, 2, 3], 255, 0, 0)],
                         dtype=[('vertex_indices', 'i4', (3,)), ('red', 'u1'), ('green', 'u1'), ('blue', 'u1')])
new_vertex = PlyElement.describe(vertex, 'vertex')
with open('colored_points.ply', mode='wb') as f:
    PlyData([plydata, new_vertex], text=True).write(f)

This method give me this error:

AttributeError: 'PlyData' object has no attribute 'name'

Thank you!

1 Answers

Explaining the exception:

PlyData represents the entire ply and is divided into elements, which you can access with: plydata.elements.

Example of ply file which has "vertex" element (containing all points) and a "face" element (containing all faces):

plydata.elements
(PlyElement('vertex', (PlyProperty('x', 'float'), PlyProperty('y', 'float'), PlyProperty('z', 'float'), PlyProperty('red', 'uchar'), PlyProperty('green', 'uchar'), PlyProperty('blue', 'uchar')), count=46596, comments=[]),
 PlyElement('face', (PlyListProperty('vertex_indices', 'uchar', 'int'),), count=91800, comments=[]))

When you write PlyData([.., ..], ..), it expects a list of PlyElement whereas plydata is PlyData.

How to add points to a ply:

You can access an element with it's name like a dict: plydata['vertex'], and the actual data (numpy) with plydata['vertex'].data

The new points should be added directly to this data:

new_vertex = numpy.array(
    (1., 2., 3., 10, 20, 30),
    dtype=plydata['vertex'].data.dtype
)
plydata['vertex'].data = np.r_[plydata['vertex'].data, new_vertex]

...

plydata.write(f)
Related