Skip to content

Commit

Permalink
Control relative path exposure through env var
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaScheller committed Nov 24, 2023
1 parent 5c53805 commit ee451cc
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 19 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
11 changes: 8 additions & 3 deletions src/CachedResolver/PythonExpose.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
Expand All @@ -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:
Expand Down
25 changes: 14 additions & 11 deletions src/CachedResolver/resolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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);
}

Expand Down
14 changes: 13 additions & 1 deletion src/CachedResolver/resolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "resolverContext.h"

#include "pxr/pxr.h"
#include "pxr/base/tf/getenv.h"
#include "pxr/usd/ar/resolver.h"

#include <memory>
Expand All @@ -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
Expand Down Expand Up @@ -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<std::string, std::string> cachedRelativePathIdentifierPairs;
};

Expand Down
1 change: 0 additions & 1 deletion src/CachedResolver/resolverContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ class CachedResolverContext

private:
std::shared_ptr<CachedResolverContextInternalData> data = std::make_shared<CachedResolverContextInternalData>();
std::string emptyString{""};
bool _GetMappingPairsFromUsdFile(const std::string& filePath);
};

Expand Down
3 changes: 3 additions & 0 deletions src/CachedResolver/testenv/testCachedResolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion src/CachedResolver/wrapResolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ wrapResolver()

class_<This, bases<ArResolver>, AR_BOOST_NAMESPACE::noncopyable>
("Resolver", no_init)
.def("GetCachedRelativePathIdentifierPairs", &This::AddCachedRelativePathIdentifierPair, return_value_policy<return_by_value>(), "Returns all cached relative path identifier pairs as a dict")
.def("GetCachedRelativePathIdentifierPairs", &This::GetCachedRelativePathIdentifierPairs, return_value_policy<return_by_value>(), "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")
Expand Down

0 comments on commit ee451cc

Please sign in to comment.