# How to get prim paths using an expression without creating a collection

**URL:** <https://forum.aousd.org/t/how-to-get-prim-paths-using-an-expression-without-creating-a-collection/1991>\
**Category:** USD\
**Tags:** python\
**Created:** [November 12, 2024, 12:45am UTC](https://forum.aousd.org/t/how-to-get-prim-paths-using-an-expression-without-creating-a-collection/1991 "2024-11-12T00:45:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![erikcarlson](https://avatars.discourse-cdn.com/v4/letter/e/51bf81/32.png) [@erikcarlson](https://forum.aousd.org/u/erikcarlson)\
**Post date:** [November 12, 2024, 12:45am UTC](https://forum.aousd.org/t/how-to-get-prim-paths-using-an-expression-without-creating-a-collection/1991/1 "2024-11-12T00:45:47Z")

</div>

Using the USD API, is it possible to get prim paths in a stage using Sdf.PathExpression without creating a collection?  
I’d like to find prims of a certain type with glob-style wildcard patterns in the path such as `/cameras/* and {isa:Camera}`, but I can only find examples of this being used with collections.  
It’s possible to do this in Houdini like this for example:

```auto
rule = hou.LopSelectionRule(pattern='/cameras/* & %type(Camera)')
prim_paths = rule.expandedPaths(stage=hou.selectedNodes()[0].stage())

```

Here’s the most relevant documentation I could find:  
[https://openusd.org/release/user\_guides/collections\_and\_patterns.html](https://openusd.org/release/user_guides/collections_and_patterns.html)  
[https://openusd.org/release/api/class\_sdf\_path\_expression.html](https://openusd.org/release/api/class_sdf_path_expression.html)

---

<div class="post-metadata">

**Author:** ![jerry.huxtable](https://avatars.discourse-cdn.com/v4/letter/j/b19c9b/32.png) [@jerry.huxtable](https://forum.aousd.org/u/jerry.huxtable)\
**Post date:** [November 12, 2024, 9:21am UTC](https://forum.aousd.org/t/how-to-get-prim-paths-using-an-expression-without-creating-a-collection/1991/2 "2024-11-12T09:21:33Z")

</div>

You can do this using `SdfMakePathExpressionEval::MakeIncrementalSearcher`, passing `UsdGetCollectionPredicateLibrary` as the predicate library, and doing your own stage traversal. If a result’s `IsConstant` method returns true, you can call `PruneChildren` to prune off branches of the stage which can’t match. `UsdGetCollectionPredicateLibrary` needs to be passed a struct with an `operator()` which returns a prim given an SdfPath.

Jerry

---

<div class="post-metadata">

**Author:** ![erikcarlson](https://avatars.discourse-cdn.com/v4/letter/e/51bf81/32.png) [@erikcarlson](https://forum.aousd.org/u/erikcarlson)\
**Post date:** [November 12, 2024, 11:45pm UTC](https://forum.aousd.org/t/how-to-get-prim-paths-using-an-expression-without-creating-a-collection/1991/3 "2024-11-12T23:45:11Z")

</div>

Thank you Jerry! Is this available in Python? I can’t get it working.

---

<div class="post-metadata">

**Author:** ![jerry.huxtable](https://avatars.discourse-cdn.com/v4/letter/j/b19c9b/32.png) [@jerry.huxtable](https://forum.aousd.org/u/jerry.huxtable)\
**Post date:** [November 13, 2024, 10:48am UTC](https://forum.aousd.org/t/how-to-get-prim-paths-using-an-expression-without-creating-a-collection/1991/4 "2024-11-13T10:48:44Z")

</div>

I came across the same problem with Python. There seems to be no Python binding for `SdfMakePathExpressionEval`, so I couldn’t get this to work and had to do it in C++.

I would point out though, that using the Collection method, iit seems that there’s no requirement for the Collection to be in the same stage as the prims you’re matching, so, at your own risk…

```auto
def match(stage, pattern):
  temp_stage = Usd.Stage.CreateInMemory()
  c = temp_stage.DefinePrim(Sdf.Path("/c"))
  e = Usd.CollectionAPI.Apply(c, "t:collection")
  m = e.CreateMembershipExpressionAttr()
  m.Set(Sdf.PathExpression(pattern))
  o = Usd.CollectionAPI.ComputeIncludedObjects(e.ComputeMembershipQuery(), stage)
  return [p.GetPath() for p in o]

```

Jerry

---

<div class="post-metadata">

**Author:** ![erikcarlson](https://avatars.discourse-cdn.com/v4/letter/e/51bf81/32.png) [@erikcarlson](https://forum.aousd.org/u/erikcarlson)\
**Post date:** [November 14, 2024, 11:10pm UTC](https://forum.aousd.org/t/how-to-get-prim-paths-using-an-expression-without-creating-a-collection/1991/5 "2024-11-14T23:10:46Z")

</div>

Interesting, I’ll give that a try.
