Save mesh to file. Args: filename (:py:class:`str`): Output file. File format is auto detected from extension. mesh (:py:class:`Mesh`): Mesh object. *attributes (:py:class:`list`): (optional) Attribute names to be saved. This field would be ignored if the o
(filename, mesh, *attributes, **setting)
| 143 | num_vertex_per_voxel) |
| 144 | |
| 145 | def save_mesh(filename, mesh, *attributes, **setting): |
| 146 | """ Save mesh to file. |
| 147 | |
| 148 | Args: |
| 149 | filename (:py:class:`str`): Output file. File format is auto detected from extension. |
| 150 | mesh (:py:class:`Mesh`): Mesh object. |
| 151 | *attributes (:py:class:`list`): (optional) Attribute names to be saved. |
| 152 | This field would be ignored if the output format does not support |
| 153 | attributes (e.g. **.obj** and **.stl** files) |
| 154 | **setting (:py:class:`dict`): (optional) The following keys are recognized. |
| 155 | |
| 156 | * ascii: whether to use ascii encoding, default is false. |
| 157 | * use_float: store scalars as float instead of double, default is |
| 158 | false. |
| 159 | * anonymous: whether to indicate the file is generated by PyMesh. |
| 160 | |
| 161 | Raises: |
| 162 | KeyError: Attributes cannot be found in mesh. |
| 163 | |
| 164 | Example: |
| 165 | |
| 166 | >>> box = pymesh.generate_box_mesh() |
| 167 | >>> pymesh.save_mesh("tmp.stl", box, ascii=True) |
| 168 | """ |
| 169 | ext = os.path.splitext(filename)[1] |
| 170 | if ext == ".geogram": |
| 171 | PyMesh.save_geogram_mesh(filename, mesh.raw_mesh) |
| 172 | return |
| 173 | elif ext == ".svg": |
| 174 | save_svg(filename, mesh) |
| 175 | return |
| 176 | writer = PyMesh.MeshWriter.create(filename) |
| 177 | for attr in attributes: |
| 178 | if not mesh.has_attribute(attr): |
| 179 | raise KeyError("Attribute {} is not found in mesh".format(attr)) |
| 180 | writer.with_attribute(attr) |
| 181 | if setting.get("ascii", False): |
| 182 | writer.in_ascii() |
| 183 | if setting.get("use_float", False): |
| 184 | writer.use_float() |
| 185 | if setting.get("anonymous", False): |
| 186 | writer.set_anonymous() |
| 187 | writer.write_mesh(mesh.raw_mesh) |