How to get user input while a method is running?


#1

For example, if I have GetObject() which is prompting the user to select an object.
But, instead of selecting an object the user inputs a number.
In this situation I want GetObject() to break and to get the number instead.

How can I do that?


#2

@Bogdan_Chipara, see if below helps:

import Rhino

def DoSomething():
    
    go = Rhino.Input.Custom.GetObject()
    go.GeometryFilter = Rhino.DocObjects.ObjectType.AnyObject
    go.SetCommandPrompt("Select object, input number or press Enter")
    go.AcceptNumber(True, True)
    go.AcceptNothing(True)
    get_rc = go.Get()
    
    if get_rc == Rhino.Input.GetResult.Cancel: 
        print "Cancel"
    elif get_rc == Rhino.Input.GetResult.Nothing: 
        print "Enter pressed"
    elif get_rc == Rhino.Input.GetResult.Number: 
        print "Number:", go.Number()
    elif get_rc == Rhino.Input.GetResult.Object: 
        print "Object Id:", go.Object(0).ObjectId
    
DoSomething()

c.


#3

Hi @clement, thank you!
This is more complicated than I thought, so I need more help.

First, I don’t need just any object, I need a PointOnCurve
Second, I realized that I might need both the point and the number.


#4

@Bogdan_Chipara, you’ll first have to prompt for a curve, then for a point and constrain to the picked curve. If you’ll pick a point on the curve, it will only return a point, so in order to also get a number, you’ll have to add a number option to the point picker. I am not sure if below is what you want, it allows to pick a point on the curve, or enter a number.

If no point is picked, it just prints the current option number
If a point is picked, it prints both, the point and the current option number
If a number is entered, it returns that, but you’ll get no point as no point was picked

import Rhino

def DoSomething():
    # get a curve
    gc = Rhino.Input.Custom.GetObject()
    gc.GeometryFilter = Rhino.DocObjects.ObjectType.Curve
    gc.SetCommandPrompt("Select curve")
    if gc.Get() != Rhino.Input.GetResult.Object: return
    
    # get point on curve
    go = Rhino.Input.Custom.GetPoint()
    go.SetCommandPrompt("Pick point on curve")
    go.Constrain(gc.Object(0).Curve(), False)
    go.AcceptNumber(True, True)
    go.AcceptNothing(True)
    
    # setup a number option
    optNumber = Rhino.Input.Custom.OptionDouble(1.25, True, 0)
    go.AddOptionDouble("MyNumber", optNumber)
    
    get_rc = go.Get()
    if get_rc == Rhino.Input.GetResult.Cancel: 
        print "Cancel (ESC pressed)"
    elif get_rc == Rhino.Input.GetResult.Nothing:
        print "Nothing (Enter pressed)"
        print "OptNumber:", optNumber.CurrentValue
    elif get_rc == Rhino.Input.GetResult.Number: 
        print "Entered Number:", go.Number()
    elif get_rc == Rhino.Input.GetResult.Point: 
        print "Point:", go.Point()
        print "OptNumber:", optNumber.CurrentValue
    
DoSomething()

btw. you can also put the point getter in a loop and allow to enter a number while it keeps asking for a point. But i am not sure this is what you need so I’ll kept it to the bare minimum.

c.


#5

Great! Thank you @clement, very useful your examples!
:smile: