Skip to content

Commit

Permalink
fix empty path export
Browse files Browse the repository at this point in the history
  • Loading branch information
mikedh committed Oct 4, 2024
1 parent 0ead52f commit e811a26
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 44 deletions.
5 changes: 3 additions & 2 deletions tests/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,9 @@ def test_ply_path_empty(self):
assert len(ply) > 0

loaded = g.trimesh.load_path(g.trimesh.util.wrap_as_stream(ply), file_type="ply")
assert g.np.allclose(loaded.entities, path3D.entities)
assert g.np.allclose(loaded.vertices, path3D.vertices)
# assert g.np.allclose(loaded.entities, path3D.entities)
# assert g.np.allclose(loaded.vertices, path3D.vertices)
assert len(loaded.geometry) == 0

def test_ply_path_line(self):
"""
Expand Down
88 changes: 46 additions & 42 deletions trimesh/exchange/ply.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,52 +293,56 @@ def export_ply(
header = [templates["intro"]]
header_params = {"encoding": encoding}

pack_edges = None
pack_edges: Optional[NDArray] = None
pack_vertex: Optional[NDarray] = None

# check if scene has geometry
# check if this is a `trimesh.path.Path` object.
if hasattr(mesh, "entities"):
if len(mesh.vertices) and mesh.vertices.shape[-1] != 3:
raise ValueError("only Path3D export is supported for ply")

# run the discrete curve step for each entity
discrete = [e.discrete(mesh.vertices) for e in mesh.entities]
if len(mesh.vertices) > 0:
# run the discrete curve step for each entity
discrete = [e.discrete(mesh.vertices) for e in mesh.entities]

# how long was each discrete curve
discrete_len = np.array([d.shape[0] for d in discrete])
# what's the index offset based on these lengths
discrete_off = np.concatenate(([0], np.cumsum(discrete_len)[:-1]))
# how long was each discrete curve
discrete_len = np.array([d.shape[0] for d in discrete])
# what's the index offset based on these lengths
discrete_off = np.concatenate(([0], np.cumsum(discrete_len)[:-1]))

# pre-stack edges we can slice and offset
longest = discrete_len.max()
stack = np.column_stack((np.arange(0, longest - 1), np.arange(1, longest)))
# pre-stack edges we can slice and offset
longest = discrete_len.max()
stack = np.column_stack((np.arange(0, longest - 1), np.arange(1, longest)))

# get the indexes that reconstruct the discrete curves when stacked
edges = np.vstack(
[
stack[:length] + offset
for length, offset in zip(discrete_len - 1, discrete_off)
]
)

vertices = np.vstack(discrete)
# create and populate the custom dtype for vertices
num_vertices = len(vertices)
# put mesh edge data into custom dtype to export
num_edges = len(edges)

if num_edges > 0 and num_vertices > 0:
header.append(templates["vertex"])
vertex = np.zeros(num_vertices, dtype=dtype_vertex)
vertex["vertex"] = np.asarray(vertices, dtype=np.float32)

# add the edge info to the header
header.append(templates["edge"])
# pack edges into our dtype
pack_edges = unstructured_to_structured(edges, dtype=dtype_edge)
# get the indexes that reconstruct the discrete curves when stacked
edges = np.vstack(
[
stack[:length] + offset
for length, offset in zip(discrete_len - 1, discrete_off)
]
)

# add the values for the header
header_params.update({"edge_count": num_edges, "vertex_count": num_vertices})
vertices = np.vstack(discrete)
# create and populate the custom dtype for vertices
num_vertices = len(vertices)
# put mesh edge data into custom dtype to export
num_edges = len(edges)

if num_edges > 0 and num_vertices > 0:
header.append(templates["vertex"])
pack_vertex = np.zeros(num_vertices, dtype=dtype_vertex)
pack_vertex["vertex"] = np.asarray(vertices, dtype=np.float32)

# add the edge info to the header
header.append(templates["edge"])
# pack edges into our dtype
pack_edges = unstructured_to_structured(edges, dtype=dtype_edge)

# add the values for the header
header_params.update(
{"edge_count": num_edges, "vertex_count": num_vertices}
)

elif hasattr(mesh, "vertices"):
header.append(templates["vertex"])
Expand Down Expand Up @@ -366,15 +370,15 @@ def export_ply(
_add_attributes_to_dtype(dtype_vertex, mesh.vertex_attributes)

# create and populate the custom dtype for vertices
vertex = np.zeros(num_vertices, dtype=dtype_vertex)
vertex["vertex"] = mesh.vertices
pack_vertex = np.zeros(num_vertices, dtype=dtype_vertex)
pack_vertex["vertex"] = mesh.vertices
if vertex_normal:
vertex["normals"] = mesh.vertex_normals
pack_vertex["normals"] = mesh.vertex_normals
if vertex_color:
vertex["rgba"] = mesh.visual.vertex_colors
pack_vertex["rgba"] = mesh.visual.vertex_colors

if include_attributes and hasattr(mesh, "vertex_attributes"):
_add_attributes_to_data_array(vertex, mesh.vertex_attributes)
_add_attributes_to_data_array(pack_vertex, mesh.vertex_attributes)

if hasattr(mesh, "faces"):
header.append(templates["face"])
Expand All @@ -401,8 +405,8 @@ def export_ply(
export = [Template("".join(header)).substitute(header_params).encode("utf-8")]

if encoding == "binary_little_endian":
if hasattr(mesh, "vertices"):
export.append(vertex.tobytes())
if pack_vertex is not None:
export.append(pack_vertex.tobytes())
if hasattr(mesh, "faces"):
export.append(faces.tobytes())
if pack_edges is not None:
Expand Down

0 comments on commit e811a26

Please sign in to comment.