Python extensions to avogadro

Hi,

I’ve written a python script that uses openbabel to calculate some
properties. Now, I’d like to try and get this to work with Avogadro in
an extension using python. The problem is, that while the
Avogadro.Molecule class does a good in covering the basic needs of
most molecule manipulation, you sometimes need access to OpenBabel
features (which can be done if oneuses C++ to program the extension).
It is my understanding (from the API documentation:
http://avogadro.openmolecules.net/wiki/API_Documentation) that there
is no direct link between the python bindings and openbabel, i.e. no
exposed openbabel API through avogadro python bindings. The last
resort is thus to try and get the OBMol object from the
Avogadro.Molecule class

but how can this be done? I’ve tried the following

import openbabel
import Avogadro
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class Extension(QObject):
def init(self):
QObject.init(self)

def actions(self):
actions = []
action = QAction(self)
action.setText(“Calculate”)
actions.append(action)
return actions

def menuPath(self, action):
return “E&xtensions”

def performAction(self, action, glwidget):
# convert Avogadro.Molecule to OpenBabels OBMol
mol = glwidget.molecule
print mol, mol.OBMol
obmol = openbabel.OBMol( mol )

the print statement reveals the following:
<Avogadro.Molecule object at 0x2b1af30> <Swig Object of type
’OpenBabel::OBMol *’ at 0x3a90230>

which reveals that mol.OBMol indeed has the correct type(apart from
the asterisk) where the openbabel.OBMol returns the following error
when called:
_openbabel.OBMol_swiginit(self,_openbabel.new_OBMol(*args))
TypeError: in method ‘new_OBMol’, argument 1 of type ‘OpenBabel::OBMol const &’

And now I’m kinda stuck. Any ideas what to do?

There is the obvious: “switch to c++”, but it kinda defeats the whole
purpose of being able to write the extensions in python. :slight_smile:

Kind Regards,

Casper

Casper,

I’ve been away from the project for a while but looking at the API docs, OBMol is a function that returns an OBMol based on the Avogadro::Molecule. Have you tried:

obmol = mol.OBMol()

As a side note, I love C++ but I don’t think anyone thinks it’s necessary to use C++ and would agree with you about writing a python extension.

-Donald


Donald Ephraim Curtis
dcurtis@gmail.com

On Nov 5, 2010, at 11:32 AM, Casper Steinmann wrote:

Hi,

I’ve written a python script that uses openbabel to calculate some
properties. Now, I’d like to try and get this to work with Avogadro in
an extension using python. The problem is, that while the
Avogadro.Molecule class does a good in covering the basic needs of
most molecule manipulation, you sometimes need access to OpenBabel
features (which can be done if oneuses C++ to program the extension).
It is my understanding (from the API documentation:
http://avogadro.openmolecules.net/wiki/API_Documentation) that there
is no direct link between the python bindings and openbabel, i.e. no
exposed openbabel API through avogadro python bindings. The last
resort is thus to try and get the OBMol object from the
Avogadro.Molecule class

but how can this be done? I’ve tried the following

import openbabel
import Avogadro
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class Extension(QObject):
def init(self):
QObject.init(self)

def actions(self):
actions = []
action = QAction(self)
action.setText(“Calculate”)
actions.append(action)
return actions

def menuPath(self, action):
return “E&xtensions”

def performAction(self, action, glwidget):

convert Avogadro.Molecule to OpenBabels OBMol

mol = glwidget.molecule
print mol, mol.OBMol
obmol = openbabel.OBMol( mol )

the print statement reveals the following:
<Avogadro.Molecule object at 0x2b1af30> <Swig Object of type
’OpenBabel::OBMol *’ at 0x3a90230>

which reveals that mol.OBMol indeed has the correct type(apart from
the asterisk) where the openbabel.OBMol returns the following error
when called:
_openbabel.OBMol_swiginit(self,_openbabel.new_OBMol(*args))
TypeError: in method ‘new_OBMol’, argument 1 of type ‘OpenBabel::OBMol const &’

And now I’m kinda stuck. Any ideas what to do?

There is the obvious: “switch to c++”, but it kinda defeats the whole
purpose of being able to write the extensions in python. :slight_smile:

Kind Regards,

Casper


The Next 800 Companies to Lead America’s Growth: New Video Whitepaper
David G. Thomson, author of the best-selling book “Blueprint to a
Billion” shares his insights and actions to help propel your
business during the next growth cycle. Listen Now!
http://p.sf.net/sfu/SAP-dev2dev


Avogadro-Discuss mailing list
Avogadro-Discuss@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/avogadro-discuss

Hey Donald,

Thanks for your reply.

I’ve been away from the project for a while but looking at the API docs, OBMol is a function that returns an OBMol based on the
Avogadro::Molecule. Have you tried:
obmol = mol.OBMol()

I think what I passed along was the last of my efforts and
unfortunately not the whole arsenal. Nonetheless, I tried that, and it
results in

Traceback (most recent call last):
File “/home/cstein/.avogadro/extensionScripts/bare.py”, line 23, in
performAction
obmol = mol.OBMol()
TypeError: ‘PySwigObject’ object is not callable

but this is because it is defined as a property in the source code
.add_property(“OBMol”, &Molecule_OBMol, &Molecule_setOBMol)

In the python code, I can do this instead
mol = glwidget.molecule
obmol = mol.OBMol
print type(mol), type(obmol)

which prints out:
<class ‘Avogadro.Molecule’> <type ‘PySwigObject’>

but parsing the obmol variable to openbabel

myobmol = openbabe.OBMol( obmol )

gives
Traceback (most recent call last):
File “/home/cstein/.avogadro/extensionScripts/bare.py”, line 24, in
performAction
myobmol = openbabel.OBMol( obmol )
File “/usr/local/lib/python2.6/dist-packages/openbabel.py”, line
2061, in init
_openbabel.OBMol_swiginit(self,_openbabel.new_OBMol(*args))
TypeError: in method ‘new_OBMol’, argument 1 of type ‘OpenBabel::OBMol const &’

As a side note, I love C++ but I don’t think anyone thinks it’s necessary to use C++ and would agree with you about writing a python extension.
Yeah well, it was just a spicy comment at a weak moment :slight_smile: I’ve never
settled quite with C++ and still have a few issues with it, but it is
getting better. I’d be all over Avogadro (Konstantin style) if I was
any better with C++, but instead, I’m trying to master the Avogadro
API through magical glue of python before I’ll pull out any of the big
guns.

Thanks for your comments though,

  • Casper

On Nov 5, 2010, at 11:32 AM, Casper Steinmann wrote:

Hi,

I’ve written a python script that uses openbabel to calculate some
properties. Now, I’d like to try and get this to work with Avogadro in
an extension using python. The problem is, that while the
Avogadro.Molecule class does a good in covering the basic needs of
most molecule manipulation, you sometimes need access to OpenBabel
features (which can be done if oneuses C++ to program the extension).
It is my understanding (from the API documentation:
http://avogadro.openmolecules.net/wiki/API_Documentation) that there
is no direct link between the python bindings and openbabel, i.e. no
exposed openbabel API through avogadro python bindings. The last
resort is thus to try and get the OBMol object from the
Avogadro.Molecule class

but how can this be done? I’ve tried the following

import openbabel
import Avogadro
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class Extension(QObject):
def init(self):
QObject.init(self)

def actions(self):
actions = []
action = QAction(self)
action.setText(“Calculate”)
actions.append(action)
return actions

def menuPath(self, action):
return “E&xtensions”

def performAction(self, action, glwidget):

convert Avogadro.Molecule to OpenBabels OBMol

mol = glwidget.molecule
print mol, mol.OBMol
obmol = openbabel.OBMol( mol )

the print statement reveals the following:
<Avogadro.Molecule object at 0x2b1af30> <Swig Object of type
’OpenBabel::OBMol *’ at 0x3a90230>

which reveals that mol.OBMol indeed has the correct type(apart from
the asterisk) where the openbabel.OBMol returns the following error
when called:
_openbabel.OBMol_swiginit(self,_openbabel.new_OBMol(*args))
TypeError: in method ‘new_OBMol’, argument 1 of type ‘OpenBabel::OBMol const &’

And now I’m kinda stuck. Any ideas what to do?

There is the obvious: “switch to c++”, but it kinda defeats the whole
purpose of being able to write the extensions in python. :slight_smile:

Kind Regards,

Casper


The Next 800 Companies to Lead America’s Growth: New Video Whitepaper
David G. Thomson, author of the best-selling book “Blueprint to a
Billion” shares his insights and actions to help propel your
business during the next growth cycle. Listen Now!
http://p.sf.net/sfu/SAP-dev2dev


Avogadro-Discuss mailing list
Avogadro-Discuss@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/avogadro-discuss


Centralized Desktop Delivery: Dell and VMware Reference Architecture
Simplifying enterprise desktop deployment and management using
Dell EqualLogic storage and VMware View: A highly scalable, end-to-end
client virtualization framework. Read more!
http://p.sf.net/sfu/dell-eql-dev2dev


Avogadro-Discuss mailing list
Avogadro-Discuss@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/avogadro-discuss

<class ‘Avogadro.Molecule’> <type ‘PySwigObject’>

but parsing the obmol variable to openbabel

myobmol = openbabel.OBMol( obmol )

gives
Traceback (most recent call last):

_openbabel.OBMol_swiginit(self,_openbabel.new_OBMol(*args))
TypeError: in method ‘new_OBMol’, argument 1 of type ‘OpenBabel::OBMol const &’

Certainly in the development effort, we tried to make sure this SWIG bridge worked, but it’s possible it’s broken now.

I’m assuming that if you use the Open Babel code directly, that works? What version of Boost do you have installed? And are you using the OB-2.3.0 bindings from the source tarfile, or did you build them yourself?

We’d definitely like to see more people “all over” contributing to Avogadro, so I’d like to see what I can do to get this to work.

Thanks very much,
-Geoff

I’m assuming that if you use the Open Babel code directly, that works? What version of Boost do you have installed? And are you using the OB-2.3.0 bindings from the source tarfile, or did you build them yourself?
OpenBabel works with python support. I built the openbabel and the
python bindings myself (revision 4300 from svn) using swig2.0.1

I’m using ubuntu 10.10 on this machine with the accompanying Boost
library (1.42)

We’d definitely like to see more people “all over” contributing to Avogadro, so I’d like to see what I can do to get this to work.
The plan was to write guides for the wiki a la what David is doing and
learn about the api on the fly. I think the python bindings for
Avogadro has very great potential and would of course like to
contribute whatever I can.

Casper

OpenBabel works with python support. I built the openbabel and the
python bindings myself (revision 4300 from svn) using swig2.0.1

Hmm. It’s been a while since I tried the Avogadro python bindings. I see basically the same problem you reported. I’ve been using the Python terminal window, since it gives me interactive debugging. :wink:

print mol, mol.OBMol
obmol = openbabel.OBMol( mol )

the print statement reveals the following:
<Avogadro.Molecule object at 0x2b1af30> <Swig Object of type
’OpenBabel::OBMol *’ at 0x3a90230>

Here, I actually get a different answer:
print molecule.OBMol
_1024062101000000_p_OpenBabel__OBMol

Maybe this is the newer boost::python (1.44) for me?

Tim, I can’t completely make sense of the swig.cpp code you added to the Avogadro python module. Where did you get this? Is it likely to break with different boost::python or SWIG versions? For example, I see different code on the boost::python wiki:
http://wiki.python.org/moin/boost.python/HowTo#SWIGexposedC.2B-.2B-objectfromPython

Any ideas?
-Geoff