Coverage for trimesh/path/exchange/load.py: 94%
34 statements
« prev ^ index » next coverage.py v7.14.1, created at 2026-06-24 04:40 +0000
« prev ^ index » next coverage.py v7.14.1, created at 2026-06-24 04:40 +0000
1from ... import util
2from ...exceptions import ExceptionWrapper
3from ...exchange.ply import load_ply
4from ..path import Path
5from . import misc
6from .dxf import _dxf_loaders
7from .svg_io import _svg_loaders
10def load_path(file_obj, file_type: str | None = None, **kwargs):
11 """
12 Load a file to a Path file_object.
14 Parameters
15 -----------
16 file_obj
17 Accepts many types:
18 - Path, Path2D, or Path3D file_objects
19 - open file file_object (dxf or svg)
20 - file name (dxf or svg)
21 - shapely.geometry.Polygon
22 - shapely.geometry.MultiLineString
23 - dict with kwargs for Path constructor
24 - `(n, 2, (2|3)) float` line segments
25 file_type
26 Type of file is required if file
27 object is passed.
29 Returns
30 ---------
31 path : Path, Path2D, Path3D file_object
32 Data as a native trimesh Path file_object
33 """
34 # avoid a circular import
35 from ...exchange.load import _load_kwargs, _parse_file_args
37 arg = _parse_file_args(file_obj=file_obj, file_type=file_type, **kwargs)
39 if isinstance(file_obj, Path):
40 # we have been passed a file object that is already a loaded
41 # trimesh.path.Path object so do nothing and return
42 return file_obj
43 elif util.is_file(arg.file_obj):
44 if arg.file_type in path_loaders:
45 kwargs.update(
46 path_loaders[arg.file_type](
47 file_obj=arg.file_obj, file_type=arg.file_type
48 )
49 )
50 elif arg.file_type == "ply":
51 # we cannot register this exporter to path_loaders since
52 # this is already reserved by Trimesh in ply format in trimesh.load()
53 kwargs.update(load_ply(file_obj=arg.file_obj, file_type=arg.file_type))
54 elif util.is_instance_named(file_obj, ["Polygon", "MultiPolygon"]):
55 # convert from shapely polygons to Path2D
56 kwargs.update(misc.polygon_to_path(file_obj))
57 elif util.is_instance_named(file_obj, "MultiLineString"):
58 # convert from shapely LineStrings to Path2D
59 kwargs.update(misc.linestrings_to_path(file_obj))
60 elif isinstance(file_obj, dict):
61 # load as kwargs
62 kwargs = file_obj
63 elif util.is_sequence(file_obj):
64 # load as lines in space
65 kwargs.update(misc.lines_to_path(file_obj))
66 else:
67 raise ValueError("Not a supported object type!")
69 # actually load
70 result = _load_kwargs(kwargs)
71 result._source = arg
73 return result
76def path_formats() -> set[str]:
77 """
78 Get a list of supported path formats.
80 Returns
81 ------------
82 loaders
83 Extensions of loadable formats, i.e. {'svg', 'dxf'}
84 """
86 return {k for k, v in path_loaders.items() if not isinstance(v, ExceptionWrapper)}
89path_loaders = {}
90path_loaders.update(_svg_loaders)
91path_loaders.update(_dxf_loaders)