|
1 |
| -import os |
2 |
| - |
3 | 1 | from ... import util
|
4 | 2 | from ...exchange.ply import load_ply
|
| 3 | +from ...typed import Optional |
5 | 4 | from ..path import Path
|
6 | 5 | from . import misc
|
7 | 6 | from .dxf import _dxf_loaders
|
8 | 7 | from .svg_io import svg_to_path
|
9 | 8 |
|
10 | 9 |
|
11 |
| -def load_path(file_obj, file_type=None, **kwargs): |
| 10 | +def load_path(file_obj, file_type: Optional[str] = None, **kwargs): |
12 | 11 | """
|
13 | 12 | Load a file to a Path file_object.
|
14 | 13 |
|
15 | 14 | Parameters
|
16 | 15 | -----------
|
17 |
| - file_obj : One of the following: |
| 16 | + file_obj |
| 17 | + Accepts many types: |
18 | 18 | - Path, Path2D, or Path3D file_objects
|
19 | 19 | - open file file_object (dxf or svg)
|
20 | 20 | - file name (dxf or svg)
|
21 | 21 | - shapely.geometry.Polygon
|
22 | 22 | - shapely.geometry.MultiLineString
|
23 | 23 | - dict with kwargs for Path constructor
|
24 |
| - - (n,2,(2|3)) float, line segments |
25 |
| - file_type : str |
| 24 | + - `(n, 2, (2|3)) float` line segments |
| 25 | + file_type |
26 | 26 | Type of file is required if file
|
27 |
| - file_object passed. |
| 27 | + object is passed. |
28 | 28 |
|
29 | 29 | Returns
|
30 | 30 | ---------
|
31 | 31 | path : Path, Path2D, Path3D file_object
|
32 |
| - Data as a native trimesh Path file_object |
| 32 | + Data as a native trimesh Path file_object |
33 | 33 | """
|
34 | 34 | # avoid a circular import
|
35 | 35 | from ...exchange.load import load_kwargs
|
36 | 36 |
|
| 37 | + if isinstance(file_type, str): |
| 38 | + # we accept full file names here so make sure we |
| 39 | + file_type = util.split_extension(file_type).lower() |
| 40 | + |
37 | 41 | # record how long we took
|
38 | 42 | tic = util.now()
|
39 | 43 |
|
40 | 44 | if isinstance(file_obj, Path):
|
41 |
| - # we have been passed a Path file_object so |
42 |
| - # do nothing and return the passed file_object |
| 45 | + # we have been passed a file object that is already a loaded |
| 46 | + # trimesh.path.Path object so do nothing and return |
43 | 47 | return file_obj
|
44 | 48 | elif util.is_file(file_obj):
|
45 | 49 | # for open file file_objects use loaders
|
46 | 50 | if file_type == "ply":
|
47 |
| - # we cannot register this exporter to path_loaders since this is already reserved by TriMesh in ply format in trimesh.load() |
| 51 | + # we cannot register this exporter to path_loaders |
| 52 | + # since this is already reserved for 3D values in `trimesh.load` |
48 | 53 | kwargs.update(load_ply(file_obj, file_type=file_type))
|
49 | 54 | else:
|
50 | 55 | kwargs.update(path_loaders[file_type](file_obj, file_type=file_type))
|
51 | 56 | elif isinstance(file_obj, str):
|
52 | 57 | # strings passed are evaluated as file file_objects
|
53 | 58 | with open(file_obj, "rb") as f:
|
54 | 59 | # get the file type from the extension
|
55 |
| - file_type = os.path.splitext(file_obj)[-1][1:].lower() |
| 60 | + file_type = util.split_extension(file_obj).lower() |
56 | 61 | if file_type == "ply":
|
57 | 62 | # we cannot register this exporter to path_loaders since this is already reserved by TriMesh in ply format in trimesh.load()
|
58 | 63 | kwargs.update(load_ply(f, file_type=file_type))
|
|
0 commit comments