def __call__(self): if self.name not in cmd.get_names('objects'): import threading threading.Thread(None, cmd.delete, args=(self.cb_name,)).start() 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 __call__(self): if self.name not in cmd.get_names('objects'): import threading threading.Thread(None, cmd.delete, args=(self.cb_name,)).start() 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, 0, 0, z, 0, 0, 0, 0, z, 0, t[0] / z, t[1] / z, t[2] / z, 1] cmd.set_object_ttt(self.name, m)
def torus(center=(0., 0., 0.), normal=(0., 0., 1.), radius=1., color='', cradius=.25, samples=20, csamples=20): ''' Generate and return a torus CGO with given center, normal and ring radius. ''' from math import cos, sin, pi if color and isinstance(color, str): color = list(cmd.get_color_tuple(color)) obj = [] axis = cpv.cross_product(normal, (0., 0., 1.)) angle = -cpv.get_angle(normal, (0., 0., 1.)) matrix = cpv.rotation_matrix(angle, cpv.normalize(axis)) obj_vertex = lambda x, y, z: obj.extend([VERTEX] + cpv.add(center, cpv.transform(matrix, [x, y, z]))) obj_normal = lambda x, y, z: obj.extend([NORMAL] + cpv.transform(matrix, [x, y, z])) r = radius cr = cradius rr = 1.5 * cr dv = 2 * pi / csamples dw = 2 * pi / samples v = 0.0 w = 0.0 while w < 2 * pi: v = 0.0 c_w = cos(w) s_w = sin(w) c_wdw = cos(w + dw) s_wdw = sin(w + dw) obj.append(BEGIN) obj.append(TRIANGLE_STRIP) if color: obj.append(COLOR) obj.extend(color) while v < 2 * pi + dv: c_v = cos(v) s_v = sin(v) c_vdv = cos(v + dv) s_vdv = sin(v + dv) obj_normal( (r + rr * c_v) * c_w - (r + cr * c_v) * c_w, (r + rr * c_v) * s_w - (r + cr * c_v) * s_w, (rr * s_v - cr * s_v)) obj_vertex( (r + cr * c_v) * c_w, (r + cr * c_v) * s_w, cr * s_v) obj_normal( (r + rr * c_vdv) * c_wdw - (r + cr * c_vdv) * c_wdw, (r + rr * c_vdv) * s_wdw - (r + cr * c_vdv) * s_wdw, rr * s_vdv - cr * s_vdv) obj_vertex( (r + cr * c_vdv) * c_wdw, (r + cr * c_vdv) * s_wdw, cr * s_vdv) v += dv obj.append(END) w += dw return obj
def set_view(view, center, trans, volume, persp): ''' view is their 4x4 center is the centor of rotation in tranformed coordinates ''' view = list(view) # print view[0:4] # print view[4:8] # print view[8:12] # print view[12:16] # print "\nmaestro_center: %8.3f %8.3f %8.3f"%tuple(center) # print "maestro_transl: %8.3f %8.3f %8.3f"%tuple(trans) mat = [ view[0:3], view[4:7], view[8:11] ] tmp = cpv.sub(center,view[12:15]) pymol_center = cpv.transform(mat,tmp) # print "volumeX: %8.3f %8.3f"%(tuple(volume[0:2])) # print "volumeY: %8.3f %8.3f"%(tuple(volume[2:4])) # print "volumeZ: %8.3f %8.3f"%(tuple(volume[4:6])) # print "pymol_center: %8.3f %8.3f %8.3f"%tuple(pymol_center) # print persp if persp<0.0: pymol_ortho = 1.0 else: pymol_ortho = 0.0 if pymol_ortho==0.0: fov = 1 + 100*(persp - 1.0)/3.0 cmd.set('field_of_view',fov) fov = (math.pi * float(cmd.get("field_of_view")) / 180.0) # unclear why we have to multiple by 1.17 to get the correct look camera_dist = 1.17 * (abs(volume[2] - volume[3])/2.0)/math.atan(fov/2.0) vol_center = [ (volume[0]+volume[1])/2.0, (volume[2]+volume[3])/2.0, (volume[4]+volume[5])/2.0 ] pymol_objective = [ trans[0] + center[0] - vol_center[0], trans[1] + center[1] - vol_center[1], -camera_dist ] # print "pymol_objective: %8.3f %8.3f %8.3f"%tuple(pymol_objective) pymol_front = center[2] + trans[2] - volume[4] - pymol_objective[2] pymol_back = center[2] + trans[2] - volume[5] - pymol_objective[2] pymol_view = cmd.get_view() cur_view = ( view[0:3] + view[4:7] + view[8:11] + pymol_objective + pymol_center + [ pymol_front, pymol_back, pymol_ortho ] ) # print cur_view cmd.set_view(tuple(cur_view))
def torus(center=(0., 0., 0.), normal=(0., 0., 1.), radius=1., color='', cradius=.25, samples=20, csamples=20, _self=cmd): ''' Generate and return a torus CGO with given center, normal and ring radius. ''' from math import cos, sin, pi if color and isinstance(color, str): color = list(_self.get_color_tuple(color)) obj = [] axis = cpv.cross_product(normal, (0., 0., 1.)) angle = -cpv.get_angle(normal, (0., 0., 1.)) matrix = cpv.rotation_matrix(angle, cpv.normalize(axis)) obj_vertex = lambda x, y, z: obj.extend([VERTEX] + cpv.add(center, cpv.transform(matrix, [x, y, z]))) obj_normal = lambda x, y, z: obj.extend([NORMAL] + cpv.transform(matrix, [x, y, z])) r = radius cr = cradius rr = 1.5 * cr dv = 2 * pi / csamples dw = 2 * pi / samples v = 0.0 w = 0.0 while w < 2 * pi: v = 0.0 c_w = cos(w) s_w = sin(w) c_wdw = cos(w + dw) s_wdw = sin(w + dw) obj.append(BEGIN) obj.append(TRIANGLE_STRIP) if color: obj.append(COLOR) obj.extend(color) while v < 2 * pi + dv: c_v = cos(v) s_v = sin(v) c_vdv = cos(v + dv) s_vdv = sin(v + dv) obj_normal( (r + rr * c_v) * c_w - (r + cr * c_v) * c_w, (r + rr * c_v) * s_w - (r + cr * c_v) * s_w, (rr * s_v - cr * s_v)) obj_vertex( (r + cr * c_v) * c_w, (r + cr * c_v) * s_w, cr * s_v) obj_normal( (r + rr * c_vdv) * c_wdw - (r + cr * c_vdv) * c_wdw, (r + rr * c_vdv) * s_wdw - (r + cr * c_vdv) * s_wdw, rr * s_vdv - cr * s_vdv) obj_vertex( (r + cr * c_vdv) * c_wdw, (r + cr * c_vdv) * s_wdw, cr * s_vdv) v += dv obj.append(END) w += dw return obj
def obj_normal(x, y, z): return [cgo.NORMAL] + cpv.transform(matrix, [x, y, z])
def obj_vertex(x, y, z): return [cgo.VERTEX] + cpv.add(self.center.array(), cpv.transform(matrix, [x, y, z]))