Rhino3dm Python Library - How to add a Layer to the model?

Hello All,

I am a new user of the rhino3dm python library. I am attempting to add a layer to the model, but I can’t figure out the appropriate syntax. Documentation here: Welcome to rhino3dm’s documentation! — rhino3dm 7.6.0 documentation

I assume I must add a new layer to Rhino model object by adding a new layer to the Layer Table? How is the following code incorrect?

layer_name = "test layer"
color_tuple = (0,0,0,0)
layer_index = File3dmLayerTable.AddLayer(layer_name,color_tuple)  

The code throws the following error:

Exception has occurred: TypeError
AddLayer(): incompatible function arguments. The following argument types are supported:
1. (self: rhino3dm._rhino3dm.File3dmLayerTable, name: str, color: tuple) → int

Invoked with: ‘test layer’, (0, 0, 0, 0)

Thank you for any assistance!

I would reference https://developer.rhino3d.com/api/rhinocommon/ for the most up to date documentation.

As for adding a layer into the “model”. What you call model is what mcneel calls the active document. First you need to target the active document then its layer table inside that.

There may be better ways about it but here is how I do it.

import Rhino as R
import rhinoscriptsyntax as rs
import System


doc = R.RhinoDoc.ActiveDoc

layer_table = doc.Layers

blue = System.Drawing.Color.Blue

layer_table.Add("Test Layer",blue)

Hope that helps !

@cncduck - he’s using CPython with Rhino3dm outside of Rhino.

@rhino_user_person - this seems to work:

import rhino3dm
import random

model = rhino3dm.File3dm()
color = (255, 0, 0, 255) #RGBA
model.Layers.AddLayer("Red", color)
for i in range(20):
	pt = rhino3dm.Point3d(random.uniform(-10,10), random.uniform(-10,10), 0)
	model.Objects.AddPoint(pt)
	circle = rhino3dm.Circle(pt, random.uniform(1,4))
	model.Objects.AddCircle(circle)
	
model.Write("Circles.3dm")

– Dale

2 Likes

@dale Ah I see should of read that more closely. @peter.leung apologies for the bad intel.

Thank you all for the responses, and Dale in particular!

I’ve expanded Dale’s code. I’m now defining two different layers and randomly assigning the circles to either layer. This is exactly the functionality I was seeking! Thank you!

import rhino3dm
import random

model = rhino3dm.File3dm()

color = (255, 0, 0, 255) #RGBA
model.Layers.AddLayer("Red", color)
color2 = (0, 0, 255, 255) #RGBA
model.Layers.AddLayer("Blue", color2)
for i in range(20):
  objattributes = rhino3dm.ObjectAttributes()
  objattributes.LayerIndex = random.randrange(2)
  pt = rhino3dm.Point3d(random.uniform(-10,10), random.uniform(-10,10), 0)
  model.Objects.AddPoint(pt)
  circle = rhino3dm.Circle(pt, random.uniform(1,4))
  model.Objects.AddCircle(circle, objattributes)
  model.Write("Circles.3dm")

Is the syntax for defining block instances (and then assigning objects to those block instances) similar?