How to identify which Modelspace viewport is which with Python?

Hi scripting gurus,

I want to write a python script that allows me to always reset the modelspace viewport in the south-east quadrant, which by default is where the Right View located. Please see screenshot below.

I can get the script to identify the Right View and reset it, however, the script only works if the Right View is visible on the screen. If I were to rename the Right viewport to a different name, the script no longer works. Is there a way to identify the viewport based on where it sits instead of its name?

I know the Rhino command _4View reset the screen back to the default 4 viewports, Top, Perspective, Front and Right views. I can’t access the codes for ‘4View’ to see how it’s identifying and referencing each viewport. Does the Right Viewport have a fixed GUID?

Any advice would be greatly appreciated. Thanks in advance.

Maybe this helps:
https://developer.rhino3d.com/api/rhinocommon/rhino.display.rhinoviewport/bounds

Viewports also have an id but I’m unsure if that changes when the name changes, you’ll have to check.

Hi @IceTea,

If you have Rhino 8, run the 4View command with Projection=FirstAngle command line option.

Does this help?

– Dale

Hi Dale,

Thanks for the reply, I am using Rhino 8. the 4View command only allows me to reset the screen back to the default 4 viewports. It isn’t what i’m looking for.

I need to identify and select the viewport in the south-eastern quadrant, which is where the Right viewport is located. So I can reset just that specific viewport via a python script. Currently, my script can identify the Right view by its view name and resets it, however, if I were to rename the Right view to a different name, then my script no longer works. Hence, I want to try a different approach by identifying the viewport by its location, so that my script will always reset that specific viewport (sitting in the south-eastern quadrant) no matter what name it has.

This is incorrect. Check the 4View command online help for details.

Is this what you want?

– Dale

Hi Dale,

Thanks for your response again.

I’m not really looking to activate Third angle projection via the 4View command. What I really want to know is - if it’s possible to identify the location of where the Right view is sitting, which is in the lower right corner (south-east quadrant).

I don’t want to reset all viewports, I only want to reset only that lower right corner viewport (which is where the Right View is sitting by default).

Do you know if there’s a GUID or some sort for the viewport sitting in the lower right corner?Thanks again!

Hi @IceTea,

Here is one way:

#! python 3
import Rhino
import scriptcontext as sc
import System.Drawing

def __GetModelViews():
    model_views = []
    rhino_views = sc.doc.Views.GetViewList(True, False)
    for view in rhino_views:
        if not view.Floating:
            model_views.append(view)
    return model_views

def Test():
    model_views = __GetModelViews()
    if len(model_views) != 4:
        return

    right_view = None
    right = -1
    bottom = -1
    for view in model_views:
        rect = view.ScreenRectangle
        if right < rect.Right or bottom < rect.Bottom:
            right_view = view
            right = rect.Right
            bottom = rect.Bottom

    print(right_view.ActiveViewport.Name)

if __name__ == "__main__":
    Test()

– Dale