Difference between revisions of "Axes"

From PyMOLWiki
Jump to: navigation, search
m
(Axes which always stay in the lower left corner)
Line 1: Line 1:
 +
== Axes with text labels ==
 +
 
<source lang="python">
 
<source lang="python">
 
# axes.py
 
# axes.py
Line 26: Line 28:
 
</source>
 
</source>
  
 +
== Axes with nice cones ==
 +
 +
[[File:Axes_demo.png|300px|right]]
  
 
This script draws a simple cartesian coordinate system.
 
This script draws a simple cartesian coordinate system.
Line 48: Line 53:
 
</source>
 
</source>
  
Save the script and run it. Tested with Pymol 1.3.
+
== Axes which always stay in the lower left corner ==
[[File:Axes_demo.png|400px|left]]
+
 
 +
<source lang="python">
 +
from pymol import cmd
 +
from chempy import cpv
 +
 
 +
class PutCenterCallback(object):
 +
    prev_v = None
 +
 
 +
    def __init__(self, name, corner=0):
 +
        self.name = name
 +
        self.corner = corner
 +
        self.cb_name = cmd.get_unused_name('_cb')
 +
 
 +
    def load(self):
 +
        cmd.load_callback(self, self.cb_name)
 +
 
 +
    def __call__(self):
 +
        if self.name not in cmd.get_names('objects'):
 +
            cmd.delete(self.cb_name)
 +
            return
 +
 
 +
        v = cmd.get_view()
 +
        if v == self.prev_v:
 +
            return
 +
        self.prev_v = v
 +
 
 +
        t = v[12:15]
 +
 
 +
        if self.corner:
 +
            vp = cmd.get_viewport()
 +
            R_mc = [v[0:3], v[3:6], v[6:9]]
 +
            off_c = [0.15 * v[11] * vp[0] / vp[1], 0.15 * v[11], 0.0]
 +
            if self.corner in [2,3]:
 +
                off_c[0] *= -1
 +
            if self.corner in [3,4]:
 +
                off_c[1] *= -1
 +
            off_m = cpv.transform(R_mc, off_c)
 +
            t = cpv.add(t, off_m)
 +
 
 +
        z = -v[11] / 30.0
 +
        m = [z, 0, 0, t[0] / z, 0, z, 0, t[1] / z, 0, 0, z, t[2] / z, 0, 0, 0, 1]
 +
        cmd.set_object_ttt(self.name, m, homogenous=1)
 +
 
 +
def axes(name='axes'):
 +
    '''
 +
DESCRIPTION
 +
 
 +
    Puts coordinate axes to the lower left corner of the viewport.
 +
    '''
 +
    from pymol import cgo
 +
 
 +
    cmd.set('auto_zoom', 0)
  
 +
    w = 0.06 # cylinder width
 +
    l = 0.75 # cylinder length
 +
    h = 0.25 # cone hight
 +
    d = w * 1.618 # cone base diameter
 +
 +
    obj = [cgo.CYLINDER, 0.0, 0.0, 0.0,  l, 0.0, 0.0, w, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0,
 +
          cgo.CYLINDER, 0.0, 0.0, 0.0, 0.0,  l, 0.0, w, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0,
 +
          cgo.CYLINDER, 0.0, 0.0, 0.0, 0.0, 0.0,  l, w, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0,
 +
          cgo.CONE,  l, 0.0, 0.0, h+l, 0.0, 0.0, d, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0,
 +
          cgo.CONE, 0.0,  l, 0.0, 0.0, h+l, 0.0, d, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 1.0,
 +
          cgo.CONE, 0.0, 0.0,  l, 0.0, 0.0, h+l, d, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0]
 +
 +
    PutCenterCallback(name, 1).load()
 +
    cmd.load_cgo(obj, name)
 +
 +
cmd.extend('axes', axes)
 +
</source>
  
 
[[Category:Script_Library|Axes]]
 
[[Category:Script_Library|Axes]]
 
[[Category:Math_Scripts|Axes]]
 
[[Category:Math_Scripts|Axes]]

Revision as of 09:08, 30 August 2012

Axes with text labels

# axes.py
from pymol.cgo import *
from pymol import cmd
from pymol.vfont import plain

# create the axes object, draw axes with cylinders coloured red, green,
#blue for X, Y and Z

obj = [
   CYLINDER, 0., 0., 0., 10., 0., 0., 0.2, 1.0, 1.0, 1.0, 1.0, 0.0, 0.,
   CYLINDER, 0., 0., 0., 0., 10., 0., 0.2, 1.0, 1.0, 1.0, 0., 1.0, 0.,
   CYLINDER, 0., 0., 0., 0., 0., 10., 0.2, 1.0, 1.0, 1.0, 0., 0.0, 1.0,
   ]

# add labels to axes object (requires pymol version 0.8 or greater, I
# believe

cyl_text(obj,plain,[-5.,-5.,-1],'Origin',0.20,axes=[[3,0,0],[0,3,0],[0,0,3]])
cyl_text(obj,plain,[10.,0.,0.],'X',0.20,axes=[[3,0,0],[0,3,0],[0,0,3]])
cyl_text(obj,plain,[0.,10.,0.],'Y',0.20,axes=[[3,0,0],[0,3,0],[0,0,3]])
cyl_text(obj,plain,[0.,0.,10.],'Z',0.20,axes=[[3,0,0],[0,3,0],[0,0,3]])

# then we load it into PyMOL
cmd.load_cgo(obj,'axes')

Axes with nice cones

Axes demo.png

This script draws a simple cartesian coordinate system.

from pymol.cgo import *
from pymol import cmd

w = 0.06 # cylinder width 
l = 0.75 # cylinder length
h = 0.25 # cone hight
d = w * 1.618 # cone base diameter

obj = [CYLINDER, 0.0, 0.0, 0.0,   l, 0.0, 0.0, w, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0,
       CYLINDER, 0.0, 0.0, 0.0, 0.0,   l, 0.0, w, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0,
       CYLINDER, 0.0, 0.0, 0.0, 0.0, 0.0,   l, w, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0,
       CONE,   l, 0.0, 0.0, h+l, 0.0, 0.0, d, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 
       CONE, 0.0,   l, 0.0, 0.0, h+l, 0.0, d, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 
       CONE, 0.0, 0.0,   l, 0.0, 0.0, h+l, d, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0]

cmd.load_cgo(obj, 'axes')

Axes which always stay in the lower left corner

from pymol import cmd
from chempy import cpv

class PutCenterCallback(object):
    prev_v = None

    def __init__(self, name, corner=0):
        self.name = name
        self.corner = corner
        self.cb_name = cmd.get_unused_name('_cb')

    def load(self):
        cmd.load_callback(self, self.cb_name)

    def __call__(self):
        if self.name not in cmd.get_names('objects'):
            cmd.delete(self.cb_name)
            return

        v = cmd.get_view()
        if v == self.prev_v:
            return
        self.prev_v = v

        t = v[12:15]

        if self.corner:
            vp = cmd.get_viewport()
            R_mc = [v[0:3], v[3:6], v[6:9]]
            off_c = [0.15 * v[11] * vp[0] / vp[1], 0.15 * v[11], 0.0]
            if self.corner in [2,3]:
                off_c[0] *= -1
            if self.corner in [3,4]:
                off_c[1] *= -1
            off_m = cpv.transform(R_mc, off_c)
            t = cpv.add(t, off_m)

        z = -v[11] / 30.0
        m = [z, 0, 0, t[0] / z, 0, z, 0, t[1] / z, 0, 0, z, t[2] / z, 0, 0, 0, 1]
        cmd.set_object_ttt(self.name, m, homogenous=1)

def axes(name='axes'):
    '''
DESCRIPTION

    Puts coordinate axes to the lower left corner of the viewport.
    '''
    from pymol import cgo

    cmd.set('auto_zoom', 0)

    w = 0.06 # cylinder width
    l = 0.75 # cylinder length
    h = 0.25 # cone hight
    d = w * 1.618 # cone base diameter

    obj = [cgo.CYLINDER, 0.0, 0.0, 0.0,   l, 0.0, 0.0, w, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0,
           cgo.CYLINDER, 0.0, 0.0, 0.0, 0.0,   l, 0.0, w, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0,
           cgo.CYLINDER, 0.0, 0.0, 0.0, 0.0, 0.0,   l, w, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0,
           cgo.CONE,   l, 0.0, 0.0, h+l, 0.0, 0.0, d, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0,
           cgo.CONE, 0.0,   l, 0.0, 0.0, h+l, 0.0, d, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 1.0,
           cgo.CONE, 0.0, 0.0,   l, 0.0, 0.0, h+l, d, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0]

    PutCenterCallback(name, 1).load()
    cmd.load_cgo(obj, name)

cmd.extend('axes', axes)