Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding GetAll method to the codegenTemplates for multiple-apply schemas #1773

Closed
24 changes: 24 additions & 0 deletions pxr/usd/usd/apiSchemaBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,30 @@ PXR_NAMESPACE_CLOSE_SCOPE

PXR_NAMESPACE_OPEN_SCOPE

/* static */
TfTokenVector
UsdAPISchemaBase::_GetMultipleApplyInstanceNames(const UsdPrim &prim,
const TfType &schemaType)
{
TfTokenVector instanceNames;

auto appliedSchemas = prim.GetAppliedSchemas();
if (appliedSchemas.empty()) {
return instanceNames;
}

TfToken schemaTypeName = UsdSchemaRegistry::GetAPISchemaTypeName(schemaType);

for (const auto &appliedSchema : appliedSchemas) {
std::pair<TfToken, TfToken> typeNameAndInstance =
UsdSchemaRegistry::GetTypeNameAndInstance(appliedSchema);
if (typeNameAndInstance.first == schemaTypeName) {
instanceNames.emplace_back(typeNameAndInstance.second);
}
}

return instanceNames;
}

/* virtual */
bool
Expand Down
5 changes: 5 additions & 0 deletions pxr/usd/usd/apiSchemaBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ class UsdAPISchemaBase : public UsdSchemaBase
return _instanceName;
}

/// Returns a vector of names of API schema objects belonging to a
/// multiple-apply API schema applied to a given prim.
static TfTokenVector _GetMultipleApplyInstanceNames(const UsdPrim &prim,
const TfType &schemaType);

protected:
/// Check whether this APISchema object is valid for the currently held
/// prim.
Expand Down
14 changes: 14 additions & 0 deletions pxr/usd/usd/codegenTemplates/schemaClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,20 @@ TF_DEFINE_PRIVATE_TOKENS(
return {{ cls.cppClassName }}(prim, name);
}

/* static */
std::vector<{{ cls.cppClassName }}>
{{ cls.cppClassName }}::GetAll(const UsdPrim &prim)
{
std::vector<{{ cls.cppClassName }}> schemas;

for (const auto &schemaName :
UsdAPISchemaBase::_GetMultipleApplyInstanceNames(prim, _GetStaticTfType())) {
schemas.emplace_back(prim, schemaName);
}

return schemas;
}

{% endif %}
{% endif %}
{% if cls.isConcrete %}
Expand Down
7 changes: 7 additions & 0 deletions pxr/usd/usd/codegenTemplates/schemaClass.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,13 @@ class {{ cls.cppClassName }} : public {{ cls.parentCppClassName }}
{% endif -%}
static {{ cls.cppClassName }}
Get(const UsdPrim &prim, const TfToken &name);

/// Return a vector of {{ cls.cppClassName }} holding the prim \p prim.
{% if useExportAPI -%}
{{ Upper(libraryName) }}_API
{% endif -%}
static std::vector<{{ cls.cppClassName }}>
GetAll(const UsdPrim &prim);
{% endif %}
{% endif %}

Expand Down
9 changes: 9 additions & 0 deletions pxr/usd/usd/codegenTemplates/wrapSchemaClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,15 @@ void wrap{{ cls.cppClassName }}()
{% endif %}
.staticmethod("Get")
{% endif %}
{% if cls.isMultipleApply %}

.def("GetAll",
(std::vector<{{ cls.cppClassName }}>(*)(const UsdPrim &prim))
&This::GetAll,
arg("prim"),
return_value_policy<TfPySequenceToList>())
.staticmethod("GetAll")
{% endif %}
{% if cls.isConcrete %}

.def("Define", &This::Define, (arg("stage"), arg("path")))
Expand Down
14 changes: 14 additions & 0 deletions pxr/usd/usd/collectionAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@ UsdCollectionAPI::Get(const UsdPrim &prim, const TfToken &name)
return UsdCollectionAPI(prim, name);
}

/* static */
std::vector<UsdCollectionAPI>
UsdCollectionAPI::GetAll(const UsdPrim &prim)
{
std::vector<UsdCollectionAPI> schemas;

for (const auto &schemaName :
UsdAPISchemaBase::_GetMultipleApplyInstanceNames(prim, _GetStaticTfType())) {
schemas.emplace_back(prim, schemaName);
}

return schemas;
}


/* static */
bool
Expand Down
8 changes: 7 additions & 1 deletion pxr/usd/usd/collectionAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,11 @@ class UsdCollectionAPI : public UsdAPISchemaBase
static UsdCollectionAPI
Get(const UsdPrim &prim, const TfToken &name);

/// Return a vector of UsdCollectionAPI holding the prim \p prim.
USD_API
static std::vector<UsdCollectionAPI>
GetAll(const UsdPrim &prim);

/// Checks if the given name \p baseName is the base name of a property
/// of CollectionAPI.
USD_API
Expand Down Expand Up @@ -424,7 +429,8 @@ class UsdCollectionAPI : public UsdAPISchemaBase
static UsdCollectionAPI GetCollection(const UsdPrim &prim,
const TfToken &name);

