Drawing a Population of Molecules (Tedious Vector Style)

Posted on Jan 18, 2012

I’ve been working on an illustration for Wikipedia, which itself paves the way to a pictorial guide for acid-base chemistry, when I decided that it’s important to show a population of molecules in a non-photorealistic style.  The illustration is shown here, as well as the process for anyone interested in replicating it – the punch-line is that this is excessively cumbersome for what it generates, and there has got to be a better way of going about it.

The final illustration

There are three distinct steps in this process: a pre-Illustrator step to generate the correct 3D look, a symbol creation step in Illustrator, and the final compositing, which will be discussed in the next post.


The process begins with a chemical structure.  My software of choice is ChemDoodles for structure (at this writing, I think it’s superior to ChemDraw in everything, and for a fraction the price); you can use anything you want.


Structure editing in ChemDoodles

Save this as a .mol file.  And since you’re reading this workflow, you’re probably a vector user — ChemDoodle has excellent support for vector images.  You can get structures rendered in fancy styles and directly copy-paste into Illustrator; this worked even back at a certain time when OSX’s native vector export was FUBAR’ed.

The wide array of choices ChemDoodle gives you!

We’ll bring this file into Avogadro, an open source molecule viewer. It natively does molecular mechanics structure optimization, and that would work just fine for our simple aniline.  The other options prepare input decks for plugging into commercial (e.g., Gaussian) and open-source (e.g., NWChem) quantum mechanics software.

Molecule optimization in Avogadro

At this point we have a structure on our screen which is of correct geometry.  Select a good angle and export as “vector graphics PDF” (which despite its name, is not a true editable vector in Illustrator!)  Repeat for two different view-points; later on we will use rotate and scale these three views to simulate a chaotic mix of molecules.  I chose the following views.

Views chosen for vector export

Illustrator Symbol Creation

To quickly recap, at this point we have 3 different PDFs for the aniline molecule.  (To keep them organized, I named them “aniline-side”, “aniline-in”, and “aniline-out” after the position of the functional groups relative to the ring.)  Each of these PDFs contain a black background as well as a separate molecule object (as non-editable construct).  Delete the back black object to get to the molecule (and an axis by default):


The molecule as exported from Avogadro

Note that if your color space is not setup correctly, there will be difference in what is observed in Avogadro and what is seen in Illustrator.  To remove the axis, draw a rectangle over the molecule, select both objects, and use the top as a clipping mask:

I thought it was too dark for what I need, so I reduced the opacity to 75%.  This is entirely optional aesthetics.

Now we come to the ambient music listening time (i.e., mind-numbing bit).  To simulate the drawn appearance, we’ll lock (cmd-2) and trace over the figure with the Pen tool Illustrator Pen tool icon(shortcut P).  (I hope you like pulling Bezier curves.)  My recommendation here is to draw closed shapes for each atom, as that allow you the flexibility to control the fills with nice gradients or gradient meshes.  The way I go about generating this is by first drawing overlapping shapes (see left figure), then use the Shapebuilder tool (short-cut shift-M) to trim the edges.  You could use the pathfinder to do these boolean operations if you prefer – but I find the shapebuilder much more speedy for this task.

How the closed shapes were cut and built with the shape builder

To generate a true vector, fill these closed shapes with appropriate gradients, using colors sampled from the original.  Adjust the line style and stroke weight to your liking.  Note that you could skip this step and create a hybrid illustration by simply unlocking the original.  In any case, group all the elements together, and you almost have a new symbol!

One note before you’re done: unlike normal objects which are knockout by default, Adobe’s transparency model place transparent objects as overprints. If you want to avoid partially transparent shapes from overlapping with one another what you could do is to apply a new opaque white fill to the back of the group (see figure for settings).  This takes care of the the overlapping transparent pieces.  (I think there must be a more technically sound way of handling this using blend modes and knockout group options in the transparency panel, but I don’t know.)

Settings for group

When you’re happy with how your molecule looks, drag it into the symbol tray, and give it a name so you can tell what it is later:

Symbol tray in illustrator

This creates a symbol, which can be introduced in large numbers using the symbol spray tool SymbolSprayTool.jpg.

Repeat for other views and molecules as necessary.  The advantage of using symbols, as opposed to straight up manipulation, lies not only in the tools available in Illustrator for spraying and manipulating them, but also in the flexibility of being able to edit any one of the instances and have it propogated to all other instances automatically.

My completed collection of molecules, in charged and uncharged forms, looks like this:

5 species with multiple views

The “charges” are simple circles with a radial gradient and the appropriate parts cut out.  The next part is fun – it leverages all the grunt work to generate pretty pictures.

Illustrator Compositing

With the symbols now created, you can simulate a population of molecules easily using the Symbol Spray tool, then use the Sizer and Spinner to create variations within the sprayed molecules.  For organization’s sake, I created three separate layers:

(a) far away layer: scaled down molecules, low opacity (40%).

(b) mid layer: mid-sized molecules, ~75% opacity.

(c) close layer: big molecules, fully opaque.

(If you have tools like Phantasm CS, it might be worthwhile to slightly darken and desaturate the back layers to simulate atmospheric perspective.)

The details of the lower left panel:

Mixed populatino of molecules, vector styel

I was surprised at how quickly resolving these shapes drag down the computer: while theoretically there are no upper bound to the number of molecules thus sprayed on, my machine was stuttering with more molecules.  This is one reason why I thought there has to be a better way of going about it – there’s simply too much grunt work involved for what it is.

The real kicker came after I uploaded it to WikiCommons.  Wikipedia takes SVGs and not PDF/AI files; between a deficient renderer and limited expressiveness of SVG, the files thus generated was displayed incoherently.  (The troubles with transparency reminds me of Illustrator 9’s troubles back 10-12 years ago with PostScript and printing.)  At the end I had to rasterize the individual pieces of graphics before packaging it up into a SVG (so that the text remains easily translatable).  While vector shapes are gorgeous (and rational), it’s sadly the wrong tool for this particular task.  The better solution will be described in the sister post, How to Draw a Population of Molecules (3D Modeling Route).

  • Geoffrey Hutchison

    We’ve definitely talked about a cleaner “export to SVG or PDF” option in Avogadro, but there just hasn’t been the time. If you have suggestions about the style for such a render, we’re all ears. :-)

    • Jon

      Hi Geoffrey – thanks for popping by (and work on a useful and much needed FOSS). I agree that a clean “export to SVG/PDF” is a low priority event – I lurked on Avodadro-Dev for some time, and you guys certainly have bigger fishes to fry than this one.

      Style: 3D tended to be rendered as vector in a number of ways, most of them based on outlines because a true approximation of casted rays is difficult (and in any case, either generates unconvincing gradients, or bloated files as details are included). FYI, the industrial standard here is eRain’s Swift3D (I think they’re licensed for Autodesk Maya and Google Sketchup) – a few of their available options in terms of style can be found here: http://www.erain.com/products/swift3d/features/exportoptions/ In terms of open-source solution, the only one I know of is called PantoGraph (http://severnclaystudio.wordpress.com/bluebeard/), but I don’t really know how far along it is.

      Anyhow – the other reason I think this is low priority is because of the rapid recent developments in the Freestyle branch of Blender (content of that next post). Amongst other things, the main dev of freestyle (a line renderer) had indicated that a SVG export is on the to-do-list; with that I think the small population of “chemists who work with vector graphics” can get to what they need. And when I have a work-flow for that, I’ll certainly share it with the public :)