This is a read-only mirror of pymolwiki.org

Slerpy

From PyMOL Wiki
Revision as of 03:55, 28 March 2014 by Bell (talk | contribs) (50 revisions)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Manual for Slerpy.py

An extension to pymol that creates a moderately easy to use environment for doing keyframe animation.


General Use

At the pymol command line type:

import slerpy

This will load the extended commands. All commands in slerpy begin with the letter s. Pymol's tab autocomplete feature will work on the additional commands.

Important concepts

The main function of slerpy is to record a series of pymol views. A movie can then be created by interpolating between these views. A pymol view consists mostly of the camera orientation (that is, the orientation of the viewers eye with respect to the molecule). It also includes information about the clipping planes.

It is important to realize that most slerpy commands act on the "current" view. You can navigate among views by using the sn, sp, and sgo commands. If you've changed the view around with the mouse or you just want to know the number of the current view you can get back to the current view with the sc command.

Pymol views do not contain information about how pymol objects and selections are displayed. If you just want to create a movie which moves around a single representation of a molecule then all you need to do is record the set of views that define the tour of the molecule.

If, on the other hand, you want to change the representation or change which items are displayed you will need to add actions to some of your views. An action is any set of pymol commands. Actions can be associated with any view in the series recorded by slerpy. The sscene command inserts a new view and simultaneously creates a pymol scene and the action to display it. The scene will include all of the objects and representations visible at the time the command was issued.

In order to control the rate of motion between the defined views in a slerpy movie, you can control the number of frames used in each interpolation. When a view is saved in slerpy it is associated by default with a transition of 50 frames to the next view. The number of frames in the transition can be altered with the ssetf command in slerpy.

The views and actions stored by slerpy can (and should) be saved to a key file with the swrite command. They can then be retrieved with the sread command. Note that swrite saves the current pymol state in a .pse file but sread does not read in the .pse file. If you're starting a new pymol session to continue work on an existing movie you should load the pse file before doing an sread.

Quick Start Tutorial

If you haven't installed slerpy yet see Installation

