Recently I have been going through Grasshopper definitions written by students. Trying to understand and follow the flow of a definition written by someone else can be incredibly time-consuming. This task is especially difficult when there is no common graphical style, and the definition is very large (thousands of components). As a result, I started playing around with scripts to standardize the look of different definitions and to do some simple analysis. Here I will show an example script (encapsulated in a Python scripting component) that provides a breakdown of the number and type of components used in a definition.
There are several ways to analyze a Grasshopper definition. First you can save the Grasshopper definition as a ghx file (which is xml) and then parse the document and query the right nodes for information. An easier method is to use the native GH_IO namespace - find the rootnode of GH_archive object, then look for the appropriate GH_Chunk objects.A third method requires the Grasshopper definition to be already open and involves directly accessing the active Grasshoper document object.
The Python component in the provided definition contains a script that will list every component used, organized according to their categories and sub-categories. It is broken down into 3 main functions - get_time(), format_dict_info() and analyze_active_ghdoc(). The logic is as follows:
- Get the active grasshopper definition using: ghenv.Component.OnPingDocument().
- Create an empty nested dictionary to store information about the components (1st level - Category, 2nd level - Subcategory, 3rd level - Type).
- Format information in the dictionary as one long piece of text that can then be displayed in a panel.
the moment this script doesn't do all that much, but perhaps it can be
something for you to build upon. For example, I run it to see how many
times students use the custom components I provide; next I would like to
know the context of their use, for example which other components are
they connected to, and are there any common patterns?
For now, you can download the Grasshopper definition here (remember you need to have Python Grasshopper installed already) and let me know how it works!