Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pxr/usd/usdGeom/plugInfo.json
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,10 @@
"schemaTypes": [
"UsdGeomSubset"
]
},
},
"YUpAxisValidator": {
"doc": "UsdStage metadata of upAxis must be Y."
},
"keywords": [
"UsdGeomValidators"
]
Expand Down
54 changes: 46 additions & 8 deletions pxr/usd/usdGeom/testenv/testUsdGeomValidators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ TestUsdGeomValidators()
UsdGeomValidatorNameTokens->subsetFamilies,
UsdGeomValidatorNameTokens->subsetParentIsImageable,
UsdGeomValidatorNameTokens->stageMetadataChecker,
UsdGeomValidatorNameTokens->yUpAxisValidator
};

const UsdValidationRegistry& registry =
Expand All @@ -41,7 +42,7 @@ TestUsdGeomValidators()

UsdValidatorMetadataVector metadata =
registry.GetValidatorMetadataForPlugin(_tokens->usdGeomPlugin);
TF_AXIOM(metadata.size() == 3);
TF_AXIOM(metadata.size() == 4);
for (const UsdValidatorMetadata& metadata : metadata) {
validatorMetadataNameSet.insert(metadata.name);
}
Expand Down Expand Up @@ -315,7 +316,7 @@ TestUsdGeomSubsetParentIsImageable()
}

static
void TestUsdStageMetadata()
void TestUsdGeomStageMetadata()
{
// Get stageMetadataChecker
UsdValidationRegistry &registry = UsdValidationRegistry::GetInstance();
Expand All @@ -333,10 +334,10 @@ void TestUsdStageMetadata()
TF_AXIOM(errors.size() == 2);
auto rootLayerIdentifier = rootLayer->GetIdentifier().c_str();
const std::vector<std::string> expectedErrorMessages = {
TfStringPrintf("Stage with root layer <%s> does not specify its linear "
"scale in metersPerUnit.", rootLayerIdentifier),
TfStringPrintf("Stage with root layer <%s> does not specify an upAxis.",
rootLayerIdentifier)
TfStringPrintf("Stage with root layer <%s> does not specify its "
"linear scale in metersPerUnit.", rootLayerIdentifier),
TfStringPrintf("Stage with root layer <%s> does not specify an "
"upAxis.", rootLayerIdentifier)
};

const std::vector<TfToken> expectedErrorIdentifiers = {
Expand All @@ -363,14 +364,51 @@ void TestUsdStageMetadata()
TF_AXIOM(errors.empty());
}

static
void TestUsdGeomYUpAxisValidator()
{
UsdValidationRegistry &registry = UsdValidationRegistry::GetInstance();
const UsdValidator *validator = registry.GetOrLoadValidatorByName(
UsdGeomValidatorNameTokens->yUpAxisValidator);
TF_AXIOM(validator);

// Create an empty stage with a Z up axis
SdfLayerRefPtr rootLayer = SdfLayer::CreateAnonymous();
UsdStageRefPtr usdStage = UsdStage::Open(rootLayer);

UsdGeomSetStageUpAxis(usdStage, UsdGeomTokens->z);

UsdValidationErrorVector errors = validator->Validate(usdStage);

// Verify the error is present
const TfToken expectedIdentifier =
TfToken("usdGeom:YUpAxisValidator.nonYUpAxis");
const std::string expectedErrorMsg =
"Stage specifies upAxis 'Z'. upAxis should be 'Y'.";
TF_AXIOM(errors.size() == 1);
TF_AXIOM(errors[0].GetType() == UsdValidationErrorType::Error);
TF_AXIOM(errors[0].GetIdentifier() == expectedIdentifier);
TF_AXIOM(errors[0].GetSites().size() == 1);
TF_AXIOM(errors[0].GetSites()[0].IsValid());
TF_AXIOM(errors[0].GetMessage() == expectedErrorMsg);

// Change the up axis to Y
UsdGeomSetStageUpAxis(usdStage, UsdGeomTokens->y);

errors = validator->Validate(usdStage);

// Verify the errors are fixed
TF_AXIOM(errors.empty());
}

int
main()
{
TestUsdGeomValidators();
TestUsdGeomSubsetFamilies();
TestUsdGeomSubsetParentIsImageable();
TestUsdStageMetadata();
TestUsdGeomStageMetadata();
TestUsdGeomYUpAxisValidator();

std::cout << "OK\n";
return EXIT_SUCCESS;
}
6 changes: 4 additions & 2 deletions pxr/usd/usdGeom/validatorTokens.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ PXR_NAMESPACE_OPEN_SCOPE
#define USD_GEOM_VALIDATOR_NAME_TOKENS \
((stageMetadataChecker, "usdGeom:StageMetadataChecker")) \
((subsetFamilies, "usdGeom:SubsetFamilies")) \
((subsetParentIsImageable, "usdGeom:SubsetParentIsImageable"))
((subsetParentIsImageable, "usdGeom:SubsetParentIsImageable")) \
((yUpAxisValidator, "usdGeom:YUpAxisValidator"))

#define USD_GEOM_VALIDATOR_KEYWORD_TOKENS \
(UsdGeomSubset) \
Expand All @@ -29,7 +30,8 @@ PXR_NAMESPACE_OPEN_SCOPE
((missingMetersPerUnitMetadata, "MissingMetersPerUnitMetadata")) \
((missingUpAxisMetadata, "MissingUpAxisMetadata")) \
((invalidSubsetFamily, "InvalidSubsetFamily")) \
((notImageableSubsetParent, "NotImageableSubsetParent"))
((notImageableSubsetParent, "NotImageableSubsetParent")) \
((nonYUpAxis, "nonYUpAxis"))

/// \def USD_GEOM_VALIDATOR_NAME_TOKENS
/// Tokens representing validator names. Note that for plugin provided
Expand Down
31 changes: 31 additions & 0 deletions pxr/usd/usdGeom/validators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,33 @@ _SubsetParentIsImageable(const UsdPrim& usdPrim)
};
}

static
UsdValidationErrorVector
_YUpAxisValidator(const UsdStagePtr &usdStage)
{
if (usdStage->HasAuthoredMetadata(UsdGeomTokens->upAxis)) {
TfToken axis;
usdStage->GetMetadata(UsdGeomTokens->upAxis, &axis);

if (axis != UsdGeomTokens->y)
{
return {
UsdValidationError(
UsdGeomValidationErrorNameTokens->nonYUpAxis,
UsdValidationErrorType::Error,
UsdValidationErrorSites{UsdValidationErrorSite(usdStage,
SdfPath("/"))},
TfStringPrintf(
"Stage specifies upAxis '%s'. upAxis should"
" be 'Y'.", axis.GetText())
)
};
}
}

return {};
}

TF_REGISTRY_FUNCTION(UsdValidationRegistry)
{
UsdValidationRegistry &registry = UsdValidationRegistry::GetInstance();
Expand All @@ -170,6 +197,10 @@ TF_REGISTRY_FUNCTION(UsdValidationRegistry)
registry.RegisterPluginValidator(
UsdGeomValidatorNameTokens->subsetParentIsImageable,
_SubsetParentIsImageable);

registry.RegisterPluginValidator(
UsdGeomValidatorNameTokens->yUpAxisValidator,
_YUpAxisValidator);
}

PXR_NAMESPACE_CLOSE_SCOPE