Step 1
You probably want to start off in a nice clean working directory that just has the coordinate files you want to work with.
Step 2
Read in your molecule(s) and create the various selections and representations that you want to include in the movie.
Step 3
At the pymol prompt, type:
import slerpy
Step 4
Get your molecule in exactly the orientation and representation that you want to use for the beginning of your movie.
Step 5
Type:
sinsert
Step 6
Using the mouse, move your molecule to the next orientation that you want to use. When you record the movie, the camera orientation will be interpolated between each consecutive pair of views. This can include changes in rotation, zooming, clipping etc.
Loop back to Step 5. Continue this until you've got all your orientations stored.
You can check how any set of transitions will look at any time by using the sshow command (see Command Reference for details).
You can adjust the rate of any transition using the ssetf command
Step 7A
Add any actions to your views using the saction command. Any number of pymol commands can be strung together separated by semicolons. If, for example, you want to change your protein from a cartoon to a surface and add a ligand when you get to view 5 you would do the following (assuming you've defined the pymol selections prot and lig):
sgo 5
saction "hide cartoon, prot; show surface, prot; show sticks, lig"
Step 7B (Alternative using scenes)
sgo 5
Now use the gui to create the representation you want and then:
sscene
Step 8
Save everything. Save your slerpy views and actions as well as a pse file of your current pymol session:
swrite mymovie

This will create mymovie.key, which has all of the views, frame counts, actions etc. and mymovie.pse, the associated pymol session file.

Step 9
Record the movie! Type:
srecord
You can then play the movie by typing the standard pymol command mplay or by clicking the play button in pymol.
Step 10
If you save the pymol session again, the pse file will contain the movie which can then be shown immediately after startup without running slerpy.py. Note that pymol will warn you when you load a pse file that contains a movie.
Step 11
If you want to, the movie can be exported using the mpng command (see the pymol documentation). Also, see the useful article Making Movies.

Tips and Tricks

Converting scenes to movies

You can just step through the scenes and type sscene for each one. This will create a duplicate slerpy scene for each of the scenes you'd already saved but that's not such a disaster. Be sure to swrite when you're done.

Note that there is some overhead associated with recalling scenes. To avoid pauses at view transitions, I prefer to actually issue the set of show and hide commands that will generate the scene rather than using the above method.

Starting off right

It's a bit of a pain, but I like to associate with the first frame of the movie an action list that hides everything and then turns on all the objects that I want to have visible at the beginning. This ensures that when your movie loops back to the beginning it will look the same as it did the first time through. For example:

sgo 0
saction "hide everything; show lines, prot; show surface, activesite; show sticks, ligand"

Alternatively, start your slerpy work with an sscene.

Live pymol presentations during a talk

Be sure to run your movie once it's been opened and before your presentation if you're presenting in pymol. This will ensure that any objects that don't appear until the middle of the movie are available in memory and won't need to be rebuilt while your audience waits.

Of course showing your movie from within pymol allows you to show movies in stereo if you've got a presentation system that allows this. If you pass out stereo glasses first it's guaranteed that everyone will remember your talk...

Pausing on a view

Just sgo to the view you want to stay on for a while and do an sinsert. This will insert a new view with the same orientation etc as the one you were just on. You can adjust the length of the pause by changing the number of frames for the transistion between these two identical views using the ssetf command.

Morphing and multi-state models

Morphs and multi-conformation models are represented in pymol as single objects with multiple states. Cycling through the states as an animation is very straightforward in pymol. Slerpy allows you to include this animation over the states of an object as a transition between slerpy views within the context of larger movie. This is done using the smorph command (see Command Reference). smorph allows you to specify the starting and ending pymol state numbers to use during the transition from the current to the next view. It will often be appropriate to have the movie continue after the morph using the final state of the morphing model, that is, the conformation to which you morphed. Since the default for a typical slerpy view is to show the first state of an object, you'll probably need to have available, in addition to your multi-state model, a single-state object containing the final conformation. You can then hide the multistate object and show the single-state final conformation as an action associated with the final view of your morphing sequence.

To generate morphs you can use the rigimol program provided by Warren as part of the incentive pymol package for subscribers or use lsqman from Gerard Kleywegt.

Command Reference

Note that it is essential to understand that slerpy uses the concept of a current or active view. This is the element in the list to which most commands are applied. It is not necessarily the view that is currently visible on the screen. It is advisable to use the sc command frequently to make sure you really know which view your command is being applied to.

saction string: Assoiciates the pymol commands in string with the current view. string must be enclosed in quotes and must contain only valid pymol commands separated by semicolons.

sappend: Add the currently displayed view at the end of the list of views in slerpy

sappendaction string: Adds the action(s) in string to the list of actions associated with the current view

sc: Go to the slerpy active view

scrossfade startobject, endobject: During the transition from the current view to the next view startobject will fade out and endobject will fade in. The two objects must be shown as sticks. They must be objects, not merely selections, as of pymol 0.99.

sdelaction: Deletes the action associated with the current view. Be sure you're sure which view you're on before you use this. This will also delete any actions etc. associated with the view so be careful.

sdelete: Remove the slerpy active view from the list of views to be interpolated.

sdeletesetting: Remove the setting interpolation for the current view.

sdumpactions: List all actions by frame.

sgo n: Change the slerpy active view to view n.

sinsert: Add the currently displayed view after the slerpy active view.

sinterpsetting setting, selection, startval, endval : The pymol setting setting will be interpolated linearly from startval to endval during the transition from the current view to the next view. You can only interpolate one setting per transition. This is the hard way to, for example, fade out an object:

sinterpsetting stick_transparency, lig, 0.0, 1.0

slist: List all views stored for interpolation. Also lists the number of frames in each transition.

smorph startmodel,endmodel: The transition from the current frame to the next frame will consist of one frame per pymol state starting with state startmodel and ending with state endmodel. Subsequent frames (i.e. from subsequent views) will revert to state 1. The state numbers apply to currently visible objects so you will most likely want to have an object with your starting conformation, an object with your multi-state morphing model, and an object with your final conformation. You would then sgo to the frame where you want the morph to start and add an action to hide the starting conformation object and show the multi-model morphing object, do an smorph 1,30 or whatever the number of states in your morph is, append another frame and give it an action where the multi-state model is hidden and the final conformation is shown.

sn: Go to the next view

sp: Go to the previous view.

sread filename: Restore all the information written with swrite. Does not read in the pse file (to avoid inadvertantly writing over some new selections, scenes or whatever).

srecord: Records the movie

sreplace: Replace the slerpy current view with the currently displayed view.

sscene: Add a the currently displayed view after the slerpy active view and create a scene to go with it. The current pymol state, including which objects are displayed and how they are shown will be captured.

ssetf: Set the number of frames to use in the transition from the slerpy active view to the next view

ssetupview n: This attempts to make sure that all objects are displayed (or not) as they would be when view n is arrived at in the movie. It goes through and executes all of the sactions from all preceeding views. For some reason this doesn't always work in complex cases.

sshow n,m: This command records and shows a segment of the movie showing the transitions starting with view n and ending with view m. If the arguments m and n are omitted the transition from the current view to the next view will be shown.

swrite filename: Writes all the information used by slerpy to a file filename.key. It also writes a pymol session file filename.pse containing all of your current objects, selections etc. The format of the .key file is supposed to be human readable and it can be convenient to edit this file rather than using saction over and over. After editing it by hand be sure to do an sread.

Why is it called slerpy?

slerp is an acronym of sorts for spherical linear interpolation. The transition between two views approximates spherical linear interpolation of the their camera positions using quaternions. See the Wikipedia article on slerps.

Script Files

Installation

To install, copy (by pasting into a text editor) the following two python code segments to files named slerpy.py and movs.py, respectively. Place these files in the pymol/modules/pymol/ directory created by the pymol installation.

If you don't have write priviledges in the pymol installation directories you can just copy these files to the working directory from which pymol will be run.

Code

slerpy.py

The command definitions for slerpy
################################################################################
#slerpy.py - Command definition routines for slerpy
#Copyright (C) 2006 Joel Bard
#
#This program is free software; you can redistribute it and/or
#modify it under the terms of the GNU General Public License
#as published by the Free Software Foundation; either version 2
#of the License, or (at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#################################################################################

from pymol import cmd
import movs

def readViews( filename ):
#obsolete
    vfile = open( filename, 'r')
    views = [] #a list of views each of which is a list of 18 numbers
    vstrings = vfile.readlines()
    for vstring in vstrings:
        vals = vstring.split()
        view = [float(v) for v in vals]
        views.append( view )
    vfile.close()
    return views

def readFrames( filename ):
#obsolete
    ffile = open( filename, 'r' )
    frames = []
    fstrings = ffile.readlines()
    for fstring in fstrings:
        frames.append( int(fstring) )
    ffile.close()
    return frames

def readActions( filename ):
#obsolete
    #actions are stored in file where
    #for each line, the first 4 chars are the view index
    #associated with the action and the rest of the
    #line is the pymol command to be executed
    #upon reading, a dictionary is returned
    #with view indices as keys and actions as values
    actions = {}
    try:
        afile = open( filename, 'r' )
    except:
        print "No actions for this project"
        return actions
    astrings = afile.readlines()
    for astring in astrings:
        try:
            aindex = int(astring[:4])
            action = astring[4:]
            actions[ aindex ] = action[:-1]
        except:
            print "empty line"
    afile.close()
    return actions

def readModels( filename ):
#obsolete
    models = {}
    try:
        mfile = open( filename, 'r' )
    except:
        print "No models for this project"
        return models
    mstrings = mfile.readlines()
    for mstring in mstrings:
        try:
            mindex = int(mstring[:4])
            model = mstring[4:]
            models[ mindex ] = model[:-1]
        except:
            print "empty line"
    mfile.close()
    return models

def readSettings( filename ):
#obsolete
    settings = {}
    try:
        sfile = open( filename, 'r' )
    except:
        print "No settings for this project"
        return settings
    sstrings = sfile.readlines()
    for sstring in sstrings:
        try:
            sindex = int(sstring[:4])
            scommas = sstring[4:]
            settingName,selection,startVal,endVal = scommas.split(',')
            setting = [settingName,selection,float(startVal),float(endVal)]
            settings[sindex] = setting
        except:
            print "unable to parse setting"
    sfile.close()
    return settings
            
def readScenes( filename ):
#obsolete
    global scene_counter

    scene_counter = 0
    scenes = {}
    try:
        sfile = open( filename, 'r' )
    except:
        print "No scenes file for this project"
        return scenes
    sstrings = sfile.readlines()
    for sstring in sstrings:
        try:
            sindex = int(sstring[:4])
            scene = sstring[4:]
            scenes[ sindex ] = scene[:-1]
            scene_counter += 1
            #print "reading scene", sstring, sindex, scene
        except:
            print "empty line"
    sfile.close()
    return scenes

def read_all( fileroot ):
#obsolete in favor of readKeyViewFile
    global views
    global frames
    global actions
    global cur_view
    global cur_index
    global scenes
    global models
    global settings
    
    views = readViews( fileroot+".txt" )
    frames = readFrames( fileroot+".frm")
    actions = readActions( fileroot+".act")
    scenes = readScenes( fileroot+".scn")
    models = readModels( fileroot+".mod")
    settings = readSettings( fileroot+".set")
    cur_view = views[0]
    cur_index = 0
    show_cur()
    
def print_view( view ):
    for i in range(0,6):
        for j in range(0,3):
            print "%12.6f"% view[ 3*i+j ] ,
        print

def show_next():
    global cur_index
    global cur_view
    global views
    if cur_index == len( views )-1:
        print "No more views."
        return
    cur_index += 1
    cur_view = views[ cur_index ]
    cmd.set_view(cur_view)
    print "Matrix: "
    print_view( cur_view )
    print "Showing view number ",cur_index

def show_prev():
    global cur_index
    global cur_view
    global views
    if cur_index == 0:
        print "No more views."
        return
    cur_index -= 1
    cur_view = views[ cur_index ]
    cmd.set_view(cur_view)
    print "Matrix: "
    print_view( cur_view )
    print "Showing view number ",cur_index

def show_cur():
    global cur_index
    global cur_view
    global views
    cur_view = views[ cur_index ]
    cmd.set_view(cur_view)
    print "Matrix: "
    print_view( cur_view )
    print "Showing view number ",cur_index

def go_to_view( arg=0 ):
    global cur_index
    global cur_view
    global views
    n = int( arg )
    if n < 0 or n >= len(views):
        print "Index out of range."
        return
    cur_index = n
    cur_view = views[n]
    cmd.set_view( cur_view )
    print "Matrix: "
    print_view( cur_view )
    print "Showing view number ", cur_index

def insert_current():
    #insert the current view into the list after the view
    #in views[cur_index]
    #set frames to default
    global cur_index
    global cur_view
    global views
    global frames
    global actions
    global scenes
    global settings
    global models
    global fades
    
    cur_index += 1
    cur_view = cmd.get_view()
    views.insert( cur_index, [cv for cv in cur_view] )
    frames.insert( cur_index, 50 )
    
    #deal with actions dictionary
    actions = incKeyAbove( actions, cur_index )
    scenes = incKeyAbove( scenes, cur_index )
    settings = incKeyAbove( settings, cur_index )
    models = incKeyAbove( models, cur_index )
    fades = incKeyAbove( fades, cur_index )

    print "New view:"
    print_view( cur_view )
    print "Inserted view with index", cur_index, "and a 50 frame transition"

def append_view():
    global views
    global frames
    global cur_index
    global cur_view

    cur_index = len(views)
    cur_view = cmd.get_view()
    views.append( [cv for cv in cur_view] )
    frames.append( 50 )

    print "New view: "
    print_view( cur_view )
    print "Appended view with index", cur_index, "and a 50 frame transition"
    print "The current view is", cur_index
    
def incKeyAbove( dict, index ):
    tempDict = {}
    for key, val in dict.iteritems():
        if key >= index:
            newkey = key + 1
            tempDict[newkey] = val
        else:
            tempDict[key] = val
    return tempDict

def decKeyAbove( dict, index ):
    tempDict = {}
    for key, val in dict.iteritems():
        if key > index:
            newkey = key - 1
            tempDict[newkey] = val
        else:
            tempDict[key] = val
    return tempDict
    
def delete_current():
    #remove the current view from the list
    #show the previous view
    global cur_index
    global cur_view
    global views
    global actions
    global scenes
    global settings
    global models
    global frames
    global fades
    
    del views[cur_index]
    del frames[cur_index]
    if cur_index in actions:
        del actions[cur_index]
    if cur_index in scenes:
        del scenes[cur_index]
    if cur_index in settings:
        del settings[cur_index]
        
    #deal with dictionaries
    actions = decKeyAbove( actions, cur_index )
    scenes = decKeyAbove( scenes, cur_index )
    settings = decKeyAbove( settings, cur_index )
    models = decKeyAbove( models, cur_index )                               
    fades = decKeyAbove( fades, cur_index )

    print "View number",cur_index,"deleted."
    if cur_index > 0:
        cur_index -= 1
    cur_view = views[cur_index]
    cmd.set_view( cur_view )
    print "Current view is number",cur_index

def delete_settings():
    global settings
    global cur_index
    del settings[cur_index]

def replace_current():
    global cur_index
    global cur_view
    global views
    cur_view = cmd.get_view()
    views[cur_index] = [cv for cv in cur_view]

def insert_scene():
    global views
    global actions
    global settings
    global frames
    global cur_index
    global cur_view
    global scenes
    global scene_counter
    global models
    global fades
 
    cur_index += 1
    cur_view = cmd.get_view()
    views.insert( cur_index, [cv for cv in cur_view] )
    frames.insert( cur_index, 50 )
    
    #deal with dictionaries
    actions = incKeyAbove( actions, cur_index )
    scenes = incKeyAbove( scenes, cur_index )
    settings = incKeyAbove( settings, cur_index )
    models = incKeyAbove( models, cur_index )
    fades = incKeyAbove( fades, cur_index )

    #this stuff has to be done after the above
    #find a free scene name
    i = 1
    while True:
        for sname in scenes.values():
            print "|"+sname+"|"
            if sname == "slerpy_"+str(i):
                break
        else:
            break
        i += 1
    newname = "slerpy_"+str(i)
        
    scene_counter += 1
    cmd.scene( newname, "store" )
    scenes[ cur_index ] = newname
    actions[ cur_index ] = "scene "+newname
    
    print "New view:"
    print_view( cur_view )
    print "Inserted view with index", cur_index, "and a 50 frame transition"
    print "Added scene",newname
    
def write_views( filename ):
#deprecated in favor of key files
    global views
    global frames
    global scenes
    global actions
    global settings
    global models
    global fades

    viewfile = open( filename+".txt", 'w')
    for view in views:
        for v in view:
            viewfile.write( str(v) + " " )
        viewfile.write('\n')
    viewfile.close()
    
    framefile = open( filename+".frm", 'w' )
    for frame in frames:
        framefile.write( str( frame )+'\n' )
    framefile.close()

    actionfile = open( filename+".act", 'w' )
    for key,action in actions.iteritems():
        keystring = str( key )
        actionfile.write( keystring.rjust(4)+action + '\n' )
    actionfile.close()

    scenefile = open( filename+".scn", 'w' )
    for key,scene in scenes.iteritems():
        keystring = str( key )
        scenefile.write( keystring.rjust(4)+scene + '\n' )
    scenefile.close()

    modelfile = open( filename+".mod", 'w' )
    for key,model in models.iteritems():
        keystring = str( key )
        modelfile.write( keystring.rjust(4)+model + '\n' )
    modelfile.close()

    settingsfile = open( filename+".set", 'w' )
    for key,setting in settings.iteritems():
        keystring = str( key )
        settingName, selection, startVal, endVal = setting
        settingsfile.write( "%s%s,%s,%f,%f\n" % (keystring.rjust(4), settingName, selection, startVal, endVal))
    settingsfile.close()
    cmd.save( filename+".pse" )
        
    print "Wrote files", filename+".txt,",filename+".frm,",filename+".pse, and",filename+".act"

def writeKeyViewFile( filename ):
    global views
    global frames
    global scenes
    global actions
    global settings
    global models
    global fades
 
    keyviewfile = open( filename + ".key", 'w' )
    for i,view in enumerate(views):
        keyviewfile.write( "VIEW: %4d " % i )
        for v in view:
            keyviewfile.write( str(v) + " " )
        keyviewfile.write('\n')
        keyviewfile.write( "FRAMES: %d\n" % frames[i] )
        if i in actions:
            keyviewfile.write( "ACTIONS: %s\n" % actions[i] )
        if i in scenes:
            keyviewfile.write( "SCENES: %s\n" % scenes[i] )
        if i in models:
            keyviewfile.write( "MODELS: %s\n" % models[i] )
        if i in settings:
            settingName, selection, startVal, endVal = settings[i]
            keyviewfile.write( "SETTINGS: %s, %s, %f, %f\n" % (settingName, selection, startVal, endVal))
        if i in fades:
            startVisSelection, endVisSelection, sticksOnly = fades[i]
            keyviewfile.write( "FADES: %s, %s, %d\n" % (startVisSelection, endVisSelection, sticksOnly))            
        keyviewfile.write("\n")
    keyviewfile.close()
    cmd.save( filename + ".pse" )
    print "Wrote files " , filename + ".key", filename + ".pse"

def readKeyViewFile( filename ):
    global views
    global frames
    global scenes
    global actions
    global settings
    global models
    global fades
    global scene_counter

    views = []
    frames = []
    actions = {}
    scenes = {}
    models = {}
    settings = {}
    fades = {}
    scene_counter = 0

    if filename.endswith(".key"): filename = filename[:-4]
    keyviewfile = open( filename + ".key", 'r' )
    viewstrings = keyviewfile.readlines()
    keyviewfile.close()
    viewindex = 0
    for line in viewstrings:
        if line.startswith("VIEW: "):
            viewindex = int( line[6:10] )
            vals = line[10:].split()
            view = [float(v) for v in vals]
            views.append( view )
        if line.startswith("FRAMES: "):
            frames.append( int( line[8:] ) )
        if line.startswith("ACTIONS: "):
            actions[ viewindex ] = line[9:-1]
        if line.startswith("SCENES: "):
            scenes[ viewindex ] = line[8:-1]
            scene_counter += 1
        if line.startswith("MODELS: "):
            models[ viewindex ] = line[8:-1]
        if line.startswith("SETTINGS: "):
            settingName,selection,startVal,endVal = line[10:-1].split(',')
            settings[ viewindex ] = [settingName,selection,float(startVal),float(endVal)]
        if line.startswith( "FADES: " ):
            startVisSelection, endVisSelection, sticksOnly = line[7:-1].split(',')
            fades[ viewindex ] = [startVisSelection, endVisSelection, int(sticksOnly) ]
    cur_view = views[0]
    cur_index = 0
    show_cur()

def set_frames_current( arg=50 ):
    global frames
    global cur_index
    frames[cur_index] = int(arg)

def list_frames():
    global frames
    global views
    global actions
    global models
    global settings
    
    f=0
    for i,view in enumerate(views[:-1]):
        if i in actions:
            a = actions[i]
        else:
            a = ""
        if i in models:
            m = "States: " + models[i]
        else:
            m = ""
        if i in settings:
            settingName, selection, startVal, endVal = settings[i]
            s = "Settings: %s %s %f %f" % (settingName, selection, startVal, endVal)
        else:
            s = ""
        print "View",i,"to",i+1,"Frames ",f,"to",f+frames[i],a,m,s
        f += frames[i]

def add_action_current( cmd ):
    global cur_index
    global actions
    actions[cur_index] = cmd[1:-1] #strip off quotes

def append_action_current( cmd ):
    global cur_index
    global actions
    actions[cur_index] += ";" + cmd[1:-1]

def clear_action_current():
    global actions
    global cur_index
    del actions[cur_index]

def list_actions():
    global actions
    for i,a in actions.iteritems():
        print i,a

def morph_models( start_model, end_model ):
    global cur_index
    global frames
    global models
    models[cur_index] = "%s -%s" % (start_model, end_model)
    frames[cur_index] = abs(int(end_model) - int(start_model)) + 1

def interpolate_settings( setting, selection, startval, endval ):
    global cur_index
    global settings
    settingEntry = [setting, selection, float(startval), float(endval)]
    settings[cur_index] = settingEntry 

def crossfade( startVisSelection, endVisSelection, sticksOnly = 1 ):
#cross fade the specified objects, vary stick transparency only if stickOnly=1
    global cur_index
    global fades
    fades[cur_index] = [startVisSelection, endVisSelection, int(sticksOnly) ]

def setup_view( index ):
    for i in range( int(index)+1 ):
        if i in actions:
            print "Executing %s from actions %d" % (actions[i],i)
            cmd.do( actions[i] )
        if i in settings:
            settingName, selection, startVal, endVal = settings[i]
            action = "set %s, %f, %s;" % (settingName, endVal, selection)
            print "Executing %s from settings %d" % (action,i)
            cmd.do( action )
        if i in fades:
            startVisSelection, endVisSelection, sticksOnly = fades[i]
            action = "set stick_transparency, 0, %s; set stick_transparency, 1, %s;" % (endVisSelection, startVisSelection)
            print "Executing %s from fades %d" % (action, i)
            cmd.do( action )

def show_transition(start_index=0, end_index=0):
    #show the transition from the current view to the next view
    global frames
    global views
    global cur_index
    global actions
    global models
    if start_index == 0 and end_index == 0:
        if cur_index >= len(views)-1:
            print "Current view is last in sequence."
            return
        start_index=cur_index
        end_index=cur_index+1
    else:
        start_index = int(start_index)
        end_index = int(end_index)
    ftot = 0
    setcommand = ""
    i = start_index
    for nframes in frames[start_index:end_index]:
        #ftot += nframes
        if i in models:
            setcommand += " " + models[i] + " "
        else:
            setcommand += " 1 x%i" % nframes
        i += 1
        
#    cmd.mset("1 x%i" % ftot)
    cmd.mset( setcommand )
    start_frame = 1
    #first do all actions that happen up to this point to make sure
    #things look the way they should
    first_action = ""
    for i in range( start_index ):
        if i in actions:
            first_action += actions[i] + ';'
            #print "Executing %s from actions %d" % (actions[i],i)
            #cmd.do( actions[i] )
        if i in settings:
            settingName, selection, startVal, endVal = settings[i]
            action = "set %s, %f, %s;" % (settingName, endVal, selection)
            first_action += action
            #print "Executing %s from settings %d" % (action,i)
            #cmd.do( action )
        if i in fades:
            startVisSelection, endVisSelection, sticksOnly = fades[i]
            action = "set stick_transparency, 0, %s; set stick_transparency, 1, %s;" % (endVisSelection, startVisSelection)
            first_action += action
            #print "Executing %s from fades %d" % (action, i)
            #cmd.do( action )
    for i in range( start_index, end_index ):
        if i in settings:
            movs.animate_transition( views[i], views[i+1], frames[i], start_frame, settings[i] )
        elif i in fades:
            movs.animate_transition( views[i], views[i+1], frames[i], start_frame, fades[i] )
        else:
            movs.animate_transition( views[i], views[i+1], frames[i], start_frame )
        #add an action
        if start_frame == 1:
            mdo_cmd = first_action
            if i in actions:
                mdo_cmd += actions[i]+";"
            #mdo_cmd += "set_view("+str(views[i])+")"
            print mdo_cmd
            cmd.mdo(start_frame, mdo_cmd)
        elif i in actions:
            mdo_cmd = actions[i]+";set_view("+str(views[i])+")"
            cmd.mdo(start_frame, mdo_cmd)
            #print mdo_cmd
        start_frame += frames[i]
    cmd.frame(1)
    cmd.mplay()

def make_all():
    #make the whole movie
    global views
    global frames
    global models
    
    #first get total number of frames
    ftot = 0
    setcommand = ""
    for i,nframes in enumerate(frames[:-1]):
        ftot += nframes
        if i in models:
            setcommand += " " + models[i] + " "
        else:
            setcommand += " 1 x%i" % nframes

    #initialize movie
    #cmd.mset("1 x%i" % ftot)
    #cmd.mset("1 x50 1 -30 30 x20")
    cmd.mset( setcommand )

    #loop through views
    start_view = views[0][:]
    first_frame = 1
    for i,view in enumerate(views[1:]):
        end_view = view[:]
        if i in settings:
            movs.animate_transition( start_view, end_view, frames[i], first_frame, settings[i] )
        elif i in fades:
            movs.animate_transition( start_view, end_view, frames[i], first_frame, fades[i] )
        else:
            movs.animate_transition( start_view, end_view, frames[i], first_frame )
        #add an action
        if i in actions:
            mdo_cmd = actions[i]#+";set_view ("+str( views[i] )+")"
            print mdo_cmd
            cmd.mdo(first_frame, mdo_cmd)
        first_frame += frames[i]
        start_view = end_view[:]
    cmd.frame(1)

## views = readViews( "viewfile.txt" )
## frames = readFrames( "viewfile.frm" )
## actions = readActions( "viewfile.act" )
##print "Length ",len(views)
#for v in views:
 #   print v
#cur_view = views[0]
views = []
frames = []
models = {}
actions = {}
scenes = {}
settings = {}
fades = {}
scene_counter = 0
cur_index = -1
cmd.set( "scene_animation_duration","0" )
#cmd.set_view( cur_view )

cmd.extend("sn", show_next )
cmd.extend("sp", show_prev )
cmd.extend("sc", show_cur )
cmd.extend("sinsert", insert_current )
cmd.extend("sdelete", delete_current )
cmd.extend("sreplace", replace_current )
cmd.extend("sappend", append_view )
cmd.extend("sscene", insert_scene )
cmd.extend("sgo", go_to_view )
cmd.extend("sreadold", read_all )
cmd.extend("swriteold", write_views )
cmd.extend("slist", list_frames )
cmd.extend("ssetf", set_frames_current )
cmd.extend("sshow", show_transition )
cmd.extend("srecord", make_all )
cmd.extend("saction", add_action_current )
cmd.extend("sdelaction", clear_action_current )
cmd.extend("sdumpactions", list_actions )
cmd.extend("sappendaction", append_action_current )
cmd.extend("smorph", morph_models )
cmd.extend("sinterpsetting", interpolate_settings )
cmd.extend("sdeletesetting", delete_settings )
cmd.extend("scrossfade", crossfade )
cmd.extend("swrite", writeKeyViewFile )
cmd.extend("sread", readKeyViewFile )
cmd.extend("ssetupview", setup_view )

movs.py

Math and animation routines for slerpy
##################################################################################
#movs.py - Math and animation routines for slerpy
#Copyright (C) 2006 Joel Bard
#
#This program is free software; you can redistribute it and/or
#modify it under the terms of the GNU General Public License
#as published by the Free Software Foundation; either version 2
#of the License, or (at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#################################################################################

from pymol import cmd,stored
from math import *

def rmat2quat( M ):
    #M is a list of 9 values being the elements of the rotation matrix in row order
    T = M[0] + M[4] + M[8] + 1
    print "Trace ",T
    if T>0:
        S = 0.5 / sqrt(T)
        W = 0.25/S
        X = (M[7] - M[5])*S
        Y = (M[2] - M[6])*S
        Z = (M[3] - M[1])*S
    elif M[0] > M[4] and M[0] > M[8]:
        S = sqrt( 1.0 + M[0] - M[4] - M[8]) * 2
        X = 0.25 * S
        Y = (M[1] + M[3])/S
        Z = (M[2] + M[6])/S
        W = (M[5] - M[7])/S
    elif M[4] > M[8]:
        S = sqrt( 1.0 + M[4] - M[0] - M[8] ) * 2
        X = (M[1] + M[3])/S
        Y = 0.25 * S
        Z = (M[5] + M[7])/S
        W = (M[2] - M[6])/S
    else:
        S = sqrt( 1.0 + M[8] - M[0] - M[4]) * 2
        X = (M[2] + M[6])/S
        Y = (M[5] + M[7])/S
        Z = 0.25 * S
        W = (M[1] - M[3])/S
    return [X,Y,Z,W]

def quat2rmat( Q ):
    #Q is a list of 4 values being the quaternion X Y Z W
    X=Q[0]
    Y=Q[1]
    Z=Q[2]
    W=Q[3]
    xx = X*X
    xy = X*Y
    xz = X*Z
    xw = X*W
    yy = Y*Y
    yz = Y*Z
    yw = Y*W
    zz = Z*Z
    zw = Z*W

    M= [1.0]*9
    M[0] = 1 - 2 * ( yy + zz )
    M[1] = 2 * ( xy - zw )
    M[2] = 2 * ( xz + yw )
    M[3] = 2 * ( xy + zw )
    M[4] = 1 - 2 * ( xx + zz )
    M[5] = 2 * ( yz - xw )
    M[6] = 2 * ( xz - yw )
    M[7] = 2 * ( yz + xw )
    M[8] = 1 - 2 * ( xx + yy )
    return M

def quatconj( Q ):
    return [-Q[0],-Q[1],-Q[2],Q[3]]

def quatmag( Q ):
    s = 0.0
    QC = quatconj(Q)
    for x in range(4):
        s += Q[x]*Q[x]
    print s
    return sqrt(s)

def quatnorm( Q ):
    m = quatmag( Q )
    return [q/m for q in Q]

def quatdotprod( q1, q2 ):
    dp = 0
    for i in range(4):
        dp += q1[i]*q2[i]
    return dp

def vectnorm( V ):
    mag = 0.0
    for x in V:
        mag += x*x
    mag = sqrt(mag)
    return [x/mag for x in V]

def quat2axisangle( Q ):
    #returns list where 0..2 are rot axis and 3 is angle
    qn = quatnorm( Q )
    cos_a = Q[3]
    angle = acos( cos_a ) * 2
    sin_a = sqrt( 1.0 - cos_a * cos_a )
    if fabs( sin_a ) < 0.000005:
        sin_a = 1
    ax_an = [ q/sin_a for q in Q[0:3] ]
    ax_an.append( angle )
    return ax_an

def axisangle2quat( ax_an ):
    #ax_an is a list with axis coordinates followed by rotation angle
    axn = vectnorm( ax_an[:3] )
    angle = ax_an[3]
    sin_a = sin( angle / 2 )
    cos_a = cos( angle / 2 )
    Q = [ x * sin_a for x in axn ]
    Q.append( cos_a )
    return Q
    
def rmat2axisangle( M ):
    q = rmat2quat( M )
    return quat2axisangle( q )

def axisangle2rmat( a ):
    q = axisangle2quat( a )
    return quat2rmat( q )

def animate_transition( start_view, end_view, nframes, first_frame, settings = [] ):
    #print "Views"
    #print start_view,'\n',end_view

    cview = start_view[:]
    cmd.set_view( start_view )

    #get initial and final quaternions for interpolation
    #print start_view[0:9]
    #get quaternions
    qstart = rmat2quat( start_view[0:9] )
    qend = rmat2quat( end_view[0:9] )
    
    #test for long way vs. short way
    if quatdotprod( qstart,qend ) < 0:
        qstart = [-q for q in qstart]

    axan_start = quat2axisangle( qstart )
    axan_end = quat2axisangle( qend )
        
    axan_cur = axan_start[:]
    frame_start = first_frame
    frame_end = frame_start + nframes
    doFade = 0
    doSetting = 0
    if len( settings ) == 4:
        settingName, selection, startVal, endVal = settings
        settingStep = (endVal-startVal)/float(nframes)
        print "Setting step ", settingStep
        doSetting = 1
    elif len( settings ) == 3:
        startVisSelection, endVisSelection, sticksOnly = settings
        settingStep = 1.0/float(nframes)
        doFade = 1
    for f in range( frame_start , frame_end):
        #get rotmat
        #using angle axis

        for i in range(4):
            axan_cur[i] = axan_cur[i] + (axan_end[i]-axan_start[i])/nframes
        newmat = axisangle2rmat( axan_cur )
        #print cview
        for i in range(9):
            cview[i] = newmat[i]

        mdo_cmd = "set_view (["
        for i in range(18):
            if i>8:
                cview[i] = cview[i]+(end_view[i]-start_view[i])/nframes
            mdo_cmd += "%12.7f,"% cview[i]
        mdo_cmd = mdo_cmd[:-1]+"])"
        if doSetting:       
            val = float(f-frame_start)*settingStep + startVal 
            print val;
            mdo_cmd += "; set %s, %f, %s" % (settingName, val, selection)
            print mdo_cmd;
        #print "mdo ", mdo_cmd
        if doFade:
            val = float(f-frame_start)*settingStep
            otherVal = 1.0 - val
            mdo_cmd += "; set stick_transparency, %f, %s; set stick_transparency, %f, %s" % ( val, startVisSelection, otherVal, endVisSelection )
            if not sticksOnly:
                #comment out surface transparency interpolation due to problem with transparent sticks in front of 
                #transparent surfaces (get black holes)
               # mdo_cmd += "; set transparency, %f, %s; set transparency, %f, %s" % ( val, startVisSelection, otherVal, endVisSelection )
                mdo_cmd += "; set cartoon_transparency, %f, %s; set cartoon_transparency, %f, %s" % ( val, startVisSelection, otherVal, endVisSelection )
        cmd.mdo(f,mdo_cmd)