Skip to content

Instantly share code, notes, and snippets.

@BigRoy
Last active February 27, 2024 23:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save BigRoy/58437e98508855d576ce12b722daedc8 to your computer and use it in GitHub Desktop.
Save BigRoy/58437e98508855d576ce12b722daedc8 to your computer and use it in GitHub Desktop.
USD find all used assets in Stage or Layer using Python - e.g. findin all used textures
from pxr import UsdUtils, Sdf
stage = hou.pwd().editableStage()
# Given a Sdf.Layer you can use UsdUtils.ComputeAllDependencies
# which returns the layers, assets and any unresolved paths
layer = stage.Flatten()
layers, assets, unresolved_paths = UsdUtils.ComputeAllDependencies(layer.identifier)
for path in assets:
print(path)
# Another approach is traversing the Usd.Stage or Sdf.Layer for
# any property that is of type `Sdf.ValueTypeNames.Asset` or
# where the Sdf.AttributeSpec is of type name "asset"
# E.g. via layer traversal
def trav(path):
if not path.IsPropertyPath():
return
spec = layer.GetObjectAtPath(path)
if not isinstance(spec, Sdf.AttributeSpec):
return
if spec.typeName != "asset":
return
print(path)
layer.Traverse("/", trav)
# Or via the traversing composed stage.
# Note: As per the USD docs for UsdAttribute this might be best to do within a ArResolverScopedCache
# https://openusd.org/dev/api/class_usd_attribute.html
# https://openusd.org/dev/api/class_ar_resolver_scoped_cache.html
for prim in stage.Traverse():
for attr in prim.GetAuthoredAttributes():
if attr.GetTypeName() != "asset":
continue
print(attr.Get())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment