wtorek, stycznia 01, 2008

New Pymol face

New year = new ideas. Since I'd a lot of free time I'd spend some of it with coding in Python. As I mentioned before I'm working on a plugin for Pymol to enable some molecule glowing effects. I had some trouble using regex (regular expressions) in Python, but finally I got the hang out of it. After some digging in Pymol's and Python's manual pages I got this idea: "Wouldn't it be cool if I could load some 3D-Models into Pymol?". As you see on the image above I did it. I've chosen the *.ASC modelfile to do this. Why? Simply because I'm a lazy pig and it was really easy to implement. If you want to play a bit find yourself some *.ASC files and load them into pymol. Here's the script I wrote:


from pymol.cgo import *
from pymol import cmd
import re

# List of *.ASC files to load
files = ['face','jet','knot','duck','tor','dolphin','tore']

for file in files:

 f = open(file+'.asc','r')
 for line in f.readlines():
  p1 = re.compile('Vertex \d+:\s+X:\s+(?P<VerX>-?\d+\.?\d*)\s+Y:\s+(?P<VerY>-?\d+\.?\d*)\s+Z:\s+(?P<VerZ>-?\d+\.?\d*)')
  p2 = re.compile('Face \d+:\s+A:(?P<FA>\d+)\s+B:(?P<FB>\d+)\s+C:(?P<FC>\d+)')
  m = p1.match(line)
  if m:
   vertex = [float(m.group('VerX')),float(m.group('VerY')),float(m.group('VerZ'))]
  m2 = p2.match(line)
  if m2:
   face = [int(m2.group('FA')),int(m2.group('FB')),int(m2.group('FC'))]
 # Model building
 print "Vertexes: %s" % (len(vertexes))
 print "Faces: %s" % (len(faces))
 #model.extend( [COLOR,    1.000,    1.000,    0.000])
 #for vertex in vertexes:
 # model.extend([SPHERE,   vertex[0],   vertex[1],   vertex[2],1.00])
 model.extend( [COLOR,    0.000,    0.500,    1.000])
 model.extend( [BEGIN, TRIANGLES])
 for face in faces:
  model.extend([VERTEX, vertexes[face[0]][0], vertexes[face[0]][1], vertexes[face[0]][2] ])
  model.extend([VERTEX, vertexes[face[1]][0], vertexes[face[1]][1], vertexes[face[1]][2] ])
  model.extend([VERTEX, vertexes[face[2]][0], vertexes[face[2]][1], vertexes[face[2]][2] ])
 model.extend( [END])
for file in files:
# Animation
cmd.mset("1 x240")
I've found some ASC-models at www.koders.com, but you can generate these files in 3DS Max. The script supports only one object per file. Place the models in the same directory as the script and edit the files list. Then run the script from pymol or create a PML-script if you like double clicking ];)


run load_asc.py
Man, Open Source really kicks ass!

Brak komentarzy: