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

usdValidation: usdGeom: YUpAxisValidator #3448

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/usdValidation/usdGeomValidators/plugInfo.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
"schemaTypes": [
"UsdGeomSubset"
]
},
},
"YUpAxisValidator": {
"doc": "UsdStage metadata of upAxis must be Y."
},
"keywords": [
"UsdGeomValidators"
]
Expand Down
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 @@ -42,7 +43,7 @@ TestUsdGeomValidators()
UsdValidationValidatorMetadataVector metadata
= registry.GetValidatorMetadataForPlugin(
_tokens->usdGeomValidatorsPlugin);
TF_AXIOM(metadata.size() == 3);
TF_AXIOM(metadata.size() == 4);
for (const UsdValidationValidatorMetadata &metadata : metadata) {
validatorMetadataNameSet.insert(metadata.name);
}
Expand Down Expand Up @@ -315,7 +316,7 @@ TestUsdGeomSubsetParentIsImageable()
}

static void
TestUsdStageMetadata()
TestUsdGeomStageMetadata()
{
// Get stageMetadataChecker
UsdValidationRegistry &registry = UsdValidationRegistry::GetInstance();
Expand All @@ -333,16 +334,17 @@ 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.",
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.",
TfStringPrintf("Stage with root layer <%s> does not specify an "
"upAxis.",
rootLayerIdentifier)
};

const std::vector<TfToken> expectedErrorIdentifiers = {
TfToken(
"usdGeomValidators:StageMetadataChecker.MissingMetersPerUnitMetadata"),
"usdGeomValidators:StageMetadataChecker.MissingMetersPerUnitMetadata"),
TfToken("usdGeomValidators:StageMetadataChecker.MissingUpAxisMetadata")
};

Expand All @@ -364,14 +366,51 @@ TestUsdStageMetadata()
TF_AXIOM(errors.empty());
}

static void
TestUsdGeomYUpAxisValidator()
{
UsdValidationRegistry &registry = UsdValidationRegistry::GetInstance();
const UsdValidationValidator *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("usdGeomValidators: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/usdValidation/usdGeomValidators/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, "usdGeomValidators:StageMetadataChecker")) \
((subsetFamilies, "usdGeomValidators:SubsetFamilies")) \
((subsetParentIsImageable, "usdGeomValidators:SubsetParentIsImageable"))
((subsetParentIsImageable, "usdGeomValidators:SubsetParentIsImageable")) \
((yUpAxisValidator, "usdGeomValidators: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
32 changes: 32 additions & 0 deletions pxr/usdValidation/usdGeomValidators/validators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,34 @@ _SubsetParentIsImageable(const UsdPrim &usdPrim)
usdPrim.GetPath().GetText(), parentPrim.GetPath().GetText())) };
}

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 @@ -156,6 +184,10 @@ TF_REGISTRY_FUNCTION(UsdValidationRegistry)
registry.RegisterPluginValidator(
UsdGeomValidatorNameTokens->subsetParentIsImageable,
_SubsetParentIsImageable);

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

PXR_NAMESPACE_CLOSE_SCOPE