/// Returns all the named collections on the given USD prim.
/// Returns all the named collections on the given USD prim.
/// \deprecated Use GetAll(prim) instead.
USD_API
static std::vector<UsdCollectionAPI> GetAllCollections(const UsdPrim &prim);

Expand Down
2 changes: 1 addition & 1 deletion pxr/usd/usd/testenv/testUsdCollectionAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ def test_CollectionEquivalence(self):
# ends up equivalent.

# Get all collections on the root test prim
collections = Usd.CollectionAPI.GetAllCollections(testPrim)
collections = Usd.CollectionAPI.GetAll(testPrim)
self.assertTrue(len(collections) > 1)

# Each of their membership queries should be equal to itself,
Expand Down
7 changes: 7 additions & 0 deletions pxr/usd/usd/wrapCollectionAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ void wrapUsdCollectionAPI()
(arg("prim"), arg("name")))
.staticmethod("Get")

.def("GetAll",
(std::vector<UsdCollectionAPI>(*)(const UsdPrim &prim))
&This::GetAll,
arg("prim"),
return_value_policy<TfPySequenceToList>())
.staticmethod("GetAll")

.def("CanApply", &_WrapCanApply, (arg("prim"), arg("name")))
.staticmethod("CanApply")

Expand Down
14 changes: 14 additions & 0 deletions pxr/usd/usdPhysics/driveAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@ UsdPhysicsDriveAPI::Get(const UsdPrim &prim, const TfToken &name)
return UsdPhysicsDriveAPI(prim, name);
}

/* static */
std::vector<UsdPhysicsDriveAPI>
UsdPhysicsDriveAPI::GetAll(const UsdPrim &prim)
{
std::vector<UsdPhysicsDriveAPI> schemas;

for (const auto &schemaName :
UsdAPISchemaBase::_GetMultipleApplyInstanceNames(prim, _GetStaticTfType())) {
schemas.emplace_back(prim, schemaName);
}

return schemas;
}


/* static */
bool
Expand Down
5 changes: 5 additions & 0 deletions pxr/usd/usdPhysics/driveAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ class UsdPhysicsDriveAPI : public UsdAPISchemaBase
static UsdPhysicsDriveAPI
Get(const UsdPrim &prim, const TfToken &name);

/// Return a vector of UsdPhysicsDriveAPI holding the prim \p prim.
USDPHYSICS_API
static std::vector<UsdPhysicsDriveAPI>
GetAll(const UsdPrim &prim);

/// Checks if the given name \p baseName is the base name of a property
/// of PhysicsDriveAPI.
USDPHYSICS_API
Expand Down
14 changes: 14 additions & 0 deletions pxr/usd/usdPhysics/limitAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@ UsdPhysicsLimitAPI::Get(const UsdPrim &prim, const TfToken &name)
return UsdPhysicsLimitAPI(prim, name);
}

/* static */
std::vector<UsdPhysicsLimitAPI>
UsdPhysicsLimitAPI::GetAll(const UsdPrim &prim)
{
std::vector<UsdPhysicsLimitAPI> schemas;

for (const auto &schemaName :
UsdAPISchemaBase::_GetMultipleApplyInstanceNames(prim, _GetStaticTfType())) {
schemas.emplace_back(prim, schemaName);
}

return schemas;
}


/* static */
bool
Expand Down
5 changes: 5 additions & 0 deletions pxr/usd/usdPhysics/limitAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ class UsdPhysicsLimitAPI : public UsdAPISchemaBase
static UsdPhysicsLimitAPI
Get(const UsdPrim &prim, const TfToken &name);

/// Return a vector of UsdPhysicsLimitAPI holding the prim \p prim.
USDPHYSICS_API
static std::vector<UsdPhysicsLimitAPI>
GetAll(const UsdPrim &prim);

/// Checks if the given name \p baseName is the base name of a property
/// of PhysicsLimitAPI.
USDPHYSICS_API
Expand Down
7 changes: 7 additions & 0 deletions pxr/usd/usdPhysics/wrapDriveAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,13 @@ void wrapUsdPhysicsDriveAPI()
(arg("prim"), arg("name")))
.staticmethod("Get")

.def("GetAll",
(std::vector<UsdPhysicsDriveAPI>(*)(const UsdPrim &prim))
&This::GetAll,
arg("prim"),
return_value_policy<TfPySequenceToList>())
.staticmethod("GetAll")

.def("CanApply", &_WrapCanApply, (arg("prim"), arg("name")))
.staticmethod("CanApply")

Expand Down
7 changes: 7 additions & 0 deletions pxr/usd/usdPhysics/wrapLimitAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ void wrapUsdPhysicsLimitAPI()
(arg("prim"), arg("name")))
.staticmethod("Get")

.def("GetAll",
(std::vector<UsdPhysicsLimitAPI>(*)(const UsdPrim &prim))
&This::GetAll,
arg("prim"),
return_value_policy<TfPySequenceToList>())
.staticmethod("GetAll")

.def("CanApply", &_WrapCanApply, (arg("prim"), arg("name")))
.staticmethod("CanApply")

Expand Down