Use HOPS to make a Viewport Capture Function

I am planning to create a modular HOPS function and run it in Rhino.Compute. My goal is to use Grasshopper to design patterns and export the drawings in bitmap format. To achieve this, I utilized the Rhino.Display.ViewCapture.CaptureToBitmap(settings) method as the core function. I wrote a Python script within the Grasshopper Python component, and it works well within the Grasshopper environment.

import Rhino
import System.Drawing
import Rhino.Geometry as rg
import Rhino.Display as rd
import scriptcontext as sc
import os
import System, operator
result = []
roaming = []
# Get x and y coordinates of frame vertices
px, py = set(frame.X), set(frame.Y)
# Get horizontal and vertical lengths
dx = abs(reduce(operator.sub, px))
dy = abs(reduce(operator.sub, py))

# Construct the points that will set the print area
offx = (dx/dy) * (3/factor)
offy = (dy/dx )* (3/factor)
p1 = rg.Point3d(min(px) + offx, min(py) + offy, 0)
p2 = rg.Point3d(max(px) - offx, max(py) - offy, 0)


# Set resolution to chosen scaling factor
pix, piy = dx*factor, dy*factor


def Capture():
    
    RhinoDocument = Rhino.RhinoDoc.ActiveDoc
#    print(RhinoDocument)
    if RhinoDocument != None:
        roaming.append(RhinoDocument)
    else:
        roaming.append('No RhinoDocument')
#    print (roaming)
    try:
        view = RhinoDocument.Views.Find(selectview, False)
    except Exception as a:
        roaming.append(a)
    size = System.Drawing.Size(pix, piy) 

    settings = rd.ViewCaptureSettings(view, size, 300) #view, size, dpi
    settings.SetWindowRect(p1, p2)
    settings.RasterMode = True
    settings.DrawGrid = False
    settings.DrawAxis = False
    settings.DrawWallpaper = False

    bitmap = rd.ViewCapture.CaptureToBitmap(settings)
    print(bitmap)
    bitmap.Save(path+filename+".png")


if save:
    try:
        if not os.path.exists(path):
            os.makedirs(path)
            result.append('new folder created')
        Capture()
    except Exception as e:
        alarm1='!An unexpected error occurred: '+str(e)
        print(alarm1)
        result.append(alarm1)
    else:
        alarm2 = str(filename)+'output successful.'
        print(alarm2)
        result.append(alarm2)
else:
    pass

However, when I tested this functionality using the HOPS function, it did not work as expected.

As I debugged the issue, I found that the error occurs because, when HOPS calls this property, Rhino.RhinoDoc.ActiveDoc cannot be found. This results in the ‘Views’ and ViewCaptureSettings also being unavailable, leading to the failure of the function.

By the way, vanilla Gradient Hatch(GHatch) could not be used in HOPS.

In my understanding, HOPS is a Rhino.Compute headless server, which likely explains why it doesn’t support display functions. If this is the case, is there an alternative method to achieve my goal of exporting drawings in bitmap format?

I am considering generating a new RhinoDocument and configuring a different view setting within it. However, when I use Rhino.RhinoDoc.Create(doc_name) to create a new RhinoDoc, the Grasshopper Python component’s terminal becomes unresponsive, and nothing is displayed anymore. I suspect this issue occurs because the active document changes, which prevents me from debugging further.

Look forward to your help and feedback.