Skip to content

Commit c773862

Browse files
committed
Feature: Link Material (#162)
* Add material to link generator * Add material xacro macro * Catch exception for frame without material
1 parent 8e086e2 commit c773862

File tree

6 files changed

+114
-19
lines changed

6 files changed

+114
-19
lines changed

clearpath_generator_common/clearpath_generator_common/description/links.py

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
from clearpath_config.links.links import Link
3838
from clearpath_config.links.types.box import Box
3939
from clearpath_config.links.types.cylinder import Cylinder
40+
from clearpath_config.links.types.frame import Frame
4041
from clearpath_config.links.types.link import BaseLink
4142
from clearpath_config.links.types.mesh import Mesh
4243
from clearpath_config.links.types.sphere import Sphere
@@ -48,6 +49,9 @@ class BaseDescription():
4849

4950
NAME = 'name'
5051
PARENT_LINK = 'parent_link'
52+
MATERIAL_NAME = 'material_name'
53+
MATERIAL_COLOR = 'material_color'
54+
MATERIAL_TEXTURE = 'material_texture'
5155

5256
def __init__(self, link: BaseLink) -> None:
5357
self.link = link
@@ -60,6 +64,23 @@ def __init__(self, link: BaseLink) -> None:
6064
self.PARENT_LINK: self.link.parent
6165
}
6266

67+
if link.material.name:
68+
self.parameters.update({self.MATERIAL_NAME: link.material.name})
69+
if link.material.color:
70+
self.parameters.update({
71+
self.MATERIAL_COLOR: ' '.join(str(i) for i in link.material.color)})
72+
if not link.material.name:
73+
self.parameters.update({self.MATERIAL_NAME: f'{link.name}_material'})
74+
if link.material.texture:
75+
if link.material.texture.package:
76+
texture_file = os.path.join(
77+
'package://' + link.material.texture.package,
78+
File.clean(link.material.texture.path, make_abs=False))
79+
else:
80+
texture_file = (
81+
'file://' + File.clean(link.material.texture.path, make_abs=False))
82+
self.parameters.update({self.MATERIAL_TEXTURE: texture_file})
83+
6384
@property
6485
def xyz(self) -> List[float]:
6586
return self.link.xyz
@@ -102,19 +123,27 @@ class MeshDescription(BaseDescription):
102123

103124
def __init__(self, link: Mesh) -> None:
104125
super().__init__(link)
126+
105127
if (link.visual.package):
106-
self.parameters.update({
107-
self.VISUAL: os.path.join('package://' + link.visual.package,
108-
File.clean(link.visual.path, make_abs=False))
109-
})
128+
mesh_filename = os.path.join('package://' + link.visual.package,
129+
File.clean(link.visual.path, make_abs=False))
110130
else:
111-
self.parameters.update({
112-
self.VISUAL: 'file://' + File.clean(link.visual.path, make_abs=False)
113-
})
131+
mesh_filename = 'file://' + File.clean(link.visual.path, make_abs=False)
132+
133+
self.parameters.update({self.VISUAL: mesh_filename})
134+
135+
class FrameDescription(BaseDescription):
136+
137+
def __init__(self, link: Frame):
138+
super().__init__(link)
139+
self.parameters.pop(self.MATERIAL_NAME, None)
140+
self.parameters.pop(self.MATERIAL_COLOR, None)
141+
self.parameters.pop(self.MATERIAL_TEXTURE, None)
114142

115143
MODEL = {
116144
Link.BOX: BoxDescription,
117145
Link.CYLINDER: CylinderDescription,
146+
Link.FRAME: FrameDescription,
118147
Link.MESH: MeshDescription,
119148
Link.SPHERE: SphereDescription,
120149
}

clearpath_platform_description/urdf/common.urdf.xacro

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,28 @@ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSI
119119
iteration="${iteration + 1}"/>
120120
</xacro:if>
121121
</xacro:macro>
122+
123+
<xacro:macro name="clearpath_material" params="name:='' color_rgba:='' texture_file:=''">
124+
<xacro:if value="${name != ''}">
125+
<material name="${name}">
126+
<xacro:if value="${color_rgba != ''}">
127+
<color rgba="${color_rgba}"/>
128+
</xacro:if>
129+
<xacro:if value="${texture_file != ''}">
130+
<texture filename="${texture_file}"/>
131+
</xacro:if>
132+
</material>
133+
</xacro:if>
134+
135+
<xacro:unless value="${name != ''}">
136+
<material>
137+
<xacro:if value="${color_rgba != ''}">
138+
<color rgba="${color_rgba}"/>
139+
</xacro:if>
140+
<xacro:if value="${texture_file != ''}">
141+
<texture filename="${texture_file}"/>
142+
</xacro:if>
143+
</material>
144+
</xacro:unless>
145+
</xacro:macro>
122146
</robot>

