How to Visualize Molecular Dynamics Trajectories in PyMOL: Animation, Key Frames and Export
MD trajectories contain the full dynamic story of how a protein moves — but that story is invisible until it’s visualized. This tutorial covers loading trajectories from GROMACS and AMBER into PyMOL, controlling playback, making smooth animations, capturing meaningful frames, and exporting MP4 or GIF files for publications and presentations.
Preparing the trajectory before loading
This step is the most commonly skipped — and the most important. Raw MD trajectories from GROMACS or AMBER contain artifacts from periodic boundary conditions: atoms appear to jump across the box boundary, the protein may be split across box edges, and solvent fills the entire simulation cell. Load a raw trajectory directly into PyMOL and you’ll see a fragmented, unrealistic structure.
Always process the trajectory before visualization. In GROMACS:
# Step 1: Fix periodic boundary — make molecule whole
gmx trjconv -s md.tpr -f md.xtc -o md_whole.xtc -pbc whole
# Select: Protein (or the molecule you want centered)
# Step 2: Center the protein, remove solvent for visualization
gmx trjconv -s md.tpr -f md_whole.xtc -o md_nojump.xtc \
-center -pbc mol -ur compact
# Select: Protein (centering group), Protein (output group)
# Step 3 (optional): Reduce frames for easier visualization
# Keep every 10th frame (1 ns MD with 1 ps output → every 10 ns)
gmx trjconv -s md.tpr -f md_nojump.xtc -o md_vis.xtc -skip 10
-pbc whole then -center -pbc mol) are mandatory preprocessing steps, not optional. If you’re using AMBER, use cpptraj with autoimage and center commands to achieve the equivalent result.
Supported trajectory formats
pymol-open-source version 3.0+ from conda-forge (which includes XTC support), or convert your XTC to a multi-model PDB using gmx trjconv -o trajectory.pdb as a workaround.
Loading the topology and trajectory
PyMOL needs two files: a topology file (the structure — a GRO, PDB, or PSF) and the trajectory file. Load the topology first to create the object, then load the trajectory into that object.
# Method 1: load topology, then load trajectory into it
load protein.gro, protein # creates the object "protein"
load_traj md_vis.xtc, protein # loads frames into same object
# Method 2: load PDB topology, then XTC trajectory
load topology.pdb, my_sim
load_traj md_vis.xtc, my_sim
# Method 3: for multi-model PDB trajectories
load trajectory.pdb # each MODEL becomes one state/frame
# Load only a subset of frames (start, stop, step in frame numbers)
load_traj md_vis.xtc, protein, start=1, stop=100, step=5
# This loads frames 1-100, every 5th frame
# Check how many states (frames) were loaded
print(cmd.count_states("protein"))
After loading, set up your display — the same coloring and representation settings you’d use for a static structure apply equally to trajectory visualization. Set these before playing the trajectory so they persist across all frames:
as cartoon
hide everything, resn HOH
util.cbc
set cartoon_fancy_helices, 1
bg_color white
Playing back and navigating the trajectory
Once the trajectory is loaded, each frame is a separate “state” in PyMOL. Navigate between states using the playback controls at the bottom of the viewer window, or control playback entirely from the command line.
# Play trajectory
mplay
# Stop
mstop
# Jump to specific frame
frame 50
# Control playback speed (frames per second)
set movie_fps, 5 # slow — good for visualizing conformational detail
set movie_fps, 15 # medium — good for general animation
set movie_fps, 30 # fast — smooth video output
# Loop playback
set movie_loop, 1 # loop continuously (1=on, 0=off)
Making smooth animations
Raw trajectory playback can appear jerky — each frame is a distinct snapshot with no interpolation between them. PyMOL’s morph and smooth features interpolate between frames to produce fluid motion:
# Smooth the trajectory playback (interpolates between states)
smooth protein, 5 # 5 = smoothing window in frames
smooth protein, 10 # wider window = smoother, but loses fast dynamics
# Create a morph between two specific frames for a smooth conformational change
# (useful for showing a specific transition, not the whole trajectory)
create frame1, protein, 1 # extract frame 1 as separate object
create frame50, protein, 50 # extract frame 50 as separate object
morph morph_obj, frame1, frame50, refinement=5
# morph creates intermediate states between the two conformations
# Play the morph
mplay morph_obj
smooth operates on the actual trajectory data, averaging nearby frames to reduce jitter while preserving the overall motion. Use it for visualizing a full trajectory. morph creates a topologically smooth interpolation between exactly two conformations — ideal for showing a specific conformational transition (e.g., open to closed state) as a clean animated figure for a paper or talk.
Capturing key frames
From a long trajectory, you often want to extract a handful of meaningful frames — the starting conformation, a key intermediate, and the end state. Extract them as static objects for comparison or publication figures:
# Extract specific frames as separate objects
create start_conf, protein, 1 # frame 1 = starting conformation
create middle_conf, protein, 50 # frame 50 = mid-simulation
create end_conf, protein, 100 # frame 100 = end conformation
# Save an extracted frame as a PDB file
frame 50
save frame50.pdb, protein, state=50
# Align all extracted frames to the starting conformation
super middle_conf, start_conf
super end_conf, start_conf
# Color each conformation distinctly for comparison
color slate, start_conf
color salmon, middle_conf
color palegreen, end_conf
# Show all three together — conformational ensemble view
as cartoon
zoom
Showing conformational change
One of the most powerful things you can show from an MD trajectory is a specific conformational change — a loop opening, a domain rotating, a binding site changing shape. Two approaches work well in PyMOL:
Overlay approach — show multiple conformations simultaneously
# Extract start and end conformations
create open_state, protein, 1
create closed_state, protein, 100
# Align on the stable core (not the region that changes)
select stable_core, resi 1-100 and name CA # stable region
super closed_state and stable_core, open_state and stable_core
# Show both as cartoons with different colors
as cartoon
color slate, open_state
color salmon, closed_state
set cartoon_transparency, 0.4, open_state # ghost the reference state
# Highlight the region that moved
select mobile_region, resi 101-130
show sticks, mobile_region
Trail approach — show the path a residue takes
# Show Cα trace of a flexible loop across all trajectory frames
# First extract just the mobile region from all states
create loop_all, protein and resi 101-130 and name CA
# Show as spheres across all states — shows the range of motion
show spheres, loop_all
set sphere_scale, 0.3, loop_all
color orange, loop_all
Exporting as MP4 or GIF
- Best quality, smallest file size
- Preferred for journal supplementary videos
- Plays in all browsers and media players
- Requires ffmpeg installed on your system
- Supports any resolution and frame rate
- No software dependency — universal format
- Loops automatically — great for talks and web
- Limited to 256 colors — lower quality
- Large file sizes at high resolution
- Best for short loops (<5 seconds)
# Step 1: Set up the movie — define frames and duration
mset 1 x100 # play states 1-100 once
mset 1 x100 1 -1 x100 # play forward then backward (boomerang loop)
# Step 2: Configure movie settings
set movie_fps, 15
bg_color white
set ray_opaque_background, 1
set cartoon_fancy_helices, 1
# Step 3: Export as PNG frames then encode with ffmpeg
mpng /tmp/frame_, ray=0 # export each state as PNG (ray=0 for speed)
# Then in terminal: ffmpeg -r 15 -i /tmp/frame_%04d.png -c:v libx264 -pix_fmt yuv420p movie.mp4
# Alternative: PyMOL's built-in movie export (educational/commercial license)
movie.produce movie.mp4, mode=normal, fps=15, quality=90
# Export frames as PNGs, then assemble GIF with ImageMagick (free)
mpng /tmp/frame_, ray=0
# In terminal:
# convert -delay 6 -loop 0 /tmp/frame_*.png animation.gif
# -delay 6 = ~16 fps (delay in 1/100 seconds)
# -loop 0 = loop forever
# Optimize GIF file size
# gifsicle --optimize=3 animation.gif -o animation_opt.gif
MD trajectory visualization in one paragraph
Always preprocess the trajectory before loading into PyMOL — -pbc whole then -center -pbc mol in GROMACS, or autoimage in cpptraj. Load the topology first, then the trajectory with load_traj. Use smooth on the full trajectory to reduce jitter, or morph between two specific conformations for a publication-quality transition animation. Extract key frames with create for static figure panels. Export as MP4 via the PNG frames + ffmpeg route for journal submission; use ImageMagick to assemble GIFs for talks and web use. Save the session before closing — reloading a large trajectory takes time.