ForceFieldHelpers: fix “No Python class registered” error (#8800)

Fixes github issue #8597.

* UFFGetMoleculeForceField was returning
PyForceField* before the class was registered via rdForceField,
which caused “No Python class registered” errors in Python when
using multiple conformers.

* This patch forces rdForceField to be
imported during rdForceFieldHelpers module init, ensuring the
class is always registered before helper functions are used.

* Add unit test
This commit is contained in:
Vandan Revanur
2025-09-23 06:03:57 +02:00
committed by GitHub
parent 951be4f06e
commit cded6cc4e0
2 changed files with 12 additions and 1 deletions

View File

@@ -248,6 +248,8 @@ PyObject *getUFFVdWParams(const RDKit::ROMol &mol, const unsigned int idx1,
} // namespace ForceFields
BOOST_PYTHON_MODULE(rdForceFieldHelpers) {
boost::python::import("rdkit.ForceField.rdForceField");
python::scope().attr("__doc__") =
"Module containing functions to handle force fields";

View File

@@ -5,7 +5,7 @@ import numpy
from rdkit import Chem, RDConfig
from rdkit.Chem import ChemicalForceFields, rdDistGeom
from rdkit.Chem.rdForceFieldHelpers import UFFGetMoleculeForceField
def feq(v1, v2, tol2=1e-4):
return abs(v1 - v2) <= tol2
@@ -408,5 +408,14 @@ M END"""
self.assertAlmostEqual((posa - posb).Length(), 100, delta=10e-5)
def test_uff_get_forcefield_runs(self):
mol = Chem.MolFromSmiles("CCO")
mol = Chem.AddHs(mol)
rdDistGeom.EmbedMolecule(mol, randomSeed=42)
ff = UFFGetMoleculeForceField(mol, ignoreInterfragInteractions=False)
self.assertIsNotNone(ff)
self.assertTrue(hasattr(ff, "CalcEnergy"))
if __name__ == "__main__":
unittest.main()