Rhino.Display.CustomDisplay using Python Grasshopper Component
We are having a small talk over on the Grasshopper forum discussing certain issues with displaying custom geometry and text in the Rhino viewport using Python from within a Grasshopper component. Here is the first post outlining the main hurdles:
"Following David's example in this thread I have been trying to implement the RhinoCommon CustomDisplay class, but using Python instead of VB. I think I got the basics down of how to add an instance of the class, add some custom geometry to it and update it properly (i.e. do a "redraw") each time the component inputs update (see the attached file for an example). You will notice that there is a boolean toggle which takes care of adding the geometry to the CustomDisplay when "True" and disposes of it when it is set back to "False". Now my main problem is how to dispose of and delete my CustomDisplay instance when the following events occur:
1) If the user alt+tabs to another Grasshopper document.
2) If the user closes the Grasshopper document
3) If the user closes Grasshopper
From David's example I've deduced that one needs to access the Grasshopper SDK and get the current "GH_Document" (see also this thread) and then from there make calls to this document and see whether it is enabled etc. I've barely scratched the surface of accessing the GH SDK from Python (except this), so I'm a little lost at sea here! I have tried trawling through the SDK documentation, but I am having a hard time translating most of the calls to Python. For instance how to get the current GH document using this.owner.OnPingDocument();
Any pointers would be greatly appreciated!"
Seeing as how David is not really a Python guy, I thought it wise to post over here as well. Hopefully Steve or someone else who has tried something similar may chime in. You'll notice that the issues presented aren't really that huge of a deal, but perhaps this would be a good occasion to discuss respectively custom displays and event handling?
I missed this conversation till today, but maybe this will help:
have you tried accessing the ghenv variable? It contains references to the environment the script is running in, so you can access the Python implementation, the Grasshopper component, etc.
Hope it helps.
It's always good you hear back from you.
Following the "small talk", I found two posts by you (1, 2), and started looking to ghenv. It has been so helpful and solved multiple problems that I had before and partially solved this problem, however it is still not easy to implement it. As an example I have been struggling to re-write David's example for customizing the Menu from Extending the GUI (C#) in Grasshopper SDK in Python for multiple days, and still I found no way to override the AppendMenuItems!!
Do you know of any other resources more than the two links above that I can look for some examples of using ghenv? It will be extremely helpful.
Hi Giulio and Mostapha,
This is great! I hadn't tried accessing the ghenv, but it certainly looks like the right way to go.
Can't wait to dig into the progress you've been furthering on this subject Mostapha. Really exciting work!