Grasshopper

algorithmic modeling for Rhino

Hi, recently I have been trying python in grasshopper. 

Does anyone know how to convert this C# script(with Timer) to python script? 

I just can not figure out.... 

would be very appreciate, thanks! 

CSharpTimer.gh

Views: 8743

Replies to This Discussion

I tried this following script and found it so weird........ get error because the variable haven't been declared. 

sometimes it works, sometimes not... I have to open the definition and setup the variable first and delete it..... quite strange. 

===============

N = 0 (once it works, I open again and delete it)

print("N = %d" %N)

N = N + 1

if(y):

    N = 0

===============

hello,

use the following.

best,

M.

ps. I can't seem to be able to replicate this error.

This is a very nice example! One way to avoid the UnboundNameException if Run is False the first time, would be to check the globals dictionary for the presence of the variable. Something like this (attached).

if reset or 'counter' not in globals():
    counter = 0
else:
    counter += 1

a = counter

Giulio

--

Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

Attachments:

Thank you Giulio,

Actually it always makes sense to perform this check in cases like that, to make your script "user friendly" and error free.

Thanks for pointing that out.

M.

You can also use the scriptcontext sticky for situations like this. Works quite well. Attached a simple example. Hope that helps..

Edit: Marios beat me by a full minute. Nice one ;)

Attachments:

Lol!

Nice edit: I think I have been at the same place quite a few times by you. maybe we should keep a score board. :)

Hehe, consider it on :D

Marios and Andres, thanks you so much!!! 

Both works great, but it seems that sticky is more flexible to extend. 

I did a little practice :) make a ball moving, so exciting haha.... 

now trying to make it more complicated like Classes.

Any suggestion how to make a better code or cleaner? 

anyway, thanks!! 

Attachments:

The classes chapter on this book is quite useful to understand classes and inheritance. If you can code in an oop way in another language you won't be having any issues. The bouncing ball example is perfect for starting working with classes.

Best,

M.

hey guys, recently I wrote a python swarm component using class :) 

please have a check here. I upload the example definition file, so please try it and do something more fun!

Dear All,

Can someone please explain how recursion takes place in Python within grasshopper. I mean when you use a GH timer recursively apply a python script.

When the timer re-runs, do the variables automatically get saved to the memory? And does that apply to more complicated data like "Lists" "Arrays"...etc?

Thanks

Just simplify put your script in the following structure:
if reset:
    # This is to set up your initial variables
else:
    # Do your loop function here

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