Grasshopper

algorithmic modeling for Rhino

Hey,

Lets say I have function ("growfunction") in my python script that extends a polyline by one segment for each execution, how do I make it so that I can see the polyline "grow" with each loop in my viewport? I've tried using time.sleep() hoping that I could sneak in an "a=polyline", but nothing shows up in the viewport until the end. How can I see the geometry generated in the python script as it executes in a loop?

import time

For i in range(10):

     growfunction(polyline)

     a=polyline

     time.sleep(.1) 'I want to see the growth of the polyline during this pause'

Thanks

Views: 1807

Replies to This Discussion

Hi Lawrence,

the Rhino viewports update when the solution is finished, so putting delays inside the solution will only slow things down and not cause an update. There are three things you can do I think:

  1. Handle your own previewing and cause a viewport redraw instead of a delay. Problem with this approach is that you're going to redraw the viewports in the middle of a Grasshopper solution, so what -if anything- Grasshopper is going to draw is anyone's guess.
  2. Handle your own previewing but don't actually draw anything until the solution is complete. This has the benefit of making sure that the rest of the GH preview is drawn correctly.
  3. Don't loop inside your script, instead use static variables and trigger new solutions.

(3) requires the least amount of code, I'd go for (2) but I'm not sure how to even begin doing that in Python.

Here's #2 implemented in C#.

Attachments:

Attached an example of a "randomwalker" I once wrote. It demonstrates how one might approach this using a Python class, the GHPython sticky and a Grasshopper timer.

Attachments:

Thanks for letting me see your script. I understand how your walk function works, but I am not understanding how st['walker'] works. What exactly does sticky do? And what how does the timer affect the loop? Is your script assuming that each iteration takes exactly 1 ms, so your walker is updated right at the end of each iteration?

Essentially I tried this:

from scriptcontext import sticky as st

st[pline]=mypolyline

if toggle:

    for i in range(100):

        growfunction(st[pline])

        a=st[pline]

As I am typing this, I am thinking a few things: does timer run the script over and over? And would that mean that my loop should not occur inside "if toggle:..."? I've noticed that python remembers data from execution to execution, am I supposed to leverage that in this case? Because I am starting new each time by doing "st['pline']=mypolyline".

The "pulse" of a timer resets the script at a given interval, which makes it necessary to store the previous results (variables, lists, etc.) somewhere that doesn't reset. In fact, this script only remembers the data stored in the sticky variable. Sticky variables are persistent, meaning that they don't reset each time the rest of the script does.

    

Thanks.

Is sticky variable the only way to keep data between each execution? Is sticky variable a grasshopper thing or a python thing?

There are other ways (well at least one), its is Rhino Python thing. See this thread for a good overview and some examples. Thanks for jumping in Marc, been a bit busy lately..

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service