If you set a variable in the paraview python console before running a
script, the script will have access to that variable.  So you can pass
arguments to a python script by just setting sys.argv in the python console
before running the script.

Sean... I happen to have a patch that allows raw_input() to work from the
paraview gui.  Patch is attached.  When the script calls raw_input() it
causes a focused qt text entry box to appear at the cursor.  The entire
paraview gui is blocked until the user hits enter.  Not perfectly elegant,
but works pretty well.

The pvblot plugin I wrote makes extensive use of querying the user for input
from the gui.  I decided not to use raw_input() because it blocks the gui.
Instead I cooked up a fancy asynchronous input scheme using python
generators, decorators, and the yield statement.  It works really well, but
requires that you write your script using yield instead of raw_input().

Berk, I think we could add a method to the paraview.simple module that does
a callback to the gui for a file dialog, it would be similar to my attached
raw_input() patch.

Pat

On Thu, Feb 18, 2010 at 12:27 PM, Sean Ziegeler <
[email protected]> wrote:

> Actually, that isn't a bad idea.  There doesn't seem to be a way of getting
> input to a Python script, as raw_input() doesn't appear to work in the
> Python Shell (in the PV GUI, at least) either.
>
>
> On 02/18/10 08:19, Berk Geveci wrote:
>
>> I didn't mention Python because it doesn't give you a way of popping up
>> a file chooser - you have to edit the script and change the file name.
>> Maybe we should add a Python command to pop up the file chooser... It is
>> not super easy but if there is enough demand, we can do it. Feel free to
>> add a feature request to the bug tracker or uservoice.
>>
>> -berk
>>
>> On Thu, Feb 18, 2010 at 8:06 AM, Jean M. Favre <[email protected]
>> <mailto:[email protected]>> wrote:
>>
>>    Natalie Happenhofer wrote:
>>     > Another topic - selecting volumes of interest: I can do that for
>>    one file, and it works well. But is it really necessary to repeat
>>    this procedure for every file I open in Paraview? Is it possible to
>>    select a volume of interest and for every file I open afterward,
>>    just this VoI is displayed?
>>     >
>>     >
>>    I would suggest doing this in Python in order to avoid reading the full
>>    resolution dataset by default. When opening a file, paraview forces you
>>    to click Apply before you can do anything else (such as creating an
>>    Extract Subset filter). Thus, the data reader uses
>>    UPDATE_EXTENT=WHOLE_EXTENT and you may run out of memory before being
>>    able to subset the grid.
>>
>>    if instead, you instantiate your reader, do not execute it, do not
>>    create a representation for it, then create an Extract Subset with the
>>    VOI you desire, and then execute the whole thing, you will end-up
>>    passing the VOI extents upstream directly to the reader, and your
>> reader
>>    will only execute once with the correct extents.
>>
>>    Use the python shell and the followin example:
>>
>>    reader = XMLImageDataReader( FileName= '/path/to/data/foo.vti'] )
>>    reader.PointArrayStatus = ['data1']
>>
>>    ExtractSubset1 = ExtractSubset()
>>
>>    ExtractSubset1.VOI = [2000, 2255, 2000, 2255, 2000, 2255]
>>
>>    DataRepresentation2 = Show(ExtractSubset1)
>>
>>    Render()
>>
>>    Jean--
>>    Swiss National Supercomputing Center
>>    _______________________________________________
>>    Powered by www.kitware.com <http://www.kitware.com>
>>
>>
>>    Visit other Kitware open-source projects at
>>    http://www.kitware.com/opensource/opensource.html
>>
>>    Please keep messages on-topic and check the ParaView Wiki at:
>>    http://paraview.org/Wiki/ParaView
>>
>>    Follow this link to subscribe/unsubscribe:
>>    http://www.paraview.org/mailman/listinfo/paraview
>>
>>
>>
>>
>> _______________________________________________
>> Powered by www.kitware.com
>>
>> Visit other Kitware open-source projects at
>> http://www.kitware.com/opensource/opensource.html
>>
>> Please keep messages on-topic and check the ParaView Wiki at:
>> http://paraview.org/Wiki/ParaView
>>
>> Follow this link to subscribe/unsubscribe:
>> http://www.paraview.org/mailman/listinfo/paraview
>>
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the ParaView Wiki at:
> http://paraview.org/Wiki/ParaView
>
> Follow this link to subscribe/unsubscribe:
> http://www.paraview.org/mailman/listinfo/paraview
>
diff --git a/Qt/Python/pqPythonShell.cxx b/Qt/Python/pqPythonShell.cxx
index c07ae51..ba1c654 100644
--- a/Qt/Python/pqPythonShell.cxx
+++ b/Qt/Python/pqPythonShell.cxx
@@ -41,6 +41,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #include "vtkPVOptions.h"
 #include "vtkEventQtSlotConnect.h"
 #include "vtkPVPythonInteractiveInterpretor.h"
+#include "vtkStdString.h"
 
 #include <QCoreApplication>
 #include <QResizeEvent>
@@ -272,6 +273,10 @@ void pqPythonShell::initializeInterpretor(int argc, char* argv[])
   this->Implementation->VTKConnect->Connect(
     this->Implementation->Interpreter, vtkCommand::WarningEvent, 
     this, SLOT(printStdout(vtkObject*, unsigned long, void*, void*))); 
+
+  this->Implementation->VTKConnect->Connect(
+    this->Implementation->Interpreter, vtkCommand::UpdateEvent, 
+    this, SLOT(readInputLine(vtkObject*, unsigned long, void*, void*))); 
 }
 
 void pqPythonShell::clear()
@@ -367,6 +372,32 @@ void pqPythonShell::printStdout(vtkObject*, unsigned long, void*, void* calldata
   this->Implementation->Interpreter->ClearMessages();
 }
 
+#include <QDialog>
+#include <QLineEdit>
+#include <QVBoxLayout>
+class LineInput : public QDialog {
+public:
+  LineInput(QWidget* parent=NULL) : QDialog(parent, Qt::FramelessWindowHint)
+  {
+    QVBoxLayout* layout = new QVBoxLayout(this);
+    layout->setMargin(0);
+    this->Edit = new QLineEdit;
+    layout->addWidget(this->Edit);
+    this->connect(this->Edit, SIGNAL(returnPressed()), SLOT(accept()));
+  }
+  QLineEdit* Edit;
+};
+
+void pqPythonShell::readInputLine(vtkObject*, unsigned long, void*, void* calldata)
+{
+  vtkStdString* ret = reinterpret_cast<vtkStdString*>(calldata);
+  QPoint pos = this->Implementation->Console.getCursorPosition();
+  LineInput input(this);
+  input.move(this->mapToGlobal(pos));
+  input.exec();
+  *ret = input.Edit->text().toAscii().data();
+}
+
 void pqPythonShell::printStderr(vtkObject*, unsigned long, void*, void* calldata)
 {
   const char* text = reinterpret_cast<const char*>(calldata);
diff --git a/Qt/Python/pqPythonShell.h b/Qt/Python/pqPythonShell.h
index 1b126bb..b001758 100644
--- a/Qt/Python/pqPythonShell.h
+++ b/Qt/Python/pqPythonShell.h
@@ -74,6 +74,7 @@ public:
 
 signals:
   void executing(bool);
+  void getInputLine(QString& input);
 
 public slots:
   void clear();
@@ -83,6 +84,8 @@ private slots:
   void printStderr(vtkObject*, unsigned long, void*, void*);
   void printStdout(vtkObject*, unsigned long, void*, void*);
 
+  void readInputLine(vtkObject*, unsigned long, void*, void*);
+
   void onExecuteCommand(const QString&);
 
 private:
@@ -97,6 +100,7 @@ private:
 
   void printStderr(const QString&);
   void printStdout(const QString&);
+
 };
 
 #endif // !_pqPythonShell_h
diff --git a/Qt/Widgets/pqConsoleWidget.cxx b/Qt/Widgets/pqConsoleWidget.cxx
index 1af729b..369f4ee 100644
--- a/Qt/Widgets/pqConsoleWidget.cxx
+++ b/Qt/Widgets/pqConsoleWidget.cxx
@@ -412,6 +412,14 @@ void pqConsoleWidget::setFormat(const QTextCharFormat& Format)
 }
 
 //-----------------------------------------------------------------------------
+QPoint pqConsoleWidget::getCursorPosition()
+{
+  QTextCursor tc = this->Implementation->textCursor();
+
+  return this->Implementation->cursorRect(tc).topLeft();
+}
+
+//-----------------------------------------------------------------------------
 void pqConsoleWidget::setCompleter(pqConsoleWidgetCompleter* completer)
 {
   this->Implementation->setCompleter(completer);
diff --git a/Qt/Widgets/pqConsoleWidget.h b/Qt/Widgets/pqConsoleWidget.h
index ecf418a..d45a700 100644
--- a/Qt/Widgets/pqConsoleWidget.h
+++ b/Qt/Widgets/pqConsoleWidget.h
@@ -64,6 +64,8 @@ public:
 
   /// Set a completer for this console widget
   void setCompleter(pqConsoleWidgetCompleter* completer);
+
+  QPoint getCursorPosition();
   
 signals:
   /// Signal emitted whenever the user enters a command
diff --git a/Utilities/VTKPythonWrapping/Executable/vtkPVPythonInterpretor.cxx b/Utilities/VTKPythonWrapping/Executable/vtkPVPythonInterpretor.cxx
index 6cc580c..9e57c6f 100644
--- a/Utilities/VTKPythonWrapping/Executable/vtkPVPythonInterpretor.cxx
+++ b/Utilities/VTKPythonWrapping/Executable/vtkPVPythonInterpretor.cxx
@@ -261,6 +261,14 @@ vtkPVPythonInterpretor::~vtkPVPythonInterpretor()
 }
 
 //-----------------------------------------------------------------------------
+vtkStdString vtkPVPythonInterpretor::GetInputLine()
+{
+  vtkStdString ret;
+  this->InvokeEvent(vtkCommand::UpdateEvent, &ret);
+  return ret;
+}
+
+//-----------------------------------------------------------------------------
 void vtkPVPythonInterpretor::DumpError(const char* str)
 {
   vtkPythonMessage msg;
@@ -369,13 +377,16 @@ void vtkPVPythonInterpretor::InitializeInternal()
     vtkPVPythonInterpretorWrapper* wrapperErr = vtkWrapInterpretor(this);
     wrapperErr->DumpToError = true;
 
-    // Redirect Python's stdout and stderr
+    // Redirect Python's stdout and stderr and stdin
     PySys_SetObject(const_cast<char*>("stdout"),
       reinterpret_cast<PyObject*>(wrapperOut));
 
     PySys_SetObject(const_cast<char*>("stderr"),
       reinterpret_cast<PyObject*>(wrapperErr));
 
+    PySys_SetObject(const_cast<char*>("stdin"),
+      reinterpret_cast<PyObject*>(wrapperErr));
+
     Py_DECREF(wrapperOut);
     Py_DECREF(wrapperErr);
     }
diff --git a/Utilities/VTKPythonWrapping/Executable/vtkPVPythonInterpretor.h b/Utilities/VTKPythonWrapping/Executable/vtkPVPythonInterpretor.h
index d87cbc2..ddd9816 100644
--- a/Utilities/VTKPythonWrapping/Executable/vtkPVPythonInterpretor.h
+++ b/Utilities/VTKPythonWrapping/Executable/vtkPVPythonInterpretor.h
@@ -126,6 +126,7 @@ protected:
 
   void DumpError(const char* string);
   void DumpOutput(const char* string);
+  vtkStdString GetInputLine();
   
 private:
   vtkPVPythonInterpretor(const vtkPVPythonInterpretor&); // Not implemented.
diff --git a/Utilities/VTKPythonWrapping/Executable/vtkPVPythonInterpretorWrapper.h b/Utilities/VTKPythonWrapping/Executable/vtkPVPythonInterpretorWrapper.h
index 5adde19..9dc8f69 100644
--- a/Utilities/VTKPythonWrapping/Executable/vtkPVPythonInterpretorWrapper.h
+++ b/Utilities/VTKPythonWrapping/Executable/vtkPVPythonInterpretorWrapper.h
@@ -48,13 +48,25 @@ struct vtkPVPythonInterpretorWrapper
         }
       }
     }
+
+  vtkStdString Read()
+    {
+    vtkStdString ret;
+    if (this->Interpretor)
+      {
+      ret = this->Interpretor->GetInputLine();
+      }
+    return ret;
+    }
 };
 
 static PyObject* vtkWrite(PyObject* self, PyObject* args);
+static PyObject* vtkRead(PyObject* self, PyObject* args);
 
 // const_cast since older versions of python are not const correct.
 static PyMethodDef vtkPVPythonInterpretorWrapperMethods[] = {
     {const_cast<char*>("write"), vtkWrite, METH_VARARGS, const_cast<char*>("Dump message")},
+    {const_cast<char*>("readline"), vtkRead, METH_VARARGS, const_cast<char*>("Read input line")},
     {0, 0, 0, 0}
 };
 
@@ -142,6 +154,24 @@ static PyObject* vtkWrite(PyObject* self, PyObject* args)
   return Py_BuildValue(const_cast<char*>(""));
 }
 
+static PyObject* vtkRead(PyObject* self, PyObject* args)
+{
+  if(!self || !PyObject_TypeCheck(self, &vtkPVPythonInterpretorWrapperType))
+    {
+    return 0;
+    }
+
+  vtkPVPythonInterpretorWrapper* wrapper = 
+    reinterpret_cast<vtkPVPythonInterpretorWrapper*>(self);
+
+  vtkStdString ret;
+  if (wrapper)
+    {
+    ret = wrapper->Read();
+    }
+  return Py_BuildValue(const_cast<char*>("s"), const_cast<char*>(ret.c_str()));
+}
+
 static vtkPVPythonInterpretorWrapper* vtkWrapInterpretor(vtkPVPythonInterpretor* interpretor)
 {
   if(PyType_Ready(&vtkPVPythonInterpretorWrapperType) < 0)
_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at 
http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the ParaView Wiki at: 
http://paraview.org/Wiki/ParaView

Follow this link to subscribe/unsubscribe:
http://www.paraview.org/mailman/listinfo/paraview

Reply via email to