Searching contents of gh files

Hi, I need to batch search several hundred gh files to find where I have used specific components…I believe I need to convert them to ghx so that they are xml files but cannot find out how to do this…there was a ghtoghx facility for windows but I cannot find it and anyway I am on a mac…any help with converting or with directly searching lots of gh files would be much appreciated

thanks

Giusseppe

Also how do I find the GUID for a component…( I think I will need to search for this in the files rather than a component name)

Hi Giusseppe,

Good news, you don’t need GhToGhx or windows for this. Rhino 8 for Mac ships GH_IO.dll and the ScriptEditor can batch read your gh files directly — no conversion needed.

Every object placed in a gh file is stored with its component GUID and Name, so a script can scan hundreds of files in seconds without opening them in Grasshopper. This works even for plugin components you don’t have installed.

Open Grasshopper once so the libraries are loaded, then run this in the ScriptEditor (Python 3). Set your folder and the component name (or GUID) at the top:

#! python 3
import os, clr
clr.AddReference("GH_IO")
from GH_IO.Serialization import GH_Archive

folder = "/Users/you/gh-files"     # top folder, searched recursively
target_name = "Voronoi"            # component name to look for...
target_guid = ""                   # ...or paste a GUID here to search by GUID instead

hits = 0
for root, dirs, files in os.walk(folder):
    for f in files:
        if not f.lower().endswith((".gh", ".ghx")):
            continue
        path = os.path.join(root, f)
        archive = GH_Archive()
        if not archive.ReadFromFile(path):
            print("could not read:", path)
            continue
        objs = archive.GetRootNode.FindChunk("Definition").FindChunk("DefinitionObjects")
        if objs is None:
            continue
        found = 0
        for i in range(objs.GetInt32("ObjectCount")):
            obj = objs.FindChunk("Object", i)
            if obj is None:
                continue
            if target_guid:
                if str(obj.GetGuid("GUID")) == target_guid.lower():
                    found += 1
            elif obj.GetString("Name") == target_name:
                found += 1
        if found:
            hits += 1
            print(f"{path}  —  {found}x")

print(f"\n{hits} file(s) contain the target component")

If you still want ghx files to text search, the conversion is the same library — drop this in the loop instead and you’ll get a .ghx next to every .gh:

archive = GH_Archive()
if archive.ReadFromFile(path):
    with open(path + "x", "w", encoding="utf-8") as w:
        w.write(archive.Serialize_Xml())

For your GUID question — no need to dig through files, you can ask Grasshopper directly:

import Grasshopper
for proxy in Grasshopper.Instances.ComponentServer.ObjectProxies:
    if "voronoi" in proxy.Desc.Name.lower():
        print(proxy.Desc.Name, proxy.Guid)

GUID is the safer search key since names aren’t unique across plugins, but the Name stored in the file is the component’s proper name (not your nickname), so a name search is fine for the built-ins.

One caveat — the search script only sees top level objects, components inside clusters won’t be found. If you use clusters heavily the ghx convert + text search route is the more thorough one.

thank you so much…this is just what I needed

is there a way to plug a component into the small guid script …I have it working with a text string input , however, since there are often several results with any word I cannot plug the guid output straight into the file search script without copying and pasting…( I have the file path , name and guid as inputs to the search files script)

Hi Giusseppe,

Glad it’s working. Yes — skip the name lookup and read the guid straight off the wire instead. Make a small script component with one input (any name, item access) and two outputs, guid and cname:

guid = None
cname = None
p = ghenv.Component.Params.Input[0]
if p.SourceCount > 0:
    obj = p.Sources[0].Attributes.GetTopLevel.DocObject
    guid = str(obj.ComponentGuid)
    cname = obj.Name
    print(cname, guid)

Plug any output of the component you’re hunting for into the input — the data doesn’t matter, the script just reads which component the wire comes from and outputs its guid. That plugs straight into your file search component, no copy paste and no multiple matches.

findComponentInGHFile.gh (8.7 KB)

that saves masses of work…thank you so much

searched 2800 gh files in 6 secs…408 results

All I need to do now is move the found files into another folder…

wombat has a move/copy/rename files component…doesnt like the included results number at the end of the file path…have to eliminate those somehow…searching for the line in the script that prints those numbers

Just out of curiosity, how come you have 2800 gh files? How do you even end up in such situation?

lots of work and experimentation

here is a complete sequence from your Guid and search scripts to clean list to move list

move part can be used for anything if a path replaces the a output

findComponentInGHFile.gh (17.4 KB)