.gif alternative .webp for smaller and nicer grasshopper animations

Hi,

webp is a nice alternative to gif, especially when animated. I noticed up to 10x smaller files and it has more than 254 colors. (no dithering)

I wrote a script using python 3 + PIL to combine exported Images from grasshopper into one webp file.

Everything works so far, but there is a Problem with the export of the images: I used animation controller by @fraguada to generate the images but, during saving the image counter seems to reset and because this value is used to drive the gh animation nothing happens.
Has anyone an idea why this might be?
thanks!

To use install python, PIL


(old) webPmaker.gh (14.0 KB)
(old) webpMaker.py (1.1 KB)

disclaimer: be careful, the python script deletes the generated .pngs; hopefully nothing else

EDIT: this is the latest version which also ca export .gif:
webPmaker_v3.gh (23.1 KB)
webpMaker_v3.py (1.7 KB)

2 Likes

update: to replicate this behavior one doesn’t need to install python or pil: attached is a simplified example using only c# script from animation controller
the ouput looks like this :


but should look somtthing like this: :point_up_2:


webPmaker_simplified.gh (10.1 KB)

edit: also animation control outputs more frames than defined by the slider; which in my case calls the python script twice

edit2 : I modified the animation control script and it now outputs the right range; viewcapture is still empty though


webPmaker_simplified_v2.gh (11.9 KB)

2 Likes

Hey Konrad,

I’ve tried the script in Rhino 6 for macOS and got the same result. The sphere isn’t showing up in the exported image files. I’m just getting the grid.

Maybe @fraguada can chime in on this?

1 Like

hm, I am really confused by this. See this screen capture. All grasshopper geometry is drawn in the rhino viewport, the data recorder shows no abnormalities but it doesn’t show up in the exported images.


even more strange when I set a static value to control the GH geometry it does show up, meaning it’s not a viewmode problem:

Yes, that’s the same result that I got yesterday. I recon there’s something wrong with the C# script. Either it used some kind of “exploit” to make this work that was fixed/updated, or something in the API changed and it thus doesn’t work anymore. It could be a minor detail, that doesn’t cause an error.

Yes, that’s what I gathered. To be honest, I don’t know enough about the “display pipeline” to even imagine how this works (and the C# editor is a nightmare to work with on macOS).

On line 46, var bitmap = capture.CaptureToBitmap(view); captures a bitmap from view. view is defined as the active Rhino viewport on line 33, and capture as Rhino view capture on line 34.
On line 54, bitmap is finally saved.

Maybe you could translate it to Python and debug it line-by-line in order to find the bug? I could help you with that, if you have specific questions.
Or simply wait for the author or another C-Cool wiz to turn up and save the day! :wink:

I’ll take a look.

1 Like

I ran webPmaker_simplified_v2.gh on my Win10/64 system and got 9 png files that were all black. Is this the expected result?

Currently the c# component doesn’t capture the grasshopper geometry properly.
It fails to progress the animation for some reason.

WebPmaker_simplified_v2.gh was indented for discourse.mcneel since it doesen’t requre the use of external python and the PIL library. The output of WebPmaker_simplified_v2.gh are X number of .pngs. These Pngs will be combined by "webpmaker.py" externally into one animated image. In the end this will hopefully be a one-click operation where you set the all the parameters in GH and only hit “toggleRun” once to start the process.

( my bad: the name _simplifed implies it has the same functionality as the one from post 1 but actually it contains a part of the original definition; for better testing )

It works!
EDIT: not quite

The trick was to separate the c# script into two: one for advancing the animation and one for saving the images.
To use you’ll have to install python, PIL and download the new, attached webPmaker.py.
I would consider this script WIP, there might be bugs.

(Old)webpMaker.py (1.3 KB)
(Old)webPmaker_v2.gh (20.2 KB)

EDIT: I just realized that discourse doesn’t allow for webp images to be used :disappointed: Well, I hope this is useful for someone nevertheless. In comparison to loading all images into PS layers and exporting as a animated gif this is much quicker, gives nicer results and smaller filesizes ( 170kb vs 2mb)

image
Untitled-1

4 Likes

Hi everyone,

I think I figured this out – the problem was that Grasshopper computes the viewtoBitmap command before the preview geometry is displayed. To fix it send it to the top of the stack by selecting the cluster and pressing crtl +F (or under edit > arrange> put to front)
Credit goes to @DavidRutten, who answered it a while ago here.

Since discourse.mcneel will not allow for .webp images until there is support for it for every platform I added the option to export traditional .gif files as well.

To use install python and PIL
webpMaker_v3.py (1.7 KB)
webPmaker_v3.gh (23.1 KB)
(I will update the first Post as well with the new files)

animTest

5 Likes

Hi Konrad!

I tried to run this today and everything seems to work except the final stage of converting the PNGs to a video file. I’ve tried to trace it back and get this error in the sorting routine.

>>> print(sorted(pngfiles,key=lambda x: int(os.path.splitext(x)[0].split("_")[1])))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 1, in <lambda>
ValueError: invalid literal for int() with base 10: 'Files\\Grasshopper'

I am running Python 3.10.5 on Windows 10 64bit

Is this something you can help with?

Thanks!