Hi guys, I’m stuck here. Trying to create a custom getobject with a toggle option. The toggle option should be flipable forever until I select an object. When I select a single object I want to check in which state that toggle option is and depending on that call the function belonging to that option using the selected object.
After calling the function I want it to return back to the getobject menu with the toggle option.
This repeats until I press Enter or Esc to exit the command.
Anybody that can help out here? I’ve had a look this old topic but I couldn’t solve it based on that.
import Rhino
def Option1(obj):
print "Do this to obj"
GetObjectOption()
def Option2(obj):
print "Do that to obj"
GetObjectOption()
def GetObjectOption():
go = Rhino.Input.Custom.GetObject()
go.SetCommandPrompt("Select object")
go.GeometryFilter = Rhino.DocObjects.ObjectType.Curve
option = Rhino.Input.Custom.OptionToggle(True,"Option1","Option2")
go.AddOptionToggle("Mode",option)
go.Get()
if I select an object while Option1 is activated:
Option1(obj)
elif I select an object while option2 is activated:
Option2(obj)
elif I press ESC or Enter:
return
GetObjectOption()
import Rhino
import rhinoscriptsyntax as rs
import scriptcontext as sc
def GetObjectPlusBoolean(prompt,b_opts,g_filt=None):
go = Rhino.Input.Custom.GetObject()
go.SetCommandPrompt(prompt)
blnOption0=Rhino.Input.Custom.OptionToggle(b_opts[1],b_opts[2],b_opts[3])
go.AddOptionToggle(b_opts[0],blnOption0)
#allow preselected object
go.EnablePreSelect(True,True)
#main object type filter
if g_filt: go.GeometryFilter=g_filt
while True:
get_rc = go.Get()
if get_rc==Rhino.Input.GetResult.Cancel: return
if get_rc==Rhino.Input.GetResult.Object:
rhobj=go.Object(0)
break
elif get_rc==Rhino.Input.GetResult.Option:
continue
return (rhobj,blnOption0.CurrentValue)
def TestFunction():
prompt="Select a surface or polysurface object"
gf=Rhino.DocObjects.ObjectType.Brep
a="OptionA" ; b="OptionB"
bool_ini=["ChooseYourOption",True,b,a]
result=GetObjectPlusBoolean(prompt,bool_ini,gf)
if result is None:
print "Function aborted"
else:
obj_Id=result[0].ObjectId
msg="You chose object {} and {}".format(obj_Id,a if result[1] else b)
print msg
TestFunction()
You can set go.EnablePreSelect(False,True) if you don’t want to accept a pre-selected object - which will short-circuit the option choice and take the default. I’ts also possible to set it up to remember the last used value via “sticky” but I didn’t do that here. You can also eliminate the geometry filter if you don’t care about the object type; or, you can also use a sub filter by setting up a go.SetCustomGeometryFilter() which I don’t have in the above example.
Ok. I’m trying to dig through this and there’s quite some stuff that looks pretty advanced to me on first sight. But as I slowly go over each step I’m start to understand more and more. There’s some very nice tricks in there I didn’t know about, thanks!