Skip to content

Commit

Permalink
Skip serializing nested model with //pose/@relative_to attribute (#1454)
Browse files Browse the repository at this point in the history
This PR prevents serialization of a model component if it has the //pose/@relative_to attribute to avoid flooding the console with error messages when there are multiple models with nested models using this attribute. Since model deserialization with //pose/@relative_to never worked, this should not affect existing behavior. I believe the model serialization / deserialization functionality is mainly used by the component editor. Related issue: #1071

Signed-off-by: Ian Chen <[email protected]>
  • Loading branch information
iche033 authored May 3, 2022
1 parent d57d9de commit c756b60
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
1 change: 1 addition & 0 deletions examples/worlds/sensors_demo.sdf
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@
<image>
<width>320</width>
<height>240</height>
<format>L8</format>
</image>
<clip>
<near>0.1</near>
Expand Down
31 changes: 28 additions & 3 deletions include/ignition/gazebo/components/Model.hh
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,38 @@ namespace serializers
sdf::ElementPtr modelElem = _model.Element();
if (!modelElem)
{
ignerr << "Unable to serialize sdf::Model" << std::endl;
ignwarn << "Unable to serialize sdf::Model" << std::endl;
return _out;
}

bool skip = false;
if (modelElem->HasElement("pose"))
{
sdf::ElementPtr poseElem = modelElem->GetElement("pose");
if (poseElem->HasAttribute("relative_to"))
{
// Skip serializing models with //pose/@relative_to attribute
// since deserialization will fail. This could be a nested model.
// see https://github.com/ignitionrobotics/ign-gazebo/issues/1071
// Once https://github.com/ignitionrobotics/sdformat/issues/820 is
// resolved, there should be an API that returns sdf::Errors objects
// instead of printing console msgs so it would be easier to ignore
// specific errors in Deserialize.
static bool warned = false;
if (!warned)
{
ignwarn << "Skipping serialization / deserialization for models "
<< "with //pose/@relative_to attribute."
<< std::endl;
warned = true;
}
skip = true;
}
}

_out << "<?xml version=\"1.0\" ?>"
<< "<sdf version='" << SDF_PROTOCOL_VERSION << "'>"
<< modelElem->ToString("")
<< (skip ? std::string() : modelElem->ToString(""))
<< "</sdf>";
return _out;
}
Expand All @@ -70,7 +95,7 @@ namespace serializers
sdf::Errors errors = root.LoadSdfString(sdf);
if (!root.Model())
{
ignerr << "Unable to unserialize sdf::Model" << std::endl;
ignwarn << "Unable to deserialize sdf::Model" << std::endl;
return _in;
}

Expand Down

0 comments on commit c756b60

Please sign in to comment.