HP Clock

Thursday, September 5, 2013

Modeling Molecules

Most of us remember making the models of molecules by snapping together multicolored plastic spheres.

Inspired by this childhood experience I decided to attempt the virtual version using Python and Blender.  The secondary motive was to gain some proficiency in reading and parsing data from a text file using Python and then use Blender to do the rendering of the molecule.  The first thing I needed was some data to parse using Python.  I was happy to discover that the data I needed for the molecules was available at the Protein Data Bank.  Initial development and parsing of the text data was done using IDLE, Python's Integrated Development Environment.  After I was satisfied with my ability to read and parse text data I moved my script into Blender.  Here you can see Blender in the scripting mode.

You can see from the screen capture where Line 22 of the script points to the text file that contains the molecule data.  In this case I’m drawing a picture of an LSD molecule.   After running the script I wanted to create a nice 3D image of my molecule.  Using Blender in the Default mode I set up a stage for the molecule to be rendered on.  Here is how the image of the LSD molecule turned out.

If you want to try this on your own you can download the scripts and text file data from this web page.  In the example on this page I created a complete 3D model and animation of a short piece of DNA.  All of it can be downloaded from this URL.

This page also contains links to the Protein Data Bank, as well as information about the color and size of the individual atoms.  I’ll try to update the page adding CSV files of different molecules when I create them from the protein database files.  You can also find the code on GIT Hub using this link