diff --git a/bindings/Sofa/src/SofaPython3/Sofa/Core/Submodule_Core.cpp b/bindings/Sofa/src/SofaPython3/Sofa/Core/Submodule_Core.cpp index 5da025dd..5266cdad 100644 --- a/bindings/Sofa/src/SofaPython3/Sofa/Core/Submodule_Core.cpp +++ b/bindings/Sofa/src/SofaPython3/Sofa/Core/Submodule_Core.cpp @@ -51,6 +51,8 @@ using sofa::helper::logging::Message; #include #include +#include + namespace sofapython3 { @@ -130,6 +132,15 @@ PYBIND11_MODULE(Core, core) moduleAddBaseMeshTopology(core); moduleAddPointSetTopologyModifier(core); moduleAddTaskScheduler(core); + + // called when the module is unloaded + auto atexit = py::module_::import("atexit"); + atexit.attr("register")(py::cpp_function([]() { + + sofa::core::cleanup(); + + msg_info("SofaPython3.Core") << "Sofa.Core unload()"; + })); } } ///namespace sofapython3 diff --git a/bindings/Sofa/src/SofaPython3/Sofa/Helper/Submodule_Helper.cpp b/bindings/Sofa/src/SofaPython3/Sofa/Helper/Submodule_Helper.cpp index d5e03992..85d11514 100644 --- a/bindings/Sofa/src/SofaPython3/Sofa/Helper/Submodule_Helper.cpp +++ b/bindings/Sofa/src/SofaPython3/Sofa/Helper/Submodule_Helper.cpp @@ -18,7 +18,6 @@ * Contact information: contact@sofa-framework.org * ******************************************************************************/ -#include #include #include #include @@ -108,7 +107,6 @@ static void parse_emitter_message_then(py::args args, const Action& action) { PYBIND11_MODULE(Helper, helper) { // These are needed to force the dynamic loading of module dependencies (found in CMakeLists.txt) - sofa::core::init(); sofa::helper::init(); helper.doc() = R"doc( @@ -155,6 +153,12 @@ PYBIND11_MODULE(Helper, helper) moduleAddMessageHandler(helper); moduleAddVector(helper); moduleAddSystem(helper); + + auto atexit = py::module_::import("atexit"); + atexit.attr("register")(py::cpp_function([]() { + sofa::helper::cleanup(); + msg_info("SofaPython3.Helper") << "Sofa.Helper unload()"; + })); } } ///namespace sofapython3 diff --git a/bindings/Sofa/src/SofaPython3/Sofa/Simulation/Submodule_Simulation.cpp b/bindings/Sofa/src/SofaPython3/Sofa/Simulation/Submodule_Simulation.cpp index dfb1eb65..28acf918 100644 --- a/bindings/Sofa/src/SofaPython3/Sofa/Simulation/Submodule_Simulation.cpp +++ b/bindings/Sofa/src/SofaPython3/Sofa/Simulation/Submodule_Simulation.cpp @@ -42,6 +42,7 @@ using sofa::simulation::Simulation; #include #include +#include #include namespace py = pybind11; @@ -83,6 +84,17 @@ PYBIND11_MODULE(Simulation, simulation) { sofa::simulation::node::initTextures(n); }); + + // called when the module is unloaded + auto atexit = py::module_::import("atexit"); + atexit.attr("register")(py::cpp_function([]() { + + sofa::simulation::core::cleanup(); + sofa::simulation::common::cleanup(); + sofa::simulation::graph::cleanup(); + + msg_info("SofaPython3.Simulation") << "Sofa.Simulation unload()"; + })); } } /// namespace sofapython3 diff --git a/bindings/SofaRuntime/src/SofaPython3/SofaRuntime/Module_SofaRuntime.cpp b/bindings/SofaRuntime/src/SofaPython3/SofaRuntime/Module_SofaRuntime.cpp index e2e1359d..65c0bd44 100644 --- a/bindings/SofaRuntime/src/SofaPython3/SofaRuntime/Module_SofaRuntime.cpp +++ b/bindings/SofaRuntime/src/SofaPython3/SofaRuntime/Module_SofaRuntime.cpp @@ -72,23 +72,6 @@ using sofa::helper::logging::MainConsoleMessageHandler; namespace sofapython3 { - -class SofaInitializer -{ -public: - // TODO, ces trucs sont fort laid. Normalement ce devrait ĂȘtre une joli plugin qui - // appelle le init. - SofaInitializer(){ - sofa::simulation::common::init(); - sofa::simulation::graph::init(); - } - - ~SofaInitializer(){ - sofa::simulation::common::cleanup(); - sofa::simulation::graph::cleanup(); - } -}; - static std::vector getCategories(const std::string& className) { std::vector categories; @@ -109,8 +92,6 @@ static std::vector getCategories(const std::string& className) return categories ; } -static SofaInitializer s; - /// The first parameter must be named the same as the module file to load. PYBIND11_MODULE(SofaRuntime, m) { @@ -132,13 +113,6 @@ PYBIND11_MODULE(SofaRuntime, m) { )doc"; - // These are needed to force the dynamic loading of module dependencies (found in CMakeLists.txt) - sofa::core::init(); - sofa::helper::init(); - sofa::simulation::core::init(); - sofa::simulation::graph::init(); - sofa::simulation::common::init(); - // Add the plugin directory to PluginRepository const std::string& pluginDir = Utils::getExecutableDirectory(); PluginRepository.addFirstPath(pluginDir); @@ -192,6 +166,7 @@ PYBIND11_MODULE(SofaRuntime, m) { m.def("getCategories", &getCategories); addSubmoduleTimer(m); + } } // namespace sofapython3