Resolving relative paths from Sdf.Layer.subLayerPaths

@jackohoeg makes some great points about absolute paths and contexts! The ComputeAbsolutePath() solution should always work (though it, too, may be affected by an ArResolverContext, so if there is an active/session context that is not already bound, you’d want to first bind it as in the second example.

The second example won’t work properly for “anchored paths” (e.g. with the ArDefaultResolver, those that start with “./” like @./anchored/layer.usd@). As of Ar 2.0, the preferred pattern to do this would be something like:

layer = Sdf.Layer.FindOrOpen("/path/to/asset.usd")
resolver = Ar.GetResolver()
lyrResolved = layer.resolvedPath
ctx = resolver.CreateDefaultContextForAsset(lyrResolved)

with Ar.ResolverContextBinder(ctx):
    for sublayer in layer.subLayerPaths:
        print(resolver.Resolve(resolver.CreateIdentifier(sublayer, lyrResolved)))
1 Like