Skip to content

Commit

Permalink
Merge pull request #21 from clemense/clemense/bugfix_material_tag
Browse files Browse the repository at this point in the history
Bugfix: Material Tag
  • Loading branch information
clemense authored May 8, 2022
2 parents 6195d3c + 6a146c1 commit e79f86f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/yourdfpy/urdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,11 +419,14 @@ def apply_visual_color(
"""
if visual.material is None:
return
color = (
material_map[visual.material.name].color
if visual.material.name
else visual.material.color
)

if visual.material.color is not None:
color = visual.material.color
elif visual.material.name is not None and visual.material.name in material_map:
color = material_map[visual.material.name].color
else:
return

if color is None:
return
if isinstance(geom.visual, trimesh.visual.ColorVisuals):
Expand Down Expand Up @@ -951,7 +954,7 @@ def load(fname_or_file, **kwargs):

try:
parser = etree.XMLParser(remove_blank_text=True)
tree = etree.parse(fname_or_file, parser)
tree = etree.parse(fname_or_file, parser=parser)
xml_root = tree.getroot()
except Exception as e:
_logger.error(e)
Expand Down
48 changes: 48 additions & 0 deletions tests/test_urdf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest
import os
import io

from yourdfpy import urdf

Expand Down Expand Up @@ -54,3 +55,50 @@ def test_equality_different_link_order():
robot_1.links.append(urdf.Link(name="link_0"))

assert robot_0 == robot_1


def test_material_color():
urdf_str = """
<robot name="material_test">
<link name="link_0">
<visual>
<geometry>
<sphere radius="1" />
</geometry>
<material name="red_material">
<color rgba="1.0 0.0 0.0 1.0" />
</material>
</visual>
</link>
</robot>
"""
with io.StringIO(urdf_str) as f:
urdf_model = urdf.URDF.load(f)

assert urdf_model.robot.links[0].visuals[0].material.name == "red_material"
assert all(
urdf_model.robot.links[0].visuals[0].material.color.rgba == [1, 0, 0, 1]
)


def test_material_mapping():
urdf_str = """
<robot name="material_test">
<link name="link_0">
<visual>
<geometry>
<sphere radius="1" />
</geometry>
<material name="red_material" />
</visual>
</link>
<material name="red_material">
<color rgba="1.0 0.0 0.0 1.0" />
</material>
</robot>
"""
with io.StringIO(urdf_str) as f:
urdf_model = urdf.URDF.load(f)

assert urdf_model.robot.links[0].visuals[0].material.name == "red_material"
assert all(urdf_model._material_map["red_material"].color.rgba == [1, 0, 0, 1])

0 comments on commit e79f86f

Please sign in to comment.