Capture viewport as image in cache

Hi,
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

Many thanks,
Thomas

Hi
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

image


With this python script you can capture viewport and extract pixels colors

capture_viewport.gh (7.7 KB)

3 Likes

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.
Thomas


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 Bitmap+ | Food4Rhino coded by @DavidMans
Or bitmap component as recommended by Seghier

  1. First part, set path to directory
  2. In panel, part two represents filename
    “!” Between filename and extension render start time is added (for better file identification only)
  3. Set extension
  4. Activate viewport for rendering
  5. Change viewport mode for preview, e.g Raytraced with low count passes.
  6. ’!’ Main function If True raytraced render starts (e.g. with higher passes)
  7. Loads gives path of last raytraced image
  8. Loads image with Bitmap+ component and can be edited, layer, filter and other and can be
    saved
    ‘!’ This is only a millionth fraction of the possibilities for automation.

RenToFile

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

2022-02-18_04-44-02

capture display views.gh (7.3 KB)

1 Like

To capture Raytraced viewport you have to give the amount of passes you want.

You have to set up a ViewCapture Class , set its ViewCapture.RealtimeRenderPasses Property and pass the view you want to capture to ViewCapture.CaptureToBitmap Method (RhinoView) on the ViewCapture instance.

A more elaborate example in using the ViewCapture class:

Fun fact: very, very long ago I had a Cycles component for the GH canvas (no longer available)

1 Like