Infinite while loop freezez in GhPython

I want to get the current time stamp constantly so that I can use it as suffix of a file name.

I added the following script in GhPython component, and it freezes once I click OK.

There is no such issue if I just run it as a normal Python script.

May I ask if this is related to the Python used in Grasshopper, and how to solve this issue?

import datetime
now = datetime.datetime.now()
while True:
…output = now.strftime("%Y-%m-%d_%H-%M")

Er … P is not my game but you are using a while loop (with no break/exit clause) meaning that you are trapped into a rabbit hole.

Hello,

Does your script work if you just assign the output without the while loop?

I believe you’ll need to avoid the loop and trigger the Python component somehow, such as connecting anything that changes when the model changes (instead of the Button below):


time_date_2019Jul8a.gh

import datetime
now = datetime.datetime.now()
Y = now.year
M = now.month
D = now.day
h = now.hour
m = now.minute
s = now.second
1 Like

Thank you, @Joseph_Oster.

Seems there is a little bit of manual work that is unavoidable…

Well, as I said, if you connect all the right outputs of the GH model to the Python input, the time will be updated any time it changes. For example:


loft_height_2019Nov15a.gh (28.4 KB)