diff --git a/include/gepetto/gui/pythonwidget.hh b/include/gepetto/gui/pythonwidget.hh index f2309f84db69a1de3b6b5d0966f9cc8a1955dcee..0fae49f375faba75c177b454fb32de135662ac3a 100644 --- a/include/gepetto/gui/pythonwidget.hh +++ b/include/gepetto/gui/pythonwidget.hh @@ -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); diff --git a/include/gepetto/gui/settings.hh b/include/gepetto/gui/settings.hh index a0e3b92435f4c7fc82c75d56861ff1cf3d94a71d..fc30f78f9d1e262340515038df1652ff64a324bb 100644 --- a/include/gepetto/gui/settings.hh +++ b/include/gepetto/gui/settings.hh @@ -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); diff --git a/src/gui/pythonwidget.cc b/src/gui/pythonwidget.cc index bfd33c39f4fe7e4484a3e3bc3f178831216870f6..1c5b5d82d443c26274760638a520919211983775 100644 --- a/src/gui/pythonwidget.cc +++ b/src/gui/pythonwidget.cc @@ -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); diff --git a/src/gui/settings.cc b/src/gui/settings.cc index 06f693a99429d12da0988fe35138740da5544ca7..b7dd63e3eaaf259fcd553f33e3938b0b49243f15 100644 --- a/src/gui/settings.cc +++ b/src/gui/settings.cc @@ -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;