GetPageViews or get RhinoView from file in the RhinoCommon API

Hello

I´ve been trying to use the Rhino.Display.ViewCapture.CaptureToBitmap() from the RhinoCommon API but it is returning None:

import Rhino

rhino_file_viewtable = rhino_file_instance.Views
viewportinfo = rhino_file_viewtable.FindName(“Top”)

rhinoviewport = Rhino.Display.RhinoViewport(viewportinfo)
viewcapturesettings = Rhino.Display.ViewCaptureSettings(rhinoviewport, 400, dpi)
bmp = Rhino.Display.ViewCapture.CaptureToBitmap(viewcapturesettings)
print (bmp) # RETURNS None

I suspect it is due to the rhinoviewport not being a RhinoView, which is returned by the GetPageViews () when using scriptcontext.

Any ideias on how to use the GetPageViews in RhinoCommon API (returning a RhinoView?) or alternativelly, getting a RhinoView from an existing file?

Thanks

Hi @alekrd,

It is not possible to capture views directly from a File3dm object. View capture only works with Rhino views attached to a currently open document.

– Dale

Hi Dale!

Thanks for your reply!

Is there any way to use the Rhino.Display.ViewCapture.CaptureToBitmap or similar? (the idea is to write an image file from a viewport)

Thanks!

@alekrd - how about this?

import Rhino
import scriptcontext as sc
import System

def test_view_capture():
    view = sc.doc.Views.ActiveView
    if view:
        view_capture = Rhino.Display.ViewCapture()
        view_capture.Width = view.ActiveViewport.Size.Width
        view_capture.Height = view.ActiveViewport.Size.Height
        view_capture.ScaleScreenItems = False
        view_capture.DrawAxes = False
        view_capture.DrawGrid = False
        view_capture.DrawGridAxes = False
        view_capture.TransparentBackground = True
        bitmap = view_capture.CaptureToBitmap(view)
        if bitmap:
            path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop)
            filename = System.IO.Path.Combine(path, "test_view_capture.png")
            bitmap.Save(filename, System.Drawing.Imaging.ImageFormat.Png)

test_view_capture()

– Dale

Dale! Thanks for your help!

Is there any way to use the scriptcontext externally from Rhino/grasshopper?

I am scripting this in pycharm using the RhinoCommon API - the rhino_file_instance is an instance of a opened rhino file (opened via Rhino.FileIO.File3dm.Read(str(rhino_file_path)). scriptcontext can not be used as an external API, correct?

Hi @alekrd,

I’ll assume you are using Rhino.Inside CPython, per this example:

In order to capture a view to a bitmap, a Rhino view must be visible. View capturing uses OpenGL. Thus an OpenGL view (e.g. a Rhino view) must be visible and operational.

So yes, you can do a view capture from CPython as long a Rhino is visible.

Does this make sense?

– Dale

Hi Dale!

Yes, I am using the Rhino.Inside CPython.

Is there any way to get any view from a rhino file instance opened via Rhino.Inside CPython? ( Rhino.Display.RhinoView or Rhino.Display.RhinoPageView). I suppose only these two kind of views work to export bitmaps or pdfs right?

I have tried to make it work via the viewportinfo:

but it doesnt work.

Capturing views using Rhino.Inside CPython is currently not going to work. This is something we are hoping to implement in the near future.

Rhino.Inside CPython currently only supports Rhino running in what we call a “headless mode”. This means that non user interface is available. It actually is possible to still use OpenGL to create captures without user interface, but the core Rhino display code needs some restructuring to be able to do this.

Hi Steve / Dale

Thanks for your reply.

Is there any alternative I could try? Do you think it can work by creating this script inside rhinoceros and running it from rhinocommon or any other alternative?

Thanks!

ViewCapture won’t currently work when running Rhino.Inside CPython. You need to be running Rhino with full user interface for this functionality.

I see

Is there any way I can run scripts within a running Rhinoceros from outside Rhinoceros?

My idea is to have a Rhinoceros program running and control it via another program (we are developping a script in python outside Rhinoceros).

You could control Rhino via COM
https://developer.rhino3d.com/api/rhinoscript/introduction/external_access.htm