Skip to content

Commit 1939751

Browse files
committed
assign correct MaterialPtr to all visuals
So far, only the first visual in visual_array got the correct MaterialPtr assigned.
1 parent 06f5f9b commit 1939751

File tree

2 files changed

+34
-29
lines changed

2 files changed

+34
-29
lines changed

urdf_parser/src/link.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -380,9 +380,7 @@ bool parseVisual(Visual &vis, TiXmlElement *config)
380380
// try to parse material element in place
381381
vis.material.reset(new Material());
382382
if (!parseMaterial(*vis.material, mat, true))
383-
{
384-
CONSOLE_BRIDGE_logDebug("urdfdom: material has only name, actual material definition may be in the model");
385-
}
383+
vis.material.reset();
386384
}
387385

388386
return true;

urdf_parser/src/model.cpp

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,33 @@ ModelInterfaceSharedPtr parseURDFFile(const std::string &path)
5959
return urdf::parseURDF( xml_str );
6060
}
6161

62+
bool assignMaterial(const VisualSharedPtr& visual, const ModelInterfaceSharedPtr& model, const char* link_name)
63+
{
64+
if (visual->material_name.empty())
65+
return true;
66+
67+
const MaterialSharedPtr& material = model->getMaterial(visual->material_name);
68+
if (material)
69+
{
70+
CONSOLE_BRIDGE_logDebug("urdfdom: setting link '%s' material to '%s'", link_name, visual->material_name.c_str());
71+
visual->material = material;
72+
}
73+
else
74+
{
75+
if (visual->material)
76+
{
77+
CONSOLE_BRIDGE_logDebug("urdfdom: link '%s' material '%s' defined in Visual.", link_name,visual->material_name.c_str());
78+
model->materials_.insert(make_pair(visual->material->name,visual->material));
79+
}
80+
else
81+
{
82+
CONSOLE_BRIDGE_logError("link '%s' material '%s' undefined.", link_name,visual->material_name.c_str());
83+
model.reset();
84+
return false;
85+
}
86+
}
87+
}
88+
6289
ModelInterfaceSharedPtr parseURDF(const std::string &xml_string)
6390
{
6491
ModelInterfaceSharedPtr model(new ModelInterface);
@@ -137,33 +164,13 @@ ModelInterfaceSharedPtr parseURDF(const std::string &xml_string)
137164
}
138165
else
139166
{
140-
// set link visual material
167+
// set link visual(s) material
141168
CONSOLE_BRIDGE_logDebug("urdfdom: setting link '%s' material", link->name.c_str());
142-
if (link->visual)
143-
{
144-
if (!link->visual->material_name.empty())
145-
{
146-
if (model->getMaterial(link->visual->material_name))
147-
{
148-
CONSOLE_BRIDGE_logDebug("urdfdom: setting link '%s' material to '%s'", link->name.c_str(),link->visual->material_name.c_str());
149-
link->visual->material = model->getMaterial( link->visual->material_name.c_str() );
150-
}
151-
else
152-
{
153-
if (link->visual->material)
154-
{
155-
CONSOLE_BRIDGE_logDebug("urdfdom: link '%s' material '%s' defined in Visual.", link->name.c_str(),link->visual->material_name.c_str());
156-
model->materials_.insert(make_pair(link->visual->material->name,link->visual->material));
157-
}
158-
else
159-
{
160-
CONSOLE_BRIDGE_logError("link '%s' material '%s' undefined.", link->name.c_str(),link->visual->material_name.c_str());
161-
model.reset();
162-
return model;
163-
}
164-
}
165-
}
166-
}
169+
if (link->visual && !assignMaterial(link->visual, model, link->name.c_str()))
170+
return model;
171+
for (const auto& visual : link->visual_array)
172+
if (!assignMaterial(visual, model, link->name.c_str()))
173+
return model;
167174

168175
model->links_.insert(make_pair(link->name,link));
169176
CONSOLE_BRIDGE_logDebug("urdfdom: successfully added a new link '%s'", link->name.c_str());

0 commit comments

Comments
 (0)