How to access layout user text

Hi!
Is there a way in Python to access a layout user text in Rhino WIP?

@pascal?

Hi @petumatr,

User text on layouts and details is stored on the RhinoViewport object member for both the page (RhinoPageView) and any details (DetailViewObject).

– Dale

Cool, could you please throw-in a simple example? I couldn’t find the method (which is not a big surprise since I am a beginner :slight_smile: )

Hi,

Does this example make sense:

import scriptcontext as sc

#set userstring on active view
def set_userstring(key, value):
    view_port = sc.doc.Views.ActiveView.MainViewport
    view_port.SetUserString(key,value)

#read back userstring
def get_userstring(key):
    view_port = sc.doc.Views.ActiveView.MainViewport
    return view_port.GetUserString(key)




print get_userstring('fookey')

set_userstring('fookey','foovalue')

print get_userstring('fookey')


-Willem

1 Like

Hi @Willem, thanks!

I have no experience with scriptcontext so far and I haven’t found any list/documentation of the methods. Any idea where should I look at?

What I wanted to do with the code is to list through all layouts and spit out all the user text (key,value) stored in each layout.

Hi @petumatr,

scriptcontext provides access to runtime information, such as the active document when the script was started (important on the Mac) and access to a sticky dictionary.

This code snippet prints all user strings attached to a page view (e.g. layout):

import Rhino
import scriptcontext as sc

page_views = sc.doc.Views.GetViewList(False, True)
for pv in page_views:
    print(pv.PageName)
    col = pv.MainViewport.GetUserStrings()
    if col:
        for s in col.AllKeys:
            print(s, col[s])

– Dale

2 Likes