meshmerger/meshmerge.py

58 lines
2.1 KiB
Python
Executable File

#! /usr/bin/env python3
import argparse
import pathlib
import json
from typing import Dict, Any
meshviewer_file_name = 'meshviewer.json'
nodelist_file_name = 'nodelist.json'
def _merge_nodelist(output_file_path: pathlib.PosixPath, input_files: list[pathlib.PosixPath]):
output_content: Dict[str, Any] = {
'version': '',
'updated_at': '',
'nodes': list()
}
for input_file in input_files:
json_content = json.loads(input_file.read_bytes())
output_content['version'] = json_content['version']
output_content['updated_at'] = json_content['updated_at']
output_content['nodes'].extend(json_content['nodes'])
with open(output_file_path, 'w') as output_file:
output_file.write(json.dumps(output_content))
def _merge_meshviewer(output_file_path: pathlib.PosixPath, input_files: list[pathlib.PosixPath]):
output_content: Dict[str, Any] = {
'timestamp': '',
'nodes': list()
}
for input_file in input_files:
json_content = json.loads(input_file.read_bytes())
output_content['timestamp'] = json_content['timestamp']
output_content['nodes'].extend(json_content['nodes'])
with open(output_file_path, 'w') as output_file:
output_file.write(json.dumps(output_content))
def _merge_files(output_dir: pathlib.PosixPath, dirs: list[pathlib.PosixPath]):
_merge_nodelist(output_dir.joinpath('nodelist.json'),
[dir.joinpath('nodelist.json') for dir in dirs if dir.joinpath('nodelist.json').exists()])
_merge_meshviewer(output_dir.joinpath('meshviewer.json'),
[dir.joinpath('meshviewer.json') for dir in dirs if dir.joinpath('meshviewer.json').exists()])
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Mergetool for meshviewer JSON files.')
parser.add_argument('output_dir', type=pathlib.Path, help='Output directory to receive the merged files.')
parser.add_argument('input_dir', nargs='+', type=pathlib.Path,
help='Directory containing meshviewer.json and modelist.json.')
args = parser.parse_args()
_merge_files(args.output_dir, args.input_dir)