-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathcli.py
54 lines (40 loc) · 1.47 KB
/
cli.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
from pathlib import Path
import rich_click as click
from tqdm import tqdm
@click.group()
def cli():
pass
@cli.command()
@click.argument("input", required=True)
def render(input):
from pymatgen.analysis.local_env import CrystalNN
from pymatgen.core.structure import Structure
from crystal_toolkit.core.scene import Scene
from crystal_toolkit.helpers.povray.renderer import POVRayRenderer
input_path = Path(input)
if input_path.is_file():
paths = [input_path] # load CIF
else:
paths = list(input_path.glob("*.cif"))
r = POVRayRenderer()
structures = {}
for path in tqdm(paths, desc="Reading structures"):
try:
structures[path] = Structure.from_file(path)
except Exception as exc:
print(f"Failed to parse {path}: {exc}")
def _get_scene(struct: Structure) -> Scene:
# opinionated defaults, would be better to be customizable
nn = CrystalNN()
sg = nn.get_bonded_structure(struct)
return sg.get_scene(explicitly_calculate_polyhedra_hull=True)
scenes = {}
for path, structure in tqdm(structures.items(), desc="Preparing scenes"):
try:
scenes[path] = _get_scene(structure)
except Exception as exc:
print(f"Failed to parse {path}: {exc}")
for path, scene in tqdm(scenes.items(), desc="Rendering scenes"):
r.write_scene_to_file(scene, filename=f"{path.stem}.png")
if __name__ == "__main__":
cli()