How can I nest layers with rhino3dm.js?

I haven’t found anything relevant in the docs. The only reference I’ve found to nested layers is the property ParentLayerId, but that’s a read-only property. If I try to change it I get an error.

I’ve also found LayerTable.AddPath(), which I thought might add a layer nested to another, but that method doesn’t exist in the js library.

Is there any example I can look into?

Hi @Daniel_González_Rein,

In looking at the Rhino3dm source, it looks like you should be able to get and set the layer’s parent id.

@fraguada - is this something you can have a look at?

– Dale

1 Like

@Daniel_González_Rein You need to set the parent layer id on the layer object. I’ve updated one of the samples to show you how to go about this, but here is a snippet:

    //parent layer for Point Objects and Point Clouds
    const pt_index = doc.layers().addLayer( 'Points', { r: 255, g: 0, b: 0, a: 255 } )
    const pts_layer_id = doc.layers().findName( 'Points', '').id

    //because we want to nest this layer, we need to set the parent layer id
    const layer_pointObjects = new rhino.Layer()
    layer_pointObjects.name = 'Point Objects'
    layer_pointObjects.color = { r: 255, g: 0, b: 0, a: 255 }
    layer_pointObjects.parentLayerId = pts_layer_id
    const ptObject_layer_index = doc.layers().add( layer_pointObjects )

    //because we want to nest this layer, we need to set the parent layer id
    const layer_pointCloud = new rhino.Layer()
    layer_pointCloud.name = 'Point Clouds'
    layer_pointCloud.color = { r: 255, g: 0, b: 0, a: 255 }
    layer_pointCloud.parentLayerId = pts_layer_id
    const ptCloud_layer_index = doc.layers().add( layer_pointCloud )

This is the result:

The sample: rhino-developer-samples/script.js at 8 · mcneel/rhino-developer-samples · GitHub

I see! Now I got it working.

I was using the index instead of the id, and was getting BindingError: Cannot pass non-string to std::string.

Now I’m getting the id using layers.findIndex(parentIdx).id.

It was a bit confusing that objects are added to a layer setting the layerIndex in the attributes, but layers are added to layers using the ID instead.

Thanks for the fast response! :slight_smile:

1 Like

I agree!

1 Like