algorithmic modeling for Rhino
Hello,
I'm sorry for the relatively simple question, I'm sure some folks have solved similar issues in other discussions, nevertheless, I was unable to adapt their solution to my case.
Here's what I need:
I'd like to achieve a script in Python that iteratively throws some inputs at a GH script and harvest its results saving them to an external file.
Stripping down the issue to its bare bones this is what I would need to achieve:
Where the ultimate goal is obtaining a file containing, line by line, the results of all the possible multiplications of the first 5 natural numbers.
The problem is that the left component runs the entire loop internally before passing the x and y values to the multiplication component. I need the GH document to recompute every time the left Python Script reaches the end of the nested for loop.
As extensively explained by David in this discussion, I believe I need to use the ExpireSolution(True) method, but I'm not sure how to call it in my case. Could anyone please show me how?
Thank you very much in advance.
Cheers,
Matteo
Tags:
Hi Matteo, I think you might be overcomplicating the logic of your solution a bit here. Unless I am misunderstanding the problem/goal you shouldn't need to expire solutions. A simple approach might instead be to use the itertools module to generate the combinations, then loop over these using list comprehension to calculate the products, and finally write these to a file. Like so:
Dear Anders,
First of all, thank you very much for your prompt answer.
Yes, the example I've brought is very trivial and can be indeed achieved within one Python component. I'm still, however, interested in how to solve the problem keeping the structure of the grasshopper document as is in my example:
In reality, the project I'm working on is actually much more complex. In a nutshell, I have a bunch of parameters that determine the shape of a building (like the number of floors, dimensional ratios etc...). Then I perform a series of analysis on that model using some plugins, such as a solar radiation analysis with LadyBug etc. What I need is to create a spreadsheet in which I associate those initial parameters with the output of the analysis. A spreadsheet that would enumerate all the possible cases: for 4 floors, the solar radiation is 100, for 5 floors, the solar radiation is 97, for 6 floors is... I need a method to automate this process so I don't have to move the sliders and record the output manually, as each iteration requires quite some time to be computed and needs more than one slider to be adjusted.
I've decided to bring up a way simpler example than the whole thing, in which I replaced all the complex geometry generation/analysis components with a simple multiplication component to avoid cluttering the thread with irrelevant logic...
I guess I could still achieve the same result by figuring out a "master slider" that would control all the others, and animate it. Nevertheless, I would love to learn how to do it in Python, for future reference.
Ah I see, so the real problem is more of a brute force search problem then. In that case I would recommend the "Brute Force" component that comes with TT Tools. It cycles through all the combinations for a list of sliders:
Ps. for saving/reading the data to file I would recommend using the Pack/Unpack component that comes with TreeSloth (as you can structure/save whatever you'd like to including geometry and other types that won't easily cast to a string).
Pps: If you do want to expire/schedule solutions through GHPython you can access the GHPython component and its GH document like so:
ghComp = ghenv.Component
ghDoc = ghenv.Component.OnPingDocument()
Thank you for your help.
Very appreciated.
Ehm,
Sorry to keep bothering, but I really desire to understand how would I make this thing work, independently whether it is or not the most efficient solution.
I understand that expiring the solution of the entire document is quite an extreme approach, but how would I do that?
I've tried this without success:
Could also someone provide a simple example on how to just expire this specific component? I've tried in many ways but I've failed...
No worries, knowing this kind of stuff can be useful in many cases I find.
To expire a GHPython component from within itself you can call this (I forget which argument i.e. True/False is appropriate, but I guess that also depends on the specific case):
ghenv.Component.ExpireSolution(True)
To expire another component from within a GHPython component you can do something like this (setting the value of a slider and then expiring it, nevermind the whole carussel bit). Hope that helps a bit :)
Not really that strange :) Calling ghenv.Component.ExpireSolution like that will simply cause the component to continuously recalculate the script (at each iteration of the inner loop no less). I'm afraid I don't understand what you are trying to achieve. Perhaps you are looking to make a persistent variable and update this over time (using a Timer or through code)?
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
© 2024 Created by Scott Davidson. Powered by