I am trying to listen to a “wire event”, meaning that each time a wire is either connected or disconnected on the canvas, I want to have a little script running.
I found some great material here, posted by @LongNguyen, where the event is triggered each time the source of any component in the canvas changes:
However, his code has a couple of issues that I can’t figure out how to solve:
It actually does not work with all the components, I tested it against a bunch and in my case it only worked with the containers components. What can I do to make it work for all?
The cache seems to be handled badly. If I change my script within the event, the changes do not apply to the components that are already placed on the canvas. However if I generate a new container component, the changes will be taken into account.
This problem is kind of secondary though, as I think I won’t change my script once I solve problem number 1
Hope all this was clear enough…
Looking forward to your answers
Best
… the moment you change a single char in your script and run it, the method “addSourceChangeHandlerToNewlyAddedObjects” will be another method. A method part of ScriptInstanceXYZ+1 and not part of ScriptInstanceXYZ.
Now what happens is the following. You run multiple methods of this. Only closing Rhino and restarting Grasshopper will give you the real behaviour. So as a consequence: Never ever do event handling in script components (if possible).
Furthermore, in my understanding not every component, such as the “point-on-curve” component is derived from GH_Component. It differently receives data and does not contain an input-output system. So my guess is, its not firing for those special components. But I don’t know, @DavidRutten can tell you more.
The only option I see is writing an own observer. This is very complicated and even worse if you try this from a plugin or scripting perspective. I wouldn’t say its impossible but it definitely requires some neat hacks.
What are you trying to do? Maybe there is a better workaround for your Problem. Hope I could help…
Thank you for the fast reply!
Right now I am prototyping within a C# component and might compile later.
This makes lots of sense:
I will try to hack this around and if I come up with anything good enough, I’ll post it here.
What I am trying to do is to watch the GH canvas and have a graph representing connections between groups which update each time the connections are modified. This is why I need to handle properly this wire event. Here is a screenshot to help you to understand better:
Here’s another approach. When activated, it subscribes to the MouseDown event of the canvas control, if the canvas has a wire interaction, it subscribes to the MouseUp event, and when is executed it displays in the rhino console the source and target parameters. The rewire interaction has yet to be implemented. WireSensor.gh (3.6 KB)
When dragging several wires from one parameter to another using Ctrl+Shift.
I haven’t gotten to the graphic part yet exactly on this. I want to do a lesson assistant to facilitate learning GH and in order to understand the algorithm, it should be simplified in an abstract way, with one or two “metagraphs”.