Core Functions¶
Auto-generated API documentation for plotlyMol's core visualization functions.
Main Visualization Function¶
draw_3D_rep
¶
draw_3D_rep(
smiles=None,
xyzfile=None,
charge=0,
cubefile=None,
molfile=None,
pbdfile=None,
resolution=DEFAULT_RESOLUTION,
radius=DEFAULT_RADIUS,
mode="ball+stick",
orbital_opacity=0.25,
orbital_colors=None,
cubedraw="orbitals",
vibration_file=None,
vibration_mode=None,
vibration_display="arrows",
vibration_amplitude=1.0,
vibration_arrow_scale=1.0,
vibration_arrow_color="red",
ambient=0,
diffuse=1,
specular=0,
roughness=1,
fresnel=0,
lightx=1000,
lighty=1000,
lightz=1000,
)
Create a complete 3D molecular visualization from various input formats.
This is the main entry point for creating molecular visualizations. Accepts multiple input formats and combines them into a single figure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
smiles
|
Optional[str]
|
SMILES string for the molecule. |
None
|
xyzfile
|
Optional[str]
|
Path to an XYZ coordinate file. |
None
|
charge
|
int
|
Molecular charge (used for XYZ bond perception). |
0
|
cubefile
|
Optional[str]
|
Path to a Gaussian cube file (for orbitals). |
None
|
molfile
|
Optional[str]
|
Path to a MOL file. |
None
|
pbdfile
|
Optional[str]
|
Path to a PDB file (not yet implemented). |
None
|
resolution
|
int
|
Mesh resolution for rendering. |
DEFAULT_RESOLUTION
|
radius
|
Union[float, str]
|
Atom/bond radius (float, "vdw", or "ball"). |
DEFAULT_RADIUS
|
mode
|
str
|
Visualization mode - "ball+stick", "ball", "stick", or "vdw". |
'ball+stick'
|
orbital_opacity
|
float
|
Opacity for orbital isosurfaces (0-1). |
0.25
|
orbital_colors
|
Optional[List[str]]
|
Colors for positive/negative orbital lobes. Defaults to ["darkorange", "skyblue"]. |
None
|
cubedraw
|
str
|
What to draw from cube file - "orbitals", "molecule", or both. |
'orbitals'
|
vibration_file
|
Optional[str]
|
Path to vibration file (.log, .out, .molden). |
None
|
vibration_mode
|
Optional[int]
|
Mode number to visualize (1-based). |
None
|
vibration_display
|
str
|
"arrows", "heatmap", or "both". |
'arrows'
|
vibration_amplitude
|
float
|
Displacement amplitude scale. |
1.0
|
vibration_arrow_scale
|
float
|
Visual scale for arrows. |
1.0
|
vibration_arrow_color
|
str
|
Color for displacement arrows. |
'red'
|
ambient
|
float
|
Ambient light intensity (0-1). |
0
|
diffuse
|
float
|
Diffuse light intensity (0-1). |
1
|
specular
|
float
|
Specular highlight intensity (0-1). |
0
|
roughness
|
float
|
Surface roughness (0-1). |
1
|
fresnel
|
float
|
Fresnel effect intensity (0-1). |
0
|
lightx
|
float
|
Light position x-coordinate. |
1000
|
lighty
|
float
|
Light position y-coordinate. |
1000
|
lightz
|
float
|
Light position z-coordinate. |
1000
|
Returns:
| Type | Description |
|---|---|
Figure
|
Plotly figure with the molecular visualization. |
Example
fig = draw_3D_rep(smiles="CCO", mode="ball+stick", ambient=0.1) fig = draw_3D_rep(cubefile="orbital.cube", cubedraw="orbitals") fig = draw_3D_rep(smiles="O", vibration_file="water.log", vibration_mode=1)
Source code in src/plotlymol3d/plotlyMol3D.py
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 | |
Drawing from RDKit Mol¶
draw_3D_mol
¶
draw_3D_mol(
fig,
rdkitmol,
resolution=DEFAULT_RESOLUTION,
radius=DEFAULT_RADIUS,
mode="ball+stick",
)
Draw a 3D molecule representation on a Plotly figure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fig
|
Figure
|
Plotly figure to draw on. |
required |
rdkitmol
|
Mol
|
RDKit molecule object with 3D coordinates. |
required |
resolution
|
int
|
Mesh resolution for atoms and bonds. |
DEFAULT_RESOLUTION
|
radius
|
Union[float, str]
|
Atom/bond radius (float, "vdw", or "ball"). |
DEFAULT_RADIUS
|
mode
|
str
|
Visualization mode - "ball+stick", "ball", "stick", or "vdw". |
'ball+stick'
|
Returns:
| Type | Description |
|---|---|
Figure
|
The figure with the molecule drawn. |
Example
fig = make_subplots() mol = smiles_to_rdkitmol("CCO") fig = draw_3D_mol(fig, mol, mode="ball+stick")
Source code in src/plotlymol3d/plotlyMol3D.py
Input Processing Functions¶
SMILES Processing¶
smiles_to_rdkitmol
¶
Convert a SMILES string to an RDKit molecule with 3D coordinates.
Parses the SMILES string, adds hydrogens, embeds in 3D space, and optimizes the geometry using the Universal Force Field (UFF).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
smiles
|
str
|
SMILES representation of the molecule. |
required |
Returns:
| Type | Description |
|---|---|
Mol
|
RDKit molecule object with optimized 3D coordinates. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the SMILES string is invalid. |
Example
mol = smiles_to_rdkitmol("CCO") mol.GetNumAtoms() 9
Source code in src/plotlymol3d/plotlyMol3D.py
XYZ File Processing¶
xyzfile_to_xyzblock
¶
Read an XYZ file and return its contents as a string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
str
|
Path to the XYZ file. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Contents of the XYZ file as a string block. |
Example
xyzblock = xyzfile_to_xyzblock("molecule.xyz")
Source code in src/plotlymol3d/plotlyMol3D.py
xyzblock_to_rdkitmol
¶
Convert an XYZ coordinate block to an RDKit molecule with bonds.
Parses XYZ coordinates and uses RDKit's bond perception algorithms to determine connectivity and bond orders.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
xyzblock
|
str
|
String containing XYZ format coordinates. |
required |
charge
|
int
|
Total molecular charge (used for bond order determination). |
0
|
Returns:
| Type | Description |
|---|---|
Mol
|
RDKit molecule object with perceived bonds. |
Note
Bond perception may fail for certain functional groups (e.g., nitro groups). For problematic molecules, consider using a MOL file instead.
Example
xyzblock = "3\n\nO 0.0 0.0 0.0\nH 0.96 0.0 0.0\nH -0.24 0.93 0.0" mol = xyzblock_to_rdkitmol(xyzblock)
Source code in src/plotlymol3d/plotlyMol3D.py
Cube File Processing¶
cubefile_to_xyzblock
¶
Extract atomic coordinates from a Gaussian cube file.
Parses a cube file and extracts the molecular geometry in XYZ format. Cube files contain volumetric data (e.g., electron density, orbitals) along with atomic coordinates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cubefile
|
str
|
Path to the cube file. |
required |
Returns:
| Type | Description |
|---|---|
Tuple[str, int]
|
A tuple containing: - xyzblock: String in XYZ format with atom count, blank line, and atomic coordinates. - total_charge: Sum of nuclear charges (integer). |
Example
xyzblock, charge = cubefile_to_xyzblock("orbital.cube") print(xyzblock[:50]) 12
C 0.00000 0.00000 0.00000
Source code in src/plotlymol3d/plotlyMol3D.py
Molecule Processing¶
rdkitmol_to_atoms_bonds_lists
¶
Convert an RDKit molecule to lists of Atom and Bond objects.
Extracts atom and bond information from an RDKit molecule object, including 3D coordinates, element types, and van der Waals radii.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mol
|
Mol
|
RDKit molecule object with 3D coordinates (must have a conformer). |
required |
Returns:
| Type | Description |
|---|---|
Tuple[List[Atom], List[Bond]]
|
A tuple containing: - atomList: List of Atom dataclass objects. - bondList: List of Bond dataclass objects. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the molecule has no conformer (3D coordinates). |
Example
mol = Chem.MolFromSmiles("CCO") AllChem.EmbedMolecule(mol) atoms, bonds = rdkitmol_to_atoms_bonds_lists(mol) len(atoms) 9
Source code in src/plotlymol3d/plotlyMol3D.py
Mesh Generation Functions¶
Sphere Tessellation¶
make_fibonacci_sphere
¶
Generate points on a sphere using the Fibonacci lattice method.
Creates evenly distributed points on a sphere surface, which produces better visual results than latitude/longitude grids.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
center
|
Sequence[float]
|
Center point [x, y, z] of the sphere. |
required |
radius
|
float
|
Radius of the sphere. |
DEFAULT_RADIUS
|
resolution
|
int
|
Number of points to generate on the sphere. |
DEFAULT_RESOLUTION
|
Returns:
| Type | Description |
|---|---|
Tuple[ndarray, ndarray, ndarray]
|
Tuple of (x, y, z) numpy arrays containing point coordinates. |
Source code in src/plotlymol3d/plotlyMol3D.py
Cylinder Meshes¶
generate_cylinder_mesh_rectangles
¶
generate_cylinder_mesh_rectangles(
point1, point2, radius=DEFAULT_RADIUS, resolution=DEFAULT_RESOLUTION
)
Generate mesh vertices for a cylinder between two points.
Creates vertices for the top and bottom circles of a cylinder, which can be used to draw bonds between atoms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
point1
|
Union[List[float], ndarray]
|
Starting point [x, y, z] of the cylinder. |
required |
point2
|
Union[List[float], ndarray]
|
Ending point [x, y, z] of the cylinder. |
required |
radius
|
float
|
Radius of the cylinder. |
DEFAULT_RADIUS
|
resolution
|
int
|
Number of vertices around the circular cross-section. |
DEFAULT_RESOLUTION
|
Returns:
| Type | Description |
|---|---|
Tuple[ndarray, ndarray, ndarray]
|
Tuple of (x, y, z) numpy arrays containing vertex coordinates. |
Source code in src/plotlymol3d/plotlyMol3D.py
Trace Creation Functions¶
Atom Traces¶
make_atom_mesh_trace
¶
Create a Plotly Mesh3d trace for a single atom.
Generates a spherical mesh representing an atom at its 3D position.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
atom
|
Atom
|
Atom object containing position and element information. |
required |
radius
|
Union[float, str]
|
Sphere radius. Can be a float, "vdw" for van der Waals radius, or "ball" for scaled VDW radius (0.2x). |
DEFAULT_RADIUS
|
resolution
|
int
|
Number of points for sphere generation. |
DEFAULT_RESOLUTION
|
color
|
str
|
Fallback color (actual color is determined by element). |
'grey'
|
Returns:
| Type | Description |
|---|---|
Mesh3d
|
Plotly Mesh3d trace object for the atom. |
Source code in src/plotlymol3d/plotlyMol3D.py
draw_atoms
¶
Add atom traces to a Plotly figure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fig
|
Figure
|
Plotly figure to add atoms to. |
required |
atomList
|
List[Atom]
|
List of Atom objects to draw. |
required |
resolution
|
int
|
Sphere resolution for each atom. |
DEFAULT_RESOLUTION
|
radius
|
Union[float, str]
|
Atom sphere radius (float, "vdw", or "ball"). |
DEFAULT_RADIUS
|
Returns:
| Type | Description |
|---|---|
Figure
|
The figure with atom traces added. |
Source code in src/plotlymol3d/plotlyMol3D.py
Bond Traces¶
make_bond_mesh_trace
¶
make_bond_mesh_trace(
point1,
point2,
radius=DEFAULT_RADIUS,
resolution=DEFAULT_RESOLUTION,
color="grey",
add_caps=True,
)
Create a Plotly Mesh3d trace for a bond (cylinder).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
point1
|
Union[List[float], ndarray]
|
Starting point [x, y, z] of the bond. |
required |
point2
|
Union[List[float], ndarray]
|
Ending point [x, y, z] of the bond. |
required |
radius
|
float
|
Radius of the bond cylinder. |
DEFAULT_RADIUS
|
resolution
|
int
|
Number of vertices around the cylinder. |
DEFAULT_RESOLUTION
|
color
|
str
|
Color of the bond. |
'grey'
|
Returns:
| Type | Description |
|---|---|
Mesh3d
|
Plotly Mesh3d trace object for the bond segment. |
Source code in src/plotlymol3d/plotlyMol3D.py
draw_bonds
¶
Add bond traces to a Plotly figure.
Draws bonds as two half-cylinders colored by each atom's element. Double and triple bonds are shown as multiple parallel cylinders. Aromatic bonds are shown as 1.5 bonds (one full + one thinner).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fig
|
Figure
|
Plotly figure to add bonds to. |
required |
bondList
|
List[Bond]
|
List of Bond objects to draw. |
required |
atomList
|
Optional[List[Atom]]
|
List of Atom objects (used for calculating ring centers). |
None
|
resolution
|
int
|
Cylinder resolution for each bond. |
DEFAULT_RESOLUTION
|
radius
|
Union[float, str]
|
Bond cylinder radius. Can be float or "ball" for ball+stick mode. |
DEFAULT_RADIUS
|
Returns:
| Type | Description |
|---|---|
Figure
|
The figure with bond traces added. |
Source code in src/plotlymol3d/plotlyMol3D.py
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 | |
Formatting Functions¶
Figure Formatting¶
format_figure
¶
Apply default formatting to a molecular visualization figure.
Hides axes and grid lines for a clean molecular visualization. Sets aspectmode='data' to ensure spheres aren't distorted.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fig
|
Figure
|
Plotly figure to format. |
required |
bgcolor
|
str
|
Background color for the 3D scene (default: transparent). |
'rgba(0,0,0,0)'
|
Returns:
| Type | Description |
|---|---|
Figure
|
The formatted figure. |
Source code in src/plotlymol3d/plotlyMol3D.py
Lighting Control¶
format_lighting
¶
format_lighting(
fig,
ambient=0,
diffuse=1,
specular=0,
roughness=1,
fresnel=0,
lightx=1000,
lighty=1000,
lightz=1000,
)
Configure lighting for 3D mesh traces.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fig
|
Figure
|
Plotly figure to configure. |
required |
ambient
|
float
|
Ambient light intensity (0-1). |
0
|
diffuse
|
float
|
Diffuse light intensity (0-1). |
1
|
specular
|
float
|
Specular highlight intensity (0-1). |
0
|
roughness
|
float
|
Surface roughness (0-1). |
1
|
fresnel
|
float
|
Fresnel effect intensity (0-1). |
0
|
lightx
|
float
|
Light position x-coordinate. |
1000
|
lighty
|
float
|
Light position y-coordinate. |
1000
|
lightz
|
float
|
Light position z-coordinate. |
1000
|
Returns:
| Type | Description |
|---|---|
Figure
|
The figure with updated lighting settings. |
Source code in src/plotlymol3d/plotlyMol3D.py
Data Classes¶
Atom¶
@dataclass
class Atom:
"""Represents an atom with position and properties.
Attributes:
atom_id: 0-indexed unique identifier
atom_number: Atomic number (e.g., 6 for carbon)
atom_symbol: Element symbol (e.g., "C")
atom_xyz: [x, y, z] coordinates in Angstroms
atom_vdw: Van der Waals radius in Angstroms
"""
atom_id: int
atom_number: int
atom_symbol: str
atom_xyz: List[float]
atom_vdw: float
Bond¶
@dataclass
class Bond:
"""Represents a bond between two atoms.
Attributes:
a1_id, a2_id: Atom IDs
a1_number, a2_number: Atomic numbers
a1_xyz, a2_xyz: Atomic coordinates
a1_vdw, a2_vdw: Van der Waals radii
bond_order: 1.0 (single), 1.5 (aromatic), 2.0 (double), 3.0 (triple)
"""
a1_id: int
a2_id: int
a1_number: int
a2_number: int
a1_xyz: List[float]
a2_xyz: List[float]
a1_vdw: float
a2_vdw: float
bond_order: float