Execute objects in GH_Document in RhinoPythonEditor

New to Grasshopper, so I am tyring to get the GH_Document object from the opened GH document, but I am not sure what’s next to get the GH_Document object so that I can do something with it? Below is all I have and I don’t know what to do next:

import Rhino
import scriptcontext
import rhinoscriptsyntax as rs

Grasshopper = Rhino.RhinoApp.GetPlugInObject("Grasshopper")

Grasshopper.OpenDocument("z:\unnamed.gh")
Grasshopper.DisableSolver()

This post contains a pretty comprehensive sample:

1 Like

Hello @piac,

Thank you very much. It seems that I run into the following error once I try to reproduce the code in that post:
Message: 'ZuiPythonComponent' object has no attribute 'ClearPersistentData'

May I know how to feed input values to ‘ZuiPythonComponent’ objects?

A very simple .gh file that contains a python plugin module and the associated .py file is much appreciated.

EDIT 1:
Here is what I have in RhinoPythonEditor:

import Grasshopper
from Rhino import RhinoApp, RhinoDoc
from Rhino.Geometry import *

docServer = Grasshopper.GH_InstanceServer.DocumentServer
doc = docServer[0] # first opened document
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")
    

for obj in doc.Objects:
        attr = obj.Attributes
        print attr.PathName

param = FindBatteryByNickname(doc.Objects, "Python (GhPython Script)")
pp = param.Params.Input[0]
pp.PersistentData.ClearData();
pp.PersistentData.Append(Grasshopper.Kernel.Types.GH_Number(5))
param.ExpireSolution(True);
doc = Grasshopper.Kernel.GH_Document()
doc.AddObject(param, False);

On the canvas:

However, it seems that it does not work as nothing happens when I run the above script. May I know how to properly use it please?

Thank you

EDIT 2
UHH, I kinda figured out what was going on. Here is my script that is sort of in working conditions now. Still experimenting other features but I think this is what I expect.

import Grasshopper
from Rhino import RhinoApp, RhinoDoc
from Rhino.Geometry import *

#gh = RhinoApp.GetPlugInObject("Grasshopper")
#gh.LoadEditor()
#gh.CloseAllDocuments()
#gh.ShowEditor()
#gh.OpenDocument("z:\unnamed.ghx")

docServer = Grasshopper.GH_InstanceServer.DocumentServer
doc = docServer[0] # first opened document
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")
    

for obj in doc.Objects:
        attr = obj.Attributes
        print attr.PathName

param = FindBatteryByNickname(doc.Objects, "input1")
print dir(param)
param.Script_ClearPersistentData()
param.AddPersistentData(85)
param.ExpireSolution(True) 

param = FindBatteryByNickname(doc.Objects, "output1")
results  = param.VolatileData[0][0].Value
f = open("z://data2.txt", "w")


f.write( repr(results))      # str() converts to string
f.close()

Thank you,

Shen

2 Likes

Great that you figured this out!

Hello @piac,

I am not sure whether or not I am using it the right way or is it how it suppose to be used?

Thank you,

Shen

1 Like