I’m using Python to automate capturing views. The idea is to create a new floating viewport, then iterate through the named views to update the viewport and then run CaptureToBitmap. The problem is that I can only work out how to do this for the active viewport, rather than the one I have just created. I’ve looked everywhere and can’t find the code that either sets the ActiveViewport to the one just made or applies PushViewInfo to a specific viewport rather than the active one.
sc.doc.Views.ActiveView.ActiveViewport.PushViewInfo(Rhino.RhinoDoc.ActiveDoc.NamedViews[i], False)
is what I’m using so far to set the named views to the active viewport (using a for loop)
import scriptcontext as sc
named_views = sc.doc.NamedViews
for nv in named_views:
sc.doc.Views.ActiveView.ActiveViewport.PushViewInfo(nv, False)
sc.doc.Views.Redraw()
Thanks for the reply, I have this already, but the problem is that it applies the named views to the viewport that happens to be the active viewport, rather than the one created further up in the script
#get named views
nv = Rhino.RhinoDoc.ActiveDoc.NamedViews
nv_list = ([v.Name for v in nv])
#take screenshots
for i in range(len(nv_list)):
#iterate through named views
rs.RestoreNamedView(new_view_name)
sc.doc.Views.ActiveView.ActiveViewport.PushViewInfo(nv[i], False)
sc.doc.Views.Redraw()
Thanks for the reply, I might be missing something but it is still taking a screenshot of the viewport that happens to be the active view when I activate the script, rather than the new floating viewport that has been created
In the end I used the command line, which isn’t ideal but forces it to pick the new viewport as the active view. Still not sure why it didn’t have it as the active view from the start but seems to work…
import rhinoscriptsyntax as rs
import System
import Rhino
import scriptcontext as sc
from scriptcontext import doc
import os
width = int(width)
height = int(height)
if activate:
#define width and width of viewport
max_edge = 800
if width > height:
ratio = height/width
vp_height = ratio*max_edge
vp_width = max_edge
else:
ratio = width/height
vp_width = ratio*max_edge
vp_height = max_edge
#print(width)
#print(height)
#placeholder view
perspective = Rhino.Display.DefinedViewportProjection.Perspective
#define rectangle for new view
location = System.Drawing.Point(200,200)
size = System.Drawing.Size(vp_width+16, vp_height+39)
rect = System.Drawing.Rectangle(location, size)
#define new viewport
current_views = Rhino.RhinoDoc.ActiveDoc.Views.GetViewList(True, False)
new_view_name = "MyNewView" + str(len(current_views))
new_view = Rhino.RhinoDoc.ActiveDoc.Views.Add(new_view_name, perspective, rect, True)
#set new view display mode
d_m = Rhino.Display.DisplayModeDescription.FindByName(display_mode)
new_view.ActiveViewport.DisplayMode = d_m
#get named views
nv = Rhino.RhinoDoc.ActiveDoc.NamedViews
nv_list = ([v.Name for v in nv])
rs.Command("SetActiveViewport"+chr(10)+new_view_name+chr(10))
#take screenshots
for i in range(len(nv_list)):
#iterate through named views
sc.doc.Views.ActiveView.ActiveViewport.PushViewInfo(nv[i], False)
sc.doc.Views.Redraw()
#take screenshots
view = sc.doc.Views.ActiveView
view_list = Rhino.RhinoDoc.ActiveDoc.Views.GetViewList(True, False)
if view:
view_capture = Rhino.Display.ViewCapture()
#image_scale_int = int(image_scale)
view_capture.Width = int(width) #* image_scale_int
view_capture.Height = int(height) #* image_scale_int
view_capture.ScaleScreenItems = False
view_capture.DrawAxes = False
view_capture.DrawGrid = False
view_capture.DrawGridAxes = False
view_capture.TransparentBackground = transparent
bitmap = view_capture.CaptureToBitmap(view)
if bitmap:
path = Rhino.RhinoDoc.ActiveDoc.Path
folder = "\\".join(path.split("\\")[0:-1])
print path
folder = folder + "\\Images"
print folder
if not os.path.exists(folder):
os.makedirs(folder)
bitmap.Save(folder + "\\" + str(nv_list[i]) + ".png"
, System.Drawing.Imaging.ImageFormat.Png);
#print(path)
#print(view)