algorithmic modeling for Rhino
Seen a few posts around this topic but nothing super straight forward.
I'm stuck. Could someone help me re-write this C# code (which works fine, although it may be ugly code) in Python? It would be super helpful. Thanks...
C# Code:
private void RunScript(object x, object y, ref object A)
{
RhinoDoc.SelectObjects -= GetSelected;
RhinoDoc.SelectObjects += GetSelected;
if (rhinoObject != null)
A = rhinoObject;
}
// <Custom additional code>
RhinoObject rhinoObject = null;
void GetSelected(object sender, RhinoObjectSelectionEventArgs args)
{
rhinoObject = args.RhinoObjects[0];
Component.ExpireSolution(true);
}
Tags:
It SHOULD be possible, considering that IronPython is written in C#. That said, it might not be straightforward and may be more complicated than just writing it in C#. Do you have a GH definition which exemplifies (in C#) what you want to do?
Uh, just remembered that I did dabble a bit with eventhandlers last year (never went anywhere with it though). Attached a simple example that might help.
Anders (et. all), thanks for the responses...
I'm attaching a file with a few C# examples, my test is not grouped. There's a quite complex example from Rutten in the upper corner. I added a bit more to mine so it at least works properly for one selected object. The code is a bit hacky, but I'm just trying to get the pattern down for how this would work.
So the globals is some dictionary that the program uses to store stuff like events and other things that need to be remembered?
I'll work with your example, and upload any improvements I come across.
Thanks so much...
It depends on whether or not you want the eventhandler to be unique to the individual component (in which case: use globals). Based on Tom's suggestions, this is probably not the case though (as you'd have to come up with a way of disposing of the handler again). I just looked up my previous tests and I indeed did use sticky on the first attempt (attached). I've been using a similar pattern for rendering stuff from within GHPython components (that is: stuffing CustomDisplays into sticky, which means you can always Clear/Dispose them even after deleting/closing the components which created them in the first place). None of this is particularly pretty and I'd definitely prefer cleaner ways of going about it. Let me know if you come up with anything ;)
Edit: Hang on, just remembered where I picked this up (from Steve).
Here are the two functions I use for the CustomDisplay to sticky workflow. Might help.
Anders... I was playing around with the global based event script you posted and am having a hard time understanding its behavior.
I added two lines of code which basically updated a sticky counter and set a sticky note to "Event Triggered", or something like that, and then ExpireSolution is called. What makes no sense to me, but probably has a good explanation, is that the event seems to be triggered correctly in that adding an object to the RhinoDoc causes the component too recompute, so I assume the ExpireSolution is being called. However, the code above it, the sticky counter + note does not seem to be running. Why would the ExpireSolution run and not the code above it? Do I need to reset the scriptcontext? Is the function being called somewhere else whereby the sticky code somehow doesn't make sense but the component ExpireSolution does? I'm very confused...
The hope here is that I can not only trigger a re-compute, but also do something else inside the event handler.
Thanks in advance if you have time to respond... I'll post whatever solutions I can come up with here in the end.
Also, I tried Steve's code. It works in the Python Editor in Rhino, but won't work in a Python component. Yet another mystery to me...
sorry... forgot to add the file... here it is
Do you mean that you're not seeing your sc.sticky["counter"] variable increment each time your addRhinoDocEvent() is being called? Two things might be causing this (at least):
1) You'll need to retrigger the component in which you're inspecting the sticky (and the counter variable) to see that the variable has been incremented by the event function.
2) You'll need to be careful not to reinitialize the counter value to 0 (which would happen if you recompute the GH canvas).
See attached example. Bit out in the weeds here, maybe Giulio will show up and set us all straight :)
Anders... thanks so much. I had turned off the initial counter component to avoid resetting it, but adding a button makes it explicit. I'm not sure why this wasn't totally working for me, but it appears like it is now. I'm going to continue to test and will post results when I can.
Thanks again!!!
No worries, looking forward to seeing what you come up with.
Exactly, in the example I posted above I tried getting around this by checking if an event already exists in the global scope of the GHPython component before assigning it. That said, that still doesn't solve how to unassign after having been created.
yeah... i've definitely run into this with my Python attempts. even though i can't get the functionality to work, it very quickly starts to take longer and longer to calculate, or gets into a feedback loop. don't fully understand all of this, i'm just learning, but your point is well taken.
Welcome to
Grasshopper
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
© 2024 Created by Scott Davidson. Powered by