A modern Python reimagining of Methfessel's XBS ball-and-stick viewer (1995), named after August Wilhelm von Hofmann who built the first ball-and-stick molecular models in 1865.
hofmann renders crystal and molecular structures as depth-sorted ball-and-stick images with static, publication-quality vector output (SVG, PDF) via matplotlib.
- Static publication-quality output (SVG, PDF, PNG) via matplotlib
- XBS
.bsand.mv(trajectory) file formats - Optional pymatgen
Structureinteroperability - Periodic boundary conditions with automatic image expansion
- Coordination polyhedra with configurable shading and slab clipping
- Unit cell wireframe rendering
- Interactive viewer with mouse rotation, zoom, and keyboard controls
- Orthographic and perspective projection
pip install hofmannFor pymatgen interoperability:
pip install "hofmann[pymatgen]"- Python 3.11+
- numpy >= 1.24
- matplotlib >= 3.7
- scipy >= 1.10
- pymatgen >= 2024.1.1 (optional)
from hofmann import StructureScene
scene = StructureScene.from_xbs("structure.bs")
scene.render_mpl("output.svg")from pymatgen.core import Lattice, Structure
from hofmann import StructureScene, BondSpec
lattice = Lattice.cubic(5.43)
structure = Structure(
lattice, ["Si"] * 8,
[[0.0, 0.0, 0.0], [0.5, 0.5, 0.0],
[0.5, 0.0, 0.5], [0.0, 0.5, 0.5],
[0.25, 0.25, 0.25], [0.75, 0.75, 0.25],
[0.75, 0.25, 0.75], [0.25, 0.75, 0.75]],
)
bonds = [BondSpec(species=("Si", "Si"), max_length=2.8)]
scene = StructureScene.from_pymatgen(structure, bonds)
scene.render_mpl("si.pdf")scene.view.look_along([1, 1, 0]) # View along [110]
scene.view.zoom = 1.5 # Zoom in
scene.view.perspective = 0.3 # Mild perspective
scene.render_mpl("rotated.svg")view, style = scene.render_mpl_interactive()
# Reuse the adjusted view for static output:
scene.view = view
scene.render_mpl("final.svg", style=style)Full documentation is available at hofmann.readthedocs.io, covering:
- Getting started -- installation and first renders
- Scenes and structures -- scenes, frames, bonds, polyhedra
- Rendering -- views, render styles, unit cells, axes
- Colouring -- per-atom data colouring, custom functions, multiple layers
- Interactive viewer -- mouse and keyboard controls
- XBS file format --
.bsand.mvformat reference - API reference -- full autodoc API
If you use hofmann in published work, please cite it:
B. J. Morgan, hofmann, https://github.com/bjmorgan/hofmann
A machine-readable citation is available in CITATION.cff.
MIT. See LICENSE for details.
