Scripting interface examples.

The scripts below are based on the MView beta 0.6 version.

 

Executing a script:

1. Directly from command line:

MView <script_file> <script_arg1> ... <script_argN>

2. From the GUI menu at Tools > Scripting > Execute from file.

 

Examples of Scripts

General

#### This is an example of the MView python scripting interface #####
# Opening two XYZ files =============================================
mview.open_file("XYZ", ["mol1.xyz", "mol2.xyz"])
print "py: Structure imported..."
print "Done with scripting!"

When executing the script from command line the GUI is NOT started autoomatically.
To do so, the following command should be executed at the end of the script.

mview.open_gui()

#### This is an example of the MView python scripting interface #####
# Opening two XYZ files =============================================
mview.open_file("XYZ", ["mol1.xyz", "mol2.xyz"]);
print "py: Structures imported..."
# Exporting one system to a file in the PDB format ==================
msys = mview.get_system(1)                                            # Warning: The system ids start at 0
if msys:
    mview.export_system("PDB", "output.pdb", msys, False)
else:
    print "py: Failed to retrieve system!"
print "Done with scripting!"

#### This is an example of the MView python scripting interface #####
# Opening two XYZ files =============================================
mview.open_file("XYZ", ["mol1.xyz", "mol2.xyz"])
print "py: Structure imported..."
# Retrieving the current system =====================================
msys = mview.get_current_system()
print "Done with scripting!"

Rotation and Translation

#### This is an example of the MView python scripting interface ####
# Retrieving the current molecule ==================================
msys = mview.get_current_system()
if msys:
    # Rotating the viewer ==========================================
    msys.rotate_viewer(-90.0, [0.0,1.0,0.0])
    # Translating the viewer =======================================
    msys.translate_viewer([0.0,2.0,0.0])
else:
    print "py: Failed to retrieve system!"
print "Done with scripting!"
#### This is an example of the MView python scripting interface ####
# Retrieving the current molecule ==================================
msys = mview.get_current_system()
if msys:
    # Rotating the coordinates =====================================
    msys.rotate(-90.0, [0.0,1.0,0.0])
    # Translating the coordinates ==================================
    msys.translate([0.0,2.0,0.0])
else:
    print "py: Failed to retrieve system!"
print "Done with scripting!"

Scene

 

#####################################################################
#### This is an example of the MView python scripting interface #####
#
# It imports a structure from a XYZ file and sets the customized lights
#
# Opening the XYZ file <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< mview.open_file("XYZ", [script_path+"/BuckyTube_196.xyz"]);
# Asking for a pointer to the current molecule <<<<<<<<<<<<<<<<<<<<<< msys = mview.get_current_system(); if(msys): print "py: Structure imported..." # Block the update of the scene after every command.............. # - Improves performance only if the script is executed from the GUI msys.set_scene_auto_update(False) # Set the global representation to VDW ========================== msys.set_representation("rep_VDW") # Setting the lights <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< # Removing the lights added automatically by MView............... print "py: Removing the standard lights." msys.remove_all_lights(); # Adding the customized lights to the scene ===================== print "py: Adding the customized lights." # Light 1........................................................ ambient_color1 = [0.0,0.0,0.0] diffuse_color1 = [1.0,1.0,0.0] specular_color1 = [0.0,0.0,0.0] msys.add_light(0.0, 0.0, -35.0, 1.0, ambient_color1, diffuse_color1, specular_color1) # Light 2........................................................ ambient_color2 = [0.0,0.0,0.0] diffuse_color2 = [0.5,0.5,0.5] specular_color2 = [0.8,0.8,0.8] msys.add_light(0.0, 0.0, 100.0, 1.0, ambient_color2, diffuse_color2, specular_color2) # Light 3........................................................ ambient_color3 = [0.2,0.2,0.2] diffuse_color3 = [0.1,0.1,0.1] specular_color3 = [0.0,0.0,0.0] msys.add_light(+50.0, 0.0, 100.0, 1.0, ambient_color3, diffuse_color3, specular_color3) # Rotate the molecule =========================================== msys.rotate_viewer(-25.0, [-1.0,1.0,1.0]) # GUI calls ##################################################### msys.update_viewer(); # Needed only if the scrited is executed from the GUI mview.open_gui() # This call is ignored if the scrited is executed from the GUI else: print "py: Failed to retrieve system!" print "py: Done with scripting!"
  • script_Lights
download files

Representations

#####################################################################
#### This is an example of the MView python scripting interface #####
print "py: Loading the atom&bond representation module."
mview.load_plugin_python_module("rep_plugin_atbond_mview")
print "py: Loading the rings representation module."
mview.load_plugin_python_module("rep_plugin_rings_mview" )
# Opening the pdb file <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
list_files = [script_path+"/C20-Ih.pdb", script_path+"/C60.pdb",
              script_path+"/C100-D2-449.pdb",script_path+"/C180-0.pdb",
              script_path+"/C240-0.pdb" ]
# Load all files merging their atoms into one system.................
mview.open_file("PDB", list_files, False, mergeType.COORD, True)
print "py: Finished opening the input file..."
# Asking for a pointer to the current system <<<<<<<<<<<<<<<<<<<<<<<<
msys = mview.get_current_system()
if(msys):
    # Removing the representation set by MView.......................
    msys.reset_representations()
    # Setting the representations <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    #
    # - Every fullerene will have its own:   
    #   . Cylinder representation
    #   . Ring     representation
    #   . Clip planes
    #
    list_gid   = list()
    list_depth = [ 5.0, 2.5, 2.0, 1.5, 1.0 ]
    list_rads  = [ 0.3, 0.25, 0.20, 0.15, 0.1 ]
    list_color = [ [0.8,0.1,0.1],[0.3,0.3,0.1],[0.2,0.1,0.3],[0.1,0.3,0.5],[0.4,0.4,0.4] ]
    list_grp   = ["c20","c60","c100","c180","c240"]    
    list_ats   = [ range(20), range(20,80), range(80,180), range(180, 360), range(360, 600) ]
    for ats, gname, color, rad, depth in zip(list_ats, list_grp, list_color, list_rads, list_depth):
        
        # Setting the bonds of a single fullerene....................
        msys.set_bonds_between_atoms(ats)
        
        # Setting a group with the atoms of the fullerene............
        gid = msys.set_subset(ssType.GRP, Info.AtId, ats) 
        msys.set_subset_label(ssType.GRP, gid, gname)
    
        # Adding a new representation of the current fullerene.......
        Rid = msys.add_representation("rep_CYL", True)
        msys.manage_rep(Rid, "group [\""+gname+"\"] and bond")
    
        # Setting the color code.....................................
        Rep_AtBo.set_color_code(msys, Rid, Rep_AtBo.cc_Custom, color)
               
        # Setting the radius and quality of the bonds................
        Rep_AtBo.set_object_radius (msys, Rid, objType.BOND, rad)
        Rep_AtBo.set_object_quality(msys, Rid, objType.BOND, 10 )
        
        # Adding a clip plane........................................
        pId = msys.add_clip_plane(Rid)
        msys.set_clip_plane_params(Rid, pId, [0.0,0.0,-1.0, depth])
    
    # Add the rings representations =================================
    #
    # - It should be done after setting all bond since all rings
    #   must be recalculated when new bonds are set in the system
    #
    for gname, color, depth in zip(list_grp, list_color, list_depth):
        Rid = msys.add_representation("rep_RING", True)
        msys.manage_rep(Rid, "group [\""+gname+"\"] and bond")
        msys.set_rep_transparency(Rid, 50.0)
        Rep_Ring.set_color_code(msys, Rid, Rep_Ring.cc_Custom, color)
    
        # Adding a clip plane........................................
        pId = msys.add_clip_plane(Rid)
        msys.set_clip_plane_params(Rid, pId, [0.0,0.0,-1.0, depth])
    
    # Rotating translating the scene <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    msys.rotate_viewer   (45.0, [0.0,1.0,0.0])
    msys.rotate_viewer   (20.0, [1.0,0.0,0.0])
    msys.translate_camera([0.0,0.0,-13.0])
    
    # Opening the GUI interface #####################################
    mview.open_gui()
#    else: print "py: Failed to create the new representations!"
else:
    print "py: Failed to retrieve system!"
print "py: Done with scripting!" 
  • Onion
download files
#####################################################################
#### This is an example of the MView python scripting interface #####
#
# Creates new representations, applying translation and rotation to
# specific fragments of the system
#
print "py: Loading the atom&bond representation module."
mview.load_plugin_python_module("rep_plugin_atbond_mview")
# Opening one XYZ file <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
print ""
print "py: Importing the structure..."
# Using the predefined variable 'script_path'
mview.open_file("XYZ", [script_path + "/nanotube_multi.xyz"]);
   # Asking for a pointer to the current molecule <<<<<<<<<<<<<<<<<<<<<<
msys = mview.get_current_system()
if(msys):
    print "py: Structure imported!"
    
    print "py: Setting the representations."
    
    # Remove the standard representation added by MView =============
    msys.reset_representations()
    # Add a customized representations per fragment =================
    R1id = msys.add_representation("rep_VDW", True)
    msys.manage_rep(R1id, "fragment [0]")
    R2id = msys.add_representation("rep_CPK", True)
    msys.manage_rep(R2id, "fragment [1] and bond")
    R3id = msys.add_representation("rep_LIN", True)
    msys.manage_rep(R3id, "fragment [2] and bond")
    # Changing the color of the fragments ===========================
    print "py: Setting the fragments color."
    Rep_AtBo.set_color_code(msys, R1id, Rep_AtBo.cc_Custom, [0.8,0.0,0.0])
    Rep_AtBo.set_color_code(msys, R2id, Rep_AtBo.cc_Custom, [0.0,0.8,0.0])
    Rep_AtBo.set_color_code(msys, R3id, Rep_AtBo.cc_Custom, [0.0,0.0,0.8])
    # Setting the representation transparency........................
    msys.set_rep_transparency(R1id, 80.0)
    
    # Translating the fragment ======================================
    print "py: Translating fragments."
    msys.translate_subset(ssType.FRAG, [1], [0.0,0.0,30.0])
    msys.translate_subset(ssType.FRAG, [2], [0.0,0.0,20.0])
    # Translating and rotating the viewer ===========================
    print "py: Translating/Rotating the viewer."
    msys.rotate_viewer   (60.0, [0.0,-1.0,1.0])
    msys.translate_viewer([25.0,10.0,0.0])       
    
    # Open the GUI interface ########################################
    # - The update is needed if the script was executed from the GUI
    msys.update_viewer()
    # - This command is ignored if the script is executed from the GUI
    mview.open_gui()
else:
    print "py: Failed to retrieve system!"
print "py: Done with scripting!" 
  • rep_trans_rot
download files

Shapes

#####################################################################
#### This is an example of the MView python scripting interface #####
#
# It will open a input file and add shapes (2 planes) to the scene ##
#
print "py: Loading the atom&bond representation module."
mview.load_plugin_python_module("rep_plugin_atbond_mview")
# Opening a XYZ file <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
print ""
print "py: Importing the structure..."
# Using the predefined variable 'script_path'
mview.open_file("XYZ", [script_path + "/chloroform.xyz"])
# Asking for a pointer to the current molecule.......................
msys = mview.get_current_system()
if(msys):
    # Block the update of the scene after every command..............
    msys.set_scene_auto_update(False)
    # Setting the molecular representation <<<<<<<<<<<<<<<<<<<<<<<<<<
    # Removing the representation set by MView.......................
    msys.reset_representations()
    # Adding a new representation of the current fullerene...........
    Rid = msys.add_representation("rep_CPK", True)
    msys.manage_rep(Rid, "all")
    Rep_AtBo.set_object_quality(msys, Rid, objType.ATOM,  5 )
    Rep_AtBo.set_object_quality(msys, Rid, objType.BOND, 10 )
    # Adding the shapes to the scene <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    scale = 2.5
    rad   = 2.5
    alpha = 0.5 
    color_tip = [0.7,0.7,0.0]
    print "py: Adding shapes."
    # Adding Arrows =================================================
    my_arrow = Shapes.Arrow()
    my_arrow.set_position( [0.0,0.0,1.0] )
    my_arrow.set_baseTip_radius(0.15)
    my_arrow.set_length    (3.0*rad)
    my_arrow.get_tip_length(0.2)
    my_arrow.set_radius    (0.05, 0.05)
    my_arrow.set_radius    (0.05, 0.05)
    my_arrow.set_body_color([0.9,0.9,0.0])
    my_arrow.set_tip_color (color_tip)
    msys.add_arrow(my_arrow)
    # Adding the text ===============================================
    my_text = Shapes.Text()
    # Axis label.....................................................
    my_text.set_text ("z")
    my_text.translate([0.0,0.0,rad+0.5])
    my_text.set_color(color_tip)
    msys.add_text(my_text);
    # System name....................................................
    my_text.set_text ("Chloroform")
    my_text.reset_position()
    my_text.translate([0.5,-1.0,-rad-0.5])
    my_text.set_color([1.0,1.0,0.8])
    msys.add_text(my_text);
    # Setting the Planes ============================================
    my_plane = Shapes.Plane()
    my_plane.show_edges(True)
    my_plane.set_edges_thickness(4.0)
    my_plane.set_color      ([0.5,0.8,0.5, alpha])
    my_plane.set_edge_color ([0.0,0.8,0.0])
    my_plane.set_coords([-scale,0.0,0.0],[0.0,scale,0.0],[scale,0.0,0.0],[0.0,-scale,0.0])
    msys.add_plane(my_plane)
    # Setting the circles ===========================================
    my_circle = Shapes.Circle()
    my_circle.show_edges(True)
    my_circle.set_edges_thickness(5.0)
    my_circle.set_color      ([0.9,0.8,0.8, alpha])
    my_circle.set_edge_color ([0.8,0.5,0.0])
    my_circle.set( 70, rad, 0.0, 360.0, [0.0,0.0,0.0], [ 1.0 ,0.0,0.0], 0.0 )
    my_circle.scale(0.0, 0.0, 0.8)
    msys.add_circle(my_circle)    
    my_circle.set( 70, rad, 0.0, 360.0, [0.0,0.0,0.0], [ 1.0 ,1.7,0.0], 0.0 )
    my_circle.scale(0.0, 0.0, 0.8)
    msys.add_circle(my_circle)    
    my_circle.set( 70, rad, 0.0, 360.0, [0.0,0.0,0.0], [-0.58,1.0,0.0], 0.0 )
    my_circle.scale(0.0, 0.0, 0.8)
    msys.add_circle(my_circle)    
    # Setting the scene <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<   
    # Rotating & translating.........................................
    msys.rotate_viewer   (-90.0, [1.0,0.0,0.0])
    msys.rotate_viewer   ( 20.0, [0.0,1.0,0.0])
    msys.rotate_viewer   ( 25.0, [1.0,0.0,0.0])
    msys.translate_camera([0.0,0.0,-8.0])
    # Fog............................................................
    msys.set_fog_status (True)
    msys.set_fog_start  (10.0)
    msys.set_fog_end    (13.0)
    # - The update is needed if the script was executed from the GUI
    msys.update_viewer()
    
    # Open the GUI interface ########################################
    # - This command is ignored if the script is executed from the GUI
    mview.open_gui()
else:
    print "py: Failed to retrieve system!"
print "Done with scripting!" 
  • chloroform
download files


Surface

This is an example of script to be run in command line:

MView <script_file> <cubefile_name>

Script lines:

#####################################################################
#### This is an example of the MView python scripting interface #####
#
# Generating a surface from a gaussian cube file (command line) #####
#
import sys, argparse as arg
# Handling the arguments <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
parser = arg.ArgumentParser("Generates a surface from a gaussian cube file.\n")
parser.add_argument("script_name")
parser.add_argument("cube_input")
args = parser.parse_args()
# Loading the MView plugin modules <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
print "py: Loading the surface representation module."
mview.load_plugin_python_module("rep_plugin_surf_mview")
# Passing the cube file by argument
cube_file = args.cube_input
# Opening geometry of a cube file <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
mview.open_file("Cube Coordinates", [cube_file]);
# Retrieving a pointer to the current molecule <<<<<<<<<<<<<<<<<<<<<<
msys = mview.get_current_system();
if(msys):
    print "py: Structure imported..."
    
    # Block the update of the scene after every command..............
    msys.set_scene_auto_update(False)
    # Loading the grids from the cube files =========================
    print "py: Loading the grids..."
    mview.load_grid_file("Cube Grid", msys, [cube_file]);
    # Computing the surface =========================================
    print "py: Generating the surface..."
    Sid = msys.generate_surface_from_grid(0.05, 0, 0)
    Rep_Surf.set_surface_visibility(msys, Sid, True)
    # Updating the scene ============================================
    msys.set_scene_auto_update(True);
        
    # - The update is needed if the script was executed from the GUI
    msys.update_viewer()
    
    # Open the GUI interface ########################################
    # - This command is ignored if the script is executed from the GUI
    mview.open_gui()
else:
    print "py: Failed to retrieve system!"
print "py: Done with scripting!" 
  • surface_grid
download files

Trackers

#####################################################################
#### This is an example of the MView python scripting interface #####
#
# Imports a MD trajectory, inserts trackers and export their
# values for each frame of the trajectory.
#
# Opening a DCD trajectory file <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
print "py: Importing the structure..."
# Using the predefined variable 'script_path'
mview.open_file("DCD Trajectory", [script_path + "/dnd.dcd"])
# Asking for a pointer to the current system <<<<<<<<<<<<<<<<<<<<<<<<
msys = mview.get_current_system()
if(msys):
    print "py: Finished importing the structure..."
    # Adding a distance tracker =====================================
    print "py: Adding the trackers."
    list_at = [11, 96]
    msys.add_tracker(trkType.DIST, list_at)
    # Exporting the values tracked ==================================
    print "py: Exporting the distance between the atoms tracked."
    output_file = "./tmp_tracker.dat"
    if msys.export_trackers_from_frames ( output_file, trkType.DIST, True ) :
        print "py: Trackers exported sucessfully to ",output_file
    else:
        print "py: Trackers export was not possible!"              
else:
    print "py: Failed to retrieve system!"
print "py: Done with scripting!" 
download files

This webpage is currently under construction.

Free business joomla templates

Distributed by SiteGround