clearpath_platform_description/urdf/links/box.urdf.xacro

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
<?xml version="1.0"?>
22
<robot xmlns:xacro="http://ros.org/wiki/xacro">
3-
<xacro:macro name="box" params="name parent_link size *origin">
3+
<xacro:macro name="box" params="
4+
name
5+
parent_link
6+
size
7+
material_name
8+
material_color:=''
9+
material_texture:=''
10+
*origin">
411
<link name="${name}_link">
512
<visual>
613
<geometry>
714
<box size="${size}"/>
815
</geometry>
9-
<material name="clearpath_dark_grey"/>
16+
<xacro:clearpath_material
17+
name="${material_name}"
18+
color_rgba="${material_color}"
19+
texture_file="${material_texture}"
20+
/>
1021
</visual>
1122
<collision>
1223
<geometry>
1324
<box size="${size}"/>
1425
</geometry>
15-
<material name="clearpath_dark_grey"/>
1626
</collision>
1727
</link>
1828

clearpath_platform_description/urdf/links/cylinder.urdf.xacro

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
<?xml version="1.0"?>
22
<robot xmlns:xacro="http://ros.org/wiki/xacro">
3-
<xacro:macro name="cylinder" params="name parent_link radius length *origin">
3+
<xacro:macro name="cylinder" params="
4+
name
5+
parent_link
6+
radius
7+
length
8+
material_name
9+
material_color:=''
10+
material_texture:=''
11+
*origin">
412
<link name="${name}_link">
513
<visual>
614
<geometry>
715
<cylinder radius="${radius}" length="${length}"/>
816
</geometry>
9-
<material name="clearpath_dark_grey"/>
17+
<xacro:clearpath_material
18+
name="${material_name}"
19+
color_rgba="${material_color}"
20+
texture_file="${material_texture}"
21+
/>
1022
</visual>
1123
<collision>
1224
<geometry>
1325
<cylinder radius="${radius}" length="${length}"/>
1426
</geometry>
15-
<material name="clearpath_dark_grey"/>
1627
</collision>
1728
</link>
1829

clearpath_platform_description/urdf/links/mesh.urdf.xacro

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
<?xml version="1.0"?>
22
<robot xmlns:xacro="http://ros.org/wiki/xacro">
3-
<xacro:macro name="mesh" params="name parent_link visual *origin">
3+
<xacro:macro name="mesh" params="
4+
name
5+
parent_link
6+
visual
7+
material_name
8+
material_color:=''
9+
material_texture:=''
10+
*origin"
11+
>
412
<link name="${name}_link">
513
<visual>
614
<geometry>
715
<mesh filename="${visual}"/>
816
</geometry>
9-
<material name="clearpath_dark_grey"/>
17+
<xacro:clearpath_material
18+
name="${material_name}"
19+
color_rgba="${material_color}"
20+
texture_file="${material_texture}"
21+
/>
1022
</visual>
1123
<collision>
1224
<geometry>
1325
<mesh filename="${visual}"/>
1426
</geometry>
15-
<material name="clearpath_dark_grey"/>
1627
</collision>
1728
</link>
1829

clearpath_platform_description/urdf/links/sphere.urdf.xacro

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
<?xml version="1.0"?>
22
<robot xmlns:xacro="http://ros.org/wiki/xacro">
3-
<xacro:macro name="sphere" params="name parent_link radius *origin">
3+
<xacro:macro name="sphere" params="
4+
name
5+
parent_link
6+
radius
7+
material_name
8+
material_color:=''
9+
material_texture:=''
10+
*origin">
411
<link name="${name}_link">
512
<visual>
613
<geometry>
714
<sphere radius="${radius}"/>
815
</geometry>
9-
<material name="clearpath_dark_grey"/>
16+
<xacro:clearpath_material
17+
name="${material_name}"
18+
color_rgba="${material_color}"
19+
texture_file="${material_texture}"
20+
/>
1021
</visual>
1122
<collision>
1223
<geometry>
1324
<sphere radius="${radius}"/>
1425
</geometry>
15-
<material name="clearpath_dark_grey"/>
1626
</collision>
1727
</link>
1828

0 commit comments

Comments
 (0)