An Applescript droplet to generate Augmented Reality files from JMol
Augmented reality is finding new applications in science, in particular the ability to enhance publications or lecture notes, and viewers can set up a free account with Augment to provide easy access.
A number of applications can be used to create the files required for 3D display
- Jmol application (not applet) export as VRML (.wrl format) or Wavefront (.obj format)
- PYMOL export as VRML2 (.wrl format).
- VMD export as VRML (.wrl format) or .stl format.
- MOE using io_vrml.svl script export as VRML (.wrl format)
- UCSF Chimera can export crystal structures as STL and VRML files
All work fine but can require command line access to generate the required files and some file manipulation. There is a tutorial here.
I was asked recently if it might be possible to generate an AppleScript droplet that you could simply drop a chemical structure file onto to generate the desired files needed for the Augment, and this is an ideal use case for a droplet.
Droplets are specialised AppleScript applets which are designed to process items dragged onto them. Their icon contains a downward pointing arrow indicating their status as a droplet.
This script uses Jmol to generate the Wavefront .obj and .mtl files. Jmol can be downloaded from sourceforge https://sourceforge.net/projects/jmol/files/Jmol/ and downloads to yield a folder named in this format Jmol-xx.xx.xx. This is a bit of a pain because the script requires the exact path to the executable.
The command to create the files needed for
java -jar /Applications/jmol-14.29.17/JmolData.jar -j 'write OBJ /Users/username/Desktop/SampleFiles/carbocation.obj' /Users/username/Desktop/SampleFiles/carbocation.xyz
This actually creates two files /carbocation.obj and /carbocation.mtl which is the material file that defines the surface
Whilst you can open and view these files in an application like Meshlab you need to compress these two files into a single Archive.zip file for uploading to the Augment site. We can use another command line application to generate the zip file.
man zip ZIP(1L) NAME zip - package and compress (archive) files SYNOPSIS zip [-aABcdDeEfFghjklLmoqrRSTuvVwXyz!@$] [--longoption ...] [-b path] [-n suffixes] [-t date] [-tt date] [zipfile [file ...]] [-xi list]
Droplets are written in a specific manner using the on open handler. This droplet will process only files which have been dragged onto its icon. It will not process folders, alias files, or files within folders.
The first step gets the list of files that have been dropped onto the icon, we then get all the file information for each file, you could use this to limit the number of filetypes that can be processed. We then need to generate the paths to the source file and the output files, in this script the output files are created in the same folder as the input files. These paths will be Mac paths so we need to convert Mac file paths (colon delimited) to unix paths (slash delimited).
set unix_path to quoted form of POSIX path of new_path
We can now construct the Jmol command, remember you need to include the full path to the JmolData.jar, in this case the Jmol folder is in the Applications folder, if you have installed it elsewhere or have a different version this will need to be updated.
set myscript to "java -jar /Applications/jmol-14.29.17/JmolData.jar -j 'write OBJ " & unix_path & "' " & input_file
The next step is to use the do shell script to run the Jmol command.
This will create the .obj and .mtl files, we next use the zip command to compress the two files into a single archive.
If you drag and drop chemical structure files onto the droplet you should end up with results like this, with a new .obj, .mtl and .zip file created for each of the structure files. Jmol can read a variety of different chemistry file formats described here with examples https://sourceforge.net/p/jmol/code/HEAD/tree/trunk/Jmol-datafiles/.
You can download the droplet here http://macinchem.org/applescript/generateARmodel_files/Jmol2AR.zip.
Last Updated 9 August 2018