From feff5fa9f0d88732331c6a5d2d5e117266aa1467 Mon Sep 17 00:00:00 2001 From: wmayer Date: Mon, 3 Jun 2019 11:30:29 +0200 Subject: [PATCH] when trying to access ViewObject attribute check that FreeCADGui is already loaded --- src/App/DocumentObjectPyImp.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) 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()));