Skip to content

Commit

Permalink
Add support for volumes in mi.xml.dict_to_xml
Browse files Browse the repository at this point in the history
  • Loading branch information
njroussel committed Mar 20, 2023
1 parent d9ea8e8 commit 15d63df
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 6 deletions.
74 changes: 72 additions & 2 deletions src/core/tests/test_write_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ def test13_xml_multiple_defaults(variants_all_rgb, tmp_path):


@fresolver_append_path
def test13_xml_empty_dir(variants_all_rgb, tmp_path):
def test14_xml_empty_dir(variants_all_rgb, tmp_path):
filepath = str(tmp_path / 'test13_xml_empty_dir.xml')
print(f"Output temporary file: {filepath}")

Expand All @@ -526,8 +526,8 @@ def test13_xml_empty_dir(variants_all_rgb, tmp_path):
"radius": 10.0,
}
mi.xml.dict_to_xml(scene_dict, os.path.split(filepath)[1])

os.chdir(cwd)

s1 = mi.load_dict({
'type': 'scene',
'sphere': {
Expand All @@ -539,3 +539,73 @@ def test13_xml_empty_dir(variants_all_rgb, tmp_path):
s2 = mi.load_file(filepath)

assert str(s1) == str(s2)


@fresolver_append_path
def test15_xml_volume(variants_all_rgb, tmp_path):
filepath = str(tmp_path / 'test_write_xml-test15_output.xml')
print(f"Output temporary file: {filepath}")

cwd = os.getcwd()
os.chdir(tmp_path)

scene_dict = {
'type': 'scene',
'sphere' : {
'type': 'sphere',
'center': [0, 0, -10],
'radius': 10.0,
'interior': {
'type': 'homogeneous'
}
}
}

for i in range(2):
mi.xml.dict_to_xml(scene_dict, os.path.split(filepath)[1], split_files=(i==0))
os.chdir(cwd)

s1 = mi.load_dict(scene_dict)
s2 = mi.load_file(filepath)

print(s1)
print(s2)

assert str(s1) == str(s2)


@fresolver_append_path
def test16_xml_volume_with_args(variants_all_rgb, tmp_path):
filepath = str(tmp_path / 'test_write_xml-test15_output.xml')
print(f"Output temporary file: {filepath}")

cwd = os.getcwd()
os.chdir(tmp_path)

scene_dict = {
'type': 'scene',
'sphere' : {
'type': 'sphere',
'center': [0, 0, -10],
'radius': 10.0,
'interior': {
'type': 'homogeneous',
'albedo': {
'type': 'rgb',
'value': [0.99, 0.9, 0.96]
},
}
}
}

for i in range(2):
mi.xml.dict_to_xml(scene_dict, os.path.split(filepath)[1], split_files=(i==0))
os.chdir(cwd)

s1 = mi.load_dict(scene_dict)
s2 = mi.load_file(filepath)

print(s1)
print(s2)

assert str(s1) == str(s2)
24 changes: 20 additions & 4 deletions src/python/python/xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Files:
GEOM = 2 # Geometry
EMIT = 3 # Emitters
CAMS = 4 # Camera and rendering params (sensor, integrator, sampler...)
#TODO: Volumes
VOLS = 5 # Volumes

class WriteXML:
'''
Expand All @@ -39,7 +39,8 @@ def __init__(self, path, subfolders=None, split_files=False):
{}, #MATS
{}, #GEOM
{}, #EMIT
{}] #CAMS
{}, #CAMS
{}] #VOLS
self.com_count = 0 # Counter for comment ids
self.exported_ids = set()
self.copy_count = {} # Counters for giving unique names to copied files with duplicate names
Expand Down Expand Up @@ -199,6 +200,12 @@ def set_filename(self, name):
self.file_stack.append([])
self.write_header(Files.CAMS, '# Cameras and Render Parameters File')

self.file_names.append('fragments/%s-volumes.xml' % base_name)
self.files.append(open(os.path.join(self.directory, self.file_names[Files.VOLS]), 'w', encoding='utf-8', newline="\n"))
self.file_tabs.append(0)
self.file_stack.append([])
self.write_header(Files.VOLS, '# Volumes File')

self.set_output_file(Files.MAIN)

def set_output_file(self, file):
Expand Down Expand Up @@ -399,6 +406,8 @@ def preprocess_scene(self, scene_dict):
elif tag == 'shape':
if 'emitter' in value.keys(): # Emitter nested in a shape (area light)
self.data_add(key, value, Files.EMIT)
elif 'medium' in value.keys(): # Volume nested in a shape
self.data_add(key, value, Files.VOLS)
else:
self.data_add(key ,value, Files.GEOM)
elif tag == 'bsdf':
Expand All @@ -424,7 +433,6 @@ def preprocess_scene(self, scene_dict):
self.scene_data[Files.MAIN].update(self.scene_data[Files.MATS])

self.add_comment("Emitters")

if self.split_files:
self.add_include(Files.EMIT)
else:
Expand All @@ -436,6 +444,12 @@ def preprocess_scene(self, scene_dict):
else:
self.scene_data[Files.MAIN].update(self.scene_data[Files.GEOM])

self.add_comment("Volumes")
if self.split_files:
self.add_include(Files.VOLS)
else:
self.scene_data[Files.MAIN].update(self.scene_data[Files.VOLS])

self.set_output_file(Files.MAIN)

def format_spectrum(self, entry, entry_type):
Expand Down Expand Up @@ -562,10 +576,12 @@ def write_dict(self, data):
raise ValueError("Id: %s is already used!" % args['id'])
self.exported_ids.add(args['id'])
if len(value) > 0: # Open a tag if there is still stuff to write
args['name'] = key
self.open_element(tag, args)
self.write_dict(value)
self.close_element()
else:
args['name'] = key
self.element(tag, args) # Write dict in one line (e.g. integrator)
else:
if value_type == 'ref':
Expand Down Expand Up @@ -621,7 +637,7 @@ def process(self, scene_dict):
'''
self.preprocess_scene(scene_dict) # Re order elements
if self.split_files:
for file in [Files.MAIN, Files.CAMS, Files.MATS, Files.GEOM, Files.EMIT]:
for file in [Files.MAIN, Files.CAMS, Files.MATS, Files.GEOM, Files.EMIT, Files.VOLS]:
self.set_output_file(file)
self.write_dict(self.scene_data[file])
else:
Expand Down

0 comments on commit 15d63df

Please sign in to comment.