Ciao !
Prove di interazione tra script e GH:
Ho imbastito una definizione per disegnare una forma ovale (sul piano XY)
oval.gh (14,9 KB)
Poi c’e’ uno script che consente di impostare raggio e lunghezza della parte rettilinea.
Inoltre c’e’ un angolo di rotazione per inclinare l’ovale (default: lungo l’asse Y)
A questo punto si seleziona il punto centrale e si possono (dopo aver mostrato l’RCP ) aggiustare i valori con gli slider nell’RCP.
Ci sono anche due slider per (piccoli) spostamenti
Poi si conferma il Bake (o si rinuncia) da Rhino.
Tutto qui …
Lo script salva i valori utilizzati per raggio, lato e angolo per usarli come default la prossima volta.
Non so se c’e’ modo di selezionare l’RCP nel pannello come-si-chiama.
Per cui bisogna andare a cliccare a mano per vederlo … se qualcuno sa come fare, per favore me lo dica.
Grazie
Ah, ho notato che nell’RCP i valori degli slider non rispettano l’espressione di output impostata …
Pazienza, speriamo in GH2 ! 
Questo e’ lo script:
import clr
clr.AddReferenceByName( 'Grasshopper' )
import Rhino
import scriptcontext
import rhinoscriptsyntax as rs
import Grasshopper
def getdocdata( key, defval ):
val = Rhino.RhinoDoc.ActiveDoc.Strings.GetValue( key )
if not val:
val = defval
return val
def setdocdata( key, val ):
if val:
Rhino.RhinoDoc.ActiveDoc.Strings.SetString( key, str( val ) )
print( 'Stored value: %s = %s' % ( key, str( val ) ) )
else:
Rhino.RhinoDoc.ActiveDoc.Strings.Delete( key )
def main():
rad = float( getdocdata( 'ov-rad', '10' ) )
sid = float( getdocdata( 'ov-sid', '10' ) )
deg = float( getdocdata( 'ov-deg', '0' ) )
while True:
gep = Rhino.Input.Custom.GetPoint()
gep.AcceptNothing( True )
gep.SetCommandPrompt( 'Center of oval ?' )
radind, radopt = gep.AddOptionDouble( 'Radius',
Rhino.Input.Custom.OptionDouble( rad ) )
sidind, sidopt = gep.AddOptionDouble( 'SideLength',
Rhino.Input.Custom.OptionDouble( sid ) )
degind, degopt = gep.AddOptionDouble( 'Degrees',
Rhino.Input.Custom.OptionDouble( deg ) )
gep.Get()
res = gep.Result()
if res == Rhino.Input.GetResult.Cancel:
return
elif res == Rhino.Input.GetResult.Nothing:
setdocdata( 'ov-rad', rad )
setdocdata( 'ov-sid', sid )
setdocdata( 'ov-deg', deg )
return
elif res == Rhino.Input.GetResult.Option:
opind = gep.OptionIndex()
if opind == radind:
rad = radopt.CurrentValue
elif opind == sidind:
sid = sidopt.CurrentValue
elif opind == degind:
deg = degopt.CurrentValue
elif res == Rhino.Input.GetResult.Point:
cen = gep.Point()
break
Gh = Rhino.RhinoApp.GetPlugInObject( 'Grasshopper' )
if not Gh:
return
Gh.LoadEditor()
Gh.CloseAllDocuments()
# Gh.ShowEditor()
defn = 'D:\\Rhino\\gh\\oval.gh'
ok = Gh.OpenDocument( defn )
if not ok:
print( 'Unable to load %s' % defn )
return
docServer = Grasshopper.GH_InstanceServer.DocumentServer
doc = docServer[ 0 ] # first opened document
Gh.DisableSolver()
Gh.HideEditor()
def FindBatteryByNickname( docObjects, name ):
if docObjects is None:
return None
for obj in docObjects:
attr = obj.Attributes
if attr.PathName == name:
return obj
raise Exception( name + ' was not found in document' )
ghob = FindBatteryByNickname( doc.Objects, 'Center' )
Gh.AssignDataToParameter( str( ghob.Attributes.InstanceGuid ), cen )
ghob = FindBatteryByNickname( doc.Objects, 'Radius' )
Gh.SetSliderRangeAndValue( str( ghob.Attributes.InstanceGuid ), rad,
( rad - 10 ) if rad > 10 else 1,
rad + 10 )
ghob = FindBatteryByNickname( doc.Objects, 'Side' )
Gh.SetSliderRangeAndValue( str( ghob.Attributes.InstanceGuid ), sid,
( sid - 10 ) if sid > 10 else 1,
sid + 10 )
ghob = FindBatteryByNickname( doc.Objects, 'Angle' )
Gh.SetSliderRangeAndValue( str( ghob.Attributes.InstanceGuid ), deg,
deg - 45, deg + 45 )
Gh.EnableSolver()
# Gh.RunSolver(True)
ok = rs.GetString(
'Adjust values from the RCP ... Then ... Bake ?', 'Yes', [ 'No', 'Yes' ] )
if ok and ok[ 0 ] in 'yY':
ghob = FindBatteryByNickname( doc.Objects, 'Result' )
result = Gh.BakeDataInObject( str( ghob.Attributes.InstanceGuid ) )
if result:
cu = result[ 0 ]
rs.SelectObject( cu )
scriptcontext.doc.Views.Redraw()
ghob = FindBatteryByNickname( doc.Objects, 'Radius' )
rad = str( ghob.VolatileData[ 0 ][ 0 ] )
setdocdata( 'ov-rad', rad )
ghob = FindBatteryByNickname( doc.Objects, 'Side' )
sid = str( ghob.VolatileData[ 0 ][ 0 ] )
setdocdata( 'ov-sid', sid )
ghob = FindBatteryByNickname( doc.Objects, 'Angle' )
deg = str( ghob.VolatileData[ 0 ][ 0 ] )
setdocdata( 'ov-deg', deg )
# Gh.CloseDocument()
# Gh.HideEditor()
main()
Chi volesse provare deve correggere il percorso della definizione nella linea:
defn = 'D:\\Rhino\\gh\\oval.gh'
Ciao !
P.S.
Certo che scriptare GH e’ una cosa piuttosto … esoterica … 
Pare che la documentazione sia un po’ scarsa …
Per cui spesso si cercano esempi ed istruzioni postati da David, Steve e Giulio (nonche’ da utilizzatori in gamba) sui vari forum … 