Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 442
Expand file tree
/
Copy pathmxformat.py
More file actions
Latest commit
91 lines (79 loc) · 3.59 KB
/
Copy pathmxformat.py
File metadata and controls
91 lines (79 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python
'''
Reformat a folder of MaterialX documents in place, optionally upgrading
the documents to the latest version of the standard.
'''
importargparse
importos
importxml.etree.ElementTreeasET
importMaterialXasmx
defis_well_formed(xml_string):
error=''
try:
ET.fromstring(xml_string)
exceptET.ParseErrorase:
error=str(e)
returnerror
defmain():
parser=argparse.ArgumentParser(description="Reformat a folder of MaterialX documents in place.")
parser.add_argument('-y', '--yes', dest='yes', action="store_true", help="Proceed without asking for confirmation from the user.")
parser.add_argument('-u', '--upgrade', dest='upgrade', action="store_true", help='Upgrade documents to the latest version of the standard.')
parser.add_argument('-v', '--validate', dest='validate', action="store_true", help='Perform MaterialX validation on documents after reformatting.')
parser.add_argument('-x', '--xml_syntax', dest='xml_syntax', action="store_true", help='Check XML syntax after reformatting.')
parser.add_argument(dest="inputFolder", help="An input folder to scan for MaterialX documents.")
opts=parser.parse_args()
validDocs=dict()
forroot, dirs, filesinos.walk(opts.inputFolder):
forfilenameinfiles:
fullpath=os.path.join(root, filename)
iffullpath.endswith('.mtlx'):
doc=mx.createDocument()
try:
readOptions=mx.XmlReadOptions()
readOptions.readComments=True
readOptions.readNewlines=True
readOptions.upgradeVersion=opts.upgrade
try:
mx.readFromXmlFile(doc, fullpath, mx.FileSearchPath(), readOptions)
exceptExceptionaserr:
print('Skipping "'+filename+'" due to exception: '+str(err))
continue
validDocs[fullpath] =doc
exceptmx.Exception:
pass
ifnotvalidDocs:
print('No MaterialX documents were found in "%s"'% (opts.inputFolder))
return
print('Found %s MaterialX files in "%s"'% (len(validDocs), opts.inputFolder))
mxVersion=mx.getVersionIntegers()
ifnotopts.yes:
ifopts.upgrade:
question='Would you like to upgrade all %i documents to MaterialX v%i.%i in place (y/n)?'% (len(validDocs), mxVersion[0], mxVersion[1])
else:
question='Would you like to reformat all %i documents in place (y/n)?'%len(validDocs)
answer=input(question)
ifanswer!='y'andanswer!='Y':
return
validate=opts.validate
ifvalidate:
print(f'- Validate documents')
xml_syntax=opts.xml_syntax
ifxml_syntax:
print(f'- Check XML syntax')
for (filename, doc) invalidDocs.items():
ifxml_syntax:
xml_string=mx.writeToXmlString(doc)
errors=is_well_formed(xml_string)
iferrors:
print(f'- Warning: Document {filename} is not well-formed XML: {errors}')
ifvalidate:
is_valid, errors=doc.validate()
ifnotis_valid:
print(f'- Warning: Document {filename} is invalid. Errors {errors}.')
mx.writeToXmlFile(doc, filename)
ifopts.upgrade:
print('Upgraded %i documents to MaterialX v%i.%i'% (len(validDocs), mxVersion[0], mxVersion[1]))
else:
print('Reformatted %i documents '%len(validDocs))
if__name__=='__main__':
main()