Command line rendering of chemical structures in Blender
go back to Main Page, Group Pages, Núria López and Group, Scripts_for_VASP
in preparation, absolute beginner friendly: Here is a procedure on how to take a CONTCAR and convert it to high quality PNG image. find files in teklahome: /home/efako/BLENDER
Installing Blender[edit]
- Blender is installed by downloading the code (https://www.blender.org/download/) unpacking it to a path of your choosing.
- In the program folder you will find an executable called blender
example: /usr/bin/blender-2.80-linux-glibc217-x86_64
- To launch blender you will execute (if you use the path from previous line)
/usr/bin/blender-2.80-linux-glibc217-x86_64/blender
- you do not need to launch blender at this point
Preparing the structure[edit]
I am assuming that you are more or less familiar with the use of vtst-scripts (http://theory.cm.utexas.edu/vtsttools/scripts.html), openbabel (http://openbabel.org/wiki/Main_Page) or just check out Scripts for VASP
- Let's start from the same spot, do this:
$ mkdir ~/blender_test $ cd ~/blender_test
move the CONTCAR you want to render to ~/blender_test
$ ls CONTCAR
- Let's use a HCl molecule as our subject:
$ cat CONTCAR HCl 1.0 10.0000000000 10.0000000000 10.0000000000 10.0000000000 10.0000000000 10.0000000000 10.0000000000 10.0000000000 10.0000000000 H Cl 1 1 Selective Cartesian +0.1000000000 +1.0000000000 +0.1000000000 T T T +1.0000000000 +0.1000000000 +0.1000000000 T T T
- if you use openbabel:
$babel CONTCAR hcl.pdb $cat hcl.pdb COMPND HCl AUTHOR GENERATED BY OPEN BABEL 2.4.1 CRYST1 17.321 17.321 17.321 0.03 0.03 0.03 P 1 1 HETATM 1 H UNL 1 0.100 1.000 0.100 1.00 0.00 H HETATM 2 CL UNL 1 1.000 0.100 0.100 1.00 0.00 Cl CONECT 1 2 CONECT 2 1 MASTER 0 0 0 0 0 0 0 0 2 0 2 0 END
- so now you have:
$ ls CONTCAR hcl.pdb
Preparing the blender scene[edit]
A scene in blender contains three main things:
- Camera (has all the features that a real camera would have, focal length etc.; you don't need to know what that is)
- Light
- Subject (that will be the hcl.pdb molecule)
Since blender has many (really many) options for setting up a scene, you can use this blender file that has the Camera and Light set up. The idea is to use the scene.blend file as a container for all the settings (and that you can change and tune using blenders graphical interface and save). Download the scene.blend file and move it to ~/test_blender
- if you want additional details watch this: https://www.youtube.com/watch?v=zqK4m8a52U8
now we have the Camera and Light (inside of scene.blend) and the molecule (hcl.pdb)
$ ls CONTCAR hcl.pdb scene.blend
let's import the pdb into the scene and render.
Rendering in the command line[edit]
- To call blender from the command line you can use the following command:
$ /usr/bin/blender-2.80-linux-glibc217-x86_64/blender -b ./scene.blend -o //./black_image -F PNG -f 1
here is what all the things mean:
/usr/bin/blender-2.80-linux-glibc217-x86_64/blender #call the blender executable -b #in the background ./scene.blend #load all settings from this file -o //./black_image #two slashes followed by the path to output file -F PNG #desired output format is PNG -f 1 #render first frame
(more details here: https://docs.blender.org/manual/en/latest/advanced/command_line/arguments.html)
- now check out the result
$ls black_image0001.png CONTCAR hcl.pdb scene.blend
the black_image0001.png is precisely as described in the file name, a 1024 by 1024 pixel png image filled with black nothing. This is because the subject is missing. Let's add it.
Importing the subject before the render[edit]
To do this we will use a python script that was frankensteined from the "atomic blender" addon (https://docs.blender.org/manual/fr/dev/addons/import_export/mesh_atomic.html). Download this script pdb_cmd_import.py and move it to ~/test_blender.
$ ls black_image0001.png CONTCAR hcl.pdb pdb_cmd_import.py scene.blend
Now try running the following command:
$ echo "filepath_pdb='$PWD/hcl.pdb'" > e.py; cat pdb_cmd_import.py >> e.py; /usr/bin/blender-2.80-linux-glibc217-x86_64/blender -b ./scene.blend -o //./hcl_image -F PNG -y -P ./e.py -f 1; rm e.py
It is a very ugly command (solution needed on how to pass arguments to the python import pdb script), however you should have an image (hcl_image0001.png) as output:
$ ls black_image0001.png CONTCAR hcl_image0001.png hcl.pdb pdb_cmd_import.py scene.blend
This one should look pretty.
Looping over multiple structures[edit]
If you want to render multiple .pdb files, make sure they are to be found in ~/test_blender, and have the correct extension <filename>.pdb. Then run:
$ for i in *.pdb; do echo "filepath_pdb='$PWD/$i'" > e.py; cat pdb_cmd_import.py >> e.py; /usr/bin/blender-2.80-linux-glibc217-x86_64/blender -b ./scene.blend -o //./$i -F PNG -y -P ./e.py -f 1; rm e.py; done
Here is a breakdown:
$ 1) for i in *.pdb; do 2) echo "filepath_pdb='$PWD/$i'" > e.py; 3) cat pdb_cmd_import.py >> e.py; 4) /usr/bin/blender-2.80-linux-glibc217-x86_64/blender -b ./scene.blend -o //./$i -F PNG -y -P ./e.py -f 1; rm e.py; 5) done #This means: 1) make a loop over all files whose filename ends with .pdb 2) write the path to the .pdb file that you want to import to a temporary script (e.py), and then add the remaining code from
MISC NOTES[edit]
Notice the two lines of hcl.pdb starting with "CONECT", those are important to draw bonds in blender. Not all converters include bonds in the output pdb file. Good news is that this also works:
COMPND HCl AUTHOR GENERATED BY OPEN BABEL 2.4.1 CRYST1 17.321 17.321 17.321 0.03 0.03 0.03 P 1 1 ATOM 1 H UNK 1 0.100 1.000 0.100 1.00 0.00 H HETATM 2 CL UNL 2 1.000 0.100 0.100 1.00 0.00 Cl MASTER 0 0 0 0 0 0 0 0 2 0 2 0 END
Back to Núria López and Group / Scripts_for_VASP.