Skip to content

Commit

Permalink
[urdf] Add no_mesh_load_mode
Browse files Browse the repository at this point in the history
  • Loading branch information
iory committed Oct 31, 2023
1 parent f09187b commit 874ca16
Showing 1 changed file with 27 additions and 14 deletions.
41 changes: 27 additions & 14 deletions skrobot/utils/urdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@


logger = getLogger(__name__)
_CONFIGURABLE_VALUES = {"mesh_simplify_factor": np.inf}
_CONFIGURABLE_VALUES = {"mesh_simplify_factor": np.inf,
'no_mesh_load_mode': False,
}


@contextlib.contextmanager
Expand All @@ -50,6 +52,13 @@ def mesh_simplify_factor(factor):
_CONFIGURABLE_VALUES["mesh_simplify_factor"] = np.inf


@contextlib.contextmanager
def no_mesh_load_mode():
_CONFIGURABLE_VALUES["no_mesh_load_mode"] = True
yield
_CONFIGURABLE_VALUES["no_mesh_load_mode"] = False


def parse_origin(node):
"""Find the ``origin`` subelement of an XML node and convert it
Expand Down Expand Up @@ -615,7 +624,8 @@ def meshes(self):
that represent this object.
"""
if len(self._meshes) == 0:
self._meshes = [trimesh.creation.box(extents=self.size)]
if _CONFIGURABLE_VALUES['no_mesh_load_mode'] is False:
self._meshes = [trimesh.creation.box(extents=self.size)]
return self._meshes


Expand Down Expand Up @@ -674,9 +684,10 @@ def meshes(self):
that represent this object.
"""
if len(self._meshes) == 0:
self._meshes = [trimesh.creation.cylinder(
radius=self.radius, height=self.length
)]
if _CONFIGURABLE_VALUES['no_mesh_load_mode'] is False:
self._meshes = [trimesh.creation.cylinder(
radius=self.radius, height=self.length
)]
return self._meshes


Expand Down Expand Up @@ -717,7 +728,8 @@ def meshes(self):
that represent this object.
"""
if len(self._meshes) == 0:
self._meshes = [trimesh.creation.icosphere(radius=self.radius)]
if _CONFIGURABLE_VALUES['no_mesh_load_mode'] is False:
self._meshes = [trimesh.creation.icosphere(radius=self.radius)]
return self._meshes


Expand Down Expand Up @@ -790,8 +802,6 @@ def meshes(self, value):
value = load_meshes(value)
elif isinstance(value, (list, tuple, set)):
value = list(value)
if len(value) == 0:
raise ValueError('Mesh must have at least one trimesh.Trimesh')
for m in value:
if not isinstance(m, trimesh.Trimesh):
raise TypeError('Mesh requires a trimesh.Trimesh or a '
Expand All @@ -810,12 +820,15 @@ def _from_xml(cls, node, path):
# visual ones separate to preserve colors and textures
fn = get_filename(path, kwargs['filename'])
combine = node.getparent().getparent().tag == Collision._TAG
meshes = load_meshes(fn)
if combine:
# Delete visuals for simplicity
for m in meshes:
m.visual = trimesh.visual.ColorVisuals(mesh=m)
meshes = [meshes[0] + meshes[1:]]
if _CONFIGURABLE_VALUES['no_mesh_load_mode'] is False:
meshes = load_meshes(fn)
if combine:
# Delete visuals for simplicity
for m in meshes:
m.visual = trimesh.visual.ColorVisuals(mesh=m)
meshes = [meshes[0] + meshes[1:]]
else:
meshes = []
kwargs['meshes'] = meshes

return Mesh(**kwargs)
Expand Down

0 comments on commit 874ca16

Please sign in to comment.