How to change display mode using script?

Hello,
I’m new to Rhino. I am trying to write a python script that changes the display mode to user-chosen display mode (by display mode I mean “rendered”, “wireframe” etc.
I tried to use some script I found online:

import rhinoscriptsyntax as rs
import scriptcontext as sc
import os
import Rhino


def SetDetailsToWireframe(debug):
    modes = rs.ViewDisplayModes()
    setmode = rs.ListBox(modes, "Select mode")
    # Find the chosen display mode
    display_mode = Rhino.Display.DisplayModeDescription.FindByName(setmode)
    if display_mode:
        DebugPrint(display_mode, debug)
        # Get all of the document's page views
        page_views = sc.doc.Views.GetPageViews()
        if page_views:
            # Process each page view
            for page_view in page_views:
                DebugPrint(page_view, debug)
                # Get all of the page view's details
                details = page_view.GetDetailViews()
                if details:
                    # Process each page view detail
                    for detail in details:
                        DebugPrint(detail, debug)
                        # If the detail's display mode is not wireframe...
                        if detail.Viewport.DisplayMode.Id != display_mode.Id:
                            # ...set it to wireframe.
                            detail.Viewport.DisplayMode = display_mode
                            detail.CommitViewportChanges()
                    # Redraw the page
                    page_view.Redraw()

but the display mode didn’t change, and in the first debug print (of the display_mode) I got:
Rhino.Display.DisplayModeDescription object at 0x0000000000000089 [Rhino.Display.DisplayModeDescription]
I am using Rhino7 on windows.
What am I doing wrong?

thanks

This seems more appropriate. The one you found was for layouts.

1 Like

It worked like a charm. Thank you! But there is still a problem. I turn the sun on from the script, but when I use your solution to render in “rendered” display mode, I can’t see the shadows until I select the object on the screen, and it’s really importent that the shadows will appear right away. Any thoughts of how can I achive that?

OK to solve the previous issue I just enabled redraw in the code snippet above. Thanks a lot!