1""" simple demo for using the PyMol RPC server 2 3 Author: Greg Landrum (Landrum@RationalDiscovery.com) 4 Created: February 2004 5 $LastChangedDate$ 6 License: PyMol 7 Requires: 8 - a python xmlrpclib distribution containing the SimpleXMLRPCServer 9 module (1.0 or greater should be fine) 10 RD Version: $Rev$ 11""" 12try: 13 import xmlrpclib 14except ImportError: 15 import xmlrpc.client as xmlrpclib 16 17def startServer(host='localhost',startPort=9123,nToTry=5): 18 done = 0 19 offset = 0 20 while offset < nToTry: 21 c = xmlrpclib.Server('http://%s:%d'%(host,startPort+offset)) 22 try: 23 c.ping() 24 except: 25 print('Failed on port %d, trying another'%(startPort+offset)) 26 offset = offset + 1 27 else: 28 done = 1 29 break 30 if done: 31 return c,startPort+offset 32 else: 33 return None,-1 34 35 36molBlock="""3d.mol 37 38 39 27 28 0 0 0 1 V2000 40 1.7032 0.2061 -1.4783 C 0 0 0 0 0 41 -1.1754 1.1362 0.5252 C 0 0 0 0 0 42 -0.8291 -0.4052 -1.6110 C 0 0 0 0 0 43 -1.2900 -0.2398 -0.1445 C 0 0 0 0 0 44 1.7764 0.1474 0.0609 C 0 0 0 0 0 45 1.2054 1.3597 0.8068 C 0 0 0 0 0 46 -0.9224 -1.4043 0.7840 C 0 0 0 0 0 47 1.4274 -1.2288 0.6468 C 0 0 0 0 0 48 0.2875 -1.2046 1.4805 O 0 0 0 0 0 49 0.3400 0.4288 -2.1474 C 0 0 0 0 0 50 0.0057 1.8686 0.2581 O 0 0 0 0 0 51 2.1441 -0.7361 -1.8853 H 0 0 0 0 0 52 2.3779 1.0240 -1.8348 H 0 0 0 0 0 53 -1.3300 1.0780 1.6257 H 0 0 0 0 0 54 -1.9986 1.7818 0.1317 H 0 0 0 0 0 55 -1.6979 -0.1528 -2.2717 H 0 0 0 0 0 56 -0.6158 -1.4800 -1.8185 H 0 0 0 0 0 57 -2.4054 -0.3977 -0.2240 H 0 0 0 0 0 58 2.8858 0.2153 0.2548 H 0 0 0 0 0 59 1.9363 2.1991 0.7014 H 0 0 0 0 0 60 1.0950 1.2014 1.9010 H 0 0 0 0 0 61 -0.9058 -2.3887 0.2658 H 0 0 0 0 0 62 -1.6992 -1.5000 1.5819 H 0 0 0 0 0 63 2.2587 -1.5596 1.3170 H 0 0 0 0 0 64 1.3356 -2.0264 -0.1211 H 0 0 0 0 0 65 0.4626 0.1900 -3.2341 H 0 0 0 0 0 66 0.0668 1.5081 -2.1636 H 0 0 0 0 0 67 1 5 1 1 0 0 68 1 10 1 0 0 0 69 1 12 1 0 0 0 70 1 13 1 0 0 0 71 2 4 1 0 0 0 72 2 11 1 0 0 0 73 2 14 1 1 0 0 74 2 15 1 0 0 0 75 3 4 1 1 0 0 76 3 10 1 0 0 0 77 3 16 1 6 0 0 78 3 17 1 0 0 0 79 4 7 1 1 0 0 80 4 18 1 0 0 0 81 5 6 1 1 0 0 82 5 8 1 0 0 0 83 5 19 1 0 0 0 84 6 11 1 0 0 0 85 6 20 1 0 0 0 86 6 21 1 1 0 0 87 7 9 1 1 0 0 88 7 22 1 6 0 0 89 7 23 1 1 0 0 90 8 9 1 1 0 0 91 8 24 1 1 0 0 92 8 25 1 6 0 0 93 10 26 1 6 0 0 94 10 27 1 0 0 0 95M END 96""" 97 98if __name__=='__main__': 99 import sys 100 serv,port = startServer() 101 if serv is not None: 102 print('connected to PyMol rpc-server on port %d'%port) 103 else: 104 print('unable to connect to PyMol') 105 sys.exit(-1) 106 serv.loadMolBlock(molBlock,'sample-mol') 107 serv.set('sphere_scale',0.25,'sample-mol') 108 serv.do('show sticks;show spheres') 109 serv.sphere((.28,-1.2,1.48),.5,(1,0,1),'demo') 110 serv.sphere((0,1.87,.26),.5,(1,0,1),'demo') 111 serv.cylinder((.28,-1.2,1.48),(0,1.87,.26),.1,(.5,0,.5),'demo') 112 serv.zoom() 113 114