-
Notifications
You must be signed in to change notification settings - Fork 0
/
to_dict.py
65 lines (51 loc) · 1.41 KB
/
to_dict.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
import mitsuba as mi
import drjit as dr
if __name__ == "__main__":
mi.set_variant("cuda_ad_rgb")
def to_dict(scene: mi.Scene):
assert isinstance(scene, mi.Scene)
unknown_counter = 0
def get_id(child: mi.Object):
nonlocal unknown_counter
id = child.id()
if id == "":
id = f"unknown{unknown_counter}"
unknown_counter += 1
return id
children = [
*scene.shapes(),
*scene.emitters(),
*scene.sensors(),
scene.integrator(),
]
return {
"type": "scene",
**{get_id(child): child for child in children},
}
if __name__ == "__main__":
scene = mi.cornell_box()
scene["sg"] = {
"type": "shapegroup",
"second_object": {
"type": "sphere",
"to_world": mi.ScalarTransform4f()
.translate([-0.5, 0, 0])
.scale([0.2, 0.2, 0.2]),
"bsdf": {
"type": "diffuse",
},
},
}
scene["first_instance"] = {
"type": "instance",
"shapegroup": {"type": "ref", "id": "sg"},
}
scene = mi.load_dict(scene)
ref = mi.render(scene, spp=128)
mi.util.write_bitmap("out/ref.exr", ref)
scene = to_dict(scene)
print(f"{scene=}")
scene = mi.load_dict(scene)
res = mi.render(scene, spp=128)
mi.util.write_bitmap("out/res.exr", ref)
assert dr.allclose(ref, res)