forked from Pymol-Scripts/Pymol-script-repo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
perp_maker.py
179 lines (147 loc) · 5.55 KB
/
perp_maker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
'''
See more here: http://www.pymolwiki.org/index.php/perp_maker
####################################################################################
#
# perp_maker.py: Creates perpendicular planes.
# =============
#
# Nothing to do with cops. Given a simple PyMol scene, attempts to
# create a CGO background triangle perpendicular to the vector created - which is
# parallel to the line segment drawn through the camera point and current center of
# mass - as obtained by "get_position," or "get_view."
#
# @COPYRIGHT: Jason Vertrees (C), 2005-2007
# @LICENSE: Released under GPL:
# 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
#
#
#
#
# To use: Load your scene. Orient the scene as you wish. Run the script.
# Could it be any simpler?!
#
# The TTT Matrix has to be the identity, do achieve this result. So,
# run the following:
# -- 'reset'
# -- then orient your molecule as desired using the EDITING features!
# -- before running this script, make sure 'get_view' shows the identity
# -- matrix for the first 9 elements.
# -- then run the script
#
'''
from __future__ import print_function
import pymol
import random
from pymol.cgo import *
############################################################
#
# Methods
#
############################################################
#
# Given the viewVector and center, creates a random sized plane
# perpendicular to the viewVector through the origin. It is then
# the next step's responsibility to move the plane back some so
# it dosen't cut the molecule/scene in half.
#
def getPPlane(viewVector, center, side_length=100):
"""Returns a 3-tuple of 3D points representing the perp. plane."""
# for reproduceable testing
# random.seed(10)
#
# The formula for a plane with our chacteristics is defined by
#
# A(x - x') + B(y - y') + C(y - y') + D = 0, where
# A, B and C are not all zero coefficients in the vector
# Ai + Bj + Ck such that the plane is perpendicular to this
# vector; x, y, and z are points on the plane; x', y', and z'
# are the coordiates through which the plane shall run.
#
# This is fool-ass. Gotta' be a better way to do this.
# Declaring that rVal is a 3-Tuple.
rVal = [[], [], [], [], [], []]
# Compose two triangles into a square.
# Never learned any GFX coding, so I'm sure there's something
# better than this; but, this works.
for i in range(0, 6):
if (i == 0) or (i == 5):
x = -side_length + center[0]
y = -side_length + center[1]
elif (i == 1):
x = -side_length + center[0]
y = side_length + center[1]
elif (i == 2) or (i == 3):
x = side_length + center[0]
y = side_length + center[1]
elif (i == 4):
x = side_length + center[0]
y = -side_length + center[1]
if (viewVector[2] != 0):
z = -(((viewVector[0] * (x - center[0])) - (viewVector[1] * (y - center[1]))) /
viewVector[2]) + center[2]
else:
print("Z-component of viewVector is zero. Now, I need a nonzero value here \
so I'm just making one up. :)")
z = random.randint(-200, 200)
rVal[i] = [x, y, z]
return rVal
############################################################
#
# End methods
#
############################################################
def perp_maker(name='pPlane', quiet=1):
'''
DESCRIPTION
Creates perpendicular planes
'''
quiet = int(quiet)
# First, get the center and camera locations
view = cmd.get_view()
camera = [view[9], view[10], view[11]]
center = [view[12], view[13], view[14]]
# Sanity check
if not quiet:
print("Camera is: " + str(camera))
print("Center is: " + str(center))
# Create the vector through the two points directed
# from the camera to the center - the viewVector
viewVector = [center[0] - camera[0],
center[1] - camera[1],
center[2] - camera[2]]
if not quiet:
print("ViewVector is: " + str(viewVector))
# Create the plane perpendicular to the viewVector
# running through the origin
pPlane = getPPlane(viewVector, center, side_length=100)
if not quiet:
print("Plane points calculated as: " + str(pPlane))
# now create the CGO and load from the points
obj = [
BEGIN, TRIANGLES,
COLOR, 0.2, 0.4, 1,
VERTEX, pPlane[0][0], pPlane[0][1], pPlane[0][2],
VERTEX, pPlane[1][0], pPlane[1][1], pPlane[1][2],
VERTEX, pPlane[2][0], pPlane[2][1], pPlane[2][2],
VERTEX, pPlane[3][0], pPlane[3][1], pPlane[3][2],
VERTEX, pPlane[4][0], pPlane[4][1], pPlane[4][2],
VERTEX, pPlane[5][0], pPlane[5][1], pPlane[5][2],
END
]
cmd.load_cgo(obj, name)
cmd.set_view(view)
if __name__ in ['pymol', '__main__']:
print('__name__ =', __name__)
perp_maker(quiet=0)
cmd.extend('perp_maker', perp_maker)
# vi:expandtab:smarttab