Create a new floating viewport and change its display mode

I’m trying to create a python code using RhinoCommon to do multiple Rhino commands at once.

  1. NewFloatingViewport Projection Perspective
  2. MaxViewport
  3. Zoom Extents
  4. SetDisplayMode Mode Shade

I think the following code makes it to step 2, MaxViewport, but I can’t seem to complete the rest quickly. Can someone help? I don’t want to use rhinoscriptsyntax.Command(), because it made my Grasshopper file very unstable executing multiple commands.

import rhinoscriptsyntax as rs
import Rhino
import System
from scriptcontext import doc

if r:
    newView = Rhino.RhinoDoc.ActiveDoc.Views.Add("Test", Rhino.Display.DefinedViewportProjection.Perspective, System.Drawing.Rectangle(0, 0, 600, 600), True)
    newView.Maximized = True

I could make it work, sort of… This will change display mode of all views. How can I change the display mode of only the newly created floating viewport? Thank you.

import Rhino
import System
import scriptcontext as sc
import rhinoscriptsyntax as rs



if r:
    #Create a new floating view
    newView = Rhino.RhinoDoc.ActiveDoc.Views.Add("TEST", Rhino.Display.DefinedViewportProjection.Perspective, System.Drawing.Rectangle(0, 0, 600, 600), True)
    #Maximize a view
    newView.Maximized = True
    #Zoom extents
    rs.ZoomExtents
    #Set display mode
    views = rs.ViewNames()
    for view in views:
        rs.ViewDisplayMode(view, "Shaded")

Can someone please let me know why the revised code below doesn’t work? It doesn’t change the display mode… I checked view name to see if Current view is set correctly, and it seems right.

import Rhino
import System
import rhinoscriptsyntax as rs

if r:
    #Create a new floating view
    newView = Rhino.RhinoDoc.ActiveDoc.Views.Add("TEST", Rhino.Display.DefinedViewportProjection.Perspective, System.Drawing.Rectangle(0, 0, 600, 600), True)
    #Maximize a view
    newView.Maximized = True
    #Zoom extents
    rs.ZoomExtents
    
    #Set display mode
    view = rs.CurrentView("TEST")
    viewName = rs.CurrentView(return_name = True)
    print viewName
    rs.ViewDisplayMode(view, "Shaded")

Hi @Dongyeop_Lee,

Try this:

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
new_view.ActiveViewport.ZoomExtents()

new_view.Redraw()

– Dale

Hi Dale,
Thank you so much for your answer, but I somehow don’t get “Add” method after “sc.doc.Views.”. There is “AddPageView” available, and I get the error message below.

  1. Solution exception:‘GhViewTable’ object has no attribute ‘Add’
    Can you let me know what the issue is?

Hi @Dongyeop_Lee,

Are you running Rhino 6?

Try running the script from Rhino, not Grasshopper.

Here is the help for ViewTable.Add.

– Dale

Yes, I’m using Rhino 6, but I need to run it in Grasshopper. It’s part of much larger Grasshopper file. Is there a way to modify your script to run in Grasshopper?

Maybe. You might ask this question in the Grasshopper category.

– Dale

OK, thank you very much for your help!

I could change Dale’s code slightly to run in Grasshopper, and post it here for anyone looking for a similar solution.

import Rhino
import System
import rhinoscriptsyntax as rs


if r:
    #Create a new floating view
    newView = Rhino.RhinoDoc.ActiveDoc.Views.Add("TEST", Rhino.Display.DefinedViewportProjection.Perspective, System.Drawing.Rectangle(0, 0, 600, 600), True)
    
    #Change display mode to "Shaded"
    shaded_id = Rhino.Display.DisplayModeDescription.ShadedId
    display_mode = Rhino.Display.DisplayModeDescription.GetDisplayMode(shaded_id)
    newView.ActiveViewport.DisplayMode = display_mode
    
    #Maximize viewport
    newView.Maximized = True
    
    #Zoom extents
    newView.ActiveViewport.ZoomExtents()
    
    newView.Redraw()