Skip to content

Commit

Permalink
Fix error resolving gazebo classic material
Browse files Browse the repository at this point in the history
Signed-off-by: Ian Chen <ichen@openrobotics.org>
  • Loading branch information
iche033 committed Jul 22, 2024
1 parent d0c0322 commit 0e59f92
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 3 deletions.
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
5 changes: 3 additions & 2 deletions 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 Expand Up @@ -197,7 +198,7 @@ Server::Server(const ServerConfig &_config)
}
}

if (!errors.empty())
if (!errors.empty()/* && !containsOnlyGazeboClassicMaterialScriptErrors(errors) */)
{
for (auto &err : errors)
gzerr << err << "\n";
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 std::string 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)
{
// Check if it is a gazebo classic material URI
// Return original URI string. The MaterialParser will handle 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
/// Only gazebo classic material script uri matching this still will be
/// migrated to gz.
public: static const std::string 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());
}

0 comments on commit 0e59f92

Please sign in to comment.