meshes_io.md 2.65 KB
Newer Older
1
2
3
4
5
---
sidebar_label: Loading from file
hide_title: true
---

facebook-github-bot's avatar
facebook-github-bot committed
6
7
8
# Meshes and IO

The Meshes object represents a batch of triangulated meshes, and is central to
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
9
much of the functionality of PyTorch3D. There is no insistence that each mesh in
facebook-github-bot's avatar
facebook-github-bot committed
10
11
12
13
14
the batch has the same number of vertices or faces. When available, it can store
other data which pertains to the mesh, for example face normals, face areas
and textures.

Two common file formats for storing single meshes are ".obj" and ".ply" files,
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
15
and PyTorch3D has functions for reading these.
facebook-github-bot's avatar
facebook-github-bot committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

## OBJ

Obj files have a standard way to store extra information about a mesh. Given an
obj file, it can be read with

```
  verts, faces, aux = load_obj(filename)
```

which sets `verts` to be a (V,3)-tensor of vertices and `faces.verts_idx` to be
an (F,3)- tensor of the vertex-indices of each of the corners of the faces.
Faces which are not triangles will be split into triangles. `aux` is an object
which may contain normals, uv coordinates, material colors and textures if they
are present, and `faces` may additionally contain indices into these normals,
textures and materials in its NamedTuple structure. A Meshes object containing a
single mesh can be created from just the vertices and faces using
```
    meshes = Meshes(verts=[verts], faces=[faces.verts_idx])
```

If there is texture information in the `.obj` it can be used to initialize a
`Textures` class which is passed into the `Meshes` constructor.  Currently we
support loading of texture maps for meshes which have one texture map for the
entire mesh e.g.

```
verts_uvs = aux.verts_uvs[None, ...]  # (1, V, 2)
faces_uvs = faces.textures_idx[None, ...]  # (1, F, 3)
tex_maps = aux.texture_images

# tex_maps is a dictionary of {material name: texture image}.
# Take the first image:
texture_image = list(tex_maps.values())[0]
texture_image = texture_image[None, ...]  # (1, H, W, 3)

# Create a textures object
tex = Textures(verts_uvs=verts_uvs, faces_uvs=faces_uvs, maps=texture_image)

# Initialise the mesh with textures
meshes = Meshes(verts=[verts], faces=[faces.verts_idx], textures=tex)
```
58
59
60

The `load_objs_as_meshes` function provides this procedure.

facebook-github-bot's avatar
facebook-github-bot committed
61
62
## PLY

Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
63
Ply files are flexible in the way they store additional information. PyTorch3D
facebook-github-bot's avatar
facebook-github-bot committed
64
65
66
67
68
69
70
71
72
73
74
75
provides a function just to read the vertices and faces from a ply file.
The call
```
    verts, faces = load_ply(filename)
```
sets `verts` to be a (V,3)-tensor of vertices and `faces` to be an (F,3)-
tensor of the vertex-indices of each of the corners of the faces. Faces which
are not triangles will be split into triangles. A Meshes object containing a
single mesh can be created from this data using
```
    meshes = Meshes(verts=[verts], faces=[faces])
```