diff --git a/src/App/DocumentObjectPyImp.cpp b/src/App/DocumentObjectPyImp.cpp index 2f91d8823a..8d6addac70 100644 --- a/src/App/DocumentObjectPyImp.cpp +++ b/src/App/DocumentObjectPyImp.cpp @@ -187,11 +187,24 @@ Py::List DocumentObjectPy::getState(void) const Py::Object DocumentObjectPy::getViewObject(void) const { try { + PyObject *dict = PySys_GetObject("modules"); + if (!dict) { + return Py::None(); + } + + // check if the FreeCADGui module is already loaded, if not then don't try to load it + Py::Dict sysmod(dict); + if (!sysmod.hasKey("FreeCADGui")) { + return Py::None(); + } + + // double-check that the module doesn't have a null pointer Py::Module module(PyImport_ImportModule("FreeCADGui"),true); - if (!module.hasAttr("getDocument")) { + if (module.isNull() || !module.hasAttr("getDocument")) { // in v0.14+, the GUI module can be loaded in console mode (but doesn't have all its document methods) return Py::None(); } + Py::Callable method(module.getAttr("getDocument")); Py::Tuple arg(1); arg.setItem(0, Py::String(getDocumentObjectPtr()->getDocument()->getName()));