Skip to content

Commit

Permalink
Expose isRootChild as DCC function.
Browse files Browse the repository at this point in the history
  • Loading branch information
deboisj committed Aug 9, 2023
1 parent 9cdbf38 commit 3ece341
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 11 deletions.
2 changes: 2 additions & 0 deletions lib/usdUfe/ufe/Global.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ Ufe::Rtid initialize(
UsdUfe::setIsAttributeLockedFn(dccFunctions.isAttributeLockedFn);
if (dccFunctions.saveStageLoadRulesFn)
UsdUfe::setSaveStageLoadRulesFn(dccFunctions.saveStageLoadRulesFn);
if (dccFunctions.isRootChildFn)
UsdUfe::setIsRootChildFn(dccFunctions.isRootChildFn);

// Create a default stages subject if none is provided.
if (nullptr == ss) {
Expand Down
1 change: 1 addition & 0 deletions lib/usdUfe/ufe/Global.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ struct USDUFE_PUBLIC DCCFunctions
// Optional: default values will be used if no function is supplied.
IsAttributeLockedFn isAttributeLockedFn = nullptr;
SaveStageLoadRulesFn saveStageLoadRulesFn = nullptr;
IsRootChildFn isRootChildFn = nullptr;
};

/*! Ufe runtime handlers used to initialize the plugin.
Expand Down
35 changes: 24 additions & 11 deletions lib/usdUfe/ufe/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ UsdUfe::UfePathToPrimFn gUfePathToPrimFn = nullptr;
UsdUfe::TimeAccessorFn gTimeAccessorFn = nullptr;
UsdUfe::IsAttributeLockedFn gIsAttributeLockedFn = nullptr;
UsdUfe::SaveStageLoadRulesFn gSaveStageLoadRulesFn = nullptr;
UsdUfe::IsRootChildFn gIsRootChildFn = nullptr;

} // anonymous namespace

Expand Down Expand Up @@ -231,6 +232,29 @@ void saveStageLoadRules(const PXR_NS::UsdStageRefPtr& stage)
gSaveStageLoadRulesFn(stage);
}

void setIsRootChildFn(IsRootChildFn fn)
{
// This function is allowed to be null in which case, the default implementation
// is used (isRootChildDefault()).
gIsRootChildFn = fn;
}

bool isRootChild(const Ufe::Path& path)
{
return gIsRootChildFn ? gIsRootChildFn(path) : isRootChildDefault(path);
}

bool isRootChildDefault(const Ufe::Path& path)
{
// When called we make the assumption that we are given a valid
// path and we are only testing whether or not we are a root child.
auto segments = path.getSegments();
if (segments.size() != 2) {
TF_RUNTIME_ERROR(kIllegalUFEPath, path.string().c_str());
}
return (segments[1].size() == 1);
}

int ufePathToInstanceIndex(const Ufe::Path& path, UsdPrim* prim)
{
int instanceIndex = UsdImagingDelegate::ALL_INSTANCES;
Expand All @@ -254,17 +278,6 @@ int ufePathToInstanceIndex(const Ufe::Path& path, UsdPrim* prim)
return instanceIndex;
}

bool isRootChild(const Ufe::Path& path)
{
// When called we make the assumption that we are given a valid
// path and we are only testing whether or not we are a root child.
auto segments = path.getSegments();
if (segments.size() != 2) {
TF_RUNTIME_ERROR(kIllegalUFEPath, path.string().c_str());
}
return (segments[1].size() == 1);
}

std::string uniqueName(const TfToken::HashSet& existingNames, std::string srcName)
{
// Compiled regular expression to find a numerical suffix to a path component.
Expand Down
14 changes: 14 additions & 0 deletions lib/usdUfe/ufe/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ typedef PXR_NS::UsdPrim (*UfePathToPrimFn)(const Ufe::Path&);
typedef PXR_NS::UsdTimeCode (*TimeAccessorFn)(const Ufe::Path&);
typedef bool (*IsAttributeLockedFn)(const PXR_NS::UsdAttribute& attr, std::string* errMsg);
typedef void (*SaveStageLoadRulesFn)(const PXR_NS::UsdStageRefPtr&);
typedef bool (*IsRootChildFn)(const Ufe::Path& path);

//------------------------------------------------------------------------------
// Helper functions
Expand Down Expand Up @@ -137,9 +138,22 @@ void saveStageLoadRules(const PXR_NS::UsdStageRefPtr& stage);
USDUFE_PUBLIC
int ufePathToInstanceIndex(const Ufe::Path& path, PXR_NS::UsdPrim* prim = nullptr);

//! Set the DCC specific "isRootChild" test function.
//! Use of this function is optional, if one is not supplied then
//! a default implementation of isRootChild is used..
USDUFE_PUBLIC
void setIsRootChildFn(IsRootChildFn fn);

//! Returns true if the path corresponds to an item at the root of a runtime.
//! Implementation can be set by the DCC.
USDUFE_PUBLIC
bool isRootChild(const Ufe::Path& path);

//! Default isRootChild() implementation. Assumes 2 segments. Will report a root child
//! if the second segment has a single component.
USDUFE_PUBLIC
bool isRootChildDefault(const Ufe::Path& path);

//! Split the source name into a base name and a numerical suffix (set to
//! 1 if absent). Increment the numerical suffix until name is unique.
USDUFE_PUBLIC
Expand Down

0 comments on commit 3ece341

Please sign in to comment.