How to apply a named view to a specific viewport

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)

Thanks!

Didn’t you forget to redraw the views?

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()
    #Create a new floating view
    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
    
    active_view_name = doc.Views.ActiveView.ActiveViewport.Name

    all_view_names = [(view.ActiveViewport.Name) for view in doc.Views]
    all_views = [(view) for view in doc.Views]

#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)):
        #print(sc.doc.Views.ActiveView.ActiveViewport.Name)
        #iterate through named views
        #Rhino.RhinoDoc.ActiveDoc.Views.GetViewList(True, False)[1]
        
        rs.RestoreNamedView(new_view_name)
        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()
            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);

Hi Mahidiyar,

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()
import System
import Rhino
import scriptcontext as sc

projection = Rhino.Display.DefinedViewportProjection.Perspective
rect = System.Drawing.Rectangle(0, 0, 600, 600)
new_view = sc.doc.Views.Add("Test", projection, rect, True)

shaded_id = Rhino.Display.DisplayModeDescription.ShadedId
display_mode = Rhino.Display.DisplayModeDescription.GetDisplayMode(shaded_id)
new_view.ActiveViewport.DisplayMode = display_mode
new_view.Maximized = True

for view in sc.doc.NamedViews:
    new_view.ActiveViewport.PushViewInfo(view, False)
    new_view.Redraw()
    # View Capture

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

I think it’s the part where the view is set here:

        #take screenshots
        view = sc.doc.Views.ActiveView
        view_list = Rhino.RhinoDoc.ActiveDoc.Views.GetViewList(True, False)
        
        #view = Rhino.RhinoDoc.ActiveDoc.Views.GetViewList(True, False)[4]
        print("hihi")
        print(doc.Views)
        #print(viewA)
        if view:
            view_capture = Rhino.Display.ViewCapture()
            #image_scale_int = int(image_scale)
            #view_capture.Width = view.ActiveViewport.Size.Width
            view_capture.Width = int(width) #* image_scale_int
            #view_capture.Height = view.ActiveViewport.Size.Height
            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)

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)