How to stop components from automatically displaying results in Grasshopper
When you run a component in Grasshopper, if there are outputs associated to it, the outputs will be visualised in Rhino without you needing to do much. This is part of the ‘preview’ feature of a component, but sometimes, you might not want the users to automatically see the output. For example, if you have a component that produces some debugging data/geometry, which is only useful in a debugging mode, then you might not want these outputs being displayed automatically and giving a potentially messy look to an otherwise clean model.
Of course, you could simply ask the user to turn the preview off, however, it is good practice to only give the users the outputs they want. Plus, if the user is a novice, they may be confused by data they aren’t expecting. This solution can be used to turn that preview function off, which means that any previews have to be specifically requested by turning it back on in the usual manner.
Code to turn preview off for Grasshopper components
The code to turn the preview function off is relatively simple. First, you just need to cast the component to the type of IGH_PreviewObject, and then change the ‘hidden’ property to true. An example of this is seen below.
IGH_PreviewObject prevObj = (IGH_PreviewObject)this; prevObj.Hidden = true;
This can be simplified into one line like this:
((IGH_PreviewObject)this).Hidden = true;
These lines of code can be put in any place where it can be called within your component class. If you put it in the constructor, then the preview function will be off from creation of the component. However, if the user turns preview on and changes the inputs, the preview will remain on. Therefore, you can put this code in the SolveInstance method, which will then turn the preview function off whenever the component runs. This means that if the user has turned it on, it can be turned back off automatically if they change the inputs, removing any potentially messy debugging again.
If you know of a different way to turn the preview off, feel free to leave a comment. This is the way I found during my quick component creation this week, and is presented here as a quick solution for others, but that doesn’t mean it’s the best way.