Loading...
Searching...
No Matches
primData.h
Go to the documentation of this file.
1//
2// Copyright 2016 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
7#ifndef PXR_USD_USD_PRIM_DATA_H
8#define PXR_USD_USD_PRIM_DATA_H
9
11
12#include "pxr/pxr.h"
13#include "pxr/usd/usd/api.h"
14#include "pxr/usd/usd/common.h"
16#include "pxr/usd/usd/primDefinition.h"
17#include "pxr/usd/usd/primTypeInfo.h"
18#include "pxr/usd/sdf/types.h"
19
21#include "pxr/base/tf/pointerAndBits.h"
22#include "pxr/base/tf/token.h"
23
24#include "pxr/usd/sdf/path.h"
25
26#include <atomic>
27#include <cstdint>
28#include <vector>
29
30PXR_NAMESPACE_OPEN_SCOPE
31
33
34// Private class that stores cached prim information and defines the prim tree
35// on a UsdStage.
36//
37// Usd_PrimData objects are arranged in a tree structure, represented as a
38// binary tree. See the _firstChild and _nextSiblingOrParent members.
39//
40// UsdStage builds and manages the tree structure of Usd_PrimData objects. The
41// Usd_PrimData objects lifetimes are governed by an internal reference count
42// (see _refCount). Two objects mutate this reference count: UsdStage owns
43// references to all the Usd_PrimData objects that represent the scene graph,
44// and UsdObject (and by inheritance its subclasses) owns a reference to its
45// prim data object via Usd_PrimDataHandle.
46//
47// Usd_PrimData has a 'dead' flag (see _IsDead and _MarkDead). UsdStage sets
48// this when a prim data object is removed from the scene graph.
49// Usd_PrimDataHandle, which is a smart pointer to Usd_PrimData consults this
50// dead flag to determine prim validity, and to issue informative crash messages
51// on invalid use. See USD_CHECK_ALL_PRIM_ACCESSES.
52//
53class Usd_PrimData
54{
55public:
56
57 // --------------------------------------------------------------------- //
59 // --------------------------------------------------------------------- //
60
67 const SdfPath &GetPath() const { return _path; }
68
69 const TfToken &GetName() const { return GetPath().GetNameToken(); }
70
71 UsdStage *GetStage() const { return _stage; }
72
74 const UsdPrimDefinition &GetPrimDefinition() const {
75 return _primTypeInfo->GetPrimDefinition();
76 }
77
80 const TfToken& GetTypeName() const {
81 return _primTypeInfo->GetTypeName();
82 }
83
85 const UsdPrimTypeInfo &GetPrimTypeInfo() const {
86 return *_primTypeInfo;
87 }
88
90 bool IsPseudoRoot() const { return _flags[Usd_PrimPseudoRootFlag]; }
91
94 bool IsActive() const { return _flags[Usd_PrimActiveFlag]; }
95
99 bool IsLoaded() const { return _flags[Usd_PrimLoadedFlag]; }
100
103 bool IsModel() const { return _flags[Usd_PrimModelFlag]; }
104
108 bool IsGroup() const { return _flags[Usd_PrimGroupFlag]; }
109
110 bool IsComponent() const { return _flags[Usd_PrimComponentFlag]; }
111
112 USD_API
113 bool IsSubComponent() const;
114
116 bool IsAbstract() const { return _flags[Usd_PrimAbstractFlag]; }
117
120 bool IsDefined() const { return _flags[Usd_PrimDefinedFlag]; }
121
124 bool HasDefiningSpecifier() const {
125 return _flags[Usd_PrimHasDefiningSpecifierFlag];
126 }
127
129 bool HasClassSpecifier() const {
130 return _flags[Usd_PrimHasClassSpecifierFlag];
131 }
132
134 bool HasPayload() const { return _flags[Usd_PrimHasPayloadFlag]; }
135
139 bool MayHaveOpinionsInClips() const { return _flags[Usd_PrimClipsFlag]; }
140
142 USD_API
143 SdfSpecifier GetSpecifier() const;
144
145 // --------------------------------------------------------------------- //
147 // --------------------------------------------------------------------- //
148
150 USD_API
151 Usd_PrimDataConstPtr GetParent() const;
152
153 // --------------------------------------------------------------------- //
154 // PrimIndex access.
155 // --------------------------------------------------------------------- //
156
173 USD_API
174 const class PcpPrimIndex &GetPrimIndex() const;
175
182 USD_API
183 const class PcpPrimIndex &GetSourcePrimIndex() const;
184
185 // --------------------------------------------------------------------- //
186 // Tree Structure
187 // --------------------------------------------------------------------- //
188
189 // Return this prim data's first child if it has one, nullptr otherwise.
190 Usd_PrimDataPtr GetFirstChild() const { return _firstChild; }
191
192 // Return this prim data's next sibling if it has one, nullptr otherwise.
193 Usd_PrimDataPtr GetNextSibling() const {
194 return !_nextSiblingOrParent.BitsAs<bool>() ?
195 _nextSiblingOrParent.Get() : nullptr;
196 }
197
198 // Return this prim data's parent if this prim data is the last in its chain
199 // of siblings. That is, if the _nextSiblingOrParent field is pointing to
200 // its parent. Return nullptr otherwise.
201 Usd_PrimDataPtr GetParentLink() const {
202 return _nextSiblingOrParent.BitsAs<bool>() ?
203 _nextSiblingOrParent.Get() : nullptr;
204 }
205
206 // Return the next prim data "to the right" of this one. That is, this
207 // prim's next sibling if it has one, otherwise the next sibling of the
208 // nearest ancestor with a sibling, if there is one, otherwise null.
209 inline Usd_PrimDataPtr GetNextPrim() const {
210 if (Usd_PrimDataPtr sibling = GetNextSibling())
211 return sibling;
212 for (Usd_PrimDataPtr p = GetParentLink(); p; p = p->GetParentLink()) {
213 if (Usd_PrimDataPtr sibling = p->GetNextSibling())
214 return sibling;
215 }
216 return nullptr;
217 }
218
219 // Return the prim data at \p path. If \p path indicates a prim
220 // beneath an instance, return the prim data for the corresponding
221 // prim in the instance's prototype.
222 USD_API Usd_PrimDataConstPtr
223 GetPrimDataAtPathOrInPrototype(const SdfPath &path) const;
224
225 // --------------------------------------------------------------------- //
226 // Instancing
227 // --------------------------------------------------------------------- //
228
231 bool IsInstance() const { return _flags[Usd_PrimInstanceFlag]; }
232
234 bool IsPrototype() const {
235 return IsInPrototype() && GetPath().IsRootPrimPath();
236 }
237
240 bool IsInPrototype() const { return _flags[Usd_PrimPrototypeFlag]; }
241
244 USD_API Usd_PrimDataConstPtr GetPrototype() const;
245
246 // --------------------------------------------------------------------- //
247 // Private Members
248 // --------------------------------------------------------------------- //
249private:
250
251 USD_API
252 Usd_PrimData(UsdStage *stage, const SdfPath& path);
253 USD_API
254 ~Usd_PrimData();
255
256 // Compute and store type info and cached flags.
257 void _ComposeAndCacheFlags(
258 Usd_PrimDataConstPtr parent, bool isPrototypePrim);
259
260 // Flags direct access for Usd_PrimFlagsPredicate.
261 friend class Usd_PrimFlagsPredicate;
262 const Usd_PrimFlagBits &_GetFlags() const {
263 return _flags;
264 }
265
266 // --------------------------------------------------------------------- //
267 // Prim Children
268 // --------------------------------------------------------------------- //
269
270 // Composes the prim children, reporting errors as they occur. Returns true
271 // on success false on failure.
272 bool _ComposePrimChildNames(TfTokenVector* nameOrder);
273
274 void _SetSiblingLink(Usd_PrimDataPtr sibling) {
275 _nextSiblingOrParent.Set(sibling, /* isParent */ false);
276 }
277
278 void _SetParentLink(Usd_PrimDataPtr parent) {
279 _nextSiblingOrParent.Set(parent, /* isParent */ true);
280 }
281
282 // Set the dead bit on this prim data object.
283 void _MarkDead() {
284 _flags[Usd_PrimDeadFlag] = true;
285 _stage = nullptr;
286 _primIndex = nullptr;
287 }
288
289 // Return true if this prim's dead flag is set, false otherwise.
290 bool _IsDead() const { return _flags[Usd_PrimDeadFlag]; }
291
292 // Set whether this prim or any of its namespace ancestors had clips
293 // specified.
294 void _SetMayHaveOpinionsInClips(bool hasClips) {
295 _flags[Usd_PrimClipsFlag] = hasClips;
296 }
297
298 inline class Usd_PrimDataSiblingIterator _ChildrenBegin() const;
299 inline class Usd_PrimDataSiblingIterator _ChildrenEnd() const;
300
301 inline class Usd_PrimDataSubtreeIterator _SubtreeBegin() const;
302 inline class Usd_PrimDataSubtreeIterator _SubtreeEnd() const;
303
304 // Data members.
305 UsdStage *_stage;
306 const PcpPrimIndex *_primIndex;
307 SdfPath _path;
308 const UsdPrimTypeInfo *_primTypeInfo;
309 Usd_PrimData *_firstChild;
310 TfPointerAndBits<Usd_PrimData> _nextSiblingOrParent;
311 mutable std::atomic<int64_t> _refCount;
312 Usd_PrimFlagBits _flags;
313
314 // intrusive_ptr core primitives implementation.
315 friend void TfDelegatedCountIncrement(const Usd_PrimData *prim) noexcept {
316 prim->_refCount.fetch_add(1, std::memory_order_relaxed);
317 }
318 friend void TfDelegatedCountDecrement(const Usd_PrimData *prim) noexcept {
319 if (prim->_refCount.fetch_sub(1, std::memory_order_release) == 1)
320 delete prim;
321 }
322
323 USD_API
324 friend void Usd_ThrowExpiredPrimAccessError(Usd_PrimData const *p);
325 friend std::string
326 Usd_DescribePrimData(const Usd_PrimData *p, SdfPath const &proxyPrimPath);
327
328 friend inline bool Usd_IsDead(Usd_PrimData const *p) {
329 return p->_IsDead();
330 }
331
332 friend class UsdPrim;
333 friend class UsdStage;
334};
335
336// Sibling iterator class.
337class Usd_PrimDataSiblingIterator {
338 using _UnderylingIterator = Usd_PrimData*;
339public:
340 using iterator_category = std::forward_iterator_tag;
341 using value_type = Usd_PrimData*;
342 using reference = Usd_PrimData*;
343 using pointer = void;
344 using difference_type = std::ptrdiff_t;
345
346 // Default ctor.
347 Usd_PrimDataSiblingIterator() = default;
348
349 reference operator*() const { return _underlyingIterator; }
350
351 // pre-increment
352 Usd_PrimDataSiblingIterator& operator++() {
353 increment();
354 return *this;
355 }
356
357 // post-increment
358 Usd_PrimDataSiblingIterator operator++(int) {
359 Usd_PrimDataSiblingIterator result = *this;
360 increment();
361 return result;
362 }
363
364 bool operator==(const Usd_PrimDataSiblingIterator& other) const {
365 return _underlyingIterator == other._underlyingIterator;
366 }
367
368 bool operator!=(const Usd_PrimDataSiblingIterator& other) const {
369 return _underlyingIterator != other._underlyingIterator;
370 }
371
372private:
373 friend class Usd_PrimData;
374
375 // Constructor used by Prim.
376 Usd_PrimDataSiblingIterator(const _UnderylingIterator &i)
377 : _underlyingIterator(i) {}
378
379 void increment() {
380 _underlyingIterator = _underlyingIterator->GetNextSibling();
381 }
382
383 _UnderylingIterator _underlyingIterator = nullptr;
384};
385
386Usd_PrimDataSiblingIterator
387Usd_PrimData::_ChildrenBegin() const
388{
389 return Usd_PrimDataSiblingIterator(_firstChild);
390}
391
392Usd_PrimDataSiblingIterator
393Usd_PrimData::_ChildrenEnd() const
394{
395 return Usd_PrimDataSiblingIterator(0);
396}
397
398// Tree iterator class.
399class Usd_PrimDataSubtreeIterator {
400 using _UnderlyingIterator = Usd_PrimData*;
401public:
402 using iterator_category = std::forward_iterator_tag;
403 using value_type = Usd_PrimData*;
404 using reference = Usd_PrimData*;
405 using pointer = void;
406 using difference_type = std::ptrdiff_t;
407
408 // Default ctor.
409 Usd_PrimDataSubtreeIterator() = default;
410
411 reference operator*() const { return _underlyingIterator; }
412
413 // pre-increment
414 Usd_PrimDataSubtreeIterator& operator++() {
415 increment();
416 return *this;
417 }
418
419 // post-increment
420 Usd_PrimDataSubtreeIterator operator++(int) {
421 Usd_PrimDataSubtreeIterator result = *this;
422 increment();
423 return result;
424 }
425
426 bool operator==(const Usd_PrimDataSubtreeIterator& other) const {
427 return _underlyingIterator == other._underlyingIterator;
428 }
429
430 bool operator!=(const Usd_PrimDataSubtreeIterator& other) const {
431 return _underlyingIterator != other._underlyingIterator;
432 }
433
434private:
435 friend class Usd_PrimData;
436 friend class UsdPrimSubtreeIterator;
437
438 // Constructor used by Prim.
439 Usd_PrimDataSubtreeIterator(const _UnderlyingIterator &i)
440 : _underlyingIterator(i) {}
441
442 void increment() {
443 _underlyingIterator = _underlyingIterator->GetFirstChild() ?
444 _underlyingIterator->GetFirstChild() :
445 _underlyingIterator->GetNextPrim();
446 }
447
448 _UnderlyingIterator _underlyingIterator = nullptr;
449};
450
451Usd_PrimDataSubtreeIterator
452Usd_PrimData::_SubtreeBegin() const
453{
454 return Usd_PrimDataSubtreeIterator(
455 _firstChild ? _firstChild : GetNextPrim());
456}
457
458Usd_PrimDataSubtreeIterator
459Usd_PrimData::_SubtreeEnd() const
460{
461 return Usd_PrimDataSubtreeIterator(GetNextPrim());
462}
463
464// Helpers for instance proxies.
465
466// Return true if the prim with prim data \p p and proxy prim path
467// \p proxyPrimPath represents an instance proxy.
468template <class PrimDataPtr>
469inline bool
470Usd_IsInstanceProxy(const PrimDataPtr &p, const SdfPath &proxyPrimPath)
471{
472 return !proxyPrimPath.IsEmpty();
473}
474
475// Helpers for subtree traversals.
476
477// Create a predicate based on \p pred for use when traversing the
478// siblings or descendants of the prim with prim data \p p and proxy
479// prim path \p proxyPrimPath. This is used by prim traversal functions
480// like UsdPrim::GetFilteredChildren, UsdPrim::GetFilteredDescendants,
481// UsdPrim::GetFilteredNextSibling, and UsdPrimRange.
482template <class PrimDataPtr>
483inline Usd_PrimFlagsPredicate
484Usd_CreatePredicateForTraversal(const PrimDataPtr &p,
485 const SdfPath &proxyPrimPath,
486 Usd_PrimFlagsPredicate pred)
487{
488 // Don't allow traversals beneath instances unless the client has
489 // explicitly requested it or the starting point is already beneath
490 // an instance (i.e., the starting point is an instance proxy).
491 if (!Usd_IsInstanceProxy(p, proxyPrimPath) &&
492 !pred.IncludeInstanceProxiesInTraversal()) {
493 pred.TraverseInstanceProxies(false);
494 }
495 return pred;
496}
497
498// Move \p p to its parent. If \p proxyPrimPath is not empty, set it to
499// its parent path. If after this \p p is a prototype prim, move \p p to
500// the prim indicated by \p proxyPrimPath. If \p p's path is then equal
501// to \p proxyPrimPath, set \p proxyPrimPath to the empty path.
502template <class PrimDataPtr>
503inline void
504Usd_MoveToParent(PrimDataPtr &p, SdfPath &proxyPrimPath)
505{
506 p = p->GetParent();
507
508 if (!proxyPrimPath.IsEmpty()) {
509 proxyPrimPath = proxyPrimPath.GetParentPath();
510
511 if (p && p->IsPrototype()) {
512 p = p->GetPrimDataAtPathOrInPrototype(proxyPrimPath);
513 if (TF_VERIFY(p, "No prim at <%s>", proxyPrimPath.GetText()) &&
514 p->GetPath() == proxyPrimPath) {
515 proxyPrimPath = SdfPath();
516 }
517 }
518 }
519}
520
521// Search for the next sibling that matches \p pred (up to \p end). If such a
522// sibling exists, move \p p to it and return false. If no such sibling exists
523// then move \p p to its parent and return true. If \p end is reached while
524// looking for siblings, move \p p to \p end and return false.
525//
526// If \p proxyPrimPath is not empty, update it based on the new value of \p p:
527// - If \p p was moved to \p end, set \p proxyPrimPath to the empty path.
528// - If \p p was moved to a sibling, set the prim name for \p proxyPrimPath
529// to the sibling's name.
530// - If \p p was moved to a parent, set \p proxyPrimPath and \p p the same
531// way as Usd_MoveToParent.
532template <class PrimDataPtr>
533inline bool
534Usd_MoveToNextSiblingOrParent(PrimDataPtr &p, SdfPath &proxyPrimPath,
535 PrimDataPtr end,
536 const Usd_PrimFlagsPredicate &pred)
537{
538 // Either all siblings are instance proxies or none are. We can just
539 // compute this once and reuse it as we scan for the next sibling.
540 const bool isInstanceProxy = Usd_IsInstanceProxy(p, proxyPrimPath);
541
542 PrimDataPtr next = p->GetNextSibling();
543 while (next && next != end &&
544 !Usd_EvalPredicate(pred, next, isInstanceProxy)) {
545 p = next;
546 next = p->GetNextSibling();
547 }
548 p = next ? next : p->GetParentLink();
549
550 if (!proxyPrimPath.IsEmpty()) {
551 if (p == end) {
552 proxyPrimPath = SdfPath();
553 }
554 else if (p == next) {
555 proxyPrimPath =
556 proxyPrimPath.GetParentPath().AppendChild(p->GetName());
557 }
558 else {
559 proxyPrimPath = proxyPrimPath.GetParentPath();
560 if (p && p->IsPrototype()) {
561 p = p->GetPrimDataAtPathOrInPrototype(proxyPrimPath);
562 if (TF_VERIFY(p, "No prim at <%s>", proxyPrimPath.GetText()) &&
563 p->GetPath() == proxyPrimPath) {
564 proxyPrimPath = SdfPath();
565 }
566 }
567 }
568 }
569
570 // Return true if we successfully moved to a parent, otherwise false.
571 return !next && p;
572}
573
574// Convenience method for calling the above with \p end = \c nullptr.
575template <class PrimDataPtr>
576inline bool
577Usd_MoveToNextSiblingOrParent(PrimDataPtr &p, SdfPath &proxyPrimPath,
578 const Usd_PrimFlagsPredicate &pred)
579{
580 return Usd_MoveToNextSiblingOrParent(p, proxyPrimPath,
581 PrimDataPtr(nullptr), pred);
582}
583
584// Search for the first direct child of \p p that matches \p pred (up to
585// \p end). If the given \p p is an instance, search for direct children
586// on the corresponding prototype prim. If such a direct child exists,
587// move \p p to it, and return true. Otherwise leave the iterator
588// unchanged and return false.
589template <class PrimDataPtr>
590inline bool
591Usd_MoveToChild(PrimDataPtr &p, SdfPath &proxyPrimPath,
592 PrimDataPtr end,
593 const Usd_PrimFlagsPredicate &pred)
594{
595 bool isInstanceProxy = Usd_IsInstanceProxy(p, proxyPrimPath);
596
597 PrimDataPtr src = p;
598 if (src->IsInstance()) {
599 src = src->GetPrototype();
600 isInstanceProxy = true;
601 }
602
603 if (PrimDataPtr child = src->GetFirstChild()) {
604 if (isInstanceProxy) {
605 proxyPrimPath = proxyPrimPath.IsEmpty() ?
606 p->GetPath().AppendChild(child->GetName()) :
607 proxyPrimPath.AppendChild(child->GetName());
608 }
609
610 p = child;
611
612 if (Usd_EvalPredicate(pred, p, isInstanceProxy) ||
613 !Usd_MoveToNextSiblingOrParent(p, proxyPrimPath, end, pred)) {
614 return true;
615 }
616 }
617 return false;
618}
619
620// Convenience method for calling the above with \p end = \c nullptr.
621template <class PrimDataPtr>
622inline bool
623Usd_MoveToChild(PrimDataPtr &p, SdfPath &proxyPrimPath,
624 const Usd_PrimFlagsPredicate &pred)
625{
626 return Usd_MoveToChild(p, proxyPrimPath, PrimDataPtr(nullptr), pred);
627}
628
629PXR_NAMESPACE_CLOSE_SCOPE
630
631#endif // PXR_USD_USD_PRIM_DATA_H
PcpPrimIndex is an index of the all sites of scene description that contribute opinions to a specific...
Definition: primIndex.h:62
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:281
SDF_API SdfPath GetParentPath() const
Return the path that identifies this path's namespace parent.
SDF_API const char * GetText() const
Returns the string representation of this path as a c string.
bool IsEmpty() const noexcept
Returns true if this is the empty path (SdfPath::EmptyPath()).
Definition: path.h:405
SDF_API SdfPath AppendChild(TfToken const &childName) const
Creates a path by appending an element for childName to this path.
This class stores a T * and a small integer in the space of a T *.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:71
Class representing the builtin definition of a prim given the schemas registered in the schema regist...
UsdPrim is the sole persistent scenegraph object on a UsdStage, and is the embodiment of a "Prim" as ...
Definition: prim.h:117
Forward traversal iterator of sibling UsdPrim s.
Definition: prim.h:2543
Class that holds the full type information for a prim.
Definition: primTypeInfo.h:31
The outermost container for scene description, which owns and presents composed prims as a scenegraph...
Definition: stage.h:135
Standard pointer typedefs.
#define TF_DECLARE_WEAK_PTRS(type)
Define standard weak pointer types.
Definition: declarePtrs.h:45
#define TF_VERIFY(cond, format,...)
Checks a condition and reports an error if it evaluates false.
Definition: diagnostic.h:266
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:440
Basic Sdf data types.
SdfSpecifier
An enum that identifies the possible specifiers for an SdfPrimSpec.
Definition: types.h:103