trimesh.path.exchange package

Submodules

trimesh.path.exchange.dxf module

trimesh.path.exchange.dxf.bulge_to_arcs(lines, bulge, bulge_idx, is_closed=False, metadata=None)

Polylines can have “vertex bulge,” which means the polyline has an arc tangent to segments, rather than meeting at a vertex.

From Autodesk reference: The bulge is the tangent of one fourth the included angle for an arc segment, made negative if the arc goes clockwise from the start point to the endpoint. A bulge of 0 indicates a straight segment, and a bulge of 1 is a semicircle.

Parameters
  • lines ((n, 2) float) – Polyline vertices in order

  • bulge ((m,) float) – Vertex bulge value

  • bulge_idx ((m,) float) – Which index of lines is bulge associated with

  • is_closed (bool) – Is segment closed

  • metadata (None, or dict) – Entity metadata to add

Returns

  • vertices ((a, 2) float) – New vertices for poly-arc

  • entities ((b,) entities.Entity) – New entities, either line or arc

trimesh.path.exchange.dxf.export_dxf(path, layers=None)

Export a 2D path object to a DXF file

Parameters

path (trimesh.path.path.Path2D) –

Returns

export

Return type

str, path formatted as a DXF file

trimesh.path.exchange.dxf.get_key(blob, field, code)

Given a loaded (n, 2) blob and a field name get a value by code.

trimesh.path.exchange.dxf.load_dwg(file_obj, **kwargs)

Load DWG files by converting them to DXF files using TeighaFileConverter.

Parameters

file_obj (file- like object) –

Returns

loaded – kwargs for a Path2D constructor

Return type

dict

trimesh.path.exchange.dxf.load_dxf(file_obj, **kwargs)

Load a DXF file to a dictionary containing vertices and entities.

Parameters

file_obj (file or file- like object (has object.read method)) –

Returns

result

Return type

dict, keys are entities, vertices and metadata

trimesh.path.exchange.export module

trimesh.path.exchange.export.export_dict(path)

Export a path as a dict of kwargs for the Path constructor.

trimesh.path.exchange.export.export_path(path, file_type=None, file_obj=None, **kwargs)

Export a Path object to a file- like object, or to a filename

Parameters
  • file_obj (None, str, or file object) – A filename string or a file-like object

  • file_type (None or str) – File type, e.g.: ‘svg’, ‘dxf’

  • kwargs (passed to loader) –

Returns

exported – Data exported

Return type

str or bytes

trimesh.path.exchange.load module

trimesh.path.exchange.load.load_path(obj, file_type=None, **kwargs)

Load a file to a Path object.

Parameters
  • obj (One of the following:) –

    • Path, Path2D, or Path3D objects

    • open file object (dxf or svg)

    • file name (dxf or svg)

    • shapely.geometry.Polygon

    • shapely.geometry.MultiLineString

    • dict with kwargs for Path constructor

    • (n,2,(2|3)) float, line segments

  • file_type (str) – Type of file is required if file object passed.

Returns

path – Data as a native trimesh Path object

Return type

Path, Path2D, Path3D object

trimesh.path.exchange.load.path_formats()

Get a list of supported path formats.

Returns

loaders – Extensions of loadable formats, ie: [‘svg’, ‘dxf’]

Return type

list of str

trimesh.path.exchange.misc module

trimesh.path.exchange.misc.dict_to_path(as_dict)

Turn a pure dict into a dict containing entity objects that can be sent directly to a Path constructor.

Parameters

as_dict (dict) – Has keys: ‘vertices’, ‘entities’

Returns

kwargs – Has keys: ‘vertices’, ‘entities’

Return type

dict

trimesh.path.exchange.misc.edges_to_path(edges, vertices, **kwargs)

Given an edge list of indices and associated vertices representing lines, generate kwargs for a Path object.

Parameters
  • edges ((n, 2) int) – Vertex indices of line segments

  • vertices ((m, dimension) float) – Vertex positions where dimension is 2 or 3

Returns

kwargs

Return type

dict, kwargs for Path constructor

trimesh.path.exchange.misc.faces_to_path(mesh, face_ids=None, **kwargs)

Given a mesh and face indices find the outline edges and turn them into a Path3D.

Parameters
  • mesh (trimesh.Trimesh) – Triangulated surface in 3D

  • face_ids ((n,) int) – Indexes referencing mesh.faces

Returns

kwargs – Kwargs for Path3D constructor

Return type

dict

trimesh.path.exchange.misc.lines_to_path(lines)

Turn line segments into a Path2D or Path3D object.

Parameters

lines ((n, 2, dimension) or (n, dimension) float) – Line segments or connected polyline curve in 2D or 3D

Returns

kwargs – kwargs for Path constructor

Return type

dict

trimesh.path.exchange.misc.linestrings_to_path(multi)

Load shapely LineString objects into a trimesh.path.Path2D object

Parameters

multi (shapely.geometry.LineString or MultiLineString) – Input 2D geometry

Returns

kwargs – Keyword arguments for Path2D constructor

Return type

dict

trimesh.path.exchange.misc.polygon_to_path(polygon)

Load shapely Polygon objects into a trimesh.path.Path2D object

Parameters

polygon (shapely.geometry.Polygon) – Input geometry

Returns

kwargs – Keyword arguments for Path2D constructor

Return type

dict

trimesh.path.exchange.svg_io module

trimesh.path.exchange.svg_io.export_svg(drawing, return_path=False, layers=None, **kwargs)

Export a Path2D object into an SVG file.

Parameters
  • drawing (Path2D) – Source geometry

  • return_path (bool) – If True return only path string

  • layers (None, or [str]) – Only export specified layers

Returns

as_svg

Return type

str, XML formatted as SVG

trimesh.path.exchange.svg_io.svg_to_path(file_obj, file_type=None)

Load an SVG file into a Path2D object.

Parameters
  • file_obj (open file object) – Contains SVG data

  • file_type (None) – Not used

Returns

loaded – With kwargs for Path2D constructor

Return type

dict

trimesh.path.exchange.svg_io.transform_to_matrices(transform)

Convert an SVG transform string to an array of matrices.

> transform = “rotate(-10 50 100)

translate(-36 45.5) skewX(40) scale(1 0.5)”

Parameters

transform (str) – Contains transformation information in SVG form

Returns

matrices – Multiple transformation matrices from input transform string

Return type

(n, 3, 3) float

Module contents