@@ -118,6 +118,8 @@ PythonEnvironmentData* PythonEnvironment::getStaticData()
118118 return m_staticdata;
119119}
120120
121+ std::string PythonEnvironment::pluginLibraryPath = " " ;
122+
121123SOFAPYTHON3_API py::module PythonEnvironment::importFromFile (const std::string& module , const std::string& path, py::object* globals)
122124{
123125 PythonEnvironment::gil lock;
@@ -238,6 +240,17 @@ void PythonEnvironment::Init()
238240
239241 // python modules are automatically reloaded at each scene loading
240242 // setAutomaticModuleReload( true );
243+
244+ // Initialize pluginLibraryPath by reading PluginManager's map
245+ std::map<std::string, Plugin>& map = PluginManager::getInstance ().getPluginMap ();
246+ for ( const auto & elem : map)
247+ {
248+ Plugin p = elem.second ;
249+ if ( p.getModuleName () == sofa_tostring (SOFA_TARGET) )
250+ {
251+ pluginLibraryPath = elem.first ;
252+ }
253+ }
241254}
242255
243256void PythonEnvironment::executePython (std::function<void ()> cb)
@@ -332,7 +345,7 @@ void PythonEnvironment::addPythonModulePathsForPlugins(const std::string& plugin
332345
333346 if (!added)
334347 {
335- msg_warning ( " PythonEnvironment " ) << " No python dir found in " << pluginsDirectory;
348+ msg_info ( " SofaPython3 " ) << " No python3 dir found in " << pluginsDirectory;
336349 }
337350}
338351
@@ -352,7 +365,30 @@ void PythonEnvironment::addPythonModulePathsForPluginsByName(const std::string&
352365 return ;
353366 }
354367 }
355- msg_warning (" PythonEnvironment" ) << pluginName << " not found in PluginManager's map." ;
368+ msg_info (" SofaPython3" ) << pluginName << " not found in PluginManager's map." ;
369+ }
370+
371+ void PythonEnvironment::addPluginManagerCallback ()
372+ {
373+ PluginManager::getInstance ().addOnPluginLoadedCallback (pluginLibraryPath,
374+ [](const std::string& pluginLibraryPath, const Plugin& plugin) {
375+ // WARNING: loaded plugin must be organized like plugin_name/lib/plugin_name.so
376+ for ( auto path : sofa::helper::system::PluginRepository.getPaths () )
377+ {
378+ std::string pluginRoot = FileSystem::cleanPath ( path + " /" + plugin.getModuleName () );
379+ if ( FileSystem::exists (pluginRoot) && FileSystem::isDirectory (pluginRoot) )
380+ {
381+ addPythonModulePathsForPlugins (pluginRoot);
382+ return ;
383+ }
384+ }
385+ }
386+ );
387+ }
388+
389+ void PythonEnvironment::removePluginManagerCallback ()
390+ {
391+ PluginManager::getInstance ().removeOnPluginLoadedCallback (pluginLibraryPath);
356392}
357393
358394
0 commit comments