Skip to content
Snippets Groups Projects
Commit 06b7ea9b authored by Joseph Mirabel's avatar Joseph Mirabel
Browse files

Add ability to run python scripts

parent 540587d4
No related branches found
No related tags found
No related merge requests found
......@@ -119,6 +119,7 @@ namespace gepetto {
void loadModulePlugin(QString moduleName);
void unloadModulePlugin(QString moduleName);
void loadScriptPlugin(QString moduleName, QString fileName);
void runScript(QString fileName);
private:
void loadPlugin(QString moduleName, PythonQtObjectPtr module);
......
......@@ -79,6 +79,7 @@ namespace gepetto {
PluginManager pluginManager_;
QStringList pluginsToInit_;
QStringList pyplugins_;
QStringList pyscripts_;
void setMainWindow (MainWindow* main);
......@@ -127,6 +128,7 @@ namespace gepetto {
void addEnvFromString (const std::string& envStr);
void addPlugin (const QString& plg, bool init);
void addPyPlugin (const QString& plg, bool init);
void addPyScript (const QString& fileName);
void addOmniORB (const QString& arg, const QString& value);
inline void log (const QString& t);
......
......@@ -151,10 +151,10 @@ namespace gepetto {
PythonQt* pqt = PythonQt::self();
PythonQtObjectPtr module = pqt->createModuleFromFile (moduleName, fileName);
if (pqt->handleError()) {
pqt->clearError();
return;
}
if (module.isNull()) {
pqt->handleError();
qDebug() << "Enable to load module" << moduleName << "from script"
<< fileName;
return;
......@@ -162,16 +162,28 @@ namespace gepetto {
loadPlugin (moduleName, module);
}
void PythonWidget::runScript(QString fileName)
{
PythonQt* pqt = PythonQt::self();
PythonQtObjectPtr mainContext = pqt->getMainModule();
mainContext.evalFile(fileName);
if (pqt->handleError()) {
pqt->clearError();
qDebug() << "Failed to run script" << fileName;
}
}
void PythonWidget::loadModulePlugin(QString moduleName)
{
PythonQt* pqt = PythonQt::self();
PythonQtObjectPtr module = pqt->importModule (moduleName);
if (pqt->handleError()) {
pqt->clearError();
return;
}
if (module.isNull()) {
pqt->handleError();
qDebug() << "Enable to load module" << moduleName;
qDebug() << "Unable to load module" << moduleName;
return;
}
loadPlugin (moduleName, module);
......
......@@ -103,6 +103,7 @@ namespace gepetto {
au->addCommandLineOption("-p or --load-plugin", "load the plugin");
#if GEPETTO_GUI_HAS_PYTHONQT
au->addCommandLineOption("-q or --load-pyplugin", "load the PythonQt module as a plugin");
au->addCommandLineOption("-x or --run-pyscript", "run a script into the PythonQt console");
#endif
au->addCommandLineOption("-P or --no-plugin", "do not load any plugin");
au->addCommandLineOption("-w or --auto-write-settings", "write the settings in the configuration file");
......@@ -134,6 +135,8 @@ namespace gepetto {
addPlugin (QString::fromStdString(opt), !noPlugin);
while (arguments.read ("-q", opt) || arguments.read ("--load-pyplugin", opt))
addPyPlugin (QString::fromStdString(opt), !noPlugin);
while (arguments.read ("-x", opt) || arguments.read ("--run-pyscript", opt))
addPyScript (QString::fromStdString(opt));
if (arguments.read("-c", configurationFile) || arguments.read("--config-file", configurationFile)) {}
if (arguments.read("--predefined-robots", predifinedRobotConf)) {}
......@@ -200,11 +203,19 @@ namespace gepetto {
} else
pw->loadModulePlugin (name);
}
// TODO Wouldn't it be better to do this later ?
foreach (QString fileName, pyscripts_) {
pw->runScript(fileName);
}
#else
foreach (QString name, pyplugins_) {
logError ("gepetto-viewer-corba was compiled without GEPETTO_GUI_HAS_"
"PYTHONQT flag. Cannot not load Python plugin " + name);
}
foreach (QString fileName, pyscripts_) {
logError ("gepetto-viewer-corba was compiled without GEPETTO_GUI_HAS_"
"PYTHONQT flag. Cannot not load Python script " + fileName);
}
#endif
}
......@@ -513,6 +524,11 @@ namespace gepetto {
if (init) pyplugins_.append (plg);
}
void Settings::addPyScript (const QString& fileName)
{
pyscripts_.append (fileName);
}
void Settings::addOmniORB (const QString& arg, const QString& value)
{
omniORBargv_ << arg << value;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment