Cleanup: USD: Remove duplicate implementation of get_unique_path

Put it in a common place.

Pull Request: https://projects.blender.org/blender/blender/pulls/136953
This commit is contained in:
Jesse Yurkovich
2025-04-03 21:27:15 +02:00
committed by Jesse Yurkovich
parent 0e840cf065
commit 778e96c033
4 changed files with 23 additions and 32 deletions

View File

@@ -6,6 +6,7 @@
#include "usd.hh"
#include "usd_hash_types.hh"
#include "usd_utils.hh"
#include "BLI_map.hh"
#include "BLI_set.hh"
@@ -21,23 +22,6 @@
#include "CLG_log.h"
static CLG_LogRef LOG = {"io.usd"};
namespace {
/* If the given path already exists on the given stage, return the path with
* a numerical suffix appended to the name that ensures the path is unique.
* If the path does not exist on the stage, it will be returned unchanged. */
pxr::SdfPath get_unique_path(pxr::UsdStageRefPtr stage, const std::string &path)
{
std::string unique_path = path;
int suffix = 2;
while (stage->GetPrimAtPath(pxr::SdfPath(unique_path)).IsValid()) {
unique_path = path + std::to_string(suffix++);
}
return pxr::SdfPath(unique_path);
}
} // End anonymous namespace
namespace blender::io::usd {
/* We need an ordered map so we use std::map. */

View File

@@ -7,6 +7,7 @@
#include "usd.hh"
#include "usd_asset_utils.hh"
#include "usd_private.hh"
#include "usd_utils.hh"
#include "usd_writer_material.hh"
#include <pxr/base/gf/rotation.h>
@@ -111,21 +112,6 @@ struct WorldNtreeSearchResults {
namespace blender::io::usd {
/**
* If the given path already exists on the given stage, return the path with
* a numerical suffix appended to the name that ensures the path is unique. If
* the path does not exist on the stage, it will be returned unchanged.
*/
static pxr::SdfPath get_unique_path(pxr::UsdStageRefPtr stage, const std::string &path)
{
std::string unique_path = path;
int suffix = 2;
while (stage->GetPrimAtPath(pxr::SdfPath(unique_path)).IsValid()) {
unique_path = path + std::to_string(suffix++);
}
return pxr::SdfPath(unique_path);
}
/**
* Load the image at the given path. Handle packing and copying based in the import options.
* Return the opened image on success or a nullptr on failure.

View File

@@ -56,4 +56,15 @@ std::string make_safe_name(const StringRef name, bool allow_unicode)
return {buf.data(), offset};
}
pxr::SdfPath get_unique_path(pxr::UsdStageRefPtr stage, const std::string &path)
{
std::string unique_path = path;
int suffix = 2;
while (stage->GetPrimAtPath(pxr::SdfPath(unique_path)).IsValid()) {
unique_path = path + std::to_string(suffix++);
}
return pxr::SdfPath(unique_path);
}
} // namespace blender::io::usd

View File

@@ -18,4 +18,14 @@ namespace blender::io::usd {
*/
std::string make_safe_name(StringRef name, bool allow_unicode);
/* Return a unique USD `SdfPath`. If the given path already exists on the given stage, return
* the path with a numerical suffix appended to the name that ensures the path is unique.
* If the path does not exist on the stage, it will be returned unchanged.
*
* \param stage: The stage
* \param path: The original path
* \return A valid, and unique, USD `SdfPath`
*/
pxr::SdfPath get_unique_path(pxr::UsdStageRefPtr stage, const std::string &path);
} // namespace blender::io::usd