Here’s the component I usually use to document Rhino/Grasshopper modelling stuff:
"""
Captures the Rhino viewport to an image file. Places the image in a folder called
Captures in the same directory as the GH definition. Names the image the same as
the GH defintion followed by _hourMinuteSecond. Edit the script directly to set more options.
Note: Do not use this script with the GH button, freezes up the canvas for some reason.
Inputs:
Toggle: Activate the component using a boolean toggle. Do NOT use a Button {item,bool}
BackgroundColor: Set the color of the Rhino viewport background {item,color,optional}
Grid: Turn on the grid {item,bool,optional}
WorldAxes:Turn on the world axes {item,bool,optional}
CPlaneAxes: Turn on the cplane axes {item,bool,optional}
OpenFile: If true the image will open in the default application {item,bool,optional}
Outputs:
Path: The path of the captured image {item,str}
Remarks:
Author: Anders Holden Deleuran
License: Apache License 2.0
Version: 160805
"""
ghenv.Component.Name = "ViewportCaptureToFile"
ghenv.Component.NickName ="VCF"
import os
import datetime
import System
import scriptcontext as sc
import Rhino as rc
def checkOrMakeFolder():
""" Check/makes a "Captures" folder in the GH def folder """
if ghdoc.Path:
folder = os.path.dirname(ghdoc.Path)
captureFolder = folder + "\\Captures"
if not os.path.isdir(captureFolder):
os.makedirs(captureFolder)
return captureFolder
def makeFileName():
""" Make a string with the gh def name + current hourMinuteSecond """
# Make hour minute seconds string
n = datetime.datetime.now()
ho,mt,sc = str(n.hour),str(n.minute),str(n.second)
if len(ho) == 1:
ho = "0"+ ho
if len(mt) == 1:
mt = "0"+mt
if len(sc) == 1:
sc = "0"+ sc
hms = ho+mt+sc
# Get name of GH def
ghDef = ghenv.LocalScope.ghdoc.Name.strip("*")
# Concatenate and return
fileName = ghDef + "_" + hms
return fileName
def captureActiveViewToFile(width,height,path,grid,worldAxes,cplaneAxes):
"""
Captures the active view to an image file at the path. Path looks like this:
"C:\\Users\\adel\\Desktop\\Captures\\foooo_112747.png"
"""
# Set the script context to the current Rhino doc
sc.doc = rc.RhinoDoc.ActiveDoc
# Get the active view and set image dimensions
activeView = sc.doc.Views.ActiveView
imageDim = System.Drawing.Size(width,height)
# Perform the capture
try:
imageCap = rc.Display.RhinoView.CaptureToBitmap(activeView,imageDim,grid,worldAxes,cplaneAxes)
System.Drawing.Bitmap.Save(imageCap,path)
rc.RhinoApp.WriteLine(path)
return path
except:
raise Exception(" Capture failed, check the path")
# Set background color
if BackgroundColor:
rc.ApplicationSettings.AppearanceSettings.ViewportBackgroundColor = BackgroundColor
# Capture
if Toggle:
if not Width:
Width = 1920
if not Height:
Height = 1080
capFolder = checkOrMakeFolder()
fileName = makeFileName()
try:
path = os.path.join(capFolder,fileName + ".png")
Path = captureActiveViewToFile(Width,Height,path,Grid,WorldAxes,CPlaneAxes)
if OpenFile:
os.startfile(path)
except:
raise Exception(" Capture failed, save the GH definition")
181217_ViewportCaptureToFile_GHPython.gh (6.7 KB)
Should get you going with capture bits at least. I’d imagine you can use the os
module to send the file to a printer as well (using os.startfile(filename, "print"
).