Best practices opening UsdStage versus usdview

I’ve been experimenting with custom ArResolverContext and populating it through CreateDefaultContextForAsset. This works excellently for any code that opens a usd stage directly from the path.

I noticed in usdview it appeared the top level file reference was not using the resolver context but everything underneath it was. Distilling the code in usdview, I found

layer = Sdf.Layer.FindOrOpen(path)
ctx = Ar.GetResolver().CreateDefaultContextForAsset(path)
stage = Usd.Stage.Open(layer, sessionLayer, ctx, loadSet)

This looks incorrect to me. It also explains why the top level layer does not get the context. Based on what I’ve been learning, I would expect the code to require this structure.

ctx = Ar.GetResolver().CreateDefaultContextForAsset(path)
with Ar.ResolverContextBinder(ctx):
    layer = Sdf.Layer.FindOrOpen(path)
stage = Usd.Stage.Open(layer, sessionLayer, ctx, loadSet)

Would the way usdview is currently loading the scene expected to be valid? Would either of these approaches be considered more valid than the other? I’m starting to worry we may be limited with what we can do with CreateDefaultContextForAsset since it seems to get used so inconsistently across existing tools and applications.

I think you’re right that the order should be flipped in usdview so you are able to create the context first.

To your point, yes, a bunch of tools may not create the context when you want them to. You can really only hope they do already, and submit issues to the ones who don’t. Of course, that’s difficult for closed source apps.

sonofagun. That’s a regression introduced in 2017(!) when we wanted to validate that the fileformat of the referred path was legal prior to opening the stage. Only the UsdStage::Open() overloads that take a filepath (rather than a layer) will use a provided Context to resolve the layer’s path. We should fix that.

I will note, though, that both usdedit and usdcat are following the same/similar pattern, i.e. the layer is resolved without benefit of a DefaultContextForAsset. All three tools (at least) should probably be aligned in their behavior.