ordito.io
Mesh file loading via the optional meshio dependency.
Classes:
| Name | Description |
|---|---|
MeshData |
The attributes |
Functions:
| Name | Description |
|---|---|
load_mesh_data |
Load every mesh attribute |
load_mesh |
Load a triangle mesh file into a |
mesh_from_numpy |
Build a |
MeshData
Bases: TypedDict
The attributes load_mesh_data read from a file, by name.
A plain dict at runtime. vertices is always present; every other key only when the
file carries that attribute.
Examples:
Only the attributes the file carries appear as keys:
import pathlib
import tempfile
import meshio
import numpy as np
import ordito as od
points = np.array([[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]])
with tempfile.TemporaryDirectory() as folder:
path = pathlib.Path(folder) / "triangle.ply"
meshio.write(path, meshio.Mesh(points, [("triangle", np.array([[0, 1, 2]]))]))
data = od.io.load_mesh_data(path)
print(sorted(data)) # ['faces', 'vertices']
Returned by 1 place
API Reference io › load_mesh_data
vertices
instance-attribute
vertices: Required[array[vec3]]
faces
instance-attribute
faces: array[int32]
vertex_normals
instance-attribute
vertex_normals: array[vec3]
uv
instance-attribute
uv: array[vec2]
corner_uv
instance-attribute
corner_uv: array[vec2]
colors
instance-attribute
colors: array[vec3] | array[vec4]
face_normals
instance-attribute
face_normals: array[vec3]
load_mesh_data
load_mesh_data(
path: str | Path, *, device: DeviceLike = None
) -> MeshData
Load every mesh attribute meshio can read from a file into Warp arrays.
Reads path once with meshio and returns a dictionary whose keys are present
only when the corresponding data exists in the file. vertices is always present;
everything else is optional.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Path to a mesh file in any format supported by |
required |
device
|
DeviceLike
|
Warp device for the returned arrays. Defaults to the current Warp device. |
None
|
Returns:
| Type | Description |
|---|---|
MeshData
|
Mapping with these possible keys:
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If an OBJ file's face names a vertex or a texcoord it does not define, or a PLY file with per-corner texcoords is malformed or names a vertex it does not define. |
See Also
Notes
Only triangle cells are consumed; non-triangle cell blocks are ignored. meshio reads
attributes verbatim and never computes them, so face_normals appears only when the
file itself contained per-face normals.
An OBJ file is read here rather than by meshio, which refuses one whose vt count
differs from its v count -- every UV-mapped OBJ with a seam. Its positions, triangles and
corner_uv are returned; polygons with more than three corners are ignored as above. A PLY
file whose faces carry a texcoord (or texcoords) list, which meshio cannot decode,
is read here too, ASCII or binary, with every attribute above.
Examples:
A box written to PLY with per-vertex normals reads back with them:
import pathlib
import tempfile
import meshio
import ordito as od
vertices, faces = od.creation.box()
normals = od.normals.vertex_normals(vertices, faces).numpy()
mesh = meshio.Mesh(
vertices.numpy(),
[("triangle", faces.numpy().reshape(-1, 3))],
point_data={"nx": normals[:, 0], "ny": normals[:, 1], "nz": normals[:, 2]},
)
with tempfile.TemporaryDirectory() as folder:
path = pathlib.Path(folder) / "box.ply"
meshio.write(path, mesh)
data = od.io.load_mesh_data(path)
print(sorted(data)) # ['faces', 'vertex_normals', 'vertices']
print(data["faces"].shape, data["vertex_normals"].shape) # (36,) (8,)
Referenced by 6 places
API Reference API reference › Find a function by task · io · io › MeshData · io › load_mesh
Getting started Getting data in and out
Migrating from another library Migrating from trimesh › Mesh construction and I/O
Source code in ordito/io.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | |
load_mesh
load_mesh(
path: str | Path, *, device: DeviceLike = None
) -> wp.Mesh
Load a triangle mesh file into a warp.Mesh.
Reads only the vertices and triangle faces (the subset
load_mesh_data returns) and wraps them in a warp.Mesh that
owns its buffers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Path to a mesh file in any format supported by |
required |
device
|
DeviceLike
|
Warp device for the returned mesh. Defaults to the current Warp device. |
None
|
Returns:
| Type | Description |
|---|---|
Mesh
|
Mesh with |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the file contains no triangle faces (e.g. a point cloud or a triangle-strip
PLY that |
See Also
Examples:
Round-trip an icosphere through an OBJ file:
import pathlib
import tempfile
import meshio
import ordito as od
vertices, faces = od.creation.icosphere(subdivisions=1)
mesh = meshio.Mesh(vertices.numpy(), [("triangle", faces.numpy().reshape(-1, 3))])
with tempfile.TemporaryDirectory() as folder:
path = pathlib.Path(folder) / "sphere.obj"
meshio.write(path, mesh)
loaded = od.io.load_mesh(path)
print(loaded.points.shape, loaded.indices.shape) # (42,) (240,)
Referenced by 5 places
API Reference API reference › Find a function by task · io › load_mesh_data · io › mesh_from_numpy · texture
Migrating from another library Migrating from trimesh › Mesh construction and I/O
Source code in ordito/io.py
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 | |
mesh_from_numpy
mesh_from_numpy(
vertices: ndarray,
faces: ndarray,
*,
device: DeviceLike = None,
) -> Trimesh
Build a Trimesh from NumPy vertex and face arrays.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
ndarray
|
|
required |
faces
|
ndarray
|
|
required |
device
|
DeviceLike
|
Warp device for the returned mesh. Defaults to the current Warp device. |
None
|
Returns:
| Type | Description |
|---|---|
Trimesh
|
New mesh with |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a face references a vertex index |
See Also
Examples:
(n_faces, 3) faces are flattened into ordito's (3 * n_faces,) layout:
import numpy as np
import ordito as od
vertices = np.array([[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [1.0, 1.0, 0.0], [0.0, 1.0, 0.0]])
faces = np.array([[0, 1, 2], [0, 2, 3]])
mesh = od.io.mesh_from_numpy(vertices, faces)
print(mesh.n_vertices, mesh.n_faces, mesh.faces.shape) # 4 2 (6,)
print(f"{mesh.area:.4f}") # 1.0000
Referenced by 1 place
Getting started Getting data in and out
Source code in ordito/io.py
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 | |