Created
July 28, 2012 17:50
-
-
Save cryos/3194164 to your computer and use it in GitHub Desktop.
Using Avogadro from Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Developed by Tim Vandermeersch, with a blog post at | |
# http://timvdm.blogspot.com/2009/05/using-avogadro-library-from-python.html | |
# | |
#import openbabel | |
# needed for OB formats on linux (can be omitted by importing openbabel first) | |
import sys | |
if sys.platform.find('linux') != -1: | |
try: | |
import dl | |
except ImportError: | |
import DLFCN as dl | |
sys.setdlopenflags(sys.getdlopenflags() | dl.RTLD_GLOBAL) | |
from PyQt4.QtCore import * | |
from PyQt4.QtGui import * | |
from PyQt4 import uic | |
import Avogadro | |
import sys | |
class MainWindow(QMainWindow): | |
def __init__(self): | |
QMainWindow.__init__(self) | |
uiFile = file('mainwindow.ui', 'r') | |
self.ui = uic.loadUi(uiFile, self) | |
self.connect(self.ui.actionOpen, SIGNAL('triggered()'), self, SLOT('open()')) | |
self.connect(self.ui.listWidget, SIGNAL('currentRowChanged(int)'), self, SLOT('load(int)')) | |
self.objects = [] | |
def newGLWidget(self): | |
glWidget = Avogadro.GLWidget() | |
glWidget.loadDefaultEngines() | |
glWidget.quality = 4 | |
toolGroup = Avogadro.ToolGroup() | |
tool = Avogadro.PluginManager.instance.tool('Navigate', None) | |
if tool: | |
toolGroup.append(tool) | |
self.objects.append(tool) | |
glWidget.toolGroup = toolGroup | |
self.objects.append(glWidget) | |
self.objects.append(toolGroup) | |
return glWidget | |
@pyqtSignature('load(int)') | |
def load(self, row): | |
mol = self.molFile.molecule(row) | |
self.objects.append(mol) | |
glWidget = self.newGLWidget() | |
glWidget.molecule = mol | |
mdiWindow = self.ui.mdiArea.addSubWindow(Avogadro.toPyQt(glWidget)) | |
print glWidget | |
print Avogadro.toPyQt(glWidget) | |
mdiWindow.setWindowTitle(self.molFile.titles[row]) | |
mdiWindow.show() | |
@pyqtSignature('readThreadFinnished()') | |
def readThreadFinnished(self): | |
self.ui.listWidget.clear() | |
self.ui.listWidget.addItems(self.molFile.titles) | |
self.ui.statusbar.showMessage('Done.', 5) | |
@pyqtSignature('open()') | |
def open(self): | |
filename = str(QFileDialog.getOpenFileName(self, 'Open', QDir.currentPath(), | |
'Chemistry files (*.cml *.sdf *.mol *.xyz)')) | |
self.molFile = Avogadro.MoleculeFile.readFile(filename, '', '', False) | |
self.connect(Avogadro.toPyQt(self.molFile), SIGNAL('ready()'), self, SLOT('readThreadFinnished()')) | |
self.ui.statusbar.showMessage('Reading file...') | |
if __name__ == "__main__": | |
app = QApplication(sys.argv) | |
window = MainWindow() | |
window.show() | |
sys.exit(app.exec_()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<ui version="4.0"> | |
<class>MainWindow</class> | |
<widget class="QMainWindow" name="MainWindow"> | |
<property name="geometry"> | |
<rect> | |
<x>0</x> | |
<y>0</y> | |
<width>681</width> | |
<height>494</height> | |
</rect> | |
</property> | |
<property name="windowTitle"> | |
<string>MainWindow</string> | |
</property> | |
<widget class="QWidget" name="centralwidget"> | |
<layout class="QHBoxLayout" name="horizontalLayout"> | |
<item> | |
<widget class="QListWidget" name="listWidget"> | |
<property name="minimumSize"> | |
<size> | |
<width>150</width> | |
<height>0</height> | |
</size> | |
</property> | |
</widget> | |
</item> | |
<item> | |
<widget class="QMdiArea" name="mdiArea"/> | |
</item> | |
</layout> | |
</widget> | |
<widget class="QMenuBar" name="menubar"> | |
<property name="geometry"> | |
<rect> | |
<x>0</x> | |
<y>0</y> | |
<width>681</width> | |
<height>28</height> | |
</rect> | |
</property> | |
<widget class="QMenu" name="menu_File"> | |
<property name="title"> | |
<string>&File</string> | |
</property> | |
<addaction name="actionOpen"/> | |
</widget> | |
<addaction name="menu_File"/> | |
</widget> | |
<widget class="QStatusBar" name="statusbar"/> | |
<action name="actionNew"> | |
<property name="text"> | |
<string>New</string> | |
</property> | |
</action> | |
<action name="actionOpen"> | |
<property name="text"> | |
<string>Open</string> | |
</property> | |
</action> | |
</widget> | |
<resources/> | |
<connections/> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello
I tried to use this code with version 1.2 of Avogadro and the molecules are not displayed. Is it possible to correct this error ????
Best Regards