OBJ

class compas.files.OBJ(filepath, precision=None)[source]

Bases: object

Class for working with OBJ files.

Currently, reading is only supported for polygonal geometry. Writing is only supported for meshes.

Parameters
  • filepath (path string | file-like object | URL string) – A path, a file-like object or a URL pointing to a file.

  • precision (str, optional) – A COMPAS precision specification.

Attributes
  • reader (OBJReader, read-only) – A OBJ file reader.

  • parser (OBJParser, read-only) – A OBJ data parser.

  • vertices (list[list[float]], read-only) – The vertices found in the parsed data.

  • lines (list[tuple[int, int]], read-only) – The lines found in the parsed data, as vertex pairs.

  • faces (list[list[int]], read-only) – The faces found in the parsed data, as lists of vertices.

  • objects (dict[str, tuple[list[list[float, float, float]], list[list[int]]]], read-only) – The objects found in the parsed data, as a mapping between object names and tuples of lists of vertices and faces.

References

Examples

Reading and writing of a single mesh.

>>> from compas.datastructures import Mesh
>>> from compas.files import OBJ

Write mesh data to a file.

>>> mesh = Mesh.from_polyhedron(12)
>>> obj = OBJ('mesh.obj')
>>> obj.write(mesh)

Read mesh data from a file.

>>> obj = OBJ('mesh.obj')
>>> obj.read()
>>> mesh = Mesh.from_vertices_and_faces(obj.vertices, obj.faces)

Reading and writing of multiple meshes as separate objects in a single OBJ file.

>>> from compas.geometry import Pointcloud, Translation
>>> from compas.datastructures import Mesh
>>> from compas.files import OBJ

Write mesh data to a file.

>>> meshes = []
>>> for point in Pointcloud.from_bounds(10, 10, 10, 100):
...     mesh = Mesh.from_polyhedron(12)
...     mesh.transform(Translation.from_vector(point))
...     meshes.append(mesh)
...
>>> obj = OBJ('meshes.obj')
>>> obj.write(meshes)

Read mesh data from a file.

>>> obj = OBJ('meshes.obj')
>>> obj.read()
>>> meshes = []
>>> for name in obj.objects:
...     mesh = Mesh.from_vertices_and_faces(* obj.objects[name])
...     mesh.name = name
...     meshes.append(mesh)
...

Methods

read

Read and parse the contents of the file.

write

Write a mesh to the file.