Examples

PUTTING EXAMPLES HERE. TODO

Simple structure

import os

from render_molecules.structure import Structure
from render_molecules.blender_utils import delete_all_objects
from render_molecules.other_utils import get_render_molecules_dir

def main():
   delete_all_objects()

   # Read the CUBE file
   render_molecules_dir = get_render_molecules_dir()
   xyz_path = os.path.join(render_molecules_dir, "../examples/structure/CH3OH.xyz")
   structure = Structure.from_xyz(xyz_path)

   # Set center of mass to origin
   structure.set_center_of_mass([0, 0,0])

   # Create the atoms
   structure.create_atoms()

   # Find bonds based on distances between atoms
   bonds = structure.find_bonds_from_distances()

   # Create the bonds
   structure.create_bonds(bonds)

if __name__ == '__main__':
   main()

Orbiting camera

from __future__ import annotations

import os

from render_molecules.blender_utils import delete_all_objects, orbit_camera
from render_molecules.other_utils import get_render_molecules_dir
from render_molecules.structure import Structure


def main():
    delete_all_objects()
    
    # Read the CUBE file
    render_molecules_dir = get_render_molecules_dir()
    xyz_path = os.path.join(render_molecules_dir, "../examples/orbit_camera/CH3OH_OPT.xyz")
    structure = Structure.from_xyz(xyz_path)
    
    # Set center of mass to origin
    structure.set_center_of_mass([0, 0,0])
    
    # Create the atoms and bonds
    structure.create_structure()
    
    orbit_camera(radius=8, height=4, set_active=True)
    return

if __name__ == '__main__':
    main()

GIF of camera orbiting around methanol

Isosurface

from __future__ import annotations

import os

from render_molecules.blender_utils import create_isosurface, delete_all_objects
from render_molecules.other_utils import get_render_molecules_dir
from render_molecules.structure import CUBEfile


def main():
    delete_all_objects()

    # Read the CUBE file
    render_molecules_dir = get_render_molecules_dir()
    cube_path = os.path.join(
        render_molecules_dir, "../examples/isosurface/CH3OH_OPT.eldens.cube"
    )
    cube = CUBEfile(cube_path)

    # Read the electron density in the CUBE file
    cube.read_volumetric_data()

    # Set center of mass to origin
    cube.set_center_of_mass([0, 0, 0])

    # Create the atoms and bonds
    cube.create_structure()

    # Calculate and render the isosurface
    isovalue = 0.025
    verts, edges, _, _ = cube.calculate_isosurface(isovalue)
    create_isosurface(verts, edges, isovalue, "CH3OH")
    return


if __name__ == "__main__":
    main()

Geometry optimization

Let’s say you did a geometry optimization, and for whatever reason you would like to render the output.

I have artificially created a structure of methanol, with the O-H bond elongated.

from __future__ import annotations

import os

from render_molecules.blender_utils import delete_all_objects
from render_molecules.other_utils import get_render_molecules_dir
from render_molecules.trajectory import Trajectory


def main():
    delete_all_objects()

    # Read the xyz file
    render_molecules_dir = get_render_molecules_dir()
    xyz_path = os.path.join(
        render_molecules_dir, "../examples/animation/CH3OH_OPT_trj.xyz"
    )
    trajectory = Trajectory.from_xyz(xyz_path)

    # Set center of mass to origin
    trajectory.set_center_of_mass([0, 0, 0])

    # Create the animation
    trajectory.create_animation()
    return


if __name__ == "__main__":
    main()

GIF of geometry optimization of methanol

Vibrations

from __future__ import annotations

import os

from render_molecules.blender_utils import delete_all_objects
from render_molecules.other_utils import get_render_molecules_dir
from render_molecules.trajectory import Trajectory


def main():
    delete_all_objects()
    
    # ORCA output file
    render_molecules_dir = get_render_molecules_dir()
    out_path = os.path.join(render_molecules_dir, "../examples/vibrations/CH3OH.out")
    
    # Use vibrations (and not geometry optimization), index (starting from 0) of vibrational mode.
    trajectory = Trajectory.from_orca(
        out_path, 
        use_vibrations=True, 
        vibration_nr = 10, 
        n_frames_per_oscillation=100
    )
    
    # Set center of mass to origin
    trajectory.set_center_of_mass([0,0,0])
    
    # Create the animation
    trajectory.create_animation()
    return  

if __name__ == '__main__':
    main()

GIF of geometry optimization of methanol