I was wondering if anybody can show me how to add a new layer and label it automatically. For example, create a new layer and name it layer01. Then run the script again and it will create a new layer and label it layer02 and so on.
Below is some script I found that creates a new layer but I have to name it myself. I have approx 160 layers to create and name so was hoping to do this automatically.
Can anyone help please?
Thanks
Dave
import Rhino
import scriptcontext
import System.Guid, System.Drawing.Color
def AddLayer():
# Cook up an unused layer name
unused_name = scriptcontext.doc.Layers.GetUnusedLayerName(False)
# Prompt the user to enter a layer name
gs = Rhino.Input.Custom.GetString()
gs.SetCommandPrompt("Name of layer to add")
gs.SetDefaultString(unused_name)
gs.AcceptNothing(True)
gs.Get()
if gs.CommandResult()!=Rhino.Commands.Result.Success:
return gs.CommandResult()
# Was a layer named entered?
layer_name = gs.StringResult().Trim()
if not layer_name:
print "Layer name cannot be blank."
return Rhino.Commands.Result.Cancel
# Is the layer name valid?
if not Rhino.DocObjects.Layer.IsValidName(layer_name):
print layer_name, "is not a valid layer name."
return Rhino.Commands.Result.Cancel
# Does a layer with the same name already exist?
layer_index = scriptcontext.doc.Layers.Find(layer_name, True)
if layer_index>=0:
print "A layer with the name", layer_name, "already exists."
return Rhino.Commands.Result.Cancel
# Add a new layer to the document
layer_index = scriptcontext.doc.Layers.Add(layer_name, System.Drawing.Color.Black)
if layer_index<0:
print "Unable to add", layer_name, "layer."
return Rhino.Commands.Result.Failure
return Rhino.Commands.Result.Success
if __name__=="__main__":
AddLayer()
Not exactly sure what you are looking for. Maybe this help?
import Rhino
import scriptcontext as sc
def test_add_layers():
color = Rhino.ApplicationSettings.AppearanceSettings.DefaultLayerColor
for i in range(0, 160):
name = sc.doc.Layers.GetUnusedLayerName()
sc.doc.Layers.Add(name, color)
if __name__ == '__main__':
test_add_layers()
That’s exactly what I’m after (almost). Instead of the new layers being named ‘Layer 1’, ‘Layer 2’ etc, how can I change the name to ‘Extract 1’, ‘Extract 2’ etc. and also to change the layer to colour it red?
Apologies, I should have put this in my original query but didn’t want to overload the question too early and I thought I could figure it out. I’m quite new to Python scripting and still learning.
import System
import Rhino
import scriptcontext as sc
def _get_unused_name(prefix):
number = 1
while True:
number_str = str(number)
name = "{0} {1}".format(prefix, number_str.zfill(2))
layer = sc.doc.Layers.FindName(name)
if layer:
number += 1
else:
return name
def test():
color = System.Drawing.Color.Red
for i in range(0, 160):
name = _get_unused_name("Extract")
sc.doc.Layers.Add(name, color)
if __name__ == '__main__':
test()
import System
import Rhino
import scriptcontext as sc
def _get_unused_name(prefix):
number = 1
while True:
number_str = str(number)
name = "{0} {1}".format(prefix, number_str.zfill(2))
layer = sc.doc.Layers.FindName(name)
if layer:
number += 1
else:
return name
def test():
color = System.Drawing.Color.Red
name = _get_unused_name("Layer")
index = sc.doc.Layers.Add(name, color)
if index >= 0:
sc.doc.Layers.SetCurrentLayerIndex(index, False)
if __name__ == '__main__':
test()