Skip to content

Commit

Permalink
Add python binding for UsdValidatorMetadata
Browse files Browse the repository at this point in the history
  • Loading branch information
roggiezhang-nv committed Aug 15, 2024
1 parent 102a3e2 commit fc17b6e
Show file tree
Hide file tree
Showing 4 changed files with 274 additions and 0 deletions.
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.keywords, keywords)
self.assertEqual(metadata.schemaTypes, 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.keywords = keywords
self.assertEqual(metadata.keywords, keywords)

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

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

# Invalid type
with self.assertRaises(Exception):
metadata.keywords = 123
with self.assertRaises(Exception):
metadata.keywords = "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.keywords = 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.keywords = "123"


if __name__ == "__main__":
unittest.main()
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)
.add_property("keywords", make_function(
&_GetMetadataKeywords, return_value_policy<TfPySequenceToList>()), _SetMetadataKeywords)
.def_readwrite("doc", &UsdValidatorMetadata::doc)
.add_property("schemaTypes", make_function(
&_GetMetadataSchemaTypes, return_value_policy<TfPySequenceToList>()), _SetMetadataSchemaTypes)
.def_readwrite("isSuite", &UsdValidatorMetadata::isSuite);
}

0 comments on commit fc17b6e

Please sign in to comment.