Skip to content

Commit

Permalink
Enhanced UsdGeomModelAPI::ComputeModelDrawMode() to allow more effici…
Browse files Browse the repository at this point in the history
…ent computation

when the parent's drawMode is known (eg. in a traversal context).

(Internal change: 1905605)
  • Loading branch information
shriramiyer authored and pixar-oss committed Oct 29, 2018
1 parent 0c0b554 commit 17a48ef
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 24 deletions.
46 changes: 32 additions & 14 deletions pxr/usd/lib/usdGeom/modelAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -463,24 +463,41 @@ UsdGeomModelAPI::GetConstraintTargets() const
return constraintTargets;
}

namespace {
static
bool
_GetAuthoredDrawMode(const UsdPrim &prim, TfToken *drawMode)
{
// Only check for the attribute on models; don't check the pseudo-root.
if (!prim.IsModel() || !prim.GetParent()) {
return false;
}

UsdGeomModelAPI modelAPI(prim);
UsdAttribute attr = modelAPI.GetModelDrawModeAttr();
return attr && attr.Get(drawMode);
}
}

TfToken
UsdGeomModelAPI::ComputeModelDrawMode() const
UsdGeomModelAPI::ComputeModelDrawMode(const TfToken &parentDrawMode) const
{
// Find the closest applicable model:drawMode among this prim's ancestors.
for (UsdPrim curPrim = GetPrim(); curPrim; curPrim = curPrim.GetParent()) {
// Only check for the attribute on models; don't check the pseudo-root.
if (!curPrim.IsModel() || !curPrim.GetParent()) {
continue;
}
TfToken drawMode;

if (_GetAuthoredDrawMode(GetPrim(), &drawMode)) {
return drawMode;
}

// If model:drawMode is set, use its value; we want the first attribute
// we find.
UsdGeomModelAPI curModel(curPrim);
UsdAttribute attr;
TfToken drawMode;
if (!parentDrawMode.IsEmpty()) {
return parentDrawMode;
}

// Find the closest applicable model:drawMode among this prim's ancestors.
for (UsdPrim curPrim = GetPrim().GetParent();
curPrim;
curPrim = curPrim.GetParent()) {

if ((attr = curModel.GetModelDrawModeAttr()) && attr &&
attr.Get(&drawMode)) {
if (_GetAuthoredDrawMode(curPrim, &drawMode)) {
return drawMode;
}
}
Expand All @@ -489,5 +506,6 @@ UsdGeomModelAPI::ComputeModelDrawMode() const
return UsdGeomTokens->default_;
}


PXR_NAMESPACE_CLOSE_SCOPE

20 changes: 11 additions & 9 deletions pxr/usd/lib/usdGeom/modelAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -589,17 +589,19 @@ class UsdGeomModelAPI : public UsdAPISchemaBase
/// Otherwise, its computed model:drawMode is that of its closest ancestor
/// with an authored model:drawMode.
///
/// This function should be considered a reference implementation for
/// correctness. <b>If called on each prim in the context of a traversal
/// we will perform massive overcomputation, because sibling prims share
/// sub-problems in the query that can be efficiently cached, but are not
/// (cannot be) by this simple implementation.</b> If you have control of
/// your traversal, it will be far more efficient to manage model:drawMode
/// on a stack as you traverse.
///
/// If this function is being called in a traversal context to compute
/// the draw mode of an entire hierarchy of prims, it would be beneficial
/// to cache and pass in the computed parent draw-mode via the
/// \p parentDrawMode parameter. This avoids repeated upward traversal to
/// look for ancestor opinions.
///
/// When \p parentDrawMode is empty (or unspecified), this function does
/// an upward traversal to find the closest ancestor with an authored
/// model:drawMode.
///
/// \sa GetModelDrawModeAttr()
USDGEOM_API
TfToken ComputeModelDrawMode() const;
TfToken ComputeModelDrawMode(const TfToken &parentDrawMode=TfToken()) const;
};


Expand Down
3 changes: 2 additions & 1 deletion pxr/usd/lib/usdGeom/wrapModelAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,8 @@ WRAP_CUSTOM {
.def("GetConstraintTargets", &UsdGeomModelAPI::GetConstraintTargets,
return_value_policy<TfPySequenceToList>())

.def("ComputeModelDrawMode", &UsdGeomModelAPI::ComputeModelDrawMode)
.def("ComputeModelDrawMode", &UsdGeomModelAPI::ComputeModelDrawMode,
(arg("parentDrawMode")=TfToken()))
;
}

Expand Down

0 comments on commit 17a48ef

Please sign in to comment.