forked from mitsuba-renderer/mitsuba-blender
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdowngrade.py
95 lines (79 loc) · 2.7 KB
/
downgrade.py
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
92
93
94
95
from os import path as osp
import re
import xml.etree.ElementTree as ET
from inflection import camelize, underscore
# from ipdb import set_trace
def convert(fname, mode="v1"):
dname = osp.dirname(fname)
base = osp.basename(fname)
# check ext
base, ext = osp.splitext(base)
if ext != ".xml":
print(f"Wrong file format {ext}")
return False
# check version if already in correct format than do nothing
tree = ET.parse(fname)
root = tree.getroot()
version = root.attrib["version"]
MAJOR_VER = int(version[0])
if mode == "v1":
if MAJOR_VER < 2:
print(f"Already in correct version {version}")
return True
else:
root.attrib["version"] = "0.6.0"
elif mode == "v2":
if MAJOR_VER > 0:
print(f"Already in correct version {version}")
return True
else:
root.attrib["version"] = "2.1.0"
# TODO : don't change filenames
f_str = ET.tostring(root).decode('utf-8')
if mode == "v1":
# converting to 0.6.0 version
# snake_case to camelCase
modified_str = camelize(f_str)
root_n = ET.fromstring(modified_str)
# ior
for i, ele_n in enumerate(root_n.findall(".//*[@name='intIor']")):
# print("Copying back %s"%ele_n)
ele_n.attrib["name"] = "intIOR"
for i, ele_n in enumerate(root_n.findall(".//*[@name='extIor']")):
# print("Copying back %s"%ele_n)
ele_n.attrib["name"] = "extIOR"
# copy back the filename
ele_iter = root.findall(".//*[@filename]")
for i, ele_n in enumerate(root_n.findall(".//*[@filename]")):
# print("Copying back %s"%ele_n)
ele = ele_iter[i]
ele_n.attrib["filename"] = ele.attrib["filename"]
ele_iter = root.findall(".//*[@name='filename']")
for i, ele_n in enumerate(root_n.findall(".//*[@name='filename']")):
# print("Copying back %s"%ele_n)
ele = ele_iter[i]
ele_n.attrib["value"] = ele.attrib["value"]
# translation
for ele in root_n.findall(".//translate"):
val = ele.attrib.pop("value")
x, y, z = val.split(" ")
ele.attrib["x"] = x
ele.attrib["y"] = y
ele.attrib["z"] = z
# include filename
for ele in root_n.findall(".//include"):
val = ele.attrib["filename"]
core, ext = osp.splitext(val)
ele.attrib["filename"] = f"{core}_v1{ext}"
# lookat -> lookAt
# for ele in root.findall(".//transform"):
# ele
# write the file
modified_str = ET.tostring(root_n).decode('utf-8')
with open(osp.join(dname, f"{base}_v1.xml"), "w") as f:
f.write(modified_str)
# ET.write(osp.join(dname, f"{base}_v1.xml"))
elif mode == "v2":
modified_str = underscore(f_str)
with open(osp.join(dname, f"{base}_v2.xml"), "w") as f:
f.write(modified_str)