rs.LayerVisible and User Strings


#1

Hi all

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 )

(6.1.18037.13441, 06/02/2018)
Evaluation

Thanks


#2

IsLocked_removes_UserString.py (1.9 KB), a modification of the example at http://developer.rhino3d.com/api/RhinoCommon/html/P_Rhino_DocObjects_Layer_IsLocked.htm
shows the various properties, including UserStringCount, that are modified after Layer.IsLocked.:

The user strings disappear from layers in both V5 and V6.


(Dale Fugier) #3

Hi @emilio,

This seems to work:

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.

– Dale


#4

Hi Dale

Thank you for the info.

Now I have tried always getting the Layer object from the layer name before checking about the attached user text.

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' )
  layer.CommitChanges()

  print( 'Get user string' )
  index = scriptcontext.doc.Layers.FindByFullPath( name, -1 )
  layer = scriptcontext.doc.Layers[ index ]
  val = layer.GetUserString( 'KEY' )
  print( 'KEY => %s' % val )

  print( 'Call rs.LayerVisible' )
  rs.LayerVisible( name, False )

  print( 'Get user string' )
  index = scriptcontext.doc.Layers.FindByFullPath( name, -1 )
  layer = scriptcontext.doc.Layers[ index ]
  val = layer.GetUserString( 'KEY' )
  print( 'KEY => %s' % val )

main()

As far as I can see, it still works fine until I call

rs.LayerVisible

But, after calling it, the user text seems lost …

I mean: this code works fine in Rhino 5, but Rhino 6 seems to delete the user text if you call rs.LayerVisible.

Am I missing something ?


(Dale Fugier) #5

Hi @emilio,

No, I don’t think you are missing anything in this case (your last script). I’ve opened an issue.

https://mcneel.myjetbrains.com/youtrack/issue/RH-44808

Thanks,

– Dale


#6

Thank you, Dale !