Materialx Protobuf API 1.39.4
Serialization API to convert between MaterialX and Google Protobuf formats.
Loading...
Searching...
No Matches
test_materialx Namespace Reference

Functions

 get_file_path (filename)
 test_file_exists (filename)
 test_json_valid (json_file)
 test_mxpb_to_protobuf (mxpb_file)
 test_materialx_to_protobuf (mtlx_file)
 test_mtlx_protobuf_mtlx_equivalence (mtlx_file)
 test_mxpb_to_mtlx (mxpb_file)

Variables

bool HAS_MATERIALX = True
 data_dir = os.path.join(os.path.dirname(__file__), '../source/data')
list data_files

Function Documentation

◆ get_file_path()

test_materialx.get_file_path ( filename)
30def get_file_path(filename):
31 return os.path.join(data_dir, filename)
32
33
34# Test that all files exist
35@pytest.mark.parametrize('filename', data_files)

◆ test_file_exists()

test_materialx.test_file_exists ( filename)
36def test_file_exists(filename):
37 assert os.path.exists(get_file_path(filename)), f"File {filename} does not exist."
38
39
40# Test that JSON files are valid JSON

◆ test_json_valid()

test_materialx.test_json_valid ( json_file)
43def test_json_valid(json_file):
44 with open(get_file_path(json_file), 'r') as f:
45 json_data = f.read()
46 try:
47 obj = json.loads(json_data)
48 except Exception as e:
49 pytest.fail(f"Invalid JSON in {json_file}: {e}")
50
51
52# Test that .mxpb files can be parsed into protobuf objects
53@pytest.mark.parametrize('mxpb_file', [f for f in data_files if f.endswith('.mxpb')])

◆ test_materialx_to_protobuf()

test_materialx.test_materialx_to_protobuf ( mtlx_file)
63def test_materialx_to_protobuf(mtlx_file):
64 if not HAS_MATERIALX:
65 pytest.skip("MaterialX Python API not installed")
66 doc = mx.createDocument()
67 mx.readFromXmlFile(doc, get_file_path(mtlx_file))
68 pb_doc = materialx_serializer.MaterialXToProtobuf().convert(doc)
69 assert pb_doc is not None
70
71# Test mtlx -> protobuf -> mtlx equivalence
72@pytest.mark.parametrize('mtlx_file', [f for f in data_files if f.endswith('.mtlx')])
Converter class to transform MaterialX document objects into Protobuf MaterialXDocument messages.
Definition materialx_serializer.py:17

◆ test_mtlx_protobuf_mtlx_equivalence()

test_materialx.test_mtlx_protobuf_mtlx_equivalence ( mtlx_file)
73def test_mtlx_protobuf_mtlx_equivalence(mtlx_file):
74 if not HAS_MATERIALX:
75 pytest.skip("MaterialX Python API not installed")
76 # Read original mtlx
77 doc1 = mx.createDocument()
78 mx.readFromXmlFile(doc1, get_file_path(mtlx_file))
79 # Convert to protobuf
80 pb_doc = materialx_serializer.MaterialXToProtobuf().convert(doc1)
81 # Convert back to MaterialX
82 doc2 = materialx_serializer.ProtobufToMaterialX().convert(pb_doc, mx)
83 # Check equivalence
84 options = mx.ElementEquivalenceOptions()
85 is_same, differences = doc1.isEquivalent(doc2, options)
86 assert is_same, f"Documents are not equivalent for {mtlx_file}: {differences}"
87
88# Test mxpb -> mtlx conversion
89@pytest.mark.parametrize('mxpb_file', [f for f in data_files if f.endswith('.mxpb')])
Converter class to transform Protobuf MaterialXDocument messages back into MaterialX document objects...
Definition materialx_serializer.py:83

◆ test_mxpb_to_mtlx()

test_materialx.test_mxpb_to_mtlx ( mxpb_file)
90def test_mxpb_to_mtlx(mxpb_file):
91 if not HAS_MATERIALX:
92 pytest.skip("MaterialX Python API not installed")
93 with open(get_file_path(mxpb_file), 'rb') as f:
94 pb_data = f.read()
96 doc = materialx_serializer.ProtobufToMaterialX().convert(pb_doc, mx)
97 # Check that doc is a MaterialX document
98 assert hasattr(doc, 'getVersionString'), f"Conversion failed for {mxpb_file}"
from_string(data)
Convert string data to Protobuf document.
Definition materialx_serializer.py:175

◆ test_mxpb_to_protobuf()

test_materialx.test_mxpb_to_protobuf ( mxpb_file)
54def test_mxpb_to_protobuf(mxpb_file):
55 with open(get_file_path(mxpb_file), 'rb') as f:
56 pb_data = f.read()
58 assert pb_doc is not None
59
60
61# Test that .mtlx files can be parsed and converted to protobuf (requires MaterialX)
62@pytest.mark.parametrize('mtlx_file', [f for f in data_files if f.endswith('.mtlx')])

Variable Documentation

◆ data_dir

test_materialx.data_dir = os.path.join(os.path.dirname(__file__), '../source/data')

◆ data_files

list test_materialx.data_files
Initial value:
1= [
2 'standard_surface_chess_set.json',
3 'standard_surface_chess_set.mtlx',
4 'standard_surface_chess_set.mxpb',
5 'standard_surface_chess_set_converted.mtlx',
6 'standard_surface_chess_set_from_pb.mtlx',
7 'unlit_cross.json',
8 'unlit_cross.mtlx',
9 'unlit_cross.mxpb',
10 'unlit_cross_converted.mtlx',
11 'unlit_cross_from_pb.mtlx',
12]

◆ HAS_MATERIALX

bool test_materialx.HAS_MATERIALX = True