Skip to content

Commit

Permalink
Remove file exists check for CachedResolver resolved paths
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaScheller committed Jan 13, 2025
1 parent fdd9a11 commit 26a207d
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 83 deletions.
46 changes: 22 additions & 24 deletions src/CachedResolver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,20 @@ endif()
### Targets ###
## Target library > cachedResolver ##
add_library(${AR_CACHEDRESOLVER_TARGET_LIB}
SHARED
debugCodes.cpp
resolver.cpp
resolverContext.cpp
resolverTokens.cpp
# Since when our resolver calls into Python it passes the ResolverContext,
# we need to ensure that Python has loaded the ResolverContext C++ representation.
# While we do have duplicate compiled code by adding the below, this allows us to not do hacky workarounds like
# LD_PRELOAD-ing the lib. See https://github.com/LucaScheller/VFX-UsdAssetResolver/issues/3 for more information.
# If you know a better way around this please file a PR :)
module.cpp
moduleDeps.cpp
wrapResolver.cpp
wrapResolverContext.cpp
wrapResolverTokens.cpp
debugCodes.cpp
resolver.cpp
resolverContext.cpp
resolverTokens.cpp
# Since when our resolver calls into Python it passes the ResolverContext,
# we need to ensure that Python has loaded the ResolverContext C++ representation.
# While we do have duplicate compiled code by adding the below, this allows us to not do hacky workarounds like
# LD_PRELOAD-ing the lib. See https://github.com/LucaScheller/VFX-UsdAssetResolver/issues/3 for more information.
# If you know a better way around this please file a PR :)
module.cpp
moduleDeps.cpp
wrapResolver.cpp
wrapResolverContext.cpp
wrapResolverTokens.cpp
)
set_boost_namespace(${AR_CACHEDRESOLVER_TARGET_LIB})
# Libs
Expand Down Expand Up @@ -66,15 +65,14 @@ install(TARGETS ${AR_CACHEDRESOLVER_TARGET_LIB} DESTINATION ${AR_CACHEDRESOLVER_

## Target library > cachedResolver Python ##
add_library(${AR_CACHEDRESOLVER_TARGET_PYTHON}
SHARED
module.cpp
moduleDeps.cpp
# resolver.cpp
resolverTokens.cpp
resolverContext.cpp
wrapResolver.cpp
wrapResolverContext.cpp
wrapResolverTokens.cpp
module.cpp
moduleDeps.cpp
# resolver.cpp
resolverTokens.cpp
resolverContext.cpp
wrapResolver.cpp
wrapResolverContext.cpp
wrapResolverTokens.cpp
)
add_dependencies(${AR_CACHEDRESOLVER_TARGET_PYTHON} ${AR_CACHEDRESOLVER_TARGET_LIB})
set_boost_namespace(${AR_CACHEDRESOLVER_TARGET_PYTHON})
Expand Down
6 changes: 4 additions & 2 deletions src/CachedResolver/PythonExpose.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,16 @@ def ResolveAndCache(context, assetPath):
LOG.debug(
"::: ResolverContext.ResolveAndCache | {} | {}".format(assetPath, context.GetCachingPairs())
)
"""Implement custom resolve logic and add the resolved path to the cache.
resolved_asset_path = "/some/path/to/a/file.usd"
context.AddCachingPair(assetPath, resolved_asset_path)
"""
To clear the context cache call:
# To clear the context cache call:
context.ClearCachingPairs()
"""
"""The code below is only needed to verify that UnitTests work."""
UnitTestHelper.resolve_and_cache_call_counter += 1
resolved_asset_path = "/some/path/to/a/file.usd"
context.AddCachingPair(assetPath, resolved_asset_path)
if assetPath == "unittest.usd":
current_dir_path = UnitTestHelper.current_directory_path
asset_a_file_path = os.path.join(current_dir_path, "assetA.usd")
Expand Down
32 changes: 5 additions & 27 deletions src/CachedResolver/resolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ _AnchorRelativePath(
const std::string& path)
{
if (TfIsRelativePath(anchorPath) ||
!_IsRelativePath(path)) {
!_IsFileRelativePath(path)) {
return path;
}
// Ensure we are using forward slashes and not back slashes.
Expand All @@ -74,18 +74,6 @@ _AnchorRelativePath(
return TfNormPath(anchoredPath);
}

static ArResolvedPath
_ResolveAnchored(
const std::string& anchorPath,
const std::string& path)
{
std::string resolvedPath = path;
if (!anchorPath.empty()) {
resolvedPath = TfStringCatPaths(anchorPath, path);
}
return TfPathExists(resolvedPath) ? ArResolvedPath(TfAbsPath(resolvedPath)) : ArResolvedPath();
}

CachedResolver::CachedResolver() {
this->SetExposeAbsolutePathIdentifierState(TfGetenvBool(DEFINE_STRING(AR_CACHEDRESOLVER_ENV_EXPOSE_ABSOLUTE_PATH_IDENTIFIERS), false));
this->SetExposeRelativePathIdentifierState(TfGetenvBool(DEFINE_STRING(AR_CACHEDRESOLVER_ENV_EXPOSE_RELATIVE_PATH_IDENTIFIERS), false));
Expand Down Expand Up @@ -174,13 +162,6 @@ 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 Expand Up @@ -213,9 +194,6 @@ CachedResolver::_Resolve(
if (assetPath.empty()) {
return ArResolvedPath();
}
if (SdfLayer::IsAnonymousLayerIdentifier(assetPath)){
return ArResolvedPath(assetPath);
}

if (this->_IsContextDependentPath(assetPath)) {
const CachedResolverContext* contexts[2] = {this->_GetCurrentContextPtr(), &_fallbackContext};
Expand All @@ -225,7 +203,7 @@ CachedResolver::_Resolve(
auto &mappingPairs = ctx->GetMappingPairs();
auto map_find = mappingPairs.find(assetPath);
if(map_find != mappingPairs.end()){
ArResolvedPath resolvedPath = _ResolveAnchored(this->emptyString, map_find->second);
ArResolvedPath resolvedPath = ArResolvedPath(TfAbsPath(map_find->second));
return resolvedPath;
// Assume that a map hit is always valid.
// if (resolvedPath) {
Expand All @@ -236,7 +214,7 @@ CachedResolver::_Resolve(
auto &cachedPairs = ctx->GetCachingPairs();
auto cache_find = cachedPairs.find(assetPath);
if(cache_find != cachedPairs.end()){
ArResolvedPath resolvedPath = _ResolveAnchored(this->emptyString, cache_find->second);
ArResolvedPath resolvedPath = ArResolvedPath(TfAbsPath(cache_find->second));
return resolvedPath;
// Assume that a cache hit is always valid.
// if (resolvedPath) {
Expand All @@ -250,7 +228,7 @@ CachedResolver::_Resolve(
allow for resolver multithreading with different contexts.
See .ResolveAndCachePair for more information.
*/
ArResolvedPath resolvedPath = _ResolveAnchored(this->emptyString, ctx->ResolveAndCachePair(assetPath));
ArResolvedPath resolvedPath = ArResolvedPath(TfAbsPath(ctx->ResolveAndCachePair(assetPath)));
if (resolvedPath) {
return resolvedPath;
}
Expand All @@ -261,7 +239,7 @@ CachedResolver::_Resolve(
return ArResolvedPath();
}

return _ResolveAnchored(std::string(), assetPath);
return TfPathExists(assetPath) ? ArResolvedPath(TfAbsPath(assetPath)) : ArResolvedPath();
}

ArResolvedPath
Expand Down
2 changes: 1 addition & 1 deletion src/CachedResolver/resolverContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ void CachedResolverContext::Initialize(){
"ResolverContext.Initialize",
this);
if (!state) {
std::cerr << "Failed to call Resolver.ResolveAndCache in " << DEFINE_STRING(AR_CACHEDRESOLVER_USD_PYTHON_EXPOSE_MODULE_NAME) << ".py. ";
std::cerr << "Failed to call Resolver.Initialize in " << DEFINE_STRING(AR_CACHEDRESOLVER_USD_PYTHON_EXPOSE_MODULE_NAME) << ".py. ";
std::cerr << "Please verify that the python code is valid!" << std::endl;
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/CachedResolver/testenv/testCachedResolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ def test_Resolve(self):
self.assertEqual(resolved_path.GetPathString(), layer_file_path)
self.assertTrue(os.path.isabs(resolved_path.GetPathString()))
resolved_path = resolver.Resolve("example.usd")
self.assertEqual(resolved_path.GetPathString(), "")
self.assertEqual(resolved_path.GetPathString(), "/some/path/to/a/file.usd")
resolved_path = resolver.Resolve("/some/invalid/path.usd")
self.assertEqual(resolved_path.GetPathString(), "")
resolved_path = resolver.Resolve(layer_file_path)
Expand Down Expand Up @@ -454,14 +454,15 @@ def test_ResolveWithScopedCache(self):
resolver.Resolve(layer_identifier),
)
# Remove file
ctx.RemoveCachingByKey(layer_identifier)
os.remove(layer_file_path)
# Query cached result
self.assertEqual(
os.path.abspath(layer_file_path),
resolver.Resolve(layer_identifier),
)
# Uncached result should now return empty result
self.assertEqual("", resolver.Resolve(layer_identifier))
# Uncached result should now return the default PythonExpose.py result
self.assertEqual(resolver.Resolve(layer_identifier), "/some/path/to/a/file.usd")

def test_ResolverCachingMechanism(self):
with tempfile.TemporaryDirectory() as temp_dir_path:
Expand Down
22 changes: 10 additions & 12 deletions src/FileResolver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ endif()
### Targets ###
## Target library > fileResolver ##
add_library(${AR_FILERESOLVER_TARGET_LIB}
SHARED
debugCodes.cpp
resolver.cpp
resolverContext.cpp
resolverTokens.cpp
debugCodes.cpp
resolver.cpp
resolverContext.cpp
resolverTokens.cpp
)
set_boost_namespace(${AR_FILERESOLVER_TARGET_LIB})
# Libs
Expand Down Expand Up @@ -53,13 +52,12 @@ install(TARGETS ${AR_FILERESOLVER_TARGET_LIB} DESTINATION ${AR_FILERESOLVER_USD_

## Target library > fileResolver Python ##
add_library(${AR_FILERESOLVER_TARGET_PYTHON}
SHARED
module.cpp
moduleDeps.cpp
resolverTokens.cpp
wrapResolver.cpp
wrapResolverContext.cpp
wrapResolverTokens.cpp
module.cpp
moduleDeps.cpp
resolverTokens.cpp
wrapResolver.cpp
wrapResolverContext.cpp
wrapResolverTokens.cpp
)
add_dependencies(${AR_FILERESOLVER_TARGET_PYTHON} ${AR_FILERESOLVER_TARGET_LIB})
set_boost_namespace(${AR_FILERESOLVER_TARGET_PYTHON})
Expand Down
25 changes: 11 additions & 14 deletions src/PythonResolver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ endif()
### Targets ###
## Target library > pythonResolver ##
add_library(${AR_PYTHONRESOLVER_TARGET_LIB}
SHARED
debugCodes.cpp
resolver.cpp
resolverContext.cpp
resolverTokens.cpp
debugCodes.cpp
resolver.cpp
resolverContext.cpp
resolverTokens.cpp
)
set_boost_namespace(${AR_PYTHONRESOLVER_TARGET_LIB})
# Libs
Expand Down Expand Up @@ -53,15 +52,13 @@ install(TARGETS ${AR_PYTHONRESOLVER_TARGET_LIB} DESTINATION ${AR_PYTHONRESOLVER_

## Target library > pythonResolver Python ##
add_library(${AR_PYTHONRESOLVER_TARGET_PYTHON}
SHARED
module.cpp
moduleDeps.cpp
resolverTokens.cpp
resolverContext.cpp
wrapResolver.cpp
wrapResolverContext.cpp
wrapResolverTokens.cpp

module.cpp
moduleDeps.cpp
resolverTokens.cpp
resolverContext.cpp
wrapResolver.cpp
wrapResolverContext.cpp
wrapResolverTokens.cpp
)
add_dependencies(${AR_PYTHONRESOLVER_TARGET_PYTHON} ${AR_PYTHONRESOLVER_TARGET_LIB})
set_boost_namespace(${AR_PYTHONRESOLVER_TARGET_PYTHON})
Expand Down

0 comments on commit 26a207d

Please sign in to comment.