# Get all references and payloads for a UsdPrim

**URL:** <https://forum.aousd.org/t/get-all-references-and-payloads-for-a-usdprim/984>\
**Category:** USD\
**Tags:** python\
**Created:** [November 27, 2023, 7:27pm UTC](https://forum.aousd.org/t/get-all-references-and-payloads-for-a-usdprim/984 "2023-11-27T19:27:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![BigRoyNL](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.aousd.org/bigroynl/32/413_2.png) [@BigRoyNL](https://forum.aousd.org/u/BigRoyNL)\
**Post date:** [November 27, 2023, 7:27pm UTC](https://forum.aousd.org/t/get-all-references-and-payloads-for-a-usdprim/984/1 "2023-11-27T19:27:57Z")

</div>

From a given `pxr.Usd.Prim` I’d like to be able to query the current active references and payloads. It seems that `prim.GetReferences()` and `prim.GetPayloads()` ironically do not allow to provide that, but allow to add/remove references (generating new opinions basically) instead of detecting the resolved opinion.

The documentation states to maybe look into the usage of a `Usd.PrimCompositionQuery`, for example using `Usd.PrimCompositionQuery.GetDirectReferences(prim).GetCompositionArcs()` but somehow those always kept returning empty lists for me.

The only way I could find was maybe going through:

```python
stack = prim.GetPrimStack()

references = []
payloads = []

for prim_spec in stack:
    for reference in prim_spec.referenceList.GetAppliedItems():
        references.append(reference)
    for payload in prim_spec.payloadList.GetAppliedItems():
        payloads.append(payload)

```

But as far as I’m aware since that’s the lower level SDF API that wouldn’t actually resolve to the resulting composed references and payloads, but only the resulting apply of that particular `Sdf.PrimSpec` per `Sdf.Layer`.

Then I’d also like to have easy access to be able to remove the opinions in a layer as well. As far as I can see I must then manually go through all list change types for `payloadList` and `referenceList`.

Something like:

```python
from dataclasses import dataclass

LIST_ATTRS = ['addedItems', 'appendedItems', 'deletedItems', 'explicitItems',
              'orderedItems', 'prependedItems']

@dataclass
class ListProxyItem:
    proxy: object
    value: object

    def update(self, new_value):
        ""Replace value in proxy with a new value""
        index = self.proxy.find(self.value)
        self.proxy[index] = new_value
        self.value = new_value

    def delete(self):
        """Remove self from the proxy's entries"""
        proxy = self.proxy
        proxy[:] = [x for x in proxy if x != self.value]

stack = prim.GetPrimStack()
items = []
for prim_spec in stack:
    for key in LIST_ATTRS:
        proxy = getattr(prim_spec.referencesList, key)
        for reference in proxy:
            item = ListProxyItem(proxy=proxy, value=reference)
            items.append(item)

# So that then I could do e.g.
item = items[0]
item.update(Sdf.Reference(assetPath="new_identfier"))
item.delete() # removing the entry

```

Are there better Python patterns or USD Python API usage to go about this?

---

<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 27, 2023, 9:09pm UTC](https://forum.aousd.org/t/get-all-references-and-payloads-for-a-usdprim/984/2 "2023-11-27T21:09:28Z")

</div>

Hi @BigRoyNL … Usd.PrimCompositionQuery is 100% what you should be using, here. Can you provide, either here or in a GitHub Issue, a simple example scene and python code you use to evaluate it in a scenario that is not returning you any arcs (including especially the prim path), where you expect to be seeing some?

---

<div class="post-metadata">

**Author:** ![BigRoyNL](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.aousd.org/bigroynl/32/413_2.png) [@BigRoyNL](https://forum.aousd.org/u/BigRoyNL)\
**Post date:** [November 27, 2023, 9:35pm UTC](https://forum.aousd.org/t/get-all-references-and-payloads-for-a-usdprim/984/3 "2023-11-27T21:35:42Z")

</div>

Here’s an example where I would have expected some output:

```python
from pxr import Usd, Sdf

stage = Usd.Stage.CreateInMemory()
prim = stage.DefinePrim("/root", "Xform")
references = prim.GetReferences()
references.AddReference(Sdf.Reference(assetPath="./hello.usd"))
references.AddReference(Sdf.Reference(assetPath="./world.usd"))

arcs = Usd.PrimCompositionQuery.GetDirectReferences(prim).GetCompositionArcs()
print(arcs)
# []

```

However, I noticed now that this does give me arcs:

```python
arcs = Usd.PrimCompositionQuery(prim).GetCompositionArcs()
print(arcs)
# [<pxr.Usd.CompositionArc object at 0x000001F6AEFB7F60>]

```

So maybe I misunderstood what _direct references_ are supposed to be. However, that’s just one _arc_? Where I maybe would have expected two. Additionally I’m not sure how to, from that `pxr.Usd.CompositionArc` now get the data I’d need.

I must be missing something obvious! 😉

---

<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 27, 2023, 10:32pm UTC](https://forum.aousd.org/t/get-all-references-and-payloads-for-a-usdprim/984/4 "2023-11-27T22:32:10Z")

</div>

I surmise one of the referenced files is missing or ill-formed. I made valid ones, and with your code, both rcs and arcs had two elements for the two references.

---

<div class="post-metadata">

**Author:** ![BigRoyNL](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.aousd.org/bigroynl/32/413_2.png) [@BigRoyNL](https://forum.aousd.org/u/BigRoyNL)\
**Post date:** [November 27, 2023, 10:41pm UTC](https://forum.aousd.org/t/get-all-references-and-payloads-for-a-usdprim/984/5 "2023-11-27T22:41:19Z")

</div>

I see - it’ll only resolve the actually found files since those would’ve only contributed to the final composition. Since I want to expose to the artist the currently set references, even if invalid, I suppose the compositon arc query is not what I’d need and I’ll be required to go through the Prim Stack across all layers via the SDF API?

---

<div class="post-metadata">

**Author:** ![paoloemilioselva](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.aousd.org/paoloemilioselva/32/267_2.png) [@paoloemilioselva](https://forum.aousd.org/u/paoloemilioselva)\
**Post date:** [December 4, 2023, 10:22pm UTC](https://forum.aousd.org/t/get-all-references-and-payloads-for-a-usdprim/984/6 "2023-12-04T22:22:07Z")

</div>

this is basically what I ended up doing for that other thread you also replied to.  
And I also had to resolve local files in their layers with

```
Sdf.ComputeAssetPathRelativeToLayer( layer, payload_path )

```
