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

Add Python binding for UsdValidatorMetadata #3232

Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 8 additions & 0 deletions pxr/usd/usd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ pxr_library(usd
wrapTyped.cpp
wrapUsdFileFormat.cpp
wrapUtils.cpp
wrapValidator.cpp
wrapVariantSets.cpp
wrapVersion.cpp
wrapZipFile.cpp
Expand Down Expand Up @@ -272,6 +273,7 @@ pxr_test_scripts(
testenv/testUsdTimeSamples.py
testenv/testUsdTimeValueAuthoring.py
testenv/testUsdUsdzFileFormat.py
testenv/testUsdValidatorMetadata.py
testenv/testUsdValueClips.py
testenv/testUsdVariantEditing.py
testenv/testUsdVariantFallbacks.py
Expand Down Expand Up @@ -1332,3 +1334,9 @@ pxr_register_test(testUsdOpaqueAttributes
COMMAND "${CMAKE_INSTALL_PREFIX}/tests/testUsdOpaqueAttributes"
EXPECTED_RETURN_CODE 0
)

pxr_register_test(testUsdValidatorMetadata
PYTHON
COMMAND "${CMAKE_INSTALL_PREFIX}/tests/testUsdValidatorMetadata"
EXPECTED_RETURN_CODE 0
)
1 change: 1 addition & 0 deletions pxr/usd/usd/module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ TF_WRAP_MODULE
TF_WRAP(UsdSpecializes);
TF_WRAP(UsdPrimRange);
TF_WRAP(UsdVariantSets);
TF_WRAP(UsdValidator);

// SchemaBase, APISchemaBase and subclasses.
TF_WRAP(UsdSchemaBase);
Expand Down
159 changes: 159 additions & 0 deletions pxr/usd/usd/testenv/testUsdValidatorMetadata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
#!/pxrpythonsubst
#
# Copyright 2024 Pixar
#
# Licensed under the terms set forth in the LICENSE.txt file available at
# https://openusd.org/license.

import unittest

from pxr import Plug, Sdf, Usd


class TestUsdValidatorMetadata(unittest.TestCase):
def _verify_metadata(
self,
metadata: Usd.ValidatorMetadata,
name="",
doc="",
keywords=[],
schemaTypes=[],
plugin=None,
isSuite=False
):
self.assertEqual(metadata.name, name)
self.assertEqual(metadata.doc, doc)
self.assertEqual(metadata.GetKeywords(), keywords)
self.assertEqual(metadata.GetSchemaTypes(), schemaTypes)
self.assertEqual(metadata.plugin, plugin)
self.assertEqual(metadata.isSuite, isSuite)

def test_create_default_metadata(self):
metadata = Usd.ValidatorMetadata()
self._verify_metadata(metadata)

def test_create_metadata_with_valid_keyword_args(self):
all_plugins = Plug.Registry().GetAllPlugins()
expected_plugin = all_plugins[0] if all_plugins else None
valid_metadatas = [
{
"name": "empty_validator"
},
{
"name": "validator1",
"doc": "This is a test validator.",
"keywords": ["validator1", "test"],
"schemaTypes": ["SomePrimType"],
"plugin": None,
"isSuite": False
},
{
"name": "validator2",
"doc": "This is another test validator.",
"keywords": ["validator2", "test"],
"schemaTypes": ["NewPrimType"],
"plugin": expected_plugin,
"isSuite": False
}
]

for args in valid_metadatas:
with self.subTest(name=args["name"]):
metadata = Usd.ValidatorMetadata(**args)
self._verify_metadata(metadata, **args)

def test_create_metadata_with_invalid_keyword_args(self):
invalid_metadatas = {
"Wrong Name Type": {
"name": 123
},
"Wrong Doc Type": {
"doc": 123
},
"Wrong Keywords Type": {
"keywords": 123
},
"Wrong Schema Types": {
"schemaTypes": 123
},
"Wrong Plugin Type": {
"plugin": 123
},
"Wrong IsSuite Type": {
"isSuite": "wrong type"
}
}

for error_category, args in invalid_metadatas.items():
with self.subTest(error_type=error_category):
with self.assertRaises(Exception):
Usd.ValidatorMetadata(**args)

def test_metadata_name_getter_and_setter(self):
metadata = Usd.ValidatorMetadata()
for name in ["validator1", "validator2"]:
metadata.name = name
self.assertEqual(metadata.name, name)

# Invalid type
with self.assertRaises(Exception):
metadata.name = 123

def test_metadata_doc_getter_and_setter(self):
metadata = Usd.ValidatorMetadata()
for doc in ["doc1", "doc2"]:
metadata.doc = doc
self.assertEqual(metadata.doc, doc)

# Invalid type
with self.assertRaises(Exception):
metadata.doc = 123

def test_metadata_keywords_getter_and_setter(self):
metadata = Usd.ValidatorMetadata()
for keywords in [["keyword1"], ["keyword2"]]:
metadata.SetKeywords(keywords)
self.assertEqual(metadata.GetKeywords(), keywords)

# Invalid type
with self.assertRaises(Exception):
metadata.SetKeywords(123)
with self.assertRaises(Exception):
metadata.SetKeywords("123")

def test_metadata_schemaTypes_getter_and_setter(self):
metadata = Usd.ValidatorMetadata()
for schema_types in [["PrimType1"], ["PrimType2"]]:
metadata.SetSchemaTypes(schema_types)
self.assertEqual(metadata.GetSchemaTypes(), schema_types)

# Invalid type
with self.assertRaises(Exception):
metadata.SetKeywords(123)
with self.assertRaises(Exception):
metadata.SetKeywords("123")

def test_metadata_plugin_getter_and_setter(self):
all_plugins = Plug.Registry().GetAllPlugins()
expected_plugin = all_plugins[0] if all_plugins else None
metadata = Usd.ValidatorMetadata()
metadata.plugin = expected_plugin
self.assertEqual(metadata.plugin, expected_plugin)

# Invalid type
with self.assertRaises(Exception):
metadata.SetKeywords(123)

def test_metadata_is_suite_getter_and_setter(self):
metadata = Usd.ValidatorMetadata()
for suite in [True, False]:
metadata.isSuite = suite
self.assertEqual(metadata.isSuite, suite)

# Invalid type
with self.assertRaises(Exception):
metadata.SetKeywords("123")


if __name__ == "__main__":
unittest.main()
29 changes: 11 additions & 18 deletions pxr/usd/usd/validationError.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,19 @@
// https://openusd.org/license.
//

#include "pxr/base/tf/enum.h"
#include "pxr/usd/usd/validationError.h"

PXR_NAMESPACE_OPEN_SCOPE

TF_REGISTRY_FUNCTION(TfEnum)
{
TF_ADD_ENUM_NAME(UsdValidationErrorType::None, "None");
TF_ADD_ENUM_NAME(UsdValidationErrorType::Error, "Error");
TF_ADD_ENUM_NAME(UsdValidationErrorType::Warn, "Warn");
TF_ADD_ENUM_NAME(UsdValidationErrorType::Info, "Info");
}

UsdValidationErrorSite::UsdValidationErrorSite(
const SdfLayerHandle &layer, const SdfPath &objectPath) :
_layer(layer), _objectPath(objectPath)
Expand Down Expand Up @@ -39,24 +48,8 @@ UsdValidationError::UsdValidationError(const UsdValidationErrorType &type,
std::string
UsdValidationError::GetErrorAsString() const
{
std::string errorTypeAsString;
switch(_errorType) {
case UsdValidationErrorType::None:
return _errorMsg;
break;
case UsdValidationErrorType::Error:
errorTypeAsString = "Error";
break;
case UsdValidationErrorType::Warn:
errorTypeAsString = "Warn";
break;
case UsdValidationErrorType::Info:
errorTypeAsString = "Info";
break;
}

const std::string separator = ": ";
return errorTypeAsString + separator + _errorMsg;
return _errorType == UsdValidationErrorType::None ? _errorMsg : TfStringPrintf(
"%s: %s", TfEnum::GetDisplayName(_errorType).c_str(), _errorMsg.c_str());
}

void
Expand Down
106 changes: 106 additions & 0 deletions pxr/usd/usd/wrapValidator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
//
// Copyright 2024 Pixar
//
// Licensed under the terms set forth in the LICENSE.txt file available at
// https://openusd.org/license.
//
#include "pxr/pxr.h"
#include "pxr/usd/usd/validator.h"

#include "pxr/base/tf/pyContainerConversions.h"
#include "pxr/base/tf/pyPtrHelpers.h"
#include "pxr/base/tf/pyResultConversions.h"

#include <boost/python/class.hpp>
#include <boost/python/make_constructor.hpp>
#include <boost/python/operators.hpp>
#include <boost/python/object.hpp>

using namespace boost::python;

PXR_NAMESPACE_USING_DIRECTIVE

namespace
{

UsdValidatorMetadata *
_NewMetadata(
const TfToken &name,
const PlugPluginPtr &plugin,
const TfTokenVector &keywords,
const TfToken &doc,
const TfTokenVector &schemaTypes,
bool isSuite)
{
return new UsdValidatorMetadata{name, plugin, keywords, doc, schemaTypes, isSuite};
}

TfToken
_GetMetadataName(const UsdValidatorMetadata &metadata)
{
return metadata.name;
}

void
_SetMetadataName(UsdValidatorMetadata &metadata, const TfToken &name)
{
metadata.name = name;
}

TfTokenVector
_GetMetadataKeywords(const UsdValidatorMetadata &metadata)
{
return metadata.keywords;
}

void
_SetMetadataKeywords(UsdValidatorMetadata &metadata, const TfTokenVector &keywords)
{
metadata.keywords = keywords;
}

TfTokenVector
_GetMetadataSchemaTypes(const UsdValidatorMetadata &metadata)
{
return metadata.schemaTypes;
}

void
_SetMetadataSchemaTypes(UsdValidatorMetadata &metadata, const TfTokenVector &schemaTypes)
{
metadata.schemaTypes = schemaTypes;
}

PlugPluginPtr
_GetMetadataPlugin(const UsdValidatorMetadata &metadata)
{
return metadata.pluginPtr;
}

void
_SetMetadataPlugin(UsdValidatorMetadata &metadata, const PlugPluginPtr &plugin)
{
metadata.pluginPtr = plugin;
}

} // anonymous namespace

void wrapUsdValidator()
{
class_<UsdValidatorMetadata>("ValidatorMetadata", no_init)
.def("__init__", make_constructor(&_NewMetadata, default_call_policies(),
(arg("name") = TfToken(),
arg("plugin") = PlugPluginPtr(),
arg("keywords") = TfTokenVector(),
arg("doc") = TfToken(),
arg("schemaTypes") = TfTokenVector(),
arg("isSuite") = false)))
.add_property("name", &_GetMetadataName, &_SetMetadataName)
.add_property("plugin", &_GetMetadataPlugin, &_SetMetadataPlugin)
.def("GetKeywords", &_GetMetadataKeywords, return_value_policy<TfPySequenceToList>())
.def("SetKeywords", &_SetMetadataKeywords)
.def_readwrite("doc", &UsdValidatorMetadata::doc)
.def("GetSchemaTypes", &_GetMetadataSchemaTypes, return_value_policy<TfPySequenceToList>())
.def("SetSchemaTypes", &_SetMetadataSchemaTypes)
.def_readwrite("isSuite", &UsdValidatorMetadata::isSuite);
}