diff --git a/qrenderdoc/Code/CaptureContext.cpp b/qrenderdoc/Code/CaptureContext.cpp index d2f5053d7..fb5a99316 100644 --- a/qrenderdoc/Code/CaptureContext.cpp +++ b/qrenderdoc/Code/CaptureContext.cpp @@ -228,7 +228,8 @@ CaptureContext::~CaptureContext() delete m_MainWindow; } -void CaptureContext::Begin(QString paramFilename, QString remoteHost, uint32_t remoteIdent, bool temp) +void CaptureContext::Begin(QString paramFilename, QString remoteHost, uint32_t remoteIdent, + bool temp, QString scriptFilename) { m_MainWindow->show(); @@ -249,6 +250,14 @@ void CaptureContext::Begin(QString paramFilename, QString remoteHost, uint32_t r m_MainWindow->takeCaptureOwnership(); } } + + if(!scriptFilename.isEmpty()) + { + ShowPythonShell(); + + if(GetPythonShell()->LoadScriptFromFilename(scriptFilename)) + GetPythonShell()->RunScript(); + } } bool CaptureContext::isRunning() diff --git a/qrenderdoc/Code/CaptureContext.h b/qrenderdoc/Code/CaptureContext.h index 13cf5332e..349bf5017 100644 --- a/qrenderdoc/Code/CaptureContext.h +++ b/qrenderdoc/Code/CaptureContext.h @@ -65,7 +65,8 @@ public: CaptureContext(PersistantConfig &cfg); ~CaptureContext(); - void Begin(QString paramFilename, QString remoteHost, uint32_t remoteIdent, bool temp); + void Begin(QString paramFilename, QString remoteHost, uint32_t remoteIdent, bool temp, + QString scriptFilename); bool isRunning(); rdcstr TempCaptureFilename(const rdcstr &appname) override; diff --git a/qrenderdoc/Code/Interface/QRDInterface.h b/qrenderdoc/Code/Interface/QRDInterface.h index 32af8b2fd..eb1574945 100644 --- a/qrenderdoc/Code/Interface/QRDInterface.h +++ b/qrenderdoc/Code/Interface/QRDInterface.h @@ -1071,6 +1071,31 @@ QWidget. )"); virtual QWidget *Widget() = 0; + DOCUMENT(R"(Sets the current script in the python shell to the given string. + +:param str script: The text of the script to set. +)"); + virtual void SetScriptText(rdcstr script) = 0; + + DOCUMENT(R"(Sets the current script in the python shell to the contents of the given file. + +:param str filename: The filename of the script to load. +:return: Whether or not the script was successfully loaded. +:rtype: bool +)"); + virtual bool LoadScriptFromFilename(rdcstr filename) = 0; + + DOCUMENT(R"(Returns the current script text. + +:return: The current script text. +:rtype: str +)"); + virtual rdcstr GetScriptText() = 0; + + DOCUMENT(R"(Runs the current script in the python shell. +)"); + virtual void RunScript() = 0; + protected: IPythonShell() = default; ~IPythonShell() = default; diff --git a/qrenderdoc/Code/qrenderdoc.cpp b/qrenderdoc/Code/qrenderdoc.cpp index 46f85926b..c3893958a 100644 --- a/qrenderdoc/Code/qrenderdoc.cpp +++ b/qrenderdoc/Code/qrenderdoc.cpp @@ -172,6 +172,11 @@ int main(int argc, char *argv[]) lit("filename.py")); parser.addOption(python); + QCommandLineOption uiscript({lit("ui-python"), lit("ui-script"), lit("ui-py")}, + tr("Run a python script after opening the main UI."), + lit("filename.py")); + parser.addOption(uiscript); + // secret non-described options QCommandLineOption installLayer(lit("install_vulkan_layer"), QString(), lit("root_or_not")); hideOption(installLayer); @@ -288,6 +293,10 @@ int main(int argc, char *argv[]) if(parser.isSet(crashReport)) crashReportPath = parser.value(crashReport); + QString uiscriptFile; + if(parser.isSet(uiscript)) + uiscriptFile = parser.value(uiscript); + QStringList pyscripts = parser.values(python); // load the first filename in the positional arguments. @@ -520,7 +529,7 @@ int main(int argc, char *argv[]) if(!pythonExited) { - ctx.Begin(filename, remoteHost, remoteIdent, temp); + ctx.Begin(filename, remoteHost, remoteIdent, temp, uiscriptFile); while(ctx.isRunning()) { diff --git a/qrenderdoc/Windows/PythonShell.cpp b/qrenderdoc/Windows/PythonShell.cpp index e103faa9c..8c7b0eb51 100644 --- a/qrenderdoc/Windows/PythonShell.cpp +++ b/qrenderdoc/Windows/PythonShell.cpp @@ -935,6 +935,62 @@ PythonContext *PythonShell::GetScriptContext() return scriptContext; } +void PythonShell::SetScriptText(rdcstr script) +{ + scriptEditor->setText(script.c_str()); +} + +bool PythonShell::LoadScriptFromFilename(rdcstr filename) +{ + if(!filename.isEmpty()) + { + QFile f(filename); + if(f.open(QIODevice::ReadOnly | QIODevice::Text)) + { + scriptEditor->setText(f.readAll().data()); + return true; + } + } + + return false; +} + +rdcstr PythonShell::GetScriptText() +{ + return scriptEditor->getText(scriptEditor->textLength() + 1).data(); +} + +void PythonShell::RunScript() +{ + PythonContext *context = newContext(); + + ANALYTIC_SET(UIFeatures.PythonInterop, true); + + ui->outputHelpTabs->setCurrentIndex(0); + + ui->scriptOutput->clear(); + + QString script = QString::fromUtf8(scriptEditor->getText(scriptEditor->textLength() + 1)); + + enableButtons(false); + + LambdaThread *thread = new LambdaThread([this, script, context]() { + + scriptContext = context; + context->executeString(lit("script.py"), script); + scriptContext = NULL; + + GUIInvoke::call(this, [this, context]() { + context->Finish(); + enableButtons(true); + }); + }); + + thread->setName(lit("Python script")); + thread->selfDelete(true); + thread->start(); +} + void PythonShell::on_execute_clicked() { QString command = ui->lineInput->text(); @@ -999,18 +1055,9 @@ void PythonShell::on_openScript_clicked() QString filename = RDDialog::getOpenFileName(this, tr("Open Python Script"), QString(), tr("Python scripts (*.py)")); - if(!filename.isEmpty()) + if(!LoadScriptFromFilename(filename)) { - QFile f(filename); - if(f.open(QIODevice::ReadOnly | QIODevice::Text)) - { - scriptEditor->setText(f.readAll().data()); - } - else - { - RDDialog::critical(this, tr("Error loading script"), - tr("Couldn't open path %1.").arg(filename)); - } + RDDialog::critical(this, tr("Error loading script"), tr("Couldn't open path %1.").arg(filename)); } } @@ -1048,33 +1095,7 @@ void PythonShell::on_saveScript_clicked() void PythonShell::on_runScript_clicked() { - PythonContext *context = newContext(); - - ANALYTIC_SET(UIFeatures.PythonInterop, true); - - ui->outputHelpTabs->setCurrentIndex(0); - - ui->scriptOutput->clear(); - - QString script = QString::fromUtf8(scriptEditor->getText(scriptEditor->textLength() + 1)); - - enableButtons(false); - - LambdaThread *thread = new LambdaThread([this, script, context]() { - - scriptContext = context; - context->executeString(lit("script.py"), script); - scriptContext = NULL; - - GUIInvoke::call(this, [this, context]() { - context->Finish(); - enableButtons(true); - }); - }); - - thread->setName(lit("Python script")); - thread->selfDelete(true); - thread->start(); + RunScript(); } void PythonShell::on_abortRun_clicked() diff --git a/qrenderdoc/Windows/PythonShell.h b/qrenderdoc/Windows/PythonShell.h index 8350e2743..1016ffdf6 100644 --- a/qrenderdoc/Windows/PythonShell.h +++ b/qrenderdoc/Windows/PythonShell.h @@ -52,6 +52,10 @@ public: // IPythonShell QWidget *Widget() override { return this; } + void SetScriptText(rdcstr script) override; + bool LoadScriptFromFilename(rdcstr filename) override; + rdcstr GetScriptText() override; + void RunScript() override; private slots: // automatic slots void on_execute_clicked();