The same script works in Rhino Python Editor yet not in GhPython Component

Hello!
A Rhino/GH frequent user but python newbie here :slight_smile:

was working on a Python script that could fix broken text objects(for example one text object “33.6” breaks into two objects “33” & “6”; the script is trying to fix it) in Rhino, it worked perfectly in Rhino Python Editor, but when I was trying to transfer the same code to GhPython Component (the reason why I do this is that I would like to use Human UI in GH to make the workflow more smooth ), some error occurred and I can’t find out why.

Here is a screen shot, it seems that the TextObjectPoint() & TextObjectText() methods are returning correct value in Rhino Python Editor but returning “none” value in GhPython:

and here is my complete script which works perfectly (a bit slow though) in Rhino Python Editor but not in GhPython Component:

python
import rhinoscriptsyntax as rs

def fixbrokentext():
    
    T01 = rs.GetObjects("Pick broken text",512,True,True,True)

    VL_f = []
    PL_f = []

    VL_i = []
    PL_i = []

    VL_t = []
    PL_t = []

    count = 0
    for i in T01:
        ID = T01[count]
        P00 = rs.TextObjectPoint(ID)
        V00 = rs.TextObjectText(ID)
        
        st = str(V00)
        
        if "," in st :
            V00 = V00.replace(",",".")
            VL_t.append(V00)
            PL_t.append(P00)
        elif float(st)<10:
            VL_f.append(V00)
            PL_f.append(P00)
        else:
            VL_i.append(V00)
            PL_i.append(P00)
        
        count += 1
        
    t = len(PL_i)
    for i in range(0,t):
        P = PL_i[i]
        Inx = rs.PointArrayClosestPoint(PL_f,P)
        F = float(VL_f[Inx])
        T = int(VL_i[i]) + F/10
        rs.AddText(T,PL_i[i],1.0)
    
    s = len(VL_t)
    for i in range(0,s):
        rs.AddText(VL_t[i],PL_t[i],1.0)
        

fixbrokentext()

Any ideas on how to make the script work also in GhPython Component?

Hi @xudong

Grasshopper has its own document, and does not use the Rhino document for computation.
You can use the rs. functions for text in Grasshopper, but you need to target the Rhino document. Grasshopper has no text as geometry data. This can be done like so:

import scriptcontext as sc
import Rhino as rh
sc.doc = rh.RhinoDoc.ActiveDoc

but you must remember to put things back to normal afterwards:

sc.doc = ghdoc

Please note that the Rhino document is visible, has undoes, and is slower to visualize and add into than the Grasshopper invisible document.

Does it help?

Thanks,

Giulio


Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

1 Like

Thanks for the fast reply!
:smiley: It works!