I am trying to extract RGB colours from the viewport view. At the moment, the only I find is to export the viewport to a image file on the hard drive and reload it into an Image. Is there a way to load an image in the Grasshopper cache directly as shown in the viewport without having to go through the export to file/hard drive:
VIEWPORT VIEW > GRASSHOPPER [img] > Other GH operations
Capture viewport as image in cache is surely programmable.
There is no native component
Is loading from cache necessarily ?
What speaks against loading image from hard disk ?
You can using this script
import Rhino as rh
view = rh.RhinoDoc.ActiveDoc.Views.ActiveView.CaptureToBitmap()
To view the image i use Quick Preview from Squid addon
With this python script you can capture viewport and extract pixels colors
capture_viewport.gh (7.7 KB)
Thank you Kahled, this is exactly what I was looking for. It works like a charm except for when the viewport is rendered (using Raytraced) which is the main point of my exercise. Is there a specific reason for it?
Very grateful for your help!
Hi Khaled Sighier,
The code you very kindly shared works very well with any view type apart from the RayTraced view. I cannot find any help on google to associate GetView with Raytraced. Would you have an idea how I can resolved this?
Many thanks in advance.
I don’t think if this possible because in Rhino if you use Capture to file this create a new render,
maybe Grasshopper capture the first passe, but the developers know more about this
Or maybe there is a way to get the image from Canvas Viewport
Oh, but it works (without new coding and as one click solution)
From _PackageManager install Human coded by @andheum
From https://www.food4rhino.com/en/app/bitmap?lang=en coded by @DavidMans
Or bitmap component as recommended by Seghier
First part, set path to directory
In panel, part two represents filename
“!” Between filename and extension render start time is added (for better file identification only) Set extension
Activate viewport for rendering
Change viewport mode for preview, e.g Raytraced with low count passes.
‘!’ Main function If True raytraced render starts (e.g. with higher passes) Loads gives path of last raytraced image
Loads image with Bitmap+ component and can be edited, layer, filter and other and can be
‘!’ This is only a millionth fraction of the possibilities for automation.
AutoRender.gh (18.7 KB)
There is no native GH_component, had to be programmed.
Check this to capture different display modes include raytraced but it is not a final render,
i don’t know if it is possible to add more settings for this mode
capture display views.gh (7.3 KB)
To capture Raytraced viewport you have to give the amount of passes you want.
You have to set up a
https://developer.rhino3d.com/api/RhinoCommon/html/T_Rhino_Display_ViewCapture.htm , set its https://developer.rhino3d.com/api/RhinoCommon/html/P_Rhino_Display_ViewCapture_RealtimeRenderPasses.htm and pass the view you want to capture to https://developer.rhino3d.com/api/RhinoCommon/html/M_Rhino_Display_ViewCapture_CaptureToBitmap.htm on the
A more elaborate example in using the
Here a script that does a capture using the ViewCapture script. Use this method if you don’t want to use the dashed command version.
# Material switcher
# and Raytraced viewcapturing
import scriptcontext as sc
import Rhino.Render as rr
import Rhino as r
import Rhino.DocObjects as rdob
av = sc.doc.Views.ActiveView
cycles = sc.doc.Views.ActiveView.RealtimeDisplayMode
oldpasses = cycles.MaxPasses
cycles.MaxPasses = 2
cycles.Paused = True
Fun fact: very, very long ago I had a Cycles component for the GH canvas (no longer available)