im trying to write a component that will take a line and show the line as is its a vector (line start point as the anchor point and line end point as the vector). ive found that i can do this using GH_Preview Utility. (see attached images)
the issue im having is that once in Rhino, after ive created a line and plugged it into my component, if i try to adjust the original line (move one of its endpoints for example) the original vector preview (and every following one) is persistent regardless of if i clear the component, freeze, etc.
Thanks for your input i will definitely use your strategy in the future. I was though, finally, able to get the preview utility to work. It involved creating the GH_PreviewUtil object in the components constructor and running its .Clear() method in the solver instance before running the .AddVector(Line ln) method.
… pretty basic stuff, im kind of mad at myself that it took this long to solve
Adds preview geometry (in this case just a point) to the Rhino Scene and Layout view just as I’d hoped, but it doesn’t seem to ‘Clear’ any existing previews? If I change the point value and run again, it adds a second point, but the first point sticks around even though I’ve added a .Clear() call in there?
I think that’s because every time you commit a change to your Python code, Grasshopper interprets that as a brand new component being created (your standard Python script component, by default, is getting a new random Guid every time you make a change to it). So every time you hit the Test button, Grasshopper creates a new instance of previewUtil, keeping the old one still in memory, but not accessible by your “new component” any more.
Okay, I have tried myself.
It seems that the Python component generates a new instance of itself every time it runs.
So here how to make it work in this case:
You need first to switch to GH_Component SDK mode, create a new class for your component, and define a class attribute that will be inherited by all of its instances.
previewUtil = Grasshopper.Kernel.GH_PreviewUtil()
previewUtil.Enabled = True
def RunScript(self, *args):
# Some Code
Then, you can (still without editing the script!) add all the geometry you need to your previewUtil and still be able to clear it in a later moment.
This is great! thanks. I’ll give your method a try and see if it works for my project here. In case it is any help to folks who might stumble on this post in the future: Jumping off this example script from Rhino Developer Docs I was able to make my custom preview work using a GH Python component in a slightly different manner by creating my own CustomConduit and then enabling / disabling that using a simple boolean toggle.