Skip to content

Commit 479b05c

Browse files
authored
Merge pull request #242 from arpastrana/master
Pull Request for handling mesh loading from URDFs
2 parents 962842b + b99d6ae commit 479b05c

File tree

2 files changed

+63
-6
lines changed

2 files changed

+63
-6
lines changed

AUTHORS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@
1616

1717
- Kathrin Dörfler <<doerfler@arch.ethz.ch>> [@Kathrin3010](https://github.com/Kathrin3010)
1818
- Robin Oval <<oval@arch.ethz.ch>> [@robin-oval](https://github.com/robin-oval)
19+
- Rafael Pastrana <<pastrana@arch.ethz.ch>> [@arpastrana](https://github.com/arpastrana)

src/compas/robots/resources/basic.py

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
__all__ = [
1010
'AbstractMeshLoader',
1111
'DefaultMeshLoader',
12-
'LocalPackageMeshLoader',
12+
'LocalPackageMeshLoader'
1313
]
1414

1515
try:
@@ -55,12 +55,34 @@ def load_mesh(self, url):
5555

5656

5757
class DefaultMeshLoader(AbstractMeshLoader):
58-
"""Handles basic mesh loader tasks, mostly from local files."""
58+
"""Handles basic mesh loader tasks, mostly from local files.
5959
60-
def __init__(self):
60+
Attributes
61+
----------
62+
kwargs (optional): dict
63+
Additional keyword arguments.
64+
"""
65+
66+
def __init__(self, **kwargs):
6167
super(DefaultMeshLoader, self).__init__()
68+
self.attr = kwargs or dict()
6269

6370
def can_load_mesh(self, url):
71+
"""Determine whether this loader can load a given mesh URL.
72+
73+
Parameters
74+
----------
75+
url : str
76+
Mesh URL.
77+
78+
Returns
79+
-------
80+
bool
81+
``True`` if the URL points to a local and valid file.
82+
Otherwise ``False``.
83+
"""
84+
85+
url = self._get_mesh_url(url)
6486
scheme = urlparse(url).scheme
6587

6688
# Local files have either:
@@ -70,18 +92,52 @@ def can_load_mesh(self, url):
7092
is_local_file = len(scheme) in (0, 1) or scheme == 'file'
7193

7294
if is_local_file:
73-
return True
95+
if os.path.isfile(url):
96+
return True
7497

7598
# Only OBJ loader supports remote files atm
7699
is_obj = _get_file_format(url) == 'obj'
77100
return scheme in ('http', 'https') and is_obj
78101

79102
def load_mesh(self, url):
80-
if url.startswith('file:///'):
81-
url = url[8:]
103+
"""Loads a mesh from local storage.
82104
105+
Parameters
106+
----------
107+
url : str
108+
Mesh location
109+
110+
Returns
111+
-------
112+
:class:`Mesh`
113+
Instance of a mesh.
114+
"""
115+
url = self._get_mesh_url(url)
83116
return _mesh_import(url, url)
84117

118+
def _get_mesh_url(self, url):
119+
"""Concatenates basepath directory to URL only if defined in the keyword arguments.
120+
It also strips out the scheme 'file:///' from the URL if present.
121+
122+
Parameters
123+
----------
124+
url : str
125+
Mesh location.
126+
127+
Returns
128+
-------
129+
url: str
130+
Extended mesh url location if basepath in kwargs.
131+
Else, it returns url.
132+
"""
133+
if url.startswith('file:///'):
134+
url = url[8:]
135+
136+
basepath = self.attr.get('basepath')
137+
if basepath:
138+
return os.path.join(basepath, url)
139+
return url
140+
85141

86142
def _get_file_format(url):
87143
# This could be much more elaborate

0 commit comments

Comments
 (0)