Scripting: Can I get a list of selected Named Views?

Hi, is there a way in Rhino 8 where I can figure out what named views are selected in the Named Views panel?

I am working on a small render script and this would be a cool way to choose frames from.

I found this thread from 2019 that says it isn’t possible, but has anything changed in UI accessibility since then @dale ?

Hi @Holo, since Rhino 8.5 you can do this:

#! python 2
import scriptcontext

def DoSomething():
    
    view_names = scriptcontext.doc.NamedViews.Selection
    if not view_names: return
    
    for view_name in view_names:
        view_index = scriptcontext.doc.NamedViews.FindByName(view_name)
        view_info = scriptcontext.doc.NamedViews.Item[view_index]
        
        print "ViewName: {} >>> view_info: {}".format(view_name, view_info)
    
DoSomething()

From view_info you can get all kinds of things eg. view_info.Viewport

_

c

Thanks, this is really helpful, but do you know why this works in Rhino Python Editor but not in Script Editor? I need it to work with Python 3 to be able to manipulate the bitmaps properly.

@eirannejad maybe this is a question for you?

Hi @Holo , below seems to work with Python 3 in the new script editor:

#! python 3
import scriptcontext

def DoSomething():
    
    view_names = scriptcontext.doc.NamedViews.Selection
    if not view_names: return
    
    for view_name in view_names:
        view_index = scriptcontext.doc.NamedViews.FindByName(view_name)
        view_info = scriptcontext.doc.NamedViews[view_index]
        
        print("ViewName: {} >>> view_info: {}".format(view_name, view_info))
    
DoSomething()

I have no idea why NamedViews.Item does not exits for the new editor.

_

c.

Amazing, how did you figure that out? Trail and error? Thank you though!

And I find it strange that it works in one editor and not the other, since both import the same scriptcontext library, don’t they?

Hi @Holo, i just thought that if the NamedViews.Item cannot be accessed then it might work as expected using the default indexer. Fun part is that the second code example also works in the old editor. I hope @eirannejad can explain to us the difference.

_

c.

Hi Martin, I don’t understand what you try to show here, can you please explain?

Nothing special. Cool that it also works in Grasshopper.