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

3 Likes

Hello,
I need help with a python script.
I am trying to get the name of the layout on wich a text is located in order to use them in building a pdf name file.
I have tried this with no luck.

import rhinoscriptsyntax as rs
x = []
a = rs.ObjectLayout(x,return_name=False)
if x: 
    print a

Can you provide any hints ?

Hello,

In line 2 you are defining x as an empty list and then in line 3 searching for its layout. If your object is assigned to an input x then you need to delete line 2, and maybe change the input type on the x input.

Graham