Create animations and movies without presentations#
While you can create sophisticated animations and export them in movies using presentations and animations in SAMSON, you can also create animations by simply modifying systems and orienting cameras, and then capturing the viewport and combining the resulting images into movies.
Below, you can find two examples that produce gif and mp4 movies of:
path (trajectory) animation
camera orbiting a molecule
Creating movies from images#
First, let’s see how can we create movies from images.
Prerequisites
For this example, you will need to install one of the packages (see: Installing Python packages):
imageio
[recommended]moviepy
import datetime
import imageio
def create_movie_using_imageio(movie_path, image_filenames, duration):
'''
Creates gif and mp4 files from images using to imageio package
'''
images = []
for filename in image_filenames:
images.append(imageio.imread(filename))
# generate a name from the current date and time
current_time = datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S')
# a name for the gif-file
output_gif_file = movie_path + '/moviepy-%s.gif' % current_time
# a name for the mp4-file
output_mp4_file = movie_path + '/moviepy-%s.mp4' % current_time
# save clip to the gif-file
imageio.mimsave(output_gif_file, images, duration=duration)
# save clip to the mp4-file
imageio.mimsave(output_mp4_file, images)
import datetime
import moviepy.editor as mpy
def create_movie_using_moviepy(movie_path, image_filenames, fps = 16):
'''
Creates gif and mp4 movies from images using the moviepy package
'''
# generate a name from the current date and time
current_time = datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S')
# a name for the gif-file
output_gif_file = movie_path + '/moviepy-%s.gif' % current_time
# a name for the mp4-file
output_mp4_file = movie_path + '/moviepy-%s.mp4' % current_time
# create a clip
clip = mpy.ImageSequenceClip(image_filenames, fps = fps)
# save clip to the gif-file
clip.write_gif(output_gif_file)
# save clip to the mp4-file
clip.write_videofile(output_mp4_file)
Trajectory animation#
This example demonstrates how to make an animation of a path in SAMSON by capturing screenshots and creating a movie from them.
First, open a trajectory in SAMSON or fetch one from RCSB PDB, e.g. SAMSON.fetchPDB('1d3z')
.
import os
from math import log10
class HaltException(Exception): pass
# get the path from the user
ret, movie_filepath = SAMSON.getPathFromUser(dialogTitle = 'Choose folder to save movie')
if ret:
# a temporary path to images
random_uuid = SBRandom(SAMSON.getTime() % 2147483647).randUUID()
path_to_image_files = movie_filepath + '/tmp/' + str(random_uuid) + '/'
# try to create a temporary folder
try: os.makedirs(path_to_image_files)
except: HaltException('Could not create a folder')
camera = SAMSON.getActiveCamera() # get the active camera
#camera.frontView() # set the view of the camera
SAMSON.processEvents() # process events: updates viewport
# get an indexer of paths from the active document
path_indexer = SAMSON.getNodes('node.type path')
if len(path_indexer):
# get the first path from the indexer
path = path_indexer[0]
ext = '.png' # extension of files
path.forwardFlag = True # set a change in positions in a forward direction
path.animationFlag = True # set the animation flag
path.animationType = SBPath.AnimationType.Loop # set Loop type of an animation
# show a progress bar to inform user about the progress
SAMSON.showProgressBar('steps', 0, path.numberOfSteps)
# store the current step
current_step = path.currentStep
path.currentStep = 0
# go through the trajectory frames
for i in range(path.numberOfSteps):
# update the state - change positions to the next step
path.updateState()
SAMSON.processEvents()
i_str = str(i).zfill(int(log10(path.numberOfSteps)) + 1)
filename = path_to_image_files + i_str + ext
# save a capture of the viewport in a file with 800x600 resolution
SAMSON.captureViewportToFile(filename, 800, 600)
SAMSON.setProgressBarValue(i) # update the progress bar
if SAMSON.isProgressBarStopped(): break # break if the rotation is finished
# restore the current step
path.currentStep = current_step
# change directory to the directory with images
os.chdir(path_to_image_files)
# get the list of created images
img_files = sorted((fn for fn in os.listdir('.') if fn.endswith('.png')))
# create a gif and mp4 files using imageio
create_movie_using_imageio(movie_path = movie_filepath,
image_filenames = img_files, duration = 0.1)
# create a gif and mp4 files using moviepy
#create_movie_using_moviepy(movie_path = movie_filepath,
# image_filenames = img_files, fps = 16)
SAMSON.hideProgressBar()
Camera orbiting animation#
This example demonstrates how to create an animation by rotating a camera, capturing screenshots, and creating a movie from them.
First, open a molecule in SAMSON or fetch one from RCSB PDB, e.g. SAMSON.fetchPDB('1yrf')
.
import os
from math import pi, log10
class HaltException(Exception): pass
def rotate_camera(camera, velocity, center):
'''
Rotates the camera with the given velocity around the given center
and updates the viewport
'''
camera.rotate(velocity, center)
SAMSON.requestViewportUpdate()
SAMSON.processEvents()
# get the path from the user
ret, movie_filepath = SAMSON.getPathFromUser(dialogTitle = 'Choose folder to save movie')
if ret:
# a temporary path to images
random_uuid = SBRandom(SAMSON.getTime() % 2147483647).randUUID()
path_to_image_files = movie_filepath + '/tmp/' + str(random_uuid) + '/'
# try to create a temporary folder
try: os.makedirs(path_to_image_files)
except: HaltException('Could not create a folder')
# get an indexer of all atoms in the active document
atom_indexer = SAMSON.getNodes('node.type atom')
if len(atom_indexer):
camera = SAMSON.getActiveCamera() # get the active camera
camera.center() # centers the camera
#camera.frontView() # set the view of the camera
SAMSON.processEvents() # process events: updates viewport
# compute the geometrical center of the molecule
center = SBStructuralModel.getCentroid(atom_indexer)
velocity = pi * 1.0/180.0 # rotation velocity
# rotation is only in y-direction
velocity3 = SBRadianPerSecond3(
SBQuantity.radPerS(0),
SBQuantity.radPerS(velocity),
SBQuantity.radPerS(0))
ext = '.png' # extension of files
numberOfRorations = 36 # rotate 36 times
# show a progress bar to inform user about the progress
SAMSON.showProgressBar('Orbiting and capturing', 0, numberOfRorations)
for i in range(numberOfRorations):
# rotate the camera around the center point with the given velocity
rotate_camera(camera, velocity3, center)
i_str = str(i).zfill(int(log10(numberOfRorations)) + 1)
filename = path_to_image_files + i_str + ext
# save a capture of the viewport in a file with 800x800 resolution
SAMSON.captureViewportToFile(filename, 800, 800)
SAMSON.setProgressBarValue(i) # update the progress bar
if SAMSON.isProgressBarStopped(): break # break if the rotation is finished
# change directory to the directory with images
os.chdir(path_to_image_files)
# get the list of created images
img_files = sorted((fn for fn in os.listdir('.') if fn.endswith('.png')))
# create a gif and mp4 files using imageio
create_movie_using_imageio(movie_path = movie_filepath,
image_filenames = img_files, duration = 0.1)
# create a gif and mp4 files using moviepy
#create_movie_using_moviepy(movie_path = movie_filepath,
# image_filenames = img_files, fps = 16)
SAMSON.hideProgressBar()