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
1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 | |
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
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 | |
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