# Resolving relative paths from Sdf.Layer.subLayerPaths

**URL:** <https://forum.aousd.org/t/resolving-relative-paths-from-sdf-layer-sublayerpaths/838>\
**Category:** USD\
**Created:** [October 23, 2023, 8:39pm UTC](https://forum.aousd.org/t/resolving-relative-paths-from-sdf-layer-sublayerpaths/838 "2023-10-23T20:39:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Truant](https://avatars.discourse-cdn.com/v4/letter/t/c6cbf5/32.png) [@Truant](https://forum.aousd.org/u/Truant)\
**Post date:** [October 23, 2023, 8:39pm UTC](https://forum.aousd.org/t/resolving-relative-paths-from-sdf-layer-sublayerpaths/838/1 "2023-10-23T20:39:38Z")

</div>

Hello!

I’ve opened a layer with Sdf.Layer.FindOrOpen() and it has a few relative sublayers. Is there a way to resolve these paths relative to the layer that I’ve opened? I can easily just use python to join them and resolve them but is there a way by creating a resolver context and the asset resolver to do that for me (some bad pseudocode)

> layer = Sdf.Layer.FindOrOpen(‘/path/to/asset.usd’)  
> ctx = Ar.ResolverContext([‘/path/to/asset.usd’])  
> resolver = Ar.GetResolver()  
> for sublayer in layer.subLayerPaths:  
> print (resolver.Resolve(sublayer, ctx))

Thanks!

---

<div class="post-metadata">

**Author:** ![jackohoeg](https://avatars.discourse-cdn.com/v4/letter/j/5e9695/32.png) [@jackohoeg](https://forum.aousd.org/u/jackohoeg)\
**Post date:** [October 25, 2023, 12:06am UTC](https://forum.aousd.org/t/resolving-relative-paths-from-sdf-layer-sublayerpaths/838/2 "2023-10-25T00:06:43Z")

</div>

I’m certainly not an expert on asset resolution, but I believe the answer is yes. I tend to prefer making the asset paths absolute before resolving them since it feels a little higher level, though binding a specific resolver context should be possible too.

Using absolute paths:

```auto
layer = Sdf.Layer.FindOrOpen("/path/to/asset.usd")
resolver = Ar.GetResolver()
for sublayer in layer.subLayerPaths:
    print(resolver.Resolve(layer.ComputeAbsolutePath(sublayer)))

```

Using resolver context:

```auto
layer = Sdf.Layer.FindOrOpen("/path/to/asset.usd")
resolver = Ar.GetResolver()
ctx = resolver.CreateDefaultContextForAsset(
    layer.repositoryPath or layer.realPath
)
with Ar.ResolverContextBinder(ctx):
    for sublayer in layer.subLayerPaths:
        print(resolver.Resolve(sublayer))

```

---

<div class="post-metadata">

**Author:** ![spiff](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.aousd.org/spiff/32/22_2.png) [@spiff](https://forum.aousd.org/u/spiff)\
**Post date:** [November 3, 2023, 10:06pm UTC](https://forum.aousd.org/t/resolving-relative-paths-from-sdf-layer-sublayerpaths/838/3 "2023-11-03T22:06:36Z")

</div>

@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:

```auto
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)))

```

---

<div class="post-metadata">

**Author:** ![spiff](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.aousd.org/spiff/32/22_2.png) [@spiff](https://forum.aousd.org/u/spiff)\
**Post date:** [November 3, 2023, 10:16pm UTC](https://forum.aousd.org/t/resolving-relative-paths-from-sdf-layer-sublayerpaths/838/4 "2023-11-03T22:16:11Z")

</div>

(the reason for possibly preferring the second approach is that `ComputeAbsolutePath()` does more work than is strictly required by your query).

---

<div class="post-metadata">

**Author:** ![Truant](https://avatars.discourse-cdn.com/v4/letter/t/c6cbf5/32.png) [@Truant](https://forum.aousd.org/u/Truant)\
**Post date:** [November 4, 2023, 2:00am UTC](https://forum.aousd.org/t/resolving-relative-paths-from-sdf-layer-sublayerpaths/838/5 "2023-11-04T02:00:46Z")

</div>

Thank you both for the answers!
