Get Grasshopper Document Object Count without opening grasshopper

I’m wanting to do a sort of ‘health check’ on our company grasshopper files.
I’d like to get some information from each file such as:

  • object count
  • author
  • plugins
  • used components
  • preview mode (hidden, wireframe, shaded)
  • preview mesh setting (disable, low, high, document etc)

I was hoping to do this in a c# console app using GH_IO.dll
So far all i can do is read a .gh file from a filepath and write it to .ghx
This seems a pretty useful start, but not enough.
(thanks @DavidRutten for posting ghxconverter.gh a while back)

I loathe the idea of doing xml scraping Python: Parse ghx files with ElementTree? i think @chanley settled on
@andheum’s excellent solution from within grasshopper.

except for this problem

has anything changed since 2017? Any suggestions to take this further?

This is my test app so far

Program.cs (1.1 KB)

Use GH_IO directly to inspect the files. Get the root object and start looping over subchunks and items. You can use the Grasshopper FileViewer.exe to look at the names of chunks in the file hierarchy, so you’ll know what to look for at each level.

At home now with only an iPad, but I can code up a small example later today.

Thanks David,
I haven’t come across the grasshopper file viewer before. Will check it out when I’m back in the office.
So you can get the chunks from GH_Archive.GetRootNode()? I thought I tried that (commented out in screen shot) but mistook it for a dead end. Yes please an example would be nice!

Program.cs (6.8 KB)

GhFileAnalysis.zip (44.0 KB)

This is a typical function which extracts and displays some information stored in a GH_Archive instance. The code could probably have more error checking if you want it to deal with corrupt files too, although it is extremely unlikely that a *.gh file is corrupt and still in any way, shape or form readable. The file is compressed so any damage to it at all will cause a failure in decompression.

    private static void DisplayDocumentStatistics(GH_Archive archive)
    {
      var root = archive.GetRootNode;
      var definition = root.FindChunk("Definition");
      var header = definition.FindChunk("DocumentHeader");
      var properties = definition.FindChunk("DefinitionProperties");

      var archiveVersion = root.GetVersion("ArchiveVersion");
      var pluginVersion = definition.GetVersion("plugin_version");
      var documentId = header.GetGuid("DocumentID");
      var documentDate = properties.GetDate("Date");
      var documentName = properties.GetString("Name");

      Console.Write("Archive Version: "); WriteLine(ConsoleColor.DarkCyan, archiveVersion.ToString());
      Console.Write("Plug-In Version: "); WriteLine(ConsoleColor.DarkCyan, pluginVersion.ToString());
      Console.Write("Document ID:     "); WriteLine(ConsoleColor.DarkCyan, $"{{{documentId}}}");
      Console.Write("Document Date:   "); WriteLine(ConsoleColor.DarkCyan, documentDate.ToLongDateString() + " " + documentDate.ToLongTimeString());
      Console.Write("Document Name:   "); WriteLine(ConsoleColor.DarkCyan, $"'{documentName}'");
    }
5 Likes

Excellent thank you!

I am trying to use the Gy file analyzer. Is there a way to export the info into a text file? or an excel file?

If you want a “framework” app which finds all the relevant files, either .gh or if you want to analyze the files as text, as .ghx (xml format) you can use the GhToGhx app which has loops which handles all these cases, and if you wish, converts any gh (file below a chosen root folder) into .ghx format, without modifying any original files (it cleans them up too).

Add (or uncomment) David Ruttens code, which I think (IIRC) still remains in the source files, although commented. Find the source code at bitbucket.

bild

My own usecase is that I convert all gh-files on disk to ghx.gz (compressed gzip) and then I scan those files using PowerGrep in order to collect all kinds of info and statistics. Converted files are placed in subfolders ("…\…\_ghx") which are easy to remove afterwards (commands for removal is in the meny).

// Rolf

1 Like