Make Figures

From PyMOLWiki
Jump to navigation Jump to search

Overview

This script will aid you in making publication quality figures for the currently displayed scene. It understands a variety of preset "modes" and sizes.


Usage

make_figure filename [, mode] [, size (default=900 pixels)] [,opaque]

The parameters are:

filename : The name of the resulting image file.

The extension .png is added automatically.


NOTE: if no further arguments are given, the script generates quick figures for general use:

  • figures are not ray-traced
  • TWO figures at 300 x 300 px, 72 dpi
  • views are rotated 180° about y (front and back view)
  • _front_quick and _back_quick are appended to the filename


mode : Type of figure desired. Possible values are as follows:


single = "single view"
  • one ray-traced 300 dpi figure of the current view


fb = "front and back view"
  • TWO ray-traced 300 dpi figures
  • views are rotated 180° about y
  • _front and _back are appended to the filename


sides = "four side views"
  • FOUR ray-traced 300 dpi figures
  • view is incrementally rotated by 90° about y
  • _1, _2, _3, and _4 are appended to the filename


stereo = "stereoview"
  • TWO ray-traced 300 dpi figures
  • views are shifted by +/- 3 degrees
  • image dimensions are fixed at 750 x 750 pixels (size arguments are ignored)
  • _L and _R are appended to the filename
  • the output files are meant to be combined side by side to generate a stereo image


size : Size of the figure(s) in pixels or in "panels"

  • DEFAULT = 900 pixels if a mode is specified
  • if size is 12 or smaller, the script interprets it as a number of "panels" to make.
  • panel sizes in pixels are hardcoded in the source but can easily be modified.


opaque : Create an opaque background.

  • By default the figure background is 100% transparent.


Examples

#quick 'n' dirty front and back views of the scene
#300x300 pixels at 72 dpi, transparent background
# filenames will be output_front_quick.png and output_back_quick.png
make_figure output

# one ray-traced PNG file 975x975 pixels at 300dpi, with opaque background
# filename will be output.png
make_figure output, single, 975, opaque

# two panels (1350x1350 px each at 300dpi) of the "front" and "back" view on transparent background
# filenames will be output_front.png and output_back.png
make_figure output, fb, 2

# four panels (900x900 px each) where the view is incrementally rotated by 90° about y, transparent background
# filenames will be output_1.png, output_2.png, output_3.png and output_4.png
make_figure output, sides,4

#stereoview of the current scene with opaque background
#size is fixed to 2x 750x750px
# filenames will be output_L.png and output_R.png
make_figure output, stereo, opaque


The Code

#make_figure v.3.0
#Copyleft Martin Christen, 2010

from pymol import cmd
def make_figure(output='', mode='',size=900,opaque='transparent'):

	"""

AUTHOR

	Martin Christen


DESCRIPTION

	"make_figure" creates publication-quality figures of the current scene.
	It understands several predefined "modes" and sizes.


USAGE

	make_figure filename [, mode] [, size (default=900 pixels)] [,opaque]


ARGUMENTS

	mode = string: type of desired figure (single, fb, sides, stereo or -nothing-)
	size = integer: size of the figure in pixels OR # panels (if <= 12)
	opaque = specify an opaque background.
	         By default, the script makes the background transparent.
EXAMPLES

	make_figure output
	make_figure output, single, 975, opaque
	make_figure output, fb, 2
	make_figure output, sides,4
	make_figure output, stereo


NOTES

	"single" mode makes a single 300 dpi figure
	
	"fb" mode makes TWO 300 dpi figure
	("front" and "back", rotating by 180 degrees about y)
	
	"sides" mode makes FOUR 300 dpi figures
	("front" "left" "right" and back, rotating by 90 degrees clockwise about y)
	
	"stereo" generates two 300 dpi, 750 px figures
	("L" and "R", to be combined as a stereo image)
	If you specify the stereo mode, the size argument is IGNORED.
		
	If no mode argument is given, the script generates quick figures
	for general	use: TWO figures (front and back) at 300 x 300 px, 72 dpi.
	
	Size is interpreted as pixels, except if the number is ridiculously small
	(<=12),	in which case the script as "number of panels" to make.

	Edit the script manually to define corresponding values.
	
	"""

	#define sizes here (in pixels)
	panel1 = 1800
	panel2 = 1350
	panel3 = 900
	panel4 = 900
	panel5 = 750
	panel6 = 750
	panel7 = 675
	panel8 = 675
	panel9 = 600
	panel10 = 585
	panel11 = 585
	panel12 = 585
	
	#verify size is an integer number and convert to pixels
	size = int(size)
	if size > 12:
		pixels = size
	
	elif size == 1:
		pixels = panel1
	
	elif size == 2:
		pixels = panel2
	
	elif size == 3:
		pixels = panel3
	
	elif size == 4:
		pixels = panel4
	
	elif size == 5:
		pixels = panel5
	
	elif size == 6:
		pixels = panel6
	
	elif size == 7:
		pixels = panel7
	
	elif size == 8:
		pixels = panel8
	
	elif size == 9:
		pixels = panel9
	
	elif size == 10:
		pixels = panel10
	
	elif size == 11:
		pixels = panel11
	
	elif size == 3:
		pixels = panel12

	#change background
	cmd.unset('opaque_background')
	if opaque == 'opaque':
		cmd.set('opaque_background')
	
	#apply mode
	if output == '':
		print 'no output filename defined\n'
		print 'try: \'make_figure filename\''
		return -1
		# abort if no output file name given

	if mode =='':
		cmd.set('surface_quality',1)
		cmd.set('opaque_background')
		cmd.png(output+"_back_quick",300,300,dpi=72)
		cmd.turn('y',180)
		cmd.png(output+"_front_quick",300,300,dpi=72)
		cmd.turn('y',180)
		cmd.set('surface_quality',0)
		# make front and back figures for quick mode

	elif mode == 'single':
		cmd.set('surface_quality',1)
		cmd.set('ray_shadow',0)
		cmd.ray(pixels, pixels)
		cmd.png(output, dpi=300)
		cmd.set('surface_quality',0)
		# make a figure for single mode
		
	elif mode == 'fb':
		cmd.set('surface_quality',1)
		cmd.set('ray_shadow',0)
		cmd.ray(pixels, pixels)
		cmd.png(output+"_front", dpi=300)
		cmd.turn('y',180)
		cmd.ray(pixels, pixels)
		cmd.png(output+"_back", dpi=300)
		cmd.turn('y',180)
		cmd.set('surface_quality',0)
		# make front and back figures for single mode

	elif mode == 'sides':
		cmd.set('surface_quality',1)
		cmd.set('ray_shadow',0)
		cmd.ray(pixels, pixels)
		cmd.png(output+"_1", dpi=300)
		cmd.turn('y',90)
		cmd.ray(pixels, pixels)
		cmd.png(output+"_2", dpi=300)
		cmd.turn('y',90)
		cmd.ray(pixels, pixels)
		cmd.png(output+"_3", dpi=300)
		cmd.turn('y',90)
		cmd.ray(pixels, pixels)
		cmd.png(output+"_4", dpi=300)
		cmd.turn('y',90)
		cmd.set('surface_quality',0)
		# make front and back figures for single mode
		
	elif mode == 'stereo':
		cmd.set('surface_quality',1)
		cmd.set('ray_shadow',0)
		cmd.ray(750, 750, angle=-3)
		cmd.png(output+"_R", dpi=300)
		cmd.ray(750, 750, angle=3)
		cmd.png(output+"_L", dpi=300)
		# make stereo figure (for more control use stereo_ray)
		
cmd.extend('make_figure',make_figure)