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()