There might be something strange with rhinoscriptsyntax.LayerVisible
Calling rs.LayerVisible seems to clear user strings attached to the layer
import Rhino
import scriptcontext
import rhinoscriptsyntax as rs
def main():
name = rs.GetString( 'Layer name ?' )
index = scriptcontext.doc.Layers.FindByFullPath( name, -1 )
layer = scriptcontext.doc.Layers[ index ]
print( 'Set user string' )
ok = layer.SetUserString( 'KEY', 'VALUE' )
if not ok:
print( 'SetUserString failed' )
val = layer.GetUserString( 'KEY' )
print( 'KEY => %s' % val )
print( 'Call rs.LayerVisible' )
rs.LayerVisible( name, False )
print( 'Get user string' )
val = layer.GetUserString( 'KEY' )
print( 'KEY => %s' % val )
main()
( I’ve found that if I use the code from Rhino 5’s rhinoscriptsyntax, that is
Layer.CommitChanges instead of Layer.SetPersistentVisibility, all works fine )
import Rhino
import rhinoscriptsyntax as rs
import scriptcontext as sc
# Sets user text to a layer
def SetLayerUserText(name, key, value):
index = sc.doc.Layers.FindByFullPath(name, -1)
if index >= 0:
layer = sc.doc.Layers[index]
if layer:
layer.SetUserString(key, value)
layer.CommitChanges()
# Gets user text to a layer
def GetLayerUserText(name, key):
index = sc.doc.Layers.FindByFullPath(name, -1)
if index >= 0:
layer = sc.doc.Layers[index]
if layer:
return layer.GetUserString(key)
return null
name = 'Default'
key = 'Test'
value = 'Hello Rhino!'
SetLayerUserText(name, key, value)
print 'Before layer lock:'
print ' ', GetLayerUserText(name, key)
rs.LayerLocked(name, True)
print 'After layer lock:'
print ' ', GetLayerUserText(name, key)
rs.LayerLocked(name, False)
print 'After layer unlock:'
print ' ', GetLayerUserText(name, key)
Keep in mind that most rhinoscriptsyntax functions modify document objects, which in turn push copies of previously unmodified objects on to an undo stack. Thus, the reference RhinoCommon objects might be obsolete after calling rhinoscriptsyntax functions.