HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UsdPrimRange Class Reference

#include <primRange.h>

Classes

class  EndSentinel
 
class  iterator
 

Public Types

using const_iterator = iterator
 

Public Member Functions

 UsdPrimRange ()
 
 UsdPrimRange (const UsdPrim &start)
 
 UsdPrimRange (const UsdPrim &start, const Usd_PrimFlagsPredicate &predicate)
 
iterator begin () const
 Return an iterator to the start of this range. More...
 
const_iterator cbegin () const
 Return a const_iterator to the start of this range. More...
 
UsdPrim front () const
 Return the first element of this range. The range must not be empty(). More...
 
iterator end () const
 Return the past-the-end iterator for this range. More...
 
const_iterator cend () const
 Return the past-the-end const_iterator for this range. More...
 
void increment_begin ()
 
void set_begin (iterator const &newBegin)
 
bool empty () const
 Return true if this range contains no prims, false otherwise. More...
 
 operator bool () const
 Return true if this range contains one or more prims, false otherwise. More...
 
bool operator== (UsdPrimRange const &other) const
 Return true if this range is equivalent to other. More...
 
bool operator!= (UsdPrimRange const &other) const
 Return true if this range is not equivalent to other. More...
 

Static Public Member Functions

static UsdPrimRange PreAndPostVisit (const UsdPrim &start)
 
static UsdPrimRange PreAndPostVisit (const UsdPrim &start, const Usd_PrimFlagsPredicate &predicate)
 
static UsdPrimRange AllPrims (const UsdPrim &start)
 
static UsdPrimRange AllPrimsPreAndPostVisit (const UsdPrim &start)
 
static USD_API UsdPrimRange Stage (const UsdStagePtr &stage, const Usd_PrimFlagsPredicate &predicate=UsdPrimDefaultPredicate)
 

Detailed Description

An forward-iterable range that traverses a subtree of prims rooted at a given prim in depth-first order.

In addition to depth-first order, UsdPrimRange provides the optional ability to traverse in depth-first pre- and post-order wher prims appear twice in the range; first before all descendants and then again immediately after all descendants. This is useful for maintaining state associated with subtrees, in a stack-like fashion. See UsdPrimRange::iterator::IsPostVisit() to detect when an iterator is visiting a prim for the second time.

There are several constructors providing different levels of configurability; ultimately, one can provide a prim predicate for a custom iteration, just as one would use UsdPrim::GetFilteredChildren() in a custom recursion.

Why would one want to use a UsdPrimRange rather than just iterating over the results of UsdPrim::GetFilteredDescendants() ? Primarily, if one of the following applies:

Using UsdPrimRange in C++

UsdPrimRange provides standard container-like semantics. For example:

// simple range-for iteration
for (UsdPrim prim: UsdPrimRange(rootPrim)) {
ProcessPrim(prim);
}
// using stl algorithms
std::vector<UsdPrim> meshes;
auto range = stage->Traverse();
std::copy_if(range.begin(), range.end(), std::back_inserter(meshes),
[](UsdPrim const &) { return prim.IsA<UsdGeomMesh>(); });
// iterator-based iterating, with subtree pruning
UsdPrimRange range(rootPrim);
for (auto iter = range.begin(); iter != range.end(); ++iter) {
if (UsdModelAPI(*iter).GetKind() == KindTokens->component) {
iter.PruneChildren();
}
else {
nonComponents.push_back(*iter);
}
}

Using Usd.PrimRange in python

The python wrapping for PrimRange is python-iterable, so it can used directly as the object of a "for x in..." clause; however in that usage one loses access to PrimRange methods such as PruneChildren() and IsPostVisit(). Simply create the iterator outside the loop to overcome this limitation. Finally, in python, prim predicates must be combined with bit-wise operators rather than logical operators because the latter are not overridable.

1 # simple iteration
2 for prim in Usd.PrimRange(rootPrim):
3  ProcessPrim(prim)
4 
5 # filtered range using iterator to invoke iterator methods
6 it = iter(Usd.PrimRange.Stage(stage, Usd.PrimIsLoaded & ~Usd.PrimIsAbstract))
7 for prim in it:
8  if Usd.ModelAPI(prim).GetKind() == Kind.Tokens.component:
9  it.PruneChildren()
10  else:
11  nonComponents.append(prim)

Finally, since iterators in python are not directly dereferencable, we provide the python only methods GetCurrentPrim() and IsValid(), documented in the python help system.

Definition at line 118 of file primRange.h.

Member Typedef Documentation

Definition at line 220 of file primRange.h.

Constructor & Destructor Documentation

UsdPrimRange::UsdPrimRange ( )
inline

Definition at line 222 of file primRange.h.

UsdPrimRange::UsdPrimRange ( const UsdPrim start)
inlineexplicit

Construct a PrimRange that traverses the subtree rooted at start in depth-first order, visiting prims that pass the default predicate (as defined by UsdPrimDefaultPredicate).

Definition at line 231 of file primRange.h.

UsdPrimRange::UsdPrimRange ( const UsdPrim start,
const Usd_PrimFlagsPredicate predicate 
)
inline

Construct a PrimRange that traverses the subtree rooted at start in depth-first order, visiting prims that pass predicate.

Definition at line 238 of file primRange.h.

Member Function Documentation

static UsdPrimRange UsdPrimRange::AllPrims ( const UsdPrim start)
inlinestatic

Construct a PrimRange that traverses the subtree rooted at start in depth-first order, visiting all prims (including deactivated, undefined, and abstract prims).

Definition at line 283 of file primRange.h.

static UsdPrimRange UsdPrimRange::AllPrimsPreAndPostVisit ( const UsdPrim start)
inlinestatic

Construct a PrimRange that traverses the subtree rooted at start in depth-first order, visiting all prims (including deactivated, undefined, and abstract prims) with pre- and post-order visitation.

Pre- and post-order visitation means that each prim appears twice in the range; not only prior to all its descendants as with an ordinary traversal but also immediately following its descendants. This lets client code maintain state for subtrees. See UsdPrimRange::iterator::IsPostVisit().

Definition at line 297 of file primRange.h.

iterator UsdPrimRange::begin ( void  ) const
inline

Return an iterator to the start of this range.

Definition at line 310 of file primRange.h.

const_iterator UsdPrimRange::cbegin ( ) const
inline

Return a const_iterator to the start of this range.

Definition at line 314 of file primRange.h.

const_iterator UsdPrimRange::cend ( ) const
inline

Return the past-the-end const_iterator for this range.

Definition at line 328 of file primRange.h.

bool UsdPrimRange::empty ( void  ) const
inline

Return true if this range contains no prims, false otherwise.

Definition at line 347 of file primRange.h.

iterator UsdPrimRange::end ( void  ) const
inline

Return the past-the-end iterator for this range.

Definition at line 326 of file primRange.h.

UsdPrim UsdPrimRange::front ( void  ) const
inline

Return the first element of this range. The range must not be empty().

Definition at line 319 of file primRange.h.

void UsdPrimRange::increment_begin ( )
inline

Modify this range by advancing the beginning by one. The range must not be empty, and the range must not be a pre- and post-order range.

Definition at line 332 of file primRange.h.

UsdPrimRange::operator bool ( ) const
inlineexplicit

Return true if this range contains one or more prims, false otherwise.

Definition at line 350 of file primRange.h.

bool UsdPrimRange::operator!= ( UsdPrimRange const other) const
inline

Return true if this range is not equivalent to other.

Definition at line 364 of file primRange.h.

bool UsdPrimRange::operator== ( UsdPrimRange const other) const
inline

Return true if this range is equivalent to other.

Definition at line 353 of file primRange.h.

static UsdPrimRange UsdPrimRange::PreAndPostVisit ( const UsdPrim start)
inlinestatic

Create a PrimRange that traverses the subtree rooted at start in depth-first order, visiting prims that pass the default predicate (as defined by UsdPrimDefaultPredicate) with pre- and post-order visitation.

Pre- and post-order visitation means that each prim appears twice in the range; not only prior to all its descendants as with an ordinary traversal but also immediately following its descendants. This lets client code maintain state for subtrees. See UsdPrimRange::iterator::IsPostVisit().

Definition at line 256 of file primRange.h.

static UsdPrimRange UsdPrimRange::PreAndPostVisit ( const UsdPrim start,
const Usd_PrimFlagsPredicate predicate 
)
inlinestatic

Create a PrimRange that traverses the subtree rooted at start in depth-first order, visiting prims that pass predicate with pre- and post-order visitation.

Pre- and post-order visitation means that each prim appears twice in the range; not only prior to all its descendants as with an ordinary traversal but also immediately following its descendants. This lets client code maintain state for subtrees. See UsdPrimRange::iterator::IsPostVisit().

Definition at line 272 of file primRange.h.

void UsdPrimRange::set_begin ( iterator const newBegin)
inline

Set the start of this range to newBegin. The newBegin iterator must be within this range's begin() and end(), and must not have UsdPrimRange::iterator::IsPostVisit() be true.

Definition at line 339 of file primRange.h.

static USD_API UsdPrimRange UsdPrimRange::Stage ( const UsdStagePtr &  stage,
const Usd_PrimFlagsPredicate predicate = UsdPrimDefaultPredicate 
)
static

Create a PrimRange that traverses all the prims on stage, and visits those that pass the default predicate (as defined by UsdPrimDefaultPredicate).


The documentation for this class was generated from the following file: