From ee451cc68cf7e4af12dac34448b5cca3984213fd Mon Sep 17 00:00:00 2001 From: Luca Scheller Date: Fri, 24 Nov 2023 16:54:00 +0100 Subject: [PATCH] Control relative path exposure through env var --- CMakeLists.txt | 4 +-- src/CachedResolver/PythonExpose.py | 11 +++++--- src/CachedResolver/resolver.cpp | 25 +++++++++++-------- src/CachedResolver/resolver.h | 14 ++++++++++- src/CachedResolver/resolverContext.h | 1 - .../testenv/testCachedResolver.py | 3 +++ src/CachedResolver/wrapResolver.cpp | 2 +- 7 files changed, 41 insertions(+), 19 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c6dfbd6..442f988 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -52,8 +52,8 @@ set(AR_CACHEDRESOLVER_USD_PYTHON_EXPOSE_MODULE_NAME PythonExpose) set(AR_CACHEDRESOLVER_TARGET_LIB cachedResolver) set(AR_CACHEDRESOLVER_TARGET_PYTHON _${AR_CACHEDRESOLVER_TARGET_LIB}) set(AR_CACHEDRESOLVER_INSTALL_PREFIX ${AR_PROJECT_NAME}/${AR_CACHEDRESOLVER_USD_PLUGIN_NAME}) -set(AR_CACHEDRESOLVER_ENV_CONTEXT_BYPASS "AR_CONTEXT_BYPASS") -set(AR_CACHEDRESOLVER_ENV_CONTEXT_DIRTY "AR_CONTEXT_DIRTY") +set(AR_CACHEDRESOLVER_ENV_EXPOSE_RELATIVE_PATH_IDENTIFIERS "AR_EXPOSE_RELATIVE_PATH_IDENTIFIERS" CACHE STRING "Environment variable that controls if relative path identifiers should be Python exposed.") + # Http Resolver option(AR_HTTPRESOLVER_BUILD "Build the HttpResolver" OFF) if("$ENV{RESOLVER_NAME}" STREQUAL "httpResolver") diff --git a/src/CachedResolver/PythonExpose.py b/src/CachedResolver/PythonExpose.py index ef7dc44..23616a7 100644 --- a/src/CachedResolver/PythonExpose.py +++ b/src/CachedResolver/PythonExpose.py @@ -9,7 +9,7 @@ # Init logger logging.basicConfig(format="%(asctime)s %(message)s", datefmt="%Y/%m/%d %I:%M:%S%p") LOG = logging.getLogger("Python | {file_name}".format(file_name=__name__)) -LOG.setLevel(level=logging.INFO) +LOG.setLevel(level=logging.DEBUG) def log_function_args(func): @@ -27,12 +27,14 @@ def wrapper(*args, **kwargs): class UnitTestHelper: + create_relative_path_identifier_call_counter = 0 context_initialize_call_counter = 0 resolve_and_cache_call_counter = 0 current_directory_path = "" @classmethod def reset(cls, current_directory_path=""): + cls.create_relative_path_identifier_call_counter = 0 cls.context_initialize_call_counter = 0 cls.resolve_and_cache_call_counter = 0 cls.current_directory_path = current_directory_path @@ -55,9 +57,12 @@ def CreateRelativePathIdentifier(resolver, anchoredAssetPath, assetPath, anchorA Returns: str: The identifier. """ + LOG.debug("::: Resolver.CreateRelativePathIdentifier") + """The code below is only needed to verify that UnitTests work.""" + UnitTestHelper.create_relative_path_identifier_call_counter += 1 remappedRelativePathIdentifier = f"{assetPath[2:]}?{anchorAssetPath}" - resolver.AddCachedRelativePathIdentifierPair(anchoredAssetPath, remappedRelativePathIdentifier) - return remappedRelativePathIdentifier + resolver.AddCachedRelativePathIdentifierPair(anchoredAssetPath, anchoredAssetPath) + return anchoredAssetPath class ResolverContext: diff --git a/src/CachedResolver/resolver.cpp b/src/CachedResolver/resolver.cpp index 01b8071..531f0bb 100644 --- a/src/CachedResolver/resolver.cpp +++ b/src/CachedResolver/resolver.cpp @@ -85,7 +85,11 @@ _ResolveAnchored( return TfPathExists(resolvedPath) ? ArResolvedPath(TfAbsPath(resolvedPath)) : ArResolvedPath(); } -CachedResolver::CachedResolver() = default; +CachedResolver::CachedResolver() { + + this->SetExposeRelativePathIdentifierState(TfGetenvBool(DEFINE_STRING(AR_EXPOSE_RELATIVE_PATH_IDENTIFIERS), false)); + +}; CachedResolver::~CachedResolver() = default; @@ -138,19 +142,11 @@ CachedResolver::_CreateIdentifier( } const std::string anchoredAssetPath = _AnchorRelativePath(anchorAssetPath, assetPath); - // Anchor non file path based identifiers and see if a file exists. - // This is mostly for debugging as it allows us to add a file relative to our - // anchor directory that has a higher priority than our (usually unanchored) - // resolved asset path. - if (_IsNotFilePath(assetPath) && Resolve(anchoredAssetPath).empty()) { - return TfNormPath(assetPath); - } - // Re-direct to Python to allow optional re-routing of relative paths // through the resolver. - if (true) { + if (this->exposeRelativePathIdentifierState) { if (_IsFileRelativePath(assetPath)) { - auto cache_find = this->cachedRelativePathIdentifierPairs.find(anchorAssetPath); + auto cache_find = this->cachedRelativePathIdentifierPairs.find(anchoredAssetPath); if(cache_find != this->cachedRelativePathIdentifierPairs.end()){ return cache_find->second; }else{ @@ -178,6 +174,13 @@ CachedResolver::_CreateIdentifier( } } } + // Anchor non file path based identifiers and see if a file exists. + // This is mostly for debugging as it allows us to add a file relative to our + // anchor directory that has a higher priority than our (usually unanchored) + // resolved asset path. + if (_IsNotFilePath(assetPath) && Resolve(anchoredAssetPath).empty()) { + return TfNormPath(assetPath); + } return TfNormPath(anchoredAssetPath); } diff --git a/src/CachedResolver/resolver.h b/src/CachedResolver/resolver.h index aa2b5d0..abd8411 100644 --- a/src/CachedResolver/resolver.h +++ b/src/CachedResolver/resolver.h @@ -6,6 +6,7 @@ #include "resolverContext.h" #include "pxr/pxr.h" +#include "pxr/base/tf/getenv.h" #include "pxr/usd/ar/resolver.h" #include @@ -30,6 +31,16 @@ class CachedResolver final : public ArResolver AR_CACHEDRESOLVER_API virtual ~CachedResolver(); + AR_CACHEDRESOLVER_API + bool GetExposeRelativePathIdentifierState(){ return this->exposeRelativePathIdentifierState; } + AR_CACHEDRESOLVER_API + void SetExposeRelativePathIdentifierState(const bool state){ + if (state != this->exposeRelativePathIdentifierState){ + this->exposeRelativePathIdentifierState = state; + this->ClearCachedRelativePathIdentifierPairs(); + } + } + AR_CACHEDRESOLVER_API void AddCachedRelativePathIdentifierPair(const std::string& sourceStr, const std::string& targetStr); AR_CACHEDRESOLVER_API @@ -81,7 +92,8 @@ class CachedResolver final : public ArResolver private: const CachedResolverContext* _GetCurrentContextPtr() const; CachedResolverContext _fallbackContext; - const std::string emptyString{}; + const std::string emptyString{""}; + bool exposeRelativePathIdentifierState{false}; std::map cachedRelativePathIdentifierPairs; }; diff --git a/src/CachedResolver/resolverContext.h b/src/CachedResolver/resolverContext.h index ec6fdb0..efa1296 100644 --- a/src/CachedResolver/resolverContext.h +++ b/src/CachedResolver/resolverContext.h @@ -85,7 +85,6 @@ class CachedResolverContext private: std::shared_ptr data = std::make_shared(); - std::string emptyString{""}; bool _GetMappingPairsFromUsdFile(const std::string& filePath); }; diff --git a/src/CachedResolver/testenv/testCachedResolver.py b/src/CachedResolver/testenv/testCachedResolver.py index f47d6af..64dda71 100644 --- a/src/CachedResolver/testenv/testCachedResolver.py +++ b/src/CachedResolver/testenv/testCachedResolver.py @@ -19,6 +19,9 @@ def setUpClass(cls): def test_CreateIdentifier(self): resolver = Ar.GetResolver() + # Reset UnitTestHelper + #PythonExpose.UnitTestHelper.reset(current_directory_path=temp_dir_path) + # Test for invalid paths self.assertEqual("", resolver.CreateIdentifier("")) self.assertEqual( diff --git a/src/CachedResolver/wrapResolver.cpp b/src/CachedResolver/wrapResolver.cpp index 6f9f2e3..a0002f3 100644 --- a/src/CachedResolver/wrapResolver.cpp +++ b/src/CachedResolver/wrapResolver.cpp @@ -18,7 +18,7 @@ wrapResolver() class_, AR_BOOST_NAMESPACE::noncopyable> ("Resolver", no_init) - .def("GetCachedRelativePathIdentifierPairs", &This::AddCachedRelativePathIdentifierPair, return_value_policy(), "Returns all cached relative path identifier pairs as a dict") + .def("GetCachedRelativePathIdentifierPairs", &This::GetCachedRelativePathIdentifierPairs, return_value_policy(), "Returns all cached relative path identifier pairs as a dict") .def("AddCachedRelativePathIdentifierPair", &This::AddCachedRelativePathIdentifierPair, "Remove a cached relative path identifier pair by value") .def("RemoveCachedRelativePathIdentifierByKey", &This::RemoveCachedRelativePathIdentifierByKey, "Add a cached relative path identifier pair") .def("RemoveCachedRelativePathIdentifierByValue", &This::RemoveCachedRelativePathIdentifierByValue, "Remove a cached relative path identifier pair by key")