Skip to content

write_mesh

write_mesh(file, V, F, UV=None, Ft=None, N=None, Fn=None, C=None, binary=True, fmt=None, writer=None)

Writes a mesh to a file.

If you have the approproate C++ extensions installed, this will use a fast C++-based writer. If you do not, this will use a slow python writer.

Currently only supports triangle meshes.

Parameters:

Name Type Description Default
file string

the path the mesh will be written to

required
V (n,3) numpy array

vertex list of a triangle mesh

required
F (m,3) numpy int array

face index list of a triangle mesh (into V)

required
UV (n_uv,2) numpy array, optional (default None)

vertex list for texture coordinates. Only supported for obj format.

None
Ft (m,3) numpy int array, optional (default None)

face index list for texture coordinates (into UV). If this is not provided, but UV is provided such that n_uv==n, the function will assume that Ft is F. Only supported for obj format.

None
N (n_n,3) numpy array, optional (default None)

vertex list for normal coordinates. Only supported for obj format.

None
Fn (m,3) numpy int array, optional (default None)

face index list for normal coordinates (into N). If this is not provided, but N is provided such that n_n==n, the function will assume that Fn is F. Only supported for obj format.

None
C (n,4) or (m,4) numpy int array with values in [0,255], optional (default None)

list of per-vertex or per-face colors. Only supported for ply format.

None
stl_binary bool, optional (default True)

whether to write the file in binary format (as opposed to ascii). Only supported for stl and ply format.

required
fmt string, optional (default None)

The file format of the mesh to write. If None, try to guess the format from the file extension. Supported formats: obj, stl

None
writer string, optional (default None)

Which writer engine to use. None, 'C++' or 'Python'. If None, will use C++ if available, and else Python.

None

Returns:

Type Description
Examples
--------
```python
# Write a mesh in OBJ format
V, F = regular_square_mesh(10)
write_mesh(obj, V, F)
```
Source code in src/gpytoolbox/write_mesh.py
 5
 6
 7
 8
 9
10
11
12
13
14
15
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
def write_mesh(file,
    V,
    F,
    UV=None,
    Ft=None,
    N=None,
    Fn=None,
    C=None,
    binary=True,
    fmt=None,
    writer=None):
    """Writes a mesh to a file.

    If you have the approproate C++ extensions installed, this will use a fast
    C++-based writer. If you do not, this will use a slow python writer.

    Currently only supports triangle meshes.

    Parameters
    ----------
    file : string
        the path the mesh will be written to
    V : (n,3) numpy array
        vertex list of a triangle mesh
    F : (m,3) numpy int array
        face index list of a triangle mesh (into V)
    UV : (n_uv,2) numpy array, optional (default None)
        vertex list for texture coordinates. Only supported for obj format.
    Ft : (m,3) numpy int array, optional (default None)
        face index list for texture coordinates (into UV).
        If this is not provided, but UV is provided such that n_uv==n, the function will assume that Ft is F.
        Only supported for obj format.
    N : (n_n,3) numpy array, optional (default None)
        vertex list for normal coordinates.
        Only supported for obj format.
    Fn : (m,3) numpy int array, optional (default None)
        face index list for normal coordinates (into N).
        If this is not provided, but N is provided such that n_n==n, the function will assume that Fn is F.
        Only supported for obj format.
    C : (n,4) or (m,4) numpy int array with values in [0,255], optional (default None)
        list of per-vertex or per-face colors.
        Only supported for ply format.
    stl_binary : bool, optional (default True)
        whether to write the file in binary format (as opposed to ascii). Only supported for stl and ply format.
    fmt : string, optional (default None)
        The file format of the mesh to write.
        If None, try to guess the format from the file extension.
        Supported formats: obj, stl
    writer : string, optional (default None)
        Which writer engine to use. None, 'C++' or 'Python'.
        If None, will use C++ if available, and else Python.

    Returns
    -------

    Examples
    --------
    ```python
    # Write a mesh in OBJ format
    V, F = regular_square_mesh(10)
    gpytoolbox.write_mesh('mesh.obj',V,F)
    ```
    """

    # Detect format if it has not been specified
    if fmt is None:
        _, fmt = os.path.splitext(file)
    fmt = fmt[1:].lower()

    # Call appropriate helper function to read mesh
    if fmt=='obj':
        _write_obj(file,V,F,UV,Ft,N,Fn,writer)
    elif fmt=='stl':
        _write_stl(file,V,F,binary)
    elif fmt=='ply':
        _write_ply(file,V,F,N,C,binary)
    else:
        assert False, "Mesh format not supported."