Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error resolving gazebo classic material when loading world #2492

Merged
merged 6 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/SdfEntityCreator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
#include "gz/sim/components/World.hh"

#include "rendering/MaterialParser/MaterialParser.hh"
#include "ServerPrivate.hh"

class gz::sim::SdfEntityCreatorPrivate
{
Expand Down Expand Up @@ -808,7 +809,8 @@ Entity SdfEntityCreator::CreateEntities(const sdf::Visual *_visual)
"https://gazebosim.org/api/sim/8/migrationsdf.html#:~:text=Materials " <<
"for details." << std::endl;
std::string scriptUri = visualMaterial.ScriptUri();
if (scriptUri != "file://media/materials/scripts/gazebo.material") {
if (scriptUri != ServerPrivate::kClassicMaterialScriptUri)
{
gzwarn << "Custom material scripts are not supported."
<< std::endl;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ Server::Server(const ServerConfig &_config)
// 'src/gui_main.cc'.
errors = sdfRoot.Load(filePath, sdfParserConfig);
if (errors.empty() || _config.BehaviorOnSdfErrors() !=
ServerConfig::SdfErrorBehavior::EXIT_IMMEDIATELY) {
ServerConfig::SdfErrorBehavior::EXIT_IMMEDIATELY)
{
if (sdfRoot.Model() == nullptr) {
this->dataPtr->sdfRoot = std::move(sdfRoot);
}
Expand Down
9 changes: 9 additions & 0 deletions src/ServerPrivate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
using namespace gz;
using namespace sim;

const char ServerPrivate::kClassicMaterialScriptUri[] =
"file://media/materials/scripts/gazebo.material";

/// \brief This struct provides access to the record plugin SDF string
struct LoggingPlugin
{
Expand Down Expand Up @@ -546,6 +549,12 @@ bool ServerPrivate::ResourcePathsResolveService(
//////////////////////////////////////////////////
std::string ServerPrivate::FetchResource(const std::string &_uri)
{
// Handle gazebo classic material URIs.
// Return original URI string as the SdfEntityCreator checks for this URI
if (_uri == kClassicMaterialScriptUri)
return _uri;

// Fetch resource from fuel
auto path =
fuel_tools::fetchResourceWithClient(_uri, *this->fuelClient.get());

Expand Down
5 changes: 5 additions & 0 deletions src/ServerPrivate.hh
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@ namespace gz
/// Server. It is used in the SDFormat world generator when saving worlds
public: std::unordered_map<std::string, std::string> fuelUriMap;

/// \brief Gazebo classic material URI string
/// A URI matching this string indicates that it is a gazebo classic
/// material.
public: static const char kClassicMaterialScriptUri[];

/// \brief List of names for all worlds loaded in this server.
private: std::vector<std::string> worldNames;

Expand Down
27 changes: 27 additions & 0 deletions test/integration/material.cc
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,30 @@ TEST_F(MaterialTest, InvalidColor)
EXPECT_EQ(math::Color(0.0f, 0.0f, 0.0f, 1.0f),
boxVisualComp->Data().Specular());
}

TEST_F(MaterialTest, WorldWithClassicMaterial)
{
ServerConfig serverConfig;
serverConfig.SetSdfFile(common::joinPaths(PROJECT_SOURCE_PATH,
"test", "worlds", "classic_material.sdf"));

std::cout << "Loading: " << serverConfig.SdfFile() << std::endl;
this->StartServer(serverConfig);

auto model = this->GetModel("box");
ASSERT_TRUE(model.Valid(*this->ecm));

auto boxVisualEntity =
this->ecm->EntityByComponents(components::Name("box_visual"));
ASSERT_NE(kNullEntity, boxVisualEntity);

// Blue color
auto boxVisualComp =
this->ecm->Component<components::Material>(boxVisualEntity);
EXPECT_EQ(math::Color(0.0f, 0.0f, 1.0f, 1.0f),
boxVisualComp->Data().Ambient());
EXPECT_EQ(math::Color(0.0f, 0.0f, 1.0f, 1.0f),
boxVisualComp->Data().Diffuse());
EXPECT_EQ(math::Color(0.1f, 0.1f, 0.1f, 1.0f),
boxVisualComp->Data().Specular());
}
32 changes: 32 additions & 0 deletions test/worlds/classic_material.sdf
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" ?>
<sdf version="1.6">
<world name="default">

<model name="box">
<link name="box_link">
<collision name="box_collision">
<geometry>
<box>
<size>1 1 1</size>
</box>
</geometry>
</collision>

<visual name="box_visual">
<geometry>
<box>
<size>1 1 1</size>
</box>
</geometry>
<material>
<script>
<uri>file://media/materials/scripts/gazebo.material</uri>
<name>Gazebo/Blue</name>
</script>
</material>
</visual>
</link>
</model>

</world>
</sdf>
Loading