diff --git a/Plugin/src/SofaPython3/PythonEnvironment.cpp b/Plugin/src/SofaPython3/PythonEnvironment.cpp index d27b41bb..b42cc325 100644 --- a/Plugin/src/SofaPython3/PythonEnvironment.cpp +++ b/Plugin/src/SofaPython3/PythonEnvironment.cpp @@ -52,27 +52,28 @@ along with sofaqtquick. If not, see . #include #endif +#include +using sofa::helper::system::PluginManager; +using sofa::helper::system::Plugin; + #include -#include #include +#include +using sofa::helper::system::FileSystem; #include +using sofa::helper::Utils; + #include using sofa::helper::getAStringCopy ; -using sofa::helper::system::FileSystem; -using sofa::helper::Utils; - #include #include -MSG_REGISTER_CLASS(sofapython3::PythonEnvironment, "SofaPython3::PythonEnvironment") #include using sofa::simulation::SceneLoaderFactory; -//#include "Python.h" - #include #include namespace py = pybind11; @@ -80,6 +81,8 @@ namespace py = pybind11; #include "SceneLoaderPY3.h" using sofapython3::SceneLoaderPY3; +MSG_REGISTER_CLASS(sofapython3::PythonEnvironment, "SofaPython3::PythonEnvironment") + namespace sofapython3 { @@ -248,6 +251,8 @@ void PythonEnvironment::Init() } } + addPythonModulePathsForPluginsByName("SofaPython3"); + getStaticData()->m_sofamodule = py::module::import("Sofa"); @@ -318,23 +323,64 @@ void PythonEnvironment::addPythonModulePathsFromConfigFile(const std::string& pa void PythonEnvironment::addPythonModulePathsForPlugins(const std::string& pluginsDirectory) { + bool added = false; + + std::vector pythonDirs = { + pluginsDirectory, + pluginsDirectory + "/lib", + pluginsDirectory + "/python3" + }; + + /// Iterate in the pluginsDirectory and add each sub directory with a 'python' name + /// this is mostly for in-tree builds. std::vector files; FileSystem::listDirectory(pluginsDirectory, files); - for (std::vector::iterator i = files.begin(); i != files.end(); ++i) { - const std::string pluginPath = pluginsDirectory + "/" + *i; - if (FileSystem::exists(pluginPath) && FileSystem::isDirectory(pluginPath)) + const std::string pluginSubdir = pluginsDirectory + "/" + *i; + if (FileSystem::exists(pluginSubdir) && FileSystem::isDirectory(pluginSubdir)) { - const std::string pythonDir = pluginPath + "/python"; - if (FileSystem::exists(pythonDir) && FileSystem::isDirectory(pythonDir)) - { - addPythonModulePath(pythonDir); - } + pythonDirs.push_back(pluginSubdir + "/python3"); } } + + /// For each of the directories in pythonDirs, search for a site-packages entry + for(std::string pythonDir : pythonDirs) + { + // Search for a subdir "site-packages" + if (FileSystem::exists(pythonDir+"/site-packages") && FileSystem::isDirectory(pythonDir+"/site-packages")) + { + addPythonModulePath(pythonDir+"/site-packages"); + added = true; + } + } + + if(!added) + { + msg_warning("PythonEnvironment") << "No python dir found in " << pluginsDirectory; + } } +void PythonEnvironment::addPythonModulePathsForPluginsByName(const std::string& pluginName) +{ + std::map& map = PluginManager::getInstance().getPluginMap(); + for( const auto& elem : map) + { + Plugin p = elem.second; + if ( p.getModuleName() == pluginName ) + { + std::string pluginLibraryPath = elem.first; + // moduleRoot should be 2 levels above the library (plugin_name/lib/plugin_name.so) + std::string moduleRoot = FileSystem::getParentDirectory(FileSystem::getParentDirectory(pluginLibraryPath)); + + addPythonModulePathsForPlugins(moduleRoot); + return; + } + } + msg_warning("PythonEnvironment") << pluginName << " not found in PluginManager's map."; +} + + // some basic RAII stuff to handle init/termination cleanly namespace { diff --git a/Plugin/src/SofaPython3/PythonEnvironment.h b/Plugin/src/SofaPython3/PythonEnvironment.h index a119cf04..bbd04dc8 100644 --- a/Plugin/src/SofaPython3/PythonEnvironment.h +++ b/Plugin/src/SofaPython3/PythonEnvironment.h @@ -94,7 +94,8 @@ class SOFAPYTHON3_API PythonEnvironment /// Add all the directories matching /*/python to sys.path /// NB: can also be used for projects /*/python - static void addPythonModulePathsForPlugins(const std::string& pluginsDirectory); + static void addPythonModulePathsForPlugins(const std::string& pluginsDirectory); + static void addPythonModulePathsForPluginsByName(const std::string& pluginName); /// set the content of sys.argv. static void setArguments(const std::string& filename,