How to become user input in a script

How to prompt the user for input into a script.

Overview

Prompting the user of a script for the input of a value, selecting a layer, picking a point or selecting a Rhino object is important to many interactive scripts. Many input methods volition also validate the user input to brand sure only the proper input is accepted.

The RhinoscriptSyntax module contains many ways to interactively prompt for several unlike types of input. At that place are iii main styles of input that are contained in Rhinosciptsyntax:

  • Get methods. These are methods that work with the command line, wait for mouse input or prompt for specific input.
  • Special Dialogs. In that location are some simple specific dialogs to prompt for input
  • File system dialogs. Browsing, saving and opening files on the organisation with Python.
  • Eto Custom Dialog Framework. A cantankerous-platform dialog framework to create more custom modal and non-modal dialog box classes that can be used in scripts.

The GET methods

Go are basic ways to prompt for specifc user feedback on the commandline and mouse input. Good examples of a Go might exist get a point, a string, or a distance. There are 25 get methods that can

  1. Commandline Gets - GetReal, GetString, GetInteger, GetBoolean
  2. Interactive Gets - GetPoint(s), GetPointCoordinates, GetLine, GetDistance, GetAngle, GetPolyline, GetRectangle, GetBox, GetCursorPos.
  3. Geometry Gets - GetObject, GetCurveObject, GetSurfaceObject, GetEdgeCurves, GetMeshFaces, GetMeshVertices, GetPointOnCurve, GetPointOnMesh, GetPointOnSurface.

Control Line gets

The simplest Become function is to ask for a specific value on the command line. For example a Go method may prompting for a number on the command line with rs.GetReal().

Getreal()

            import rhinoscriptsyntax equally rs  # GetReal prompts on the command line with optional defaults and a minimum allowable value radius = rs.GetReal("Radius of new circumvolve", 3.fourteen, i.0) if radius: rs.AddCircle( (0,0,0), radius )                      

/images/getreal.png

rs.GetReal() accepts any number, including decimals. In some cases your code may need merely whole numbers- in this example use rs.GetInteger()

The other command line gets are GetString, GetInteger, and GetBoolean. They all work substantially the same as GetReal.

Interactive gets

GetPoint()

Some Get functions will prompt on the commandline and also allow for input from the mouse. A typical interactive get is rs.GetPoint()` to ask the user for a single point location. Equally an example, let's say y'all would similar to prompt for the center of a circle. The default prompt of GetPoint is "Pick bespeak", but yous can specify a different prompt, for example, "Set eye bespeak" depending on what you wish to convey to the user.

            pt = rs.GetPoint("Ready heart point")                      

If the function succeeds, a Rhino point is returned, which can be treated every bit a list of iii numbers representing the world x, y and z coordinates of the point.

            import rhinoscriptsyntax as rs pt = rs.GetPoint("Click to get data near a indicate location") if pt is not None:# annotation information technology is a good thought to bank check if in that location is a result you lot tin can use     impress "That point has an x coordinate of " + str(pt[0]) # when yous build a string that includes elements that are not text, convert to a string with str()                      

GetPoints()

Use rs.GetPoints() to ask the user for multiple point locations. As in rs.GetPoint(), all parameters are optional. Notation that there is a split up prompt for the first betoken, and a second one for subsequent points.

You need to set the parameters in order, separated past commas. If you do not desire to specify a parameter at all, and accept the default, y'all can leave information technology out only you must and so specify any post-obit parameters explicitly using the parameter proper name. For case, this will not work to fix a custom showtime prompt:

                          import rhinoscriptsyntax equally rs  pts = rs.GetPoints(  "Gear up the first point", "Set the next point")                      

Why? because the function has two parameters that come before the start prompt, 'draw_lines' and 'in_plane'. If you leave these out, you must specify what parameters you are setting explicitly in order for it to exist recognized:

            import rhinoscriptsyntax as rs pts = rs.GetPoints(  message1= "Set the first point", message2= "Ready the side by side point")                      

Yous could also make sure to set the other parameters even if you don't care what they are i.e. defaults are OK:

            import rhinoscriptsyntax as rs pts = rs.GetPoints( None, None, "Set the first bespeak", "Gear up the next point")                      

Many of the interactive get functions as well allow a kickoff point to be placed. This mode a rubber band line is fatigued from the point to the cusor, emulating a fatigued line.

            import rhinoscriptsyntax as rs  point1 = rs.GetPoint("Pick first point")  #By adding the first point to this prompt, a line is interactively drawn. point2 = rs.GetPoint("Pick second betoken", point1)                      

The interactive gets have more options on how the input is filtered from the mouse. For details on all the Get functions in RhinoScriptSyntax for Python go to the RhinoScriptSyntax User interface methods. Additional interactive gets include: GetLine, GetDistance, GetAngle, GetPolyline, GetRectangle, GetBox, GetCursorPos.

Geometry Gets

Geometry gets are used to pick geometry off the screen or related geometry off an existing objects. Be enlightened the Gemetry gets can pass very unlike values back from the functions. So, have special note on what is being returned past these functions. In addition to object identifiers there may exist additional information on what was selected and how it was selected.

GetObject()

The basic get for an Object in the scene is the GetObject() role. GetObject will return the Guid of the object selected. Guids are identifiers of existing geometry items that can exist used in other RhinoScriptSyntax functions to identify the objects.

            import rhinoscriptsyntax as rs objectId = rs.GetObject("Pick any object") if objectId: print objectID                      

Like other Geometry gets, there are optional arguments to filter out unwanted option by type. The instance below will only permit curves and surfaces to exist selected.

            objectId = rs.GetObject("Choice a curve or surface", rs.filter.curve | rs.filter.surface) if objectId: print objectID                      

Additional geometry gets are: GetCurveObject, GetSurfaceObject, GetEdgeCurves, GetMeshFaces, GetMeshVertices, GetPointOnCurve, GetPointOnMesh, GetPointOnSurface.

Special Dialogs

The Dialog methods in RhinoScript syntax are used to prompt of with generic custom data. Dialogs can be used to depict more than attention to a required interaction with the user. Dialogs generally interrupt the workflow - the script cannot continue until the dialog is dealt with past the user.

MessageBox()

The simplest dialog box is the rs.MessageBox() function. The rs.MessageBox() comes with many options to customize the buttons based on your needs:

            import rhinoscriptsyntax as rs  rs.MessageBox("Hello Rhinoceros!") # Simple message dialog rs.MessageBox("Hello Rhinoceros!", four | 32) # A Aye, No dialog rs.MessageBox("Hi Rhinoceros!", 2 | 48) # An Arrest, Retry dialog                      

/images/yes_no-dialog.png

Notation that rs.MessageBox() returns a value - yous tin can fix a variable to record the result from a message box so that you tin tell which button the user has clicked.

            import rhinoscriptsyntax equally rs  push button = rs.MessageBox("Hello Rhino!", 2 | 48) # An Arrest, Retry dialog                      

The value of 'button' in the code above volition tell the script which button was clicked and it can continue appropriately. See the Rhino IronPython Help for details on the available buttons and the return codes from rs.MessageBox()

ListBox()

Some of the more than avant-garde dialogs tin can be populated with custom selections:

            import rhinoscriptsyntax as rs  options = ('First Pick', 'Second Pick', 'Tertiary Pick')  if options:     event = rs.ListBox(options, "Pick an option")     if result: rs.MessageBox( result + " was selected" )                      

Will result in:

/images/dialog-listbox.png

Pre-divers dialog box methods:

CheckListBox - Displays a list of strings in a checkable listing. The user can pick multiple items.
/images/dialog-checklistbox.png

ComboListBox - Displays a listing of strings in a combo list.

/images/dialog-combolistbox.png

EditBox - Displays a dialog box with a multi-line edit control.

/images/dialog-editbox.png

PopupMenu - Displays a context-like popup menu.

/images/dialog-popupbox.png

PropertyListBox - Displays a listing of items and values in a holding list.

/images/dialog-propertybox.png

RealBox - Displays a dialog box prompting the user to enter a number. </td>

/images/dialog-realbox.png

StringBox - Displays a dialog box prompting the user to enter a string.

/images/dialog-stringbox.png

GetLayer - Displays dialog box prompting the user to select a layer

/images/getlayer.png

For details on all the dialog box functions in RhinoScriptSyntax for Python go to the RhinoScriptSyntax User interface methods

File System dialogs

Working with files and folders on the reckoner have a special class of dialogs.

            import rhinoscriptsyntax as rs  filename = rs.OpenFileName() if filename: rs.MessageBox(filename)                      

RunPythonScript

Additional File System Dialogs

Method Clarification
BrowseForFolder Displays a Windows browse-for-folder dialog box.
OpenFileName Displays a Windows file open dialog box.
OpenFileNames Displays a Windows file open up dialog box.
SaveFileName Displays a Windows file save dialog box.

For a complete detailed sample meet the How to read and write a simple file guide.

Eto Custom Dialog Framework

Eto is an open up source cross-platform dialog box framework available in Rhino half dozen. Eto can be used to create advanced dialog boxes from within C#, C++ and Rhinoceros.Python.

If the pre-defined dialogs in a higher place are not plenty for your purposes, a Eto dialog might be the right solution.

As an instance, here is a custom collapsing dialog that uses many controls:

/images/dialog-collapse.png

Eto is very powerful, but that power comes with more sophisticated Python syntax. Understanding how best to write, organize and use Eto dialogs will take some work. To start learning near the Eto framework in Python, go to the Eto Forms in Python guide.


  • Reading and Writing files with Python
  • RhinoScriptSyntax User interface methods
  • Eto Forms in Python guide