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

USD to SDF: material fixed #895

Merged
merged 11 commits into from
Mar 24, 2022
6 changes: 5 additions & 1 deletion usd/src/usd_parser/USDData_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,19 @@ TEST(USDData, Constructor)
auto systemPaths = ignition::common::systemPaths();
systemPaths->AddFilePaths(sdf::testing::TestFile("usd"));

// Open a valid USD file
{
// Remove the material folder if there is already one created
ignition::common::removeAll(
ignition::common::joinPaths(ignition::common::cwd(), "materials"));
adlarkin marked this conversation as resolved.
Show resolved Hide resolved

sdf::testing::ScopeExit removeCopiedMaterials(
[]
{
ignition::common::removeAll(
ignition::common::joinPaths(ignition::common::cwd(), "materials"));
});

// Open a valid USD file
std::string filename = sdf::testing::TestFile("usd", "upAxisZ.usda");
sdf::usd::USDData usdData(filename);
EXPECT_EQ(0u, usdData.Init().size());
Expand Down
56 changes: 32 additions & 24 deletions usd/src/usd_parser/USDMaterial.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ namespace sdf
/////////////////////////////////////////////////
/// \brief Copy a file from one destination to another
/// \param[in] _ori The original file to copy
/// \param[in] _dest The destination for the copy of _ori
/// \param[inout] _dest The destination for the copy of _ori. If _dest
/// represents a file that already exists, a unique numeric suffix in the
/// form of _<number> will be appended to the end of the file name.
/// \return A list of UsdErrors. An empty list means no errors occurred when
/// copying _ori to _dest
UsdErrors copyFile(const std::string &_ori, std::string &_dest)
Expand All @@ -49,31 +51,37 @@ namespace sdf
// For example:
// /bar/foo.extension
// /bar/foo_X.extension

std::string tmpDest = _dest;
std::string parentPath = ignition::common::parentPath(_dest);
std::string::size_type fileExtensionIndex = tmpDest.rfind(".");

std::string fileExtension = "." + tmpDest.substr(fileExtensionIndex + 1);
std::string fileNameWithoutExtension = ignition::common::basename(_dest);
size_t pos = fileNameWithoutExtension.find(fileExtension);
if (pos != std::string::npos)
{
// If found then erase it from string
fileNameWithoutExtension.erase(pos, fileExtension.length());
}
int index = 0;
while (ignition::common::exists(tmpDest))
if (ignition::common::exists(_dest))
{
tmpDest = ignition::common::joinPaths(
parentPath,
fileNameWithoutExtension + "_" + std::to_string(index) +
fileExtension);
++index;
}

_dest = tmpDest;
const std::string parentPath = ignition::common::parentPath(_dest);
std::string::size_type fileExtensionIndex = _dest.rfind(".");
if (fileExtensionIndex == std::string::npos)
{
errors.emplace_back(
Error(ErrorCode::FILE_READ, "Unable to find the extension of the "
"file [" + _dest + "] which should be copied"));
return errors;
}

const std::string fileExtension = _dest.substr(fileExtensionIndex);
std::string fileNameWithoutExtension =
ignition::common::basename(_dest);
size_t pos = fileNameWithoutExtension.find(fileExtension);
if (pos != std::string::npos)
{
// If found then erase it from string
fileNameWithoutExtension.erase(pos, fileExtension.length());
}
int index = 0;
while (ignition::common::exists(_dest))
{
_dest = ignition::common::joinPaths(
parentPath,
fileNameWithoutExtension + "_" + std::to_string(index) +
fileExtension);
++index;
}
}
std::string baseName = ignition::common::basename(_dest);
std::string pathDest = ignition::common::replaceAll(_dest, baseName, "");
ignition::common::createDirectories(pathDest);
Expand Down