Presentation: play path#
This example combines what is written in the Presenting and animating section and demonstrates how to create a presentation with the Play path animation.
Open a a trajectory in SAMSON or fetch it as shown in the example below, e.g. SAMSON.fetchPDB('1yrf')
.
Create a presentation with the Play path animation#
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()
Creating movies from images#
The code above can make use of the following functions below to create movies from images.
Prerequisites
For this example, you will need to install one of the packages (see: Installing Python packages):
imageio
[recommended]moviepy
Create a movie from images using imageio#
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)
Create a movie from images using moviepy#
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)