Grasshopper

algorithmic modeling for Rhino

Is there anyone who has experience in using the PythonEditor to run a grasshopper file. 
I'm trying to generate information in grasshopper and once the first loop is done, change the values in the sliders and run the same process again. 
Unfortunately you can not backloop in grasshopper so i was told to use the PythonEditor.
But i can't find any examples of how to combine this with grasshopper. 


Thanks. 

Tijl

Views: 1491

Reply to This

Replies to This Discussion

Hi Tijl,

who told you to use the PythonEditor? It seems like you want to achieve something similar to Galapagos. Set a few slider values, run a solution, harvest data, repeat.

Grasshopper has a completely open SDK so this kind of stuff is definitely possible, but the easiest would be to do so from within Grasshopper. I.e. you can write an object (typically in C# or VB.NET) that sits inside the Grasshopper document and which can take control of the grasshopper solutions.

Which programming languages (apart from Python) are you familiar with?

--
David Rutten
david@mcneel.com
Seattle, WA
Hi David,
Thanks for pointing me to Galapagos! As far as i can judge now it seems to be a very powerful tool! But for me at this point the tool is a little too advanced.
My idea works on pretty much the same ideas as Galapagos, but i would like to have the process something different.
My goal is to use grasshopper as a dataBase generator, without any optimization in it. And to have the system work through all the possibilities one by one, so no generic algorithms (yet). This is because i want to generate all the options possible and then start comparing the options.
One thing that galapagos does that i am looking into is the changing the sliders in accordance with the outcome. This 'backlooping' is very interesting for me, since i don't know how long the generating of data takes i can't do this with a timer. (i'll add my first try with a timer).
To 'hack' my way round this problem Giulio advised me to work with the EditPythonScript editor. (see his reaction below!) I'm now trying to understand his code and see if this works for me.
Unfortunately i don't have any other programming skills than Python, but managed to work with VB if a tutorial helps me converting the actions and keeping the code very simple and basic...

Thanks for your reaction!
Tijl
Attachments:
Oh, I see that "theEngine" script is not working anymore with 0.8.3... it seems there has been a change in behavior in the Grasshopper SDK and I did not account for the possible change.

Please, if you use the new version, use this new file. Or check the new Grasshopper timer component.

- Giulio
_______________
giulio@mcneel.com
McNeel Europe, Barcelona
Attachments:
David,
I've been going trough some older posts and one of your comments is:

"But note that you're not supposed to update sliders while you're inside a solution. So if you're doing this from within GH_Component.SolveInstance, there's really no way this is valid, as you're expiring a slider while a solution is being computed." (http://www.grasshopper3d.com/forum/topics/scripting-sliders?id=2985...)

So i was wondering if it is wise to study the PythonEditor? Can i achieve my goal with this, or is this only to update the values once?
I do seem to have trouble with accessing the sliders and anything else but a rhino command.. (line, circle, point, etc)

So would it be possible to have a component like Galapagos, but then less fancy? Only adding 1 to a slider if the solution is ready? Continue doing this until the slider is at its max,
then that add 1 to an other slider(no2) if the first slider has reached its max, and start at min with the slider(no1) again??

see previous file where i do this with a timer.

Thanks,
Tijl
Hi Tijl,

you can definitely code this up. Either from a Script component or as a GHA library. I'm happy to write something for you, but you'll need to tell me which language you're most comfortable with C# or VB? Script component or Visual Studio project?

--
David Rutten
david@mcneel.com
Seattle, WA
Hi David,
Unfortunately i can only work with Python (until now). But it must be possible to learn C# as well (or at least some of the basics). Most people i work/study with, are very comfortable in C#.
I don't run Visual Studio, but work in eclipse most of the time (but this i only use for python, but can offcourse be useful for other languages).

I still want to go through your manual to learn how to learn how to script in Rhino. (RhinoScript101) so this will be my next step then.

If you could write something, that would be great!!

Thanks,

Tijl

I've been looking for a solution to this on the forum: controlling a grasshopper model using C# in a VS studio project. This post is the closest I have found. Can you tell me how to go about this? I have discovered, as noted above, that you can't update sliders while you're inside a solution.

 

Thanks,

 

Kirk Martini

Hi Kirk,

how is this development going? If you still need some help, could you please start a new discussion? (this one is a bit old)

Thanks,

- Giulio
__________________
giulio@mcneel.com
McNeel Europe

Hi Tijl,

it's a bit of code, but here is how you can do it. This sets a circle into Grasshopper and receives the result. In the EditPythonScript editor:

import clr
clr.AddReferenceByName("Grasshopper")

import System
import Rhino
import Grasshopper
from Rhino import RhinoApp, RhinoDoc
from Rhino.Geometry import *

gh = RhinoApp.GetPlugInObject("Grasshopper")
gh.LoadEditor()
gh.CloseAllDocuments()
gh.ShowEditor()
gh.OpenDocument("C:\\Users\\Giulio\\Desktop\\test.ghx")

docServer = Grasshopper.GH_InstanceServer.DocumentServer
doc = docServer[0] # first opened document

def FindBatteryByNickname(docObjects, name):
    if docObjects is None: return None

    for obj in docObjects:
        attr = obj.Attributes
        if attr.PathName == name:
            return obj
    raise Exception(name + " was not found in document")

param = FindBatteryByNickname(doc.Objects, "myBegin")
if not (param is None):
    param.ClearPersistentData()
    newCircle = Circle(Point3d(1,11,111), 32)
    param.AddPersistentData(newCircle)          # you must add the right type here!
    param.ExpireSolution(True)                  # recomputes the whole solution
    
    param = FindBatteryByNickname(doc.Objects, "myResult")
    if not (param is None):
        ghCircles = param.VolatileData          # access persistent data as a tree
        ghCircle = ghCircles[0][0]              # a list of all grasshopper-types
        #circle = ghCircle.Value                # access the "real" circle
        rhinoDoc = RhinoDoc.ActiveDoc
        ghCircle.BakeGeometry(rhinoDoc, rhinoDoc.CreateDefaultAttributes(), System.Guid())


The file I had on the desktop is attached.
Also, you get autocompletion with types after using clr.AddReferenceByName the first time, which is pretty useful.

- Giulio
________________
giulio@mcneel.com
McNeel Europe, Barcelona
Attachments:
Giulio,
Thanks for this piece of code! It seems that the last line lacks a small bit of information though. I haven't been able to replace or add this..
I'm trying to understand what is happening with this code and than change it so it suits my wishes!
Again thanks for all your help!

Tijl
Sure, here the whole text in a file.

- Giulio
________________
giulio@mcneel.com
McNeel Europe, Barcelona
Attachments:
Thanks! Now its digging into the code and extending it to my wishes!
Wonderful!

Tijl

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

© 2019